"vscode:/vscode.git/clone" did not exist on "439e977d9c751ef80f1ed72f03078dc408137a74"
pod.go 8.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package controller_common

import (
	"sort"

	corev1 "k8s.io/api/core/v1"
)

// CanonicalizePodSpec sorts the pod spec in a way that is deterministic and easy to reason about.
//
//nolint:gocyclo
func CanonicalizePodSpec(podSpec *corev1.PodSpec) *corev1.PodSpec {
	// Helper function to get EnvFromSource sort key
	envFromKey := func(e corev1.EnvFromSource) string {
		if e.ConfigMapRef != nil {
			return "cm:" + e.ConfigMapRef.Name + ":" + e.Prefix
		}
		if e.SecretRef != nil {
			return "sec:" + e.SecretRef.Name + ":" + e.Prefix
		}
		return "other:" + e.Prefix
	}

	// Helper function to sort container-like fields (works for both Container and EphemeralContainer)
	sortContainerFields := func(env []corev1.EnvVar, envFrom []corev1.EnvFromSource, ports []corev1.ContainerPort, volumeMounts []corev1.VolumeMount, securityContext *corev1.SecurityContext) {
		// Sort env vars by name
		if len(env) > 1 {
			sort.Slice(env, func(i, j int) bool { return env[i].Name < env[j].Name })
		}

		// Sort envFrom by referenced source and prefix
		if len(envFrom) > 1 {
			sort.Slice(envFrom, func(i, j int) bool {
				return envFromKey(envFrom[i]) < envFromKey(envFrom[j])
			})
		}

		// Sort ports by name then port number
		if len(ports) > 1 {
			sort.Slice(ports, func(i, j int) bool {
				if ports[i].Name == ports[j].Name {
					return ports[i].ContainerPort < ports[j].ContainerPort
				}
				return ports[i].Name < ports[j].Name
			})
		}

		// Sort volume mounts by name then mount path
		if len(volumeMounts) > 1 {
			sort.Slice(volumeMounts, func(i, j int) bool {
				if volumeMounts[i].Name == volumeMounts[j].Name {
					return volumeMounts[i].MountPath < volumeMounts[j].MountPath
				}
				return volumeMounts[i].Name < volumeMounts[j].Name
			})
		}

		// Sort security context capability lists
		if securityContext != nil && securityContext.Capabilities != nil {
			if caps := securityContext.Capabilities.Add; len(caps) > 1 {
				sort.Slice(caps, func(i, j int) bool { return string(caps[i]) < string(caps[j]) })
			}
			if caps := securityContext.Capabilities.Drop; len(caps) > 1 {
				sort.Slice(caps, func(i, j int) bool { return string(caps[i]) < string(caps[j]) })
			}
		}
	}

	// Sort regular containers
	for i := range podSpec.Containers {
		c := &podSpec.Containers[i]
		sortContainerFields(c.Env, c.EnvFrom, c.Ports, c.VolumeMounts, c.SecurityContext)
	}
	if len(podSpec.Containers) > 1 {
		sort.Slice(podSpec.Containers, func(i, j int) bool {
			return podSpec.Containers[i].Name < podSpec.Containers[j].Name
		})
	}

	// Sort init containers
	for i := range podSpec.InitContainers {
		c := &podSpec.InitContainers[i]
		sortContainerFields(c.Env, c.EnvFrom, c.Ports, c.VolumeMounts, c.SecurityContext)
	}
	if len(podSpec.InitContainers) > 1 {
		sort.Slice(podSpec.InitContainers, func(i, j int) bool {
			return podSpec.InitContainers[i].Name < podSpec.InitContainers[j].Name
		})
	}

	// Sort ephemeral containers
	for i := range podSpec.EphemeralContainers {
		ec := &podSpec.EphemeralContainers[i]
		sortContainerFields(ec.Env, ec.EnvFrom, ec.Ports, ec.VolumeMounts, ec.SecurityContext)
	}
	if len(podSpec.EphemeralContainers) > 1 {
		sort.Slice(podSpec.EphemeralContainers, func(i, j int) bool {
			return podSpec.EphemeralContainers[i].Name < podSpec.EphemeralContainers[j].Name
		})
	}

	// Sort image pull secrets
	if len(podSpec.ImagePullSecrets) > 1 {
104
105
106
		uniqueSecrets := ensureUniqueImagePullSecrets(podSpec.ImagePullSecrets)
		sort.Slice(uniqueSecrets, func(i, j int) bool {
			return uniqueSecrets[i].Name < uniqueSecrets[j].Name
107
		})
108
		podSpec.ImagePullSecrets = uniqueSecrets
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
	}

	// Sort volumes and their nested items
	sortKeyToPathItems := func(items []corev1.KeyToPath) {
		if len(items) > 1 {
			sort.Slice(items, func(i, j int) bool {
				if items[i].Key == items[j].Key {
					return items[i].Path < items[j].Path
				}
				return items[i].Key < items[j].Key
			})
		}
	}

	for i := range podSpec.Volumes {
		v := &podSpec.Volumes[i]

		// ConfigMap items
		if v.ConfigMap != nil {
			sortKeyToPathItems(v.ConfigMap.Items)
		}

		// Secret items
		if v.Secret != nil {
			sortKeyToPathItems(v.Secret.Items)
		}

		// DownwardAPI items
		if v.DownwardAPI != nil && len(v.DownwardAPI.Items) > 1 {
			sort.Slice(v.DownwardAPI.Items, func(i, j int) bool {
				return v.DownwardAPI.Items[i].Path < v.DownwardAPI.Items[j].Path
			})
		}

		// Projected sources
		if v.Projected != nil {
			// Sort projected sources
			if len(v.Projected.Sources) > 1 {
				sort.Slice(v.Projected.Sources, func(i, j int) bool {
					getProjectionKey := func(p corev1.VolumeProjection) string {
						if p.ConfigMap != nil {
							return "cm:" + p.ConfigMap.Name
						}
						if p.Secret != nil {
							return "sec:" + p.Secret.Name
						}
						if p.DownwardAPI != nil {
							return "downward:"
						}
						if p.ServiceAccountToken != nil {
							return "sat:" + p.ServiceAccountToken.Audience
						}
						return "z:other"
					}
					return getProjectionKey(v.Projected.Sources[i]) < getProjectionKey(v.Projected.Sources[j])
				})
			}

			// Sort nested items for each projection
			for j := range v.Projected.Sources {
				p := &v.Projected.Sources[j]
				if p.ConfigMap != nil {
					sortKeyToPathItems(p.ConfigMap.Items)
				}
				if p.Secret != nil {
					sortKeyToPathItems(p.Secret.Items)
				}
				if p.DownwardAPI != nil && len(p.DownwardAPI.Items) > 1 {
					sort.Slice(p.DownwardAPI.Items, func(i, j int) bool {
						return p.DownwardAPI.Items[i].Path < p.DownwardAPI.Items[j].Path
					})
				}
			}
		}
	}

	// Sort volumes by name
	if len(podSpec.Volumes) > 1 {
		sort.Slice(podSpec.Volumes, func(i, j int) bool {
			return podSpec.Volumes[i].Name < podSpec.Volumes[j].Name
		})
	}

	// Sort tolerations
	if len(podSpec.Tolerations) > 1 {
		sort.Slice(podSpec.Tolerations, func(i, j int) bool {
			a, b := podSpec.Tolerations[i], podSpec.Tolerations[j]

			if a.Key != b.Key {
				return a.Key < b.Key
			}
			if string(a.Operator) != string(b.Operator) {
				return string(a.Operator) < string(b.Operator)
			}
			if a.Value != b.Value {
				return a.Value < b.Value
			}
			if string(a.Effect) != string(b.Effect) {
				return string(a.Effect) < string(b.Effect)
			}

			// Handle TolerationSeconds (could be nil)
			aSec, bSec := int64(0), int64(0)
			if a.TolerationSeconds != nil {
				aSec = *a.TolerationSeconds
			}
			if b.TolerationSeconds != nil {
				bSec = *b.TolerationSeconds
			}
			return aSec < bSec
		})
	}

	// Sort topology spread constraints
	if len(podSpec.TopologySpreadConstraints) > 1 {
		sort.Slice(podSpec.TopologySpreadConstraints, func(i, j int) bool {
			a, b := podSpec.TopologySpreadConstraints[i], podSpec.TopologySpreadConstraints[j]
			if a.TopologyKey != b.TopologyKey {
				return a.TopologyKey < b.TopologyKey
			}
			if string(a.WhenUnsatisfiable) != string(b.WhenUnsatisfiable) {
				return string(a.WhenUnsatisfiable) < string(b.WhenUnsatisfiable)
			}
			return a.MaxSkew < b.MaxSkew
		})
	}

	// Sort host aliases
	if len(podSpec.HostAliases) > 1 {
		// First sort hostnames within each alias
		for i := range podSpec.HostAliases {
			if len(podSpec.HostAliases[i].Hostnames) > 1 {
				sort.Strings(podSpec.HostAliases[i].Hostnames)
			}
		}
		// Then sort aliases by IP
		sort.Slice(podSpec.HostAliases, func(i, j int) bool {
			return podSpec.HostAliases[i].IP < podSpec.HostAliases[j].IP
		})
	}

	// Sort DNS config
	if podSpec.DNSConfig != nil {
		// Sort DNS options
		if len(podSpec.DNSConfig.Options) > 1 {
			sort.Slice(podSpec.DNSConfig.Options, func(i, j int) bool {
				if podSpec.DNSConfig.Options[i].Name == podSpec.DNSConfig.Options[j].Name {
					vi, vj := "", ""
					if podSpec.DNSConfig.Options[i].Value != nil {
						vi = *podSpec.DNSConfig.Options[i].Value
					}
					if podSpec.DNSConfig.Options[j].Value != nil {
						vj = *podSpec.DNSConfig.Options[j].Value
					}
					return vi < vj
				}
				return podSpec.DNSConfig.Options[i].Name < podSpec.DNSConfig.Options[j].Name
			})
		}

		// Sort nameservers and search domains
		if len(podSpec.DNSConfig.Nameservers) > 1 {
			sort.Strings(podSpec.DNSConfig.Nameservers)
		}
		if len(podSpec.DNSConfig.Searches) > 1 {
			sort.Strings(podSpec.DNSConfig.Searches)
		}
	}

	return podSpec
}
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

func ensureUniqueImagePullSecrets(secrets []corev1.LocalObjectReference) []corev1.LocalObjectReference {
	if len(secrets) == 0 {
		return nil
	}
	uniqueSecrets := make(map[string]corev1.LocalObjectReference)
	for _, secret := range secrets {
		uniqueSecrets[secret.Name] = secret
	}
	uniqueSecretsList := make([]corev1.LocalObjectReference, 0, len(uniqueSecrets))
	for secretName := range uniqueSecrets {
		uniqueSecretsList = append(uniqueSecretsList, corev1.LocalObjectReference{Name: secretName})
	}
	return uniqueSecretsList
}