Skip to content

Commit a7511ee

Browse files
committed
more formatting and fix test errors
Signed-off-by: Atif Ali <[email protected]>
1 parent 97a3ba0 commit a7511ee

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

pkg/sync/sync_context.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -973,34 +973,47 @@ func formatValue(v interface{}) string {
973973

974974
// Special case for volumeClaimTemplates
975975
if templates, ok := v.([]interface{}); ok {
976-
var details []string
976+
var names []string
977977
for _, t := range templates {
978978
if template, ok := t.(map[string]interface{}); ok {
979979
if metadata, ok := template["metadata"].(map[string]interface{}); ok {
980980
if name, ok := metadata["name"].(string); ok {
981-
// Only include templates that have changes
982981
if spec, ok := template["spec"].(map[string]interface{}); ok {
983982
if resources, ok := spec["resources"].(map[string]interface{}); ok {
984983
if requests, ok := resources["requests"].(map[string]interface{}); ok {
985984
if storage, ok := requests["storage"].(string); ok {
986-
details = append(details, fmt.Sprintf("%s(%s)", name, storage))
985+
names = append(names, fmt.Sprintf("%s(%s)", name, storage))
987986
continue
988987
}
989988
}
990989
}
991990
}
992-
details = append(details, name)
991+
names = append(names, name)
993992
}
994993
}
995994
}
996995
}
997-
return fmt.Sprintf("[%s]", strings.Join(details, ", "))
996+
return fmt.Sprintf("[%s]", strings.Join(names, ", "))
998997
}
999998

999+
// Special case for selector matchLabels
1000+
if m, ok := v.(map[string]interface{}); ok {
1001+
if matchLabels, exists := m["matchLabels"].(map[string]interface{}); exists {
1002+
var labels []string
1003+
for k, v := range matchLabels {
1004+
labels = append(labels, fmt.Sprintf("%s:%s", k, v))
1005+
}
1006+
sort.Strings(labels)
1007+
return fmt.Sprintf("{%s}", strings.Join(labels, ", "))
1008+
}
1009+
}
1010+
1011+
// Add quotes for string values
10001012
if str, ok := v.(string); ok {
10011013
return fmt.Sprintf("%q", str)
10021014
}
10031015

1016+
// For other types, use standard formatting
10041017
return fmt.Sprintf("%v", v)
10051018
}
10061019

@@ -1066,22 +1079,19 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R
10661079
for k, desiredVal := range desiredSpec {
10671080
if !mutableFields[k] {
10681081
currentVal, exists := currentSpec[k]
1082+
// Only add to changes if values are actually different
10691083
if !exists || !reflect.DeepEqual(currentVal, desiredVal) {
1070-
// Format the values for better readability
10711084
currentStr := formatValue(currentVal)
10721085
desiredStr := formatValue(desiredVal)
10731086
changes = append(changes, fmt.Sprintf(" - %s:\n from: %s\n to: %s",
10741087
k, currentStr, desiredStr))
10751088
}
10761089
}
10771090
}
1078-
10791091
if len(changes) > 0 {
1080-
// Sort the changes to ensure consistent order
10811092
sort.Strings(changes)
1082-
message := fmt.Sprintf("one or more objects failed to apply, reason: attempting to change immutable fields:\n%s\n\nForbidden: updates to statefulset spec for fields other than 'replicas', 'ordinals', 'template', 'updateStrategy', 'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are forbidden",
1093+
return common.ResultCodeSyncFailed, fmt.Sprintf("attempting to change immutable fields:\n%s\n\nForbidden: updates to statefulset spec for fields other than 'replicas', 'ordinals', 'template', 'updateStrategy', 'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are forbidden",
10831094
strings.Join(changes, "\n"))
1084-
return common.ResultCodeSyncFailed, message
10851095
}
10861096
}
10871097
}

pkg/sync/sync_context_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,8 +2040,6 @@ func TestWaitForCleanUpBeforeNextWave(t *testing.T) {
20402040

20412041
}
20422042

2043-
///////////////
2044-
20452043
func TestStatefulSetImmutableFieldErrors(t *testing.T) {
20462044
tests := []struct {
20472045
name string
@@ -2123,8 +2121,8 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
21232121
},
21242122
expectedMessage: `attempting to change immutable fields:
21252123
- selector:
2126-
from: map[matchLabels:map[app:old-app]]
2127-
to: map[matchLabels:map[app:new-app]]
2124+
from: {app:old-app}
2125+
to: {app:new-app}
21282126
21292127
Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordinals', 'template', 'updateStrategy', 'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are forbidden`,
21302128
},

0 commit comments

Comments
 (0)