Skip to content

Commit 83e3ebf

Browse files
perdasilvaawgreene
andauthored
Return copy of annotations in grpcCatalogSourceDecorator (#2813)
* Update catalog source pod creation to not mutate parameters Signed-off-by: perdasilva <[email protected]> * Update pkg/controller/registry/reconciler/reconciler_test.go Co-authored-by: Alexander Greene <[email protected]> Signed-off-by: perdasilva <[email protected]> Co-authored-by: Alexander Greene <[email protected]>
1 parent d3bfaf2 commit 83e3ebf

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

pkg/controller/registry/reconciler/reconciler.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,26 @@ func Pod(source *operatorsv1alpha1.CatalogSource, name string, image string, saN
113113
pullPolicy = corev1.PullAlways
114114
}
115115

116+
// make a copy of the labels and annotations to avoid mutating the input parameters
117+
podLabels := make(map[string]string)
118+
podAnnotations := make(map[string]string)
119+
120+
for key, value := range labels {
121+
podLabels[key] = value
122+
}
123+
124+
for key, value := range annotations {
125+
podAnnotations[key] = value
126+
}
127+
116128
readOnlyRootFilesystem := false
117129

118130
pod := &corev1.Pod{
119131
ObjectMeta: metav1.ObjectMeta{
120132
GenerateName: source.GetName() + "-",
121133
Namespace: source.GetNamespace(),
122-
Labels: labels,
123-
Annotations: annotations,
134+
Labels: podLabels,
135+
Annotations: podAnnotations,
124136
},
125137
Spec: corev1.PodSpec{
126138
Containers: []corev1.Container{
@@ -207,25 +219,17 @@ func Pod(source *operatorsv1alpha1.CatalogSource, name string, image string, saN
207219
}
208220

209221
// Set priorityclass if its annotation exists
210-
if prio, ok := annotations[CatalogPriorityClassKey]; ok && prio != "" {
222+
if prio, ok := podAnnotations[CatalogPriorityClassKey]; ok && prio != "" {
211223
pod.Spec.PriorityClassName = prio
212224
}
213225

214226
// Add PodSpec hash
215227
// This hash info will be used to detect PodSpec changes
216-
if labels == nil {
217-
labels = make(map[string]string)
218-
}
219-
labels[PodHashLabelKey] = hashPodSpec(pod.Spec)
220-
pod.SetLabels(labels)
228+
podLabels[PodHashLabelKey] = hashPodSpec(pod.Spec)
221229

222230
// add eviction annotation to enable the cluster autoscaler to evict the pod in order to drain the node
223231
// since catalog pods are not backed by a controller, they cannot be evicted by default
224-
if annotations == nil {
225-
annotations = make(map[string]string)
226-
}
227-
annotations[ClusterAutoscalingAnnotationKey] = "true"
228-
pod.SetAnnotations(annotations)
232+
podAnnotations[ClusterAutoscalingAnnotationKey] = "true"
229233

230234
return pod
231235
}

pkg/controller/registry/reconciler/reconciler_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,37 @@ func TestPodContainerSecurityContext(t *testing.T) {
9595
require.Equal(t, expectedContainerSecCtx, gotContainerSecCtx)
9696
}
9797

98+
// TestPodAvoidsConcurrentWrite is a regression test for
99+
// https://bugzilla.redhat.com/show_bug.cgi?id=2101357
100+
// we were mutating the input annotations and labels parameters causing
101+
// concurrent write issues
102+
func TestPodAvoidsConcurrentWrite(t *testing.T) {
103+
catsrc := &v1alpha1.CatalogSource{
104+
ObjectMeta: metav1.ObjectMeta{
105+
Name: "test",
106+
Namespace: "testns",
107+
},
108+
}
109+
110+
labels := map[string]string{
111+
"label": "something",
112+
}
113+
114+
annotations := map[string]string{
115+
"annotation": "somethingelse",
116+
}
117+
118+
gotPod := Pod(catsrc, "hello", "busybox", "", labels, annotations, int32(0), int32(0))
119+
120+
// check labels and annotations point to different addresses between parameters and what's in the pod
121+
require.NotEqual(t, &labels, &gotPod.Labels)
122+
require.NotEqual(t, &annotations, &gotPod.Annotations)
123+
124+
// check that labels and annotations from the parameters were copied down to the pod's
125+
require.Equal(t, labels["label"], gotPod.Labels["label"])
126+
require.Equal(t, annotations["annotation"], gotPod.Annotations["annotation"])
127+
}
128+
98129
func TestPodSchedulingOverrides(t *testing.T) {
99130
// This test ensures that any overriding pod scheduling configuration elements
100131
// defined in spec.grpcPodConfig are applied to the catalog source pod created

0 commit comments

Comments
 (0)