Skip to content

Commit

Permalink
Remove resource_version and live_resource_version fields causing cons…
Browse files Browse the repository at this point in the history
…tant drift in Terraform v1.x
  • Loading branch information
gavinbunney committed Oct 10, 2021
1 parent 85235fa commit c79527a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
2 changes: 0 additions & 2 deletions docs/resources/kubectl_manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ YAML
* `name` - Extracted object name from `yaml_body`.
* `namespace` - Extracted object namespace from `yaml_body`.
* `uid` - Kubernetes unique identifier from last run.
* `resource_version` - Resource version from kubernetes from last run.
* `live_uid` - Current uuid from kubernetes.
* `live_resource_version` - Current uuid from kubernetes.
* `yaml_incluster` - Current yaml within kubernetes.
* `live_manifest_incluster` - Current manifest within kubernetes.

Expand Down
46 changes: 9 additions & 37 deletions kubernetes/resource_kubectl_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,9 @@ metadata:
return []*schema.ResourceData{}, fmt.Errorf("failed to parse item and get UUID: %+v", metaObjLive)
}

// Capture the UID and Resource_version from the cluster at the current time

// Capture the UID from the cluster at the current time
_ = d.Set("uid", metaObjLive.GetUID())
_ = d.Set("live_uid", metaObjLive.GetUID())
_ = d.Set("resource_version", metaObjLive.GetResourceVersion())
_ = d.Set("live_resource_version", metaObjLive.GetResourceVersion())

liveManifestFingerprint := getLiveManifestFingerprint(d, metaObjLive, metaObjLive)
_ = d.Set("yaml_incluster", liveManifestFingerprint)
Expand Down Expand Up @@ -303,32 +300,18 @@ metadata:
return nil
}

// Get the ResourceVersion of the K8s resource as it was when the `resourceKubectlManifestCreate` func completed.
createdAtResourceVersion := d.Get("resource_version").(string)
// Get it as it currently is in the cluster
resourceVersion, exists := d.Get("live_resource_version").(string)
if !exists {
return nil
}

// If either UID or ResourceVersion differ between the current state and the cluster
// trigger an update on the resource to get back in sync
if UID != createdAtUID {
log.Printf("[TRACE] DETECTED %s vs %s", UID, createdAtUID)
_ = d.SetNewComputed("uid")
return nil
}

if resourceVersion != createdAtResourceVersion {
log.Printf("[TRACE] DETECTED RESOURCE VERSION %s vs %s", resourceVersion, createdAtResourceVersion)
// Check that the fields specified in our YAML for diff against cluster representation
stateYaml := d.Get("yaml_incluster").(string)
liveStateYaml := d.Get("live_manifest_incluster").(string)
if stateYaml != liveStateYaml {
log.Printf("[TRACE] DETECTED YAML STATE %s vs %s", stateYaml, liveStateYaml)
_ = d.SetNewComputed("yaml_incluster")
}
return nil
// Check that the fields specified in our YAML for diff against cluster representation
stateYaml := d.Get("yaml_incluster").(string)
liveStateYaml := d.Get("live_manifest_incluster").(string)
if stateYaml != liveStateYaml {
log.Printf("[TRACE] DETECTED YAML STATE %s vs %s", stateYaml, liveStateYaml)
_ = d.SetNewComputed("yaml_incluster")
}

return nil
Expand Down Expand Up @@ -361,18 +344,10 @@ var (
Type: schema.TypeString,
Computed: true,
},
"resource_version": {
Type: schema.TypeString,
Computed: true,
},
"live_uid": {
Type: schema.TypeString,
Computed: true,
},
"live_resource_version": {
Type: schema.TypeString,
Computed: true,
},
"yaml_incluster": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -575,13 +550,11 @@ func resourceKubectlManifestApply(ctx context.Context, d *schema.ResourceData, m
d.SetId(selfLink)
log.Printf("[DEBUG] %v fetched successfully, set id to: %v", manifest, d.Id())

// Capture the UID and Resource_version at time of update
// Capture the UID at time of update
// this allows us to diff these against the actual values
// read in by the 'resourceKubectlManifestRead'
_ = d.Set("uid", response.GetUID())
_ = d.Set("live_uid", response.GetUID())
_ = d.Set("resource_version", response.GetResourceVersion())
_ = d.Set("live_resource_version", response.GetResourceVersion())

liveManifestFingerprint := getLiveManifestFingerprint(d, manifest.unstruct, response)
_ = d.Set("yaml_incluster", liveManifestFingerprint)
Expand Down Expand Up @@ -658,9 +631,8 @@ func resourceKubectlManifestReadUsingClient(ctx context.Context, d *schema.Resou
return fmt.Errorf("%v failed to parse item and get UUID: %+v", manifest, metaObjLive)
}

// Capture the UID and Resource_version from the cluster at the current time
// Capture the UID from the cluster at the current time
_ = d.Set("live_uid", metaObjLive.GetUID())
_ = d.Set("live_resource_version", metaObjLive.GetResourceVersion())

liveManifestFingerprint := getLiveManifestFingerprint(d, manifest.unstruct, metaObjLive)
_ = d.Set("live_manifest_incluster", liveManifestFingerprint)
Expand Down

0 comments on commit c79527a

Please sign in to comment.