Skip to content

Commit

Permalink
resolve linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: Atif Ali <[email protected]>
  • Loading branch information
aali309 committed Feb 11, 2025
1 parent 8b51f31 commit 5bedc33
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 82 deletions.
34 changes: 17 additions & 17 deletions pkg/sync/sync_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,16 +1031,16 @@ func (sc *syncContext) shouldUseServerSideApply(targetObj *unstructured.Unstruct
return sc.serverSideApply || resourceutil.HasAnnotationOption(targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)
}

func formatValue(v interface{}) string {
func formatValue(v any) string {
if v == nil {
return "<nil>"
}

// Special case for volumeClaimTemplates
if templates, ok := v.([]interface{}); ok {
if templates, ok := v.([]any); ok {
// For a single volumeClaimTemplate field change
if len(templates) == 1 {
if template, ok := templates[0].(map[string]interface{}); ok {
if template, ok := templates[0].(map[string]any); ok {
if storage := getTemplateStorage(template); storage != "" {
return fmt.Sprintf("%q", storage)
}
Expand All @@ -1049,8 +1049,8 @@ func formatValue(v interface{}) string {
// For multiple templates or other array types format
var names []string
for _, t := range templates {
if template, ok := t.(map[string]interface{}); ok {
if metadata, ok := template["metadata"].(map[string]interface{}); ok {
if template, ok := t.(map[string]any); ok {
if metadata, ok := template["metadata"].(map[string]any); ok {
if name, ok := metadata["name"].(string); ok {
if storage := getTemplateStorage(template); storage != "" {
names = append(names, fmt.Sprintf("%s(%s)", name, storage))
Expand All @@ -1065,8 +1065,8 @@ func formatValue(v interface{}) string {
}

// Special case for selector matchLabels
if m, ok := v.(map[string]interface{}); ok {
if matchLabels, exists := m["matchLabels"].(map[string]interface{}); exists {
if m, ok := v.(map[string]any); ok {
if matchLabels, exists := m["matchLabels"].(map[string]any); exists {
var labels []string
for k, v := range matchLabels {
labels = append(labels, fmt.Sprintf("%s:%s", k, v))
Expand All @@ -1084,16 +1084,16 @@ func formatValue(v interface{}) string {
}

// Get storage size from template
func getTemplateStorage(template map[string]interface{}) string {
spec, ok := template["spec"].(map[string]interface{})
func getTemplateStorage(template map[string]any) string {
spec, ok := template["spec"].(map[string]any)
if !ok {
return ""
}
resources, ok := spec["resources"].(map[string]interface{})
resources, ok := spec["resources"].(map[string]any)
if !ok {
return ""
}
requests, ok := resources["requests"].(map[string]interface{})
requests, ok := resources["requests"].(map[string]any)
if !ok {
return ""
}
Expand All @@ -1105,7 +1105,7 @@ func getTemplateStorage(template map[string]interface{}) string {
}

// Format field changes for error messages
func formatFieldChange(field string, currentVal, desiredVal interface{}) string {
func formatFieldChange(field string, currentVal, desiredVal any) string {
return fmt.Sprintf(" - %s:\n from: %s\n to: %s",
field, formatValue(currentVal), formatValue(desiredVal))
}
Expand Down Expand Up @@ -1177,8 +1177,8 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R
} else if !reflect.DeepEqual(currentVal, desiredVal) {
if k == "volumeClaimTemplates" {
// Handle volumeClaimTemplates specially
currentTemplates := currentVal.([]interface{})
desiredTemplates := desiredVal.([]interface{})
currentTemplates := currentVal.([]any)
desiredTemplates := desiredVal.([]any)

// If template count differs or we're adding/removing templates,
// use the standard array format
Expand All @@ -1188,10 +1188,10 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R
// Compare each template
for i, desired := range desiredTemplates {
current := currentTemplates[i]
desiredTemplate := desired.(map[string]interface{})
currentTemplate := current.(map[string]interface{})
desiredTemplate := desired.(map[string]any)
currentTemplate := current.(map[string]any)

name := desiredTemplate["metadata"].(map[string]interface{})["name"].(string)
name := desiredTemplate["metadata"].(map[string]any)["name"].(string)
desiredStorage := getTemplateStorage(desiredTemplate)
currentStorage := getTemplateStorage(currentTemplate)

Expand Down
130 changes: 65 additions & 65 deletions pkg/sync/sync_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2072,14 +2072,14 @@ func TestWaitForCleanUpBeforeNextWave(t *testing.T) {
assert.Equal(t, synccommon.ResultCodePruned, result[2].Status)
}

func templateWithStorage(name, storage string) map[string]interface{} {
return map[string]interface{}{
"metadata": map[string]interface{}{
func templateWithStorage(name, storage string) map[string]any {
return map[string]any{
"metadata": map[string]any{
"name": name,
},
"spec": map[string]interface{}{
"resources": map[string]interface{}{
"requests": map[string]interface{}{
"spec": map[string]any{
"resources": map[string]any{
"requests": map[string]any{
"storage": storage,
},
},
Expand All @@ -2090,16 +2090,16 @@ func templateWithStorage(name, storage string) map[string]interface{} {
func TestStatefulSetImmutableFieldErrors(t *testing.T) {
tests := []struct {
name string
currentSpec map[string]interface{}
desiredSpec map[string]interface{}
currentSpec map[string]any
desiredSpec map[string]any
expectedMessage string
}{
{
name: "single field change - serviceName",
currentSpec: map[string]interface{}{
currentSpec: map[string]any{
"serviceName": "old-svc",
},
desiredSpec: map[string]interface{}{
desiredSpec: map[string]any{
"serviceName": "new-svc",
},
expectedMessage: `attempting to change immutable fields:
Expand All @@ -2111,31 +2111,31 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
},
{
name: "volumeClaimTemplates change with storage size",
currentSpec: map[string]interface{}{
"volumeClaimTemplates": []interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
currentSpec: map[string]any{
"volumeClaimTemplates": []any{
map[string]any{
"metadata": map[string]any{
"name": "data",
},
"spec": map[string]interface{}{
"resources": map[string]interface{}{
"requests": map[string]interface{}{
"spec": map[string]any{
"resources": map[string]any{
"requests": map[string]any{
"storage": "1Gi",
},
},
},
},
},
},
desiredSpec: map[string]interface{}{
"volumeClaimTemplates": []interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
desiredSpec: map[string]any{
"volumeClaimTemplates": []any{
map[string]any{
"metadata": map[string]any{
"name": "data",
},
"spec": map[string]interface{}{
"resources": map[string]interface{}{
"requests": map[string]interface{}{
"spec": map[string]any{
"resources": map[string]any{
"requests": map[string]any{
"storage": "2Gi",
},
},
Expand All @@ -2152,16 +2152,16 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
},
{
name: "selector change",
currentSpec: map[string]interface{}{
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
currentSpec: map[string]any{
"selector": map[string]any{
"matchLabels": map[string]any{
"app": "old-app",
},
},
},
desiredSpec: map[string]interface{}{
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
desiredSpec: map[string]any{
"selector": map[string]any{
"matchLabels": map[string]any{
"app": "new-app",
},
},
Expand All @@ -2175,14 +2175,14 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
},
{
name: "volumeClaimTemplates change from nil",
currentSpec: map[string]interface{}{
currentSpec: map[string]any{
"serviceName": "test-svc",
},
desiredSpec: map[string]interface{}{
desiredSpec: map[string]any{
"serviceName": "test-svc",
"volumeClaimTemplates": []interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
"volumeClaimTemplates": []any{
map[string]any{
"metadata": map[string]any{
"name": "data",
},
},
Expand All @@ -2197,24 +2197,24 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
},
{
name: "complex volumeClaimTemplates change",
currentSpec: map[string]interface{}{
"volumeClaimTemplates": []interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
currentSpec: map[string]any{
"volumeClaimTemplates": []any{
map[string]any{
"metadata": map[string]any{
"name": "data1",
},
},
},
},
desiredSpec: map[string]interface{}{
"volumeClaimTemplates": []interface{}{
map[string]interface{}{
"metadata": map[string]interface{}{
desiredSpec: map[string]any{
"volumeClaimTemplates": []any{
map[string]any{
"metadata": map[string]any{
"name": "data1",
},
},
map[string]interface{}{
"metadata": map[string]interface{}{
map[string]any{
"metadata": map[string]any{
"name": "data2",
},
},
Expand All @@ -2229,27 +2229,27 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
},
{
name: "multiple volumeClaimTemplate change",
currentSpec: map[string]interface{}{
currentSpec: map[string]any{
"serviceName": postgresqlSvc,
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"selector": map[string]any{
"matchLabels": map[string]any{
"app": "postgresql",
},
},
"volumeClaimTemplates": []interface{}{
"volumeClaimTemplates": []any{
templateWithStorage(staticFiles, "1Gi"),
templateWithStorage(dexconfig, "1Gi"),
templateWithStorage(argocdDexServerTLS, "1Gi"),
},
},
desiredSpec: map[string]interface{}{
desiredSpec: map[string]any{
"serviceName": postgresqlSvc,
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"selector": map[string]any{
"matchLabels": map[string]any{
"app": "postgresql",
},
},
"volumeClaimTemplates": []interface{}{
"volumeClaimTemplates": []any{
templateWithStorage(staticFiles, "2Gi"),
templateWithStorage(dexconfig, "3Gi"),
templateWithStorage(argocdDexServerTLS, "4Gi"),
Expand All @@ -2270,27 +2270,27 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
},
{
name: "multiple field changes",
currentSpec: map[string]interface{}{
currentSpec: map[string]any{
"serviceName": postgresqlSvc,
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"selector": map[string]any{
"matchLabels": map[string]any{
"app": "postgresql",
},
},
"volumeClaimTemplates": []interface{}{
"volumeClaimTemplates": []any{
templateWithStorage(staticFiles, "1Gi"),
templateWithStorage(dexconfig, "1Gi"),
templateWithStorage(argocdDexServerTLS, "1Gi"),
},
},
desiredSpec: map[string]interface{}{
desiredSpec: map[string]any{
"serviceName": "postgresql-svc-new",
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"selector": map[string]any{
"matchLabels": map[string]any{
"app": "postgresql-new",
},
},
"volumeClaimTemplates": []interface{}{
"volumeClaimTemplates": []any{
templateWithStorage(staticFiles, "2Gi"),
templateWithStorage(dexconfig, "1Gi"),
templateWithStorage(argocdDexServerTLS, "1Gi"),
Expand All @@ -2314,10 +2314,10 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
current := &unstructured.Unstructured{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": testAPIVersion,
"kind": "StatefulSet",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": testStatefulSet,
"namespace": testNamespace,
},
Expand All @@ -2326,10 +2326,10 @@ Forbidden: updates to statefulset spec for fields other than 'replicas', 'ordina
}

desired := &unstructured.Unstructured{
Object: map[string]interface{}{
Object: map[string]any{
"apiVersion": testAPIVersion,
"kind": "StatefulSet",
"metadata": map[string]interface{}{
"metadata": map[string]any{
"name": testStatefulSet,
"namespace": testNamespace,
},
Expand Down

0 comments on commit 5bedc33

Please sign in to comment.