|
8 | 8 | "testing"
|
9 | 9 | "time"
|
10 | 10 |
|
| 11 | + "github.com/ghodss/yaml" |
11 | 12 | "github.com/stretchr/testify/assert"
|
12 | 13 | appsv1 "k8s.io/api/apps/v1"
|
13 | 14 | corev1 "k8s.io/api/core/v1"
|
@@ -1167,3 +1168,54 @@ func TestGetPodsOwnedByReplicaSet(t *testing.T) {
|
1167 | 1168 | assert.Len(t, pods, 1)
|
1168 | 1169 | assert.Equal(t, "guestbook-abc123", pods[0].Name)
|
1169 | 1170 | }
|
| 1171 | + |
| 1172 | +// TestPodTemplateEqualIgnoreHashWithServiceAccount catches a corner case where the K8s ComputeHash |
| 1173 | +// function changed from underneath us, and we fell back to deep equality checking, which then |
| 1174 | +// incorrectly detected a diff because of a deprecated field being present in the live but not desired. |
| 1175 | +func TestPodTemplateEqualIgnoreHashWithServiceAccount(t *testing.T) { |
| 1176 | + var desired corev1.PodTemplateSpec |
| 1177 | + desiredTemplate := ` |
| 1178 | +metadata: |
| 1179 | + labels: |
| 1180 | + app: serviceaccount-ro |
| 1181 | +spec: |
| 1182 | + containers: |
| 1183 | + - image: nginx:1.19-alpine |
| 1184 | + name: app |
| 1185 | + serviceAccountName: default |
| 1186 | +` |
| 1187 | + err := yaml.Unmarshal([]byte(desiredTemplate), &desired) |
| 1188 | + assert.NoError(t, err) |
| 1189 | + |
| 1190 | + // liveTemplate was captured from a ReplicaSet generated from the above desired template using |
| 1191 | + // Argo Rollouts v0.10. The rollouts-pod-template-hash value will not match newer hashing |
| 1192 | + // versions, causing PodTemplateEqualIgnoreHash to fall back to a deep equality check and |
| 1193 | + // pod template defaulting. |
| 1194 | + liveTemplate := ` |
| 1195 | +metadata: |
| 1196 | + creationTimestamp: null |
| 1197 | + labels: |
| 1198 | + app: serviceaccount-ro |
| 1199 | + rollouts-pod-template-hash: 8684587d99 |
| 1200 | +spec: |
| 1201 | + containers: |
| 1202 | + - image: nginx:1.19-alpine |
| 1203 | + imagePullPolicy: IfNotPresent |
| 1204 | + name: app |
| 1205 | + resources: {} |
| 1206 | + terminationMessagePath: /dev/termination-log |
| 1207 | + terminationMessagePolicy: File |
| 1208 | + dnsPolicy: ClusterFirst |
| 1209 | + restartPolicy: Always |
| 1210 | + schedulerName: default-scheduler |
| 1211 | + securityContext: {} |
| 1212 | + serviceAccount: default |
| 1213 | + serviceAccountName: default |
| 1214 | + terminationGracePeriodSeconds: 30 |
| 1215 | +` |
| 1216 | + var live corev1.PodTemplateSpec |
| 1217 | + err = yaml.Unmarshal([]byte(liveTemplate), &live) |
| 1218 | + assert.NoError(t, err) |
| 1219 | + |
| 1220 | + assert.True(t, PodTemplateEqualIgnoreHash(&live, &desired)) |
| 1221 | +} |
0 commit comments