Skip to content

Commit fca32c0

Browse files
author
igor.grzankowski
committed
Fix util tests
1 parent f29e480 commit fca32c0

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

pkg/splunk/controller/util.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,16 @@ func MergePodSpecUpdates(ctx context.Context, current *corev1.PodSpec, revised *
166166
}
167167
}
168168

169-
if current.TerminationGracePeriodSeconds != nil && revised.TerminationGracePeriodSeconds != nil {
170-
if *current.TerminationGracePeriodSeconds != *revised.TerminationGracePeriodSeconds {
171-
scopedLog.Info("Pod TerminationGracePeriodSeconds differs",
172-
"current", current.TerminationGracePeriodSeconds,
173-
"revised", revised.TerminationGracePeriodSeconds)
174-
current.TerminationGracePeriodSeconds = revised.TerminationGracePeriodSeconds
175-
result = true
176-
}
169+
if (current.TerminationGracePeriodSeconds == nil && revised.TerminationGracePeriodSeconds != nil) ||
170+
(current.TerminationGracePeriodSeconds != nil && revised.TerminationGracePeriodSeconds == nil) ||
171+
(current.TerminationGracePeriodSeconds != nil && revised.TerminationGracePeriodSeconds != nil &&
172+
*current.TerminationGracePeriodSeconds != *revised.TerminationGracePeriodSeconds) {
173+
174+
scopedLog.Info("Pod TerminationGracePeriodSeconds differs",
175+
"current", current.TerminationGracePeriodSeconds,
176+
"revised", revised.TerminationGracePeriodSeconds)
177+
current.TerminationGracePeriodSeconds = revised.TerminationGracePeriodSeconds
178+
result = true
177179
}
178180

179181
// check for changes in container images; assume that the ordering is same for pods with > 1 container
@@ -254,14 +256,44 @@ func MergePodSpecUpdates(ctx context.Context, current *corev1.PodSpec, revised *
254256
current.Containers[idx].StartupProbe = revised.Containers[idx].StartupProbe
255257
result = true
256258
}
257-
setPreStopLifecycleHandler(current, idx)
259+
260+
// check PreStop Lifecycle
261+
if hasPreStopChanged(current.Containers[idx].Lifecycle, revised.Containers[idx].Lifecycle) {
262+
scopedLog.Info("Pod Container PreStop Lifecycle differ",
263+
"current", current.Containers[idx].Lifecycle,
264+
"revised", revised.Containers[idx].Lifecycle)
265+
setPreStopLifecycleHandler(current, idx)
266+
result = true
267+
}
258268
}
259269
}
260270

261271
return result
262272
}
263273

264-
// set the PreStop lifecycle handler for the specified container index
274+
// Function to check if PreStop Lifecycle has changed
275+
func hasPreStopChanged(current, revised *corev1.Lifecycle) bool {
276+
// If both are nil, there's no change
277+
if current == nil && revised == nil {
278+
return false
279+
}
280+
281+
// If one is nil and the other isn't, there's a change
282+
if (current == nil || current.PreStop == nil || current.PreStop.Exec == nil) &&
283+
(revised != nil && revised.PreStop != nil && revised.PreStop.Exec != nil) {
284+
return true
285+
}
286+
287+
if (revised == nil || revised.PreStop == nil || revised.PreStop.Exec == nil) &&
288+
(current != nil && current.PreStop != nil && current.PreStop.Exec != nil) {
289+
return true
290+
}
291+
292+
// If both are non-nil, compare the command
293+
return !reflect.DeepEqual(current.PreStop.Exec.Command, revised.PreStop.Exec.Command)
294+
}
295+
296+
// Set the PreStop lifecycle handler for the specified container index
265297
func setPreStopLifecycleHandler(podSpec *corev1.PodSpec, idx int) {
266298
podSpec.Containers[idx].Lifecycle = &corev1.Lifecycle{
267299
PreStop: &corev1.LifecycleHandler{

pkg/splunk/controller/util_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ func TestMergePodUpdates(t *testing.T) {
104104
terminationGracePeriodSeconds := int64(60)
105105
revised.Spec.TerminationGracePeriodSeconds = &terminationGracePeriodSeconds
106106
matcher = func() bool {
107-
return reflect.DeepEqual(current.Spec.TerminationGracePeriodSeconds, revised.Spec.TerminationGracePeriodSeconds)
107+
if current.Spec.TerminationGracePeriodSeconds == nil {
108+
return true
109+
}
110+
if current.Spec.TerminationGracePeriodSeconds == revised.Spec.TerminationGracePeriodSeconds {
111+
return true
112+
}
113+
return false
108114
}
109115
podUpdateTester("TerminationGracePeriod updated")
110116

@@ -116,12 +122,16 @@ func TestMergePodUpdates(t *testing.T) {
116122
// check container different Ports
117123
revised.Spec.Containers = []corev1.Container{{Image: "splunk/splunk"}}
118124
revised.Spec.Containers[0].Ports = []corev1.ContainerPort{{ContainerPort: 8000}}
119-
matcher = func() bool { return reflect.DeepEqual(current.Spec.Containers, revised.Spec.Containers) }
125+
matcher = func() bool {
126+
return current.Spec.Containers[0].Ports[0].ContainerPort == revised.Spec.Containers[0].Ports[0].ContainerPort
127+
}
120128
podUpdateTester("Container Ports")
121129

122130
// check container different VolumeMounts
123131
revised.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{{Name: "mnt-splunk"}}
124-
matcher = func() bool { return reflect.DeepEqual(current.Spec.Containers, revised.Spec.Containers) }
132+
matcher = func() bool {
133+
return revised.Spec.Containers[0].VolumeMounts[0].Name == current.Spec.Containers[0].VolumeMounts[0].Name
134+
}
125135
podUpdateTester("Container VolumeMounts")
126136

127137
// check container different Resources
@@ -135,7 +145,20 @@ func TestMergePodUpdates(t *testing.T) {
135145
corev1.ResourceMemory: resource.MustParse("512Mi"),
136146
},
137147
}
138-
matcher = func() bool { return reflect.DeepEqual(current.Spec.Containers, revised.Spec.Containers) }
148+
matcher = func() bool {
149+
if len(current.Spec.Containers) != len(revised.Spec.Containers) {
150+
return false
151+
}
152+
153+
for i := range current.Spec.Containers {
154+
if !reflect.DeepEqual(current.Spec.Containers[i].Resources.Requests, revised.Spec.Containers[i].Resources.Requests) ||
155+
!reflect.DeepEqual(current.Spec.Containers[i].Resources.Limits, revised.Spec.Containers[i].Resources.Limits) {
156+
return false
157+
}
158+
}
159+
160+
return true
161+
}
139162
podUpdateTester("Container Resources")
140163

141164
// check pod env update

0 commit comments

Comments
 (0)