Skip to content

Commit

Permalink
Fix issues with empty planned attribute keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinbunney committed Oct 10, 2021
1 parent a1e9f2e commit f5b27e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
4 changes: 4 additions & 0 deletions flatten/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func Flatten(thing map[string]interface{}) map[string]string {
}

func flatten(result map[string]string, prefix string, v reflect.Value) {
if v.Kind() == reflect.Invalid {
return
}

if v.Kind() == reflect.Interface {
v = v.Elem()
}
Expand Down
13 changes: 8 additions & 5 deletions kubernetes/resource_kubectl_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,17 +945,20 @@ func getLiveManifestFields_WithIgnoredFields(ignoredFields []string, userProvide
// this implicitly excludes anything that the user didn't provide as it was added by kubernetes runtime (annotations/mutations etc)
userKeys := []string{}
for userKey, userValue := range flattenedUser {
normalizedUserValue := strings.TrimSpace(userValue)

// only include the value if it exists in the live version
// that is, don't add to the userKeys array unless the key still exists in the live manifest
if _, exists := flattenedLive[userKey]; exists {
userKeys = append(userKeys, userKey)
flattenedUser[userKey] = strings.TrimSpace(flattenedLive[userKey])
if strings.TrimSpace(userValue) != flattenedUser[userKey] {
log.Printf("[TRACE] yaml drift detected in %s for %s, was:\n%s now:\n%s", selfLink, userKey, userValue, flattenedLive[userKey])
normalizedLiveValue := strings.TrimSpace(flattenedLive[userKey])
flattenedUser[userKey] = normalizedLiveValue
if normalizedUserValue != normalizedLiveValue {
log.Printf("[TRACE] yaml drift detected in %s for %s, was: %s now: %s", selfLink, userKey, normalizedUserValue, normalizedLiveValue)
}
} else {
if strings.TrimSpace(userValue) != "" {
log.Printf("[TRACE] yaml drift detected in %s for %s, was %s now blank", selfLink, userKey, userValue)
if normalizedUserValue != "" {
log.Printf("[TRACE] yaml drift detected in %s for %s, was %s now blank", selfLink, userKey, normalizedUserValue)
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions kubernetes/resource_kubectl_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,26 @@ func TestGetLiveManifestFilteredForUserProvidedOnly(t *testing.T) {
expectedFingerprint: "5d9a5cd23ce01763e52f171e6bf2d98ca3cfed982974579af4c011ff6010694f",
expectedDrift: false,
},
{
description: "Map with empty annotations in user manifest",
userProvided: map[string]interface{}{
"atest": "test",
"metadata": map[string]interface{}{
"annotations": map[string]interface{}{},
},
},
liveManifest: map[string]interface{}{
"atest": "test",
"metadata": map[string]interface{}{
"annotations": map[string]string{
"kubectl.kubernetes.io/last-applied-configuration": "{\"should-be-ignored\"}",
},
},
},
expectedFields: "atest=test",
expectedFingerprint: "df296364dd3346f0aa05c63f0b0df19b7aa850e44e9f4a80cf6ac06a889d9868",
expectedDrift: false,
},
{
description: "Deployment manifest without changes",
userProvided: loadRealDeploymentManifest().unstruct.Object,
Expand Down

0 comments on commit f5b27e9

Please sign in to comment.