Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix skipping not overridden labels uisng '--merge-labels' #1435

Conversation

pawcykca
Copy link
Contributor

@pawcykca pawcykca commented Sep 1, 2022

Close #1327

This PR should fix issue when TF try to recreate the existing cluster every time even if there are no changes to the code using '--merge-labels option.

@MrFreezeex
Copy link
Contributor

MrFreezeex commented Sep 2, 2022

Are you trying to fix the case where a cluster that is being imported in Terraform? Because if that's the case it would still want to recreate the cluster after that. The issue being that merge_labels can't be read in the magnum API: it's not in gophercloud and the actual json returned by magnum seems to always return false (at least in my env). So basically on import, the state of merge_labels would always be false and if you have it set to true in your .tf code it would want to replace the cluster...

Possible ways to fix it would be to try to make magnum fixing that + having it in gophercloud and actually read it in terraform or finding a trick to determine if merge_labels is supposed to be true in cluster read function...

if err != nil {
return diag.FromErr(err)
}
labels = containerInfraV1GetLabelsMerged(s.LabelsAdded, s.LabelsSkipped, s.LabelsOverridden, s.Labels, resourceDataLabels)
Copy link
Contributor

@MrFreezeex MrFreezeex Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in what case you would need to get the labels from the state? It seems to me that with this change if you would want to delete a label it would not consider that the cluster changed.

But since I am not sure what you are trying to fix exactly, perhaps I missed something.

Copy link
Contributor Author

@pawcykca pawcykca Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code fixes issue described in #1327.

Currently when you create cluster and nodegroup using 'merge_label' option with labels that looks like below:

$ cat cluster.tf 
resource "openstack_containerinfra_cluster_v1" "cluster_1" {
  ...
  merge_labels        = true
  labels = {
    # unchanged (the same as in the cluster template)
    availability_zone = "zone1"
    # overridden
    boot_volume_size = 15
    # added
    boot_volume_type = "gp-fast"
  }

$ cat nodegroup.tf 
resource "openstack_containerinfra_nodegroup_v1" "nodegroup" {
  ...
  merge_labels        = true
  labels = {
    # unchanged (the same as in the cluster template)
    availability_zone = "zone1"
    # overridden
    boot_volume_size = 20
    # added
    docker_volume_type = "gp-slow"
  }

and perform again apply operation (without any changes in .tf files) then TF will try to recreate both cluster and nodegroup again

Terraform will perform the following actions:

  # openstack_containerinfra_cluster_v1.cluster_1 must be replaced
-/+ resource "openstack_containerinfra_cluster_v1" "cluster_1" {
	  ...
      ~ labels              = { # forces replacement
          + "availability_zone" = "zone1"
            # (2 unchanged elements hidden)
        }
      ...
        # (7 unchanged attributes hidden)
    }

  # openstack_containerinfra_nodegroup_v1.nodegroup must be replaced
-/+ resource "openstack_containerinfra_nodegroup_v1" "nodegroup" {
	  ...
      ~ labels             = { # forces replacement
          + "availability_zone"  = "zone1"
            # (2 unchanged elements hidden)
        }
      ...
        # (6 unchanged attributes hidden)
    }

Plan: 2 to add, 0 to change, 2 to destroy.

This is because defined labels that are the same as in the cluster template are not stored in cluster/nodegroup object by Openstack API.

Copy link
Contributor

@MrFreezeex MrFreezeex Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird I would expect to find those labels in labels skipped or even overridden somehow... But by reading the state of terrwform I don't think that you are able to remove an existing labels anymore... Could you test this scenario? If it's indeed not returned anywhere in the Openstack cluster API, it would probably require fetching the labels from the cluster template unfortunately...

Copy link
Contributor Author

@pawcykca pawcykca Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird I would expect to find those labels in labels skipped or even overridden somehow...

This is mapped as below

  merge_labels        = true
  labels = {
    # unchanged (the same as in the cluster template) -> not stored in any field on Magnum API
    availability_zone = "zone1"
    # overridden -> stored in 'labels_overridden'
    boot_volume_size = 15
    # added -> stored in 'labels_added'
    boot_volume_type = "gp-fast"
  }
-------
  merge_labels        = false
  labels = {
    ...
    # not copied/inherited from cluster's labels or commented (as below)-> stored in 'labels_skipped'
    #boot_volume_type = "gp-fast"
  }

But by reading the state of terrwform I don't think that you are able to remove an existing labels anymore... Could you test this scenario?

Works fine for each type of below labels

  merge_labels        = true
  labels = {
    # unchanged (the same as in the cluster template)
    # availability_zone = "zone1"
    # overridden
    #boot_volume_size = 15
    # added
    #boot_volume_type = "gp-fast"
  }
Terraform will perform the following actions:

  # openstack_containerinfra_nodegroup_v1.nodegroup must be replaced
-/+ resource "openstack_containerinfra_nodegroup_v1" "nodegroup" {
     ...
      ~ labels             = {
          - "availability_zone"  = "zone1"
          - "boot_volume_size"   = "20"
          - "docker_volume_type" = "gp-fast"
        } -> (known after apply) # forces replacement
     ...
        # (7 unchanged attributes hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok I see then it should work indeed! Thanks :).

@nikParasyr
Copy link
Collaborator

@ozerovandrei Can you have a look into this as well?
From my side this looks ok, but i dont have any environment to actually test it

Copy link
Member

@ozerovandrei ozerovandrei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't have a Magnum environment currently but LGTM

@nikParasyr nikParasyr merged commit 9077d55 into terraform-provider-openstack:main Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants