Skip to content

Commit f5b27e9

Browse files
committed
Fix issues with empty planned attribute keys
1 parent a1e9f2e commit f5b27e9

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

flatten/flatten.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func Flatten(thing map[string]interface{}) map[string]string {
3636
}
3737

3838
func flatten(result map[string]string, prefix string, v reflect.Value) {
39+
if v.Kind() == reflect.Invalid {
40+
return
41+
}
42+
3943
if v.Kind() == reflect.Interface {
4044
v = v.Elem()
4145
}

kubernetes/resource_kubectl_manifest.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -945,17 +945,20 @@ func getLiveManifestFields_WithIgnoredFields(ignoredFields []string, userProvide
945945
// this implicitly excludes anything that the user didn't provide as it was added by kubernetes runtime (annotations/mutations etc)
946946
userKeys := []string{}
947947
for userKey, userValue := range flattenedUser {
948+
normalizedUserValue := strings.TrimSpace(userValue)
949+
948950
// only include the value if it exists in the live version
949951
// that is, don't add to the userKeys array unless the key still exists in the live manifest
950952
if _, exists := flattenedLive[userKey]; exists {
951953
userKeys = append(userKeys, userKey)
952-
flattenedUser[userKey] = strings.TrimSpace(flattenedLive[userKey])
953-
if strings.TrimSpace(userValue) != flattenedUser[userKey] {
954-
log.Printf("[TRACE] yaml drift detected in %s for %s, was:\n%s now:\n%s", selfLink, userKey, userValue, flattenedLive[userKey])
954+
normalizedLiveValue := strings.TrimSpace(flattenedLive[userKey])
955+
flattenedUser[userKey] = normalizedLiveValue
956+
if normalizedUserValue != normalizedLiveValue {
957+
log.Printf("[TRACE] yaml drift detected in %s for %s, was: %s now: %s", selfLink, userKey, normalizedUserValue, normalizedLiveValue)
955958
}
956959
} else {
957-
if strings.TrimSpace(userValue) != "" {
958-
log.Printf("[TRACE] yaml drift detected in %s for %s, was %s now blank", selfLink, userKey, userValue)
960+
if normalizedUserValue != "" {
961+
log.Printf("[TRACE] yaml drift detected in %s for %s, was %s now blank", selfLink, userKey, normalizedUserValue)
959962
}
960963
}
961964
}

kubernetes/resource_kubectl_manifest_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,26 @@ func TestGetLiveManifestFilteredForUserProvidedOnly(t *testing.T) {
842842
expectedFingerprint: "5d9a5cd23ce01763e52f171e6bf2d98ca3cfed982974579af4c011ff6010694f",
843843
expectedDrift: false,
844844
},
845+
{
846+
description: "Map with empty annotations in user manifest",
847+
userProvided: map[string]interface{}{
848+
"atest": "test",
849+
"metadata": map[string]interface{}{
850+
"annotations": map[string]interface{}{},
851+
},
852+
},
853+
liveManifest: map[string]interface{}{
854+
"atest": "test",
855+
"metadata": map[string]interface{}{
856+
"annotations": map[string]string{
857+
"kubectl.kubernetes.io/last-applied-configuration": "{\"should-be-ignored\"}",
858+
},
859+
},
860+
},
861+
expectedFields: "atest=test",
862+
expectedFingerprint: "df296364dd3346f0aa05c63f0b0df19b7aa850e44e9f4a80cf6ac06a889d9868",
863+
expectedDrift: false,
864+
},
845865
{
846866
description: "Deployment manifest without changes",
847867
userProvided: loadRealDeploymentManifest().unstruct.Object,

0 commit comments

Comments
 (0)