Skip to content

Commit

Permalink
fix: diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
dfroberg committed Mar 26, 2024
1 parent 29f6db1 commit 73749ba
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ dist:
goreleaser build --single-target --skip-validate --rm-dist

test:
go test -i $(TEST) || exit 1
go -v test -i $(TEST) || exit 1
echo $(TEST) | \
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
xargs -t -n4 go -v test $(TESTARGS) -timeout=30s -parallel=4

testacc:
TF_ACC=1 go test ./kubernetes -v $(TESTARGS) -timeout 120m -count=1
TF_ACC=1 go test -v ./kubernetes -v $(TESTARGS) -timeout 120m -count=1

publish:
goreleaser release --rm-dist
Expand Down
17 changes: 17 additions & 0 deletions _examples/namespaces/first-namespace.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "kubectl_manifest" "test" {
apply_only = false
ignore_fields = ["metadata.labels"]
yaml_body = yamlencode({
apiVersion : "v1",
kind : "Namespace",
metadata : {
name : "name-here",
annotations : {
"argocd.argoproj.io/sync-options" : "Prune=false"
},
labels : {
"goldilocks.fairwinds.com/enabled" : "true"
}
}
})
}
File renamed without changes.
25 changes: 17 additions & 8 deletions kubernetes/resource_kubectl_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,20 @@ metadata:

// clear out fields user can't set to try and get parity with yaml_body
// remove any fields from the user provided set or control fields that we want to ignore
// this is to ensure that the yaml_body is as close to the original as possible
for _, field := range kubernetesControlFields {
meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", field)
// ensure we traverse the fields in kubernetesControlFields to remove nested fields
fields := strings.Split(field, ".")
// remove the field
meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, fields...)
}
// meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "creationTimestamp")
// meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "resourceVersion")
// meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "selfLink")
// meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "uid")
// meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "generation")
// meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "annotations", "kubectl.kubernetes.io/last-applied-configuration")

// Remove status
//meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "status")
// Sepcial case for kubectl.kubernetes.io/last-applied-configuration
//ßmeta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "annotations", "kubectl.kubernetes.io/last-applied-configuration")

// clean up empty fields
if len(metaObjLive.Raw.GetAnnotations()) == 0 {
meta_v1_unstruct.RemoveNestedField(metaObjLive.Raw.Object, "metadata", "annotations")
}
Expand All @@ -219,6 +223,9 @@ metadata:
if err != nil {
return []*schema.ResourceData{}, fmt.Errorf("failed to convert manifest to yaml: %+v", err)
}
// log readable yaml_body and yaml_body_parsed
log.Printf("[TRACE] import yaml_body:\n%s", yamlString)
log.Printf("[TRACE] import yaml_body_parsed:\n%s", yamlParsed)

_ = d.Set("yaml_body", yamlParsed)
_ = d.Set("yaml_body_parsed", yamlParsed)
Expand Down Expand Up @@ -1099,6 +1106,8 @@ func getLiveManifestFields_WithIgnoredFields(ignoredFields []string, userProvide
}

var kubernetesControlFields = []string{
"metadata.annotations.kubectl.kubernetes.io/last-applied-configuration",
"metadata.deletionTimestamp",
"status",
"metadata.finalizers",
"metadata.initializers",
Expand All @@ -1107,6 +1116,6 @@ var kubernetesControlFields = []string{
"metadata.generation",
"metadata.resourceVersion",
"metadata.uid",
"metadata.annotations.kubectl.kubernetes.io/last-applied-configuration",
"metadata.selfLink",
"metadata.managedFields",
}

0 comments on commit 73749ba

Please sign in to comment.