Skip to content

Commit 16d413d

Browse files
committed
Move the contain func-
Signed-off-by: lubronzhan <[email protected]>
1 parent 35e410d commit 16d413d

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

internal/provisioner/objects/deployment/deployment.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ func DesiredDeployment(contour *model.Contour, image string) *appsv1.Deployment
101101
args = append(args, "--debug")
102102
}
103103

104+
contains := func(elements []string, v string) bool {
105+
for _, a := range elements {
106+
if a == v {
107+
return true
108+
}
109+
}
110+
return false
111+
}
112+
104113
if contour.Spec.WatchNamespaces != nil && len(contour.Spec.WatchNamespaces) > 0 && !contains(contour.Spec.WatchNamespaces, corev1.NamespaceAll) {
105114
args = append(args, fmt.Sprintf("--watch-namespaces=%s", strings.Join(contour.Spec.WatchNamespaces, ",")))
106115
}
@@ -330,12 +339,3 @@ func contourPodAnnotations(contour *model.Contour) map[string]string {
330339

331340
return annotations
332341
}
333-
334-
func contains(s []string, e string) bool {
335-
for _, a := range s {
336-
if a == e {
337-
return true
338-
}
339-
}
340-
return false
341-
}

internal/provisioner/objects/deployment/deployment_test.go

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,22 @@ func checkDeploymentHasStrategy(t *testing.T, ds *appsv1.Deployment, expected ap
136136
t.Errorf("deployment has unexpected strategy %q", expected)
137137
}
138138

139+
func ensureContainerDoesntHaveArg(t *testing.T, container *corev1.Container, arg string) {
140+
t.Helper()
141+
142+
for _, a := range container.Args {
143+
if a == arg {
144+
t.Errorf("container has arg %q", arg)
145+
}
146+
}
147+
}
148+
139149
func TestDesiredDeployment(t *testing.T) {
140150
name := "deploy-test"
141151
cntr := model.Default(fmt.Sprintf("%s-ns", name), name)
142152
icName := "test-ic"
143153
cntr.Spec.IngressClassName = &icName
144-
testNamespaces := []string{"ns1", "ns2", "ns3"}
154+
145155
resQutoa := corev1.ResourceRequirements{
146156
Limits: corev1.ResourceList{
147157
corev1.ResourceCPU: resource.MustParse("400m"),
@@ -165,9 +175,6 @@ func TestDesiredDeployment(t *testing.T) {
165175
// Change the Contour log level to test --debug.
166176
cntr.Spec.ContourLogLevel = v1alpha1.DebugLog
167177

168-
// Change the Contour watch namespaces flag
169-
cntr.Spec.WatchNamespaces = testNamespaces
170-
171178
cntr.Spec.ResourceLabels = map[string]string{
172179
"key": "value",
173180
}
@@ -211,14 +218,53 @@ func TestDesiredDeployment(t *testing.T) {
211218
arg = fmt.Sprintf("--kubernetes-debug=%d", cntr.Spec.KubernetesLogLevel)
212219
checkContainerHasArg(t, container, arg)
213220

214-
arg = fmt.Sprintf("--watch-namespaces=%s", strings.Join(testNamespaces, ","))
215-
checkContainerHasArg(t, container, arg)
216-
217221
checkDeploymentHasNodeSelector(t, deploy, nil)
218222
checkDeploymentHasTolerations(t, deploy, nil)
219223
checkDeploymentHasResourceRequirements(t, deploy, resQutoa)
220224
checkDeploymentHasStrategy(t, deploy, cntr.Spec.ContourDeploymentStrategy)
225+
}
221226

227+
func TestDesiredDeploymentWhenSettingWatchNamespaces(t *testing.T) {
228+
testCases := []struct {
229+
description string
230+
namespaces []string
231+
expectArgExist bool
232+
}{
233+
{
234+
description: "several valid namespaces",
235+
namespaces: []string{"ns1", "ns2"},
236+
expectArgExist: true,
237+
},
238+
{
239+
description: "single valid namespace",
240+
namespaces: []string{"ns1", "ns2"},
241+
expectArgExist: true,
242+
},
243+
{
244+
description: "include empty namespace",
245+
namespaces: []string{"ns1", ""},
246+
expectArgExist: false,
247+
},
248+
}
249+
250+
for _, tc := range testCases {
251+
t.Run(tc.description, func(t *testing.T) {
252+
name := "deploy-test"
253+
cntr := model.Default(fmt.Sprintf("%s-ns", name), name)
254+
icName := "test-ic"
255+
cntr.Spec.IngressClassName = &icName
256+
// Change the Contour watch namespaces flag
257+
cntr.Spec.WatchNamespaces = tc.namespaces
258+
deploy := DesiredDeployment(cntr, "ghcr.io/projectcontour/contour:test")
259+
container := checkDeploymentHasContainer(t, deploy, contourContainerName, true)
260+
arg := fmt.Sprintf("--watch-namespaces=%s", strings.Join(tc.namespaces, ","))
261+
if tc.expectArgExist {
262+
checkContainerHasArg(t, container, arg)
263+
} else {
264+
ensureContainerDoesntHaveArg(t, container, arg)
265+
}
266+
})
267+
}
222268
}
223269

224270
func TestNodePlacementDeployment(t *testing.T) {

0 commit comments

Comments
 (0)