Skip to content

Commit 3ba6c17

Browse files
ChrisJBurnsclaude
andcommitted
Extract env var conversion to fix gocyclo complexity
DeployWorkload exceeded gocyclo's 15-branch threshold after the deterministic env var sorting was added. Move the conversion into a dedicated helper to keep DeployWorkload under the limit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 22875ee commit 3ba6c17

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

pkg/container/kubernetes/client.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -358,19 +358,7 @@ func (c *Client) DeployWorkload(ctx context.Context,
358358

359359
attachStdio := options == nil || options.AttachStdio
360360

361-
// Convert environment variables to Kubernetes format.
362-
// Sort keys so the resulting list is deterministic — Go map iteration
363-
// is randomized, and any ordering shift here changes the pod template
364-
// hash and triggers an unnecessary StatefulSet rollout (#5063).
365-
envKeys := make([]string, 0, len(envVars))
366-
for k := range envVars {
367-
envKeys = append(envKeys, k)
368-
}
369-
sort.Strings(envKeys)
370-
envVarList := make([]*corev1apply.EnvVarApplyConfiguration, 0, len(envKeys))
371-
for _, k := range envKeys {
372-
envVarList = append(envVarList, corev1apply.EnvVar().WithName(k).WithValue(envVars[k]))
373-
}
361+
envVarList := buildSortedEnvVarList(envVars)
374362

375363
// Create a pod template spec
376364
podTemplateSpec := ensureObjectMetaApplyConfigurationExists(corev1apply.PodTemplateSpec())
@@ -457,6 +445,23 @@ func (c *Client) DeployWorkload(ctx context.Context,
457445
return 0, nil
458446
}
459447

448+
// buildSortedEnvVarList converts an env var map to Kubernetes apply configurations
449+
// with deterministically ordered keys. Go map iteration is randomized, and any
450+
// ordering shift changes the pod template hash and triggers an unnecessary
451+
// StatefulSet rollout (#5063).
452+
func buildSortedEnvVarList(envVars map[string]string) []*corev1apply.EnvVarApplyConfiguration {
453+
envKeys := make([]string, 0, len(envVars))
454+
for k := range envVars {
455+
envKeys = append(envKeys, k)
456+
}
457+
sort.Strings(envKeys)
458+
envVarList := make([]*corev1apply.EnvVarApplyConfiguration, 0, len(envKeys))
459+
for _, k := range envKeys {
460+
envVarList = append(envVarList, corev1apply.EnvVar().WithName(k).WithValue(envVars[k]))
461+
}
462+
return envVarList
463+
}
464+
460465
// runConfigGeneration extracts the RunConfig MCPServer generation from options,
461466
// returning 0 when options is nil (backward-compat / non-operator callers).
462467
func runConfigGeneration(options *runtime.DeployWorkloadOptions) int64 {

0 commit comments

Comments
 (0)