Skip to content

Commit 10229e5

Browse files
committed
Add more logging and ignore some mutable fields to only check the init container and main container images
1 parent 68fe5c8 commit 10229e5

File tree

2 files changed

+27
-129
lines changed

2 files changed

+27
-129
lines changed

controllers/update_status.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ func podSpecIsCorrect(ctx context.Context,
12921292

12931293
// Check if any of the mutable fields of the Pod have been changed outside the operators control, e.g.
12941294
// by using kubectl.
1295-
if !mutablePodFieldsAreCorrect(desiredPodSpec, pod.Spec.DeepCopy()) {
1295+
if !mutablePodFieldsAreCorrect(logger, desiredPodSpec, pod.Spec.DeepCopy()) {
12961296
logger.Info(
12971297
"IncorrectPodSpec",
12981298
"currentSpecHash",
@@ -1332,39 +1332,31 @@ func podSpecIsCorrect(ctx context.Context,
13321332
// mutablePodFieldsAreCorrect checks if any of the pods mutable fields have been changed, see:
13331333
// https://kubernetes.io/docs/concepts/workloads/pods/#pod-update-and-replacement
13341334
func mutablePodFieldsAreCorrect(
1335+
logger logr.Logger,
13351336
desiredPodSpec *corev1.PodSpec,
13361337
currentPodSpec *corev1.PodSpec,
13371338
) bool {
13381339
for idx, container := range desiredPodSpec.Containers {
1340+
logger.V(1).
1341+
Info("compare images for container", "desiredContainerName", container.Name, "desiredImage", container.Name, "currentContainerName", currentPodSpec.Containers[idx].Name, "currentImage", currentPodSpec.Containers[idx].Image)
13391342
if !equality.Semantic.DeepEqual(container.Image, currentPodSpec.Containers[idx].Image) {
13401343
return false
13411344
}
13421345
}
13431346

13441347
for idx, container := range desiredPodSpec.InitContainers {
1348+
logger.V(1).
1349+
Info("compare images for init container", "desiredContainerName", container.Name, "desiredImage", container.Name, "currentContainerName", currentPodSpec.Containers[idx].Name, "currentImage", currentPodSpec.Containers[idx].Image)
13451350
if !equality.Semantic.DeepEqual(container.Image, currentPodSpec.InitContainers[idx].Image) {
13461351
return false
13471352
}
13481353
}
13491354

1350-
if !equality.Semantic.DeepEqual(
1351-
desiredPodSpec.ActiveDeadlineSeconds,
1352-
currentPodSpec.ActiveDeadlineSeconds,
1353-
) {
1354-
return false
1355-
}
1356-
1357-
if !equality.Semantic.DeepEqual(
1358-
desiredPodSpec.TerminationGracePeriodSeconds,
1359-
currentPodSpec.TerminationGracePeriodSeconds,
1360-
) {
1361-
return false
1362-
}
1363-
13641355
// We ignore the spec.schedulingGates in the comparison here. Otherwise, a component could make the Pod ready for
13651356
// being scheduled by removing the scheduling gate which then would case a recreation of the Pod. This would
1366-
// cause the Pod to be stuck in pending or recreation.
1357+
// cause the Pod to be stuck in pending or recreation. We also ignore the `Tolerations`, `ActiveDeadlineSeconds`
1358+
// and `TerminationGracePeriodSeconds`. Those have some restrictions on the update anyway.
13671359
// We could make use of the mutability of the tolerations field and remove the need for a Pod recreation when only
13681360
// tolerations are added.
1369-
return equality.Semantic.DeepEqual(desiredPodSpec.Tolerations, currentPodSpec.Tolerations)
1361+
return true
13701362
}

controllers/update_status_test.go

Lines changed: 18 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,34 +1790,6 @@ var _ = Describe("update_status", func() {
17901790
})
17911791
})
17921792

1793-
When("tolerations have been changed", func() {
1794-
BeforeEach(func() {
1795-
pod.Spec.Tolerations = append(
1796-
pod.Spec.Tolerations,
1797-
corev1.Toleration{
1798-
Key: "test-key",
1799-
Operator: corev1.TolerationOpEqual,
1800-
Value: "test-value",
1801-
Effect: corev1.TaintEffectNoSchedule,
1802-
},
1803-
)
1804-
Expect(k8sClient.Update(context.TODO(), pod)).NotTo(HaveOccurred())
1805-
})
1806-
1807-
It("should return false due to toleration mismatch", func() {
1808-
correct, err := podSpecIsCorrect(
1809-
context.TODO(),
1810-
clusterReconciler,
1811-
cluster,
1812-
pod,
1813-
processGroupStatus,
1814-
logger,
1815-
)
1816-
Expect(err).NotTo(HaveOccurred())
1817-
Expect(correct).To(BeFalse())
1818-
})
1819-
})
1820-
18211793
When("an error occurs getting the Pod spec", func() {
18221794
It("should still complete without panicking", func() {
18231795
// The method should handle errors gracefully even in edge cases
@@ -1863,7 +1835,9 @@ var _ = Describe("update_status", func() {
18631835

18641836
When("all mutable fields match", func() {
18651837
It("should return true", func() {
1866-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeTrue())
1838+
Expect(
1839+
mutablePodFieldsAreCorrect(testLogger, desiredPodSpec, currentPodSpec),
1840+
).To(BeTrue())
18671841
})
18681842
})
18691843

@@ -1873,7 +1847,9 @@ var _ = Describe("update_status", func() {
18731847
})
18741848

18751849
It("should return false", func() {
1876-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeFalse())
1850+
Expect(
1851+
mutablePodFieldsAreCorrect(testLogger, desiredPodSpec, currentPodSpec),
1852+
).To(BeFalse())
18771853
})
18781854
})
18791855

@@ -1889,7 +1865,9 @@ var _ = Describe("update_status", func() {
18891865
})
18901866

18911867
It("should return false", func() {
1892-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeFalse())
1868+
Expect(
1869+
mutablePodFieldsAreCorrect(testLogger, desiredPodSpec, currentPodSpec),
1870+
).To(BeFalse())
18931871
})
18941872
})
18951873

@@ -1908,85 +1886,9 @@ var _ = Describe("update_status", func() {
19081886
})
19091887

19101888
It("should return false", func() {
1911-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeFalse())
1912-
})
1913-
})
1914-
1915-
When("ActiveDeadlineSeconds is changed", func() {
1916-
BeforeEach(func() {
1917-
desiredPodSpec.ActiveDeadlineSeconds = ptr.To(int64(3600))
1918-
currentPodSpec.ActiveDeadlineSeconds = ptr.To(int64(7200))
1919-
})
1920-
1921-
It("should return false", func() {
1922-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeFalse())
1923-
})
1924-
})
1925-
1926-
When("TerminationGracePeriodSeconds is changed", func() {
1927-
BeforeEach(func() {
1928-
desiredPodSpec.TerminationGracePeriodSeconds = ptr.To(int64(30))
1929-
currentPodSpec.TerminationGracePeriodSeconds = ptr.To(int64(60))
1930-
})
1931-
1932-
It("should return false", func() {
1933-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeFalse())
1934-
})
1935-
})
1936-
1937-
When("tolerations are added", func() {
1938-
BeforeEach(func() {
1939-
currentPodSpec.Tolerations = append(
1940-
currentPodSpec.Tolerations,
1941-
corev1.Toleration{
1942-
Key: "added-toleration",
1943-
Operator: corev1.TolerationOpEqual,
1944-
Value: "added-value",
1945-
Effect: corev1.TaintEffectNoSchedule,
1946-
},
1947-
)
1948-
})
1949-
1950-
It("should return false", func() {
1951-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeFalse())
1952-
})
1953-
})
1954-
1955-
When("tolerations are removed", func() {
1956-
BeforeEach(func() {
1957-
desiredPodSpec.Tolerations = []corev1.Toleration{
1958-
{
1959-
Key: "test-toleration",
1960-
Operator: corev1.TolerationOpEqual,
1961-
Value: "test-value",
1962-
Effect: corev1.TaintEffectNoSchedule,
1963-
},
1964-
}
1965-
currentPodSpec.Tolerations = []corev1.Toleration{}
1966-
})
1967-
1968-
It("should return false", func() {
1969-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeFalse())
1970-
})
1971-
})
1972-
1973-
When("tolerations are modified", func() {
1974-
BeforeEach(func() {
1975-
toleration := corev1.Toleration{
1976-
Key: "test-toleration",
1977-
Operator: corev1.TolerationOpEqual,
1978-
Value: "original-value",
1979-
Effect: corev1.TaintEffectNoSchedule,
1980-
}
1981-
desiredPodSpec.Tolerations = []corev1.Toleration{toleration}
1982-
1983-
modifiedToleration := toleration
1984-
modifiedToleration.Value = "modified-value"
1985-
currentPodSpec.Tolerations = []corev1.Toleration{modifiedToleration}
1986-
})
1987-
1988-
It("should return false", func() {
1989-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeFalse())
1889+
Expect(
1890+
mutablePodFieldsAreCorrect(testLogger, desiredPodSpec, currentPodSpec),
1891+
).To(BeFalse())
19901892
})
19911893
})
19921894

@@ -2004,7 +1906,9 @@ var _ = Describe("update_status", func() {
20041906
})
20051907

20061908
It("should return false", func() {
2007-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeFalse())
1909+
Expect(
1910+
mutablePodFieldsAreCorrect(testLogger, desiredPodSpec, currentPodSpec),
1911+
).To(BeFalse())
20081912
})
20091913
})
20101914

@@ -2019,7 +1923,9 @@ var _ = Describe("update_status", func() {
20191923

20201924
It("should still return true as schedulingGates are ignored", func() {
20211925
// The method explicitly ignores schedulingGates per the comment in the code
2022-
Expect(mutablePodFieldsAreCorrect(desiredPodSpec, currentPodSpec)).To(BeTrue())
1926+
Expect(
1927+
mutablePodFieldsAreCorrect(testLogger, desiredPodSpec, currentPodSpec),
1928+
).To(BeTrue())
20231929
})
20241930
})
20251931
})

0 commit comments

Comments
 (0)