Skip to content

Commit d952cdf

Browse files
knative-prow-robotCali0707creydr
authored
[release-1.18] fix: containersource template labels are correctly set on deployment (#8645)
* fix: containersource deployment respects template labels Signed-off-by: Calum Murray <[email protected]> * test: containersource deployment respects template labels Signed-off-by: Calum Murray <[email protected]> * fix: containersource also propagates annotations to deployment Signed-off-by: Calum Murray <[email protected]> * Fix linter issue --------- Signed-off-by: Calum Murray <[email protected]> Co-authored-by: Calum Murray <[email protected]> Co-authored-by: Christoph Stäbler <[email protected]>
1 parent 9b25049 commit d952cdf

File tree

2 files changed

+123
-5
lines changed

2 files changed

+123
-5
lines changed

pkg/reconciler/containersource/resources/deployment.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package resources
1818

1919
import (
20+
"maps"
21+
2022
appsv1 "k8s.io/api/apps/v1"
2123
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224
v1 "knative.dev/eventing/pkg/apis/sources/v1"
@@ -29,9 +31,7 @@ func MakeDeployment(source *v1.ContainerSource) *appsv1.Deployment {
2931
template.Labels = make(map[string]string)
3032
}
3133
labels := Labels(source.Name)
32-
for k, v := range labels {
33-
template.Labels[k] = v
34-
}
34+
maps.Copy(template.Labels, labels)
3535

3636
deploy := &appsv1.Deployment{
3737
TypeMeta: metav1.TypeMeta{
@@ -44,11 +44,12 @@ func MakeDeployment(source *v1.ContainerSource) *appsv1.Deployment {
4444
OwnerReferences: []metav1.OwnerReference{
4545
*kmeta.NewControllerRef(source),
4646
},
47-
Labels: labels,
47+
Labels: template.Labels,
48+
Annotations: template.Annotations,
4849
},
4950
Spec: appsv1.DeploymentSpec{
5051
Selector: &metav1.LabelSelector{
51-
MatchLabels: labels,
52+
MatchLabels: template.Labels,
5253
},
5354
Template: template,
5455
},

pkg/reconciler/containersource/resources/deployment_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,123 @@ func TestMakeDeployment(t *testing.T) {
148148
},
149149
},
150150

151+
{
152+
name: "valid container source with one container, labels are respected",
153+
source: &v1.ContainerSource{
154+
ObjectMeta: metav1.ObjectMeta{
155+
Name: name,
156+
Namespace: "test-namespace",
157+
UID: uid,
158+
},
159+
Spec: v1.ContainerSourceSpec{
160+
Template: corev1.PodTemplateSpec{
161+
ObjectMeta: metav1.ObjectMeta{
162+
Labels: map[string]string{
163+
"example": "value",
164+
},
165+
},
166+
Spec: corev1.PodSpec{
167+
ServiceAccountName: "test-service-account",
168+
Containers: []corev1.Container{
169+
{
170+
Name: "test-source",
171+
Image: "test-image",
172+
Args: []string{"--test1=args1", "--test2=args2"},
173+
Env: []corev1.EnvVar{
174+
{
175+
Name: "test1",
176+
Value: "arg1",
177+
},
178+
{
179+
Name: "test2",
180+
ValueFrom: &corev1.EnvVarSource{
181+
SecretKeyRef: &corev1.SecretKeySelector{
182+
Key: "test2-secret",
183+
},
184+
},
185+
},
186+
},
187+
ImagePullPolicy: corev1.PullIfNotPresent,
188+
},
189+
},
190+
},
191+
},
192+
SourceSpec: duckv1.SourceSpec{
193+
Sink: duckv1.Destination{
194+
URI: apis.HTTP("test-sink"),
195+
},
196+
},
197+
},
198+
},
199+
want: &appsv1.Deployment{
200+
TypeMeta: metav1.TypeMeta{
201+
APIVersion: "apps/v1",
202+
Kind: "Deployment",
203+
},
204+
ObjectMeta: metav1.ObjectMeta{
205+
Name: fmt.Sprintf("%s-deployment", name),
206+
Namespace: "test-namespace",
207+
OwnerReferences: []metav1.OwnerReference{{
208+
APIVersion: "sources.knative.dev/v1",
209+
Kind: "ContainerSource",
210+
Name: name,
211+
UID: uid,
212+
Controller: &yes,
213+
BlockOwnerDeletion: &yes,
214+
}},
215+
Labels: map[string]string{
216+
"sources.knative.dev/containerSource": name,
217+
"sources.knative.dev/source": "container-source-controller",
218+
"example": "value",
219+
},
220+
},
221+
Spec: appsv1.DeploymentSpec{
222+
Selector: &metav1.LabelSelector{
223+
MatchLabels: map[string]string{
224+
"sources.knative.dev/containerSource": name,
225+
"sources.knative.dev/source": "container-source-controller",
226+
"example": "value",
227+
},
228+
},
229+
Template: corev1.PodTemplateSpec{
230+
ObjectMeta: metav1.ObjectMeta{
231+
Labels: map[string]string{
232+
"sources.knative.dev/containerSource": name,
233+
"sources.knative.dev/source": "container-source-controller",
234+
"example": "value",
235+
},
236+
},
237+
Spec: corev1.PodSpec{
238+
ServiceAccountName: "test-service-account",
239+
Containers: []corev1.Container{
240+
{
241+
Name: "test-source",
242+
Image: "test-image",
243+
Args: []string{
244+
"--test1=args1",
245+
"--test2=args2",
246+
},
247+
Env: []corev1.EnvVar{
248+
{
249+
Name: "test1",
250+
Value: "arg1",
251+
}, {
252+
Name: "test2",
253+
ValueFrom: &corev1.EnvVarSource{
254+
SecretKeyRef: &corev1.SecretKeySelector{
255+
Key: "test2-secret",
256+
},
257+
},
258+
}},
259+
ImagePullPolicy: corev1.PullIfNotPresent,
260+
},
261+
},
262+
},
263+
},
264+
},
265+
},
266+
},
267+
151268
{
152269
name: "valid container source with two containers",
153270
source: &v1.ContainerSource{

0 commit comments

Comments
 (0)