Skip to content

Commit 641696e

Browse files
committed
getRolloutsCommandArgs should return an error
Signed-off-by: Jonathan West <[email protected]>
1 parent 85db81b commit 641696e

File tree

4 files changed

+76
-35
lines changed

4 files changed

+76
-35
lines changed

controllers/configmap_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"sigs.k8s.io/controller-runtime/pkg/client"
1010

1111
appsv1 "k8s.io/api/apps/v1"
12-
v1 "k8s.io/api/apps/v1"
1312
corev1 "k8s.io/api/core/v1"
1413
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1514
)
@@ -19,7 +18,7 @@ var _ = Describe("ConfigMap Test", func() {
1918
var a v1alpha1.RolloutManager
2019
var r *RolloutManagerReconciler
2120
var sa *corev1.ServiceAccount
22-
var existingDeployment *v1.Deployment
21+
var existingDeployment *appsv1.Deployment
2322
const trafficrouterPluginLocation = "https://custom-traffic-plugin-location"
2423
const metricPluginLocation = "https://custom-metric-plugin-location"
2524

@@ -38,7 +37,11 @@ var _ = Describe("ConfigMap Test", func() {
3837
}
3938
Expect(r.Client.Create(ctx, sa)).To(Succeed())
4039

41-
existingDeployment = deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin-test", "tmp-test"}, "linux-test", sa.Name, a)
40+
var err error
41+
42+
existingDeployment, err = deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin-test", "tmp-test"}, "linux-test", sa.Name, a)
43+
Expect(err).ToNot(HaveOccurred())
44+
4245
Expect(r.Client.Create(ctx, existingDeployment)).To(Succeed())
4346

4447
})

controllers/deployment.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1717
)
1818

19-
func generateDesiredRolloutsDeployment(cr rolloutsmanagerv1alpha1.RolloutManager, sa corev1.ServiceAccount) appsv1.Deployment {
19+
func generateDesiredRolloutsDeployment(cr rolloutsmanagerv1alpha1.RolloutManager, sa corev1.ServiceAccount) (appsv1.Deployment, error) {
2020

2121
// NOTE: When updating this function, ensure that normalizeDeployment is updated as well. See that function for details.
2222

@@ -111,8 +111,12 @@ func generateDesiredRolloutsDeployment(cr rolloutsmanagerv1alpha1.RolloutManager
111111

112112
desiredPodSpec.ServiceAccountName = sa.ObjectMeta.Name
113113

114+
rolloutsCont, err := rolloutsContainer(cr)
115+
if err != nil {
116+
return appsv1.Deployment{}, err
117+
}
114118
desiredPodSpec.Containers = []corev1.Container{
115-
rolloutsContainer(cr),
119+
rolloutsCont,
116120
}
117121

118122
desiredPodSpec.Volumes = []corev1.Volume{
@@ -130,13 +134,16 @@ func generateDesiredRolloutsDeployment(cr rolloutsmanagerv1alpha1.RolloutManager
130134
},
131135
}
132136

133-
return desiredDeployment
137+
return desiredDeployment, nil
134138
}
135139

136140
// Reconcile the Rollouts controller deployment.
137141
func (r *RolloutManagerReconciler) reconcileRolloutsDeployment(ctx context.Context, cr rolloutsmanagerv1alpha1.RolloutManager, sa corev1.ServiceAccount) error {
138142

139-
desiredDeployment := generateDesiredRolloutsDeployment(cr, sa)
143+
desiredDeployment, err := generateDesiredRolloutsDeployment(cr, sa)
144+
if err != nil {
145+
return err
146+
}
140147

141148
normalizedDesiredDeployment, err := normalizeDeployment(desiredDeployment, cr)
142149
if err != nil {
@@ -292,7 +299,7 @@ func defaultRolloutsContainerResources() corev1.ResourceRequirements {
292299
}
293300
}
294301

295-
func rolloutsContainer(cr rolloutsmanagerv1alpha1.RolloutManager) corev1.Container {
302+
func rolloutsContainer(cr rolloutsmanagerv1alpha1.RolloutManager) (corev1.Container, error) {
296303

297304
// NOTE: When updating this function, ensure that normalizeDeployment is updated as well. See that function for details.
298305

@@ -308,8 +315,13 @@ func rolloutsContainer(cr rolloutsmanagerv1alpha1.RolloutManager) corev1.Contain
308315
containerResources = &defaultContainerResources
309316
}
310317

318+
commandArgs, err := getRolloutsCommandArgs(cr)
319+
if err != nil {
320+
return corev1.Container{}, err
321+
}
322+
311323
return corev1.Container{
312-
Args: getRolloutsCommandArgs(cr),
324+
Args: commandArgs,
313325
Env: rolloutsEnv,
314326
Image: getRolloutsContainerImage(cr),
315327
ImagePullPolicy: corev1.PullAlways,
@@ -374,7 +386,7 @@ func rolloutsContainer(cr rolloutsmanagerv1alpha1.RolloutManager) corev1.Contain
374386
},
375387
},
376388
Resources: *containerResources,
377-
}
389+
}, nil
378390

379391
}
380392

@@ -661,7 +673,7 @@ func getRolloutsContainerImage(cr rolloutsmanagerv1alpha1.RolloutManager) string
661673
}
662674

663675
// getRolloutsCommand will return the command for the Rollouts controller component.
664-
func getRolloutsCommandArgs(cr rolloutsmanagerv1alpha1.RolloutManager) []string {
676+
func getRolloutsCommandArgs(cr rolloutsmanagerv1alpha1.RolloutManager) ([]string, error) {
665677
args := make([]string, 0)
666678

667679
if cr.Spec.NamespaceScoped {
@@ -675,9 +687,9 @@ func getRolloutsCommandArgs(cr rolloutsmanagerv1alpha1.RolloutManager) []string
675687
extraArgs := cr.Spec.ExtraCommandArgs
676688
err := isMergable(extraArgs, args)
677689
if err != nil {
678-
return args
690+
return args, err
679691
}
680692

681693
args = append(args, extraArgs...)
682-
return args
694+
return args, nil
683695
}

controllers/deployment_test.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ var _ = Describe("Deployment Test", func() {
4949
fetchedDeployment := &appsv1.Deployment{}
5050
Expect(fetchObject(ctx, r.Client, a.Namespace, DefaultArgoRolloutsResourceName, fetchedDeployment)).To(Succeed())
5151

52-
expectedDeployment := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
52+
expectedDeployment, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
53+
Expect(err).ToNot(HaveOccurred())
5354

5455
By("verify that the fetched Deployment matches the desired one")
5556
Expect(fetchedDeployment.Name).To(Equal(expectedDeployment.Name))
@@ -67,7 +68,8 @@ var _ = Describe("Deployment Test", func() {
6768
When("Rollouts Deployment already exists, but then is modified away from default values", func() {
6869
It("should update the Deployment back to default values, but preserve any added annotations/labels", func() {
6970
By("create a new Deployment with custom values")
70-
existingDeployment := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin-test", "tmp-test"}, "linux-test", sa.Name, a)
71+
existingDeployment, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin-test", "tmp-test"}, "linux-test", sa.Name, a)
72+
Expect(err).ToNot(HaveOccurred())
7173

7274
existingDeployment.Labels["new-label"] = "new-label-value"
7375
existingDeployment.Annotations["new-annotation"] = "new-annotation-value"
@@ -81,7 +83,8 @@ var _ = Describe("Deployment Test", func() {
8183
fetchedDeployment := &appsv1.Deployment{}
8284
Expect(fetchObject(ctx, r.Client, a.Namespace, DefaultArgoRolloutsResourceName, fetchedDeployment)).To(Succeed())
8385

84-
expectedDeployment := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", sa.Name, a)
86+
expectedDeployment, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", sa.Name, a)
87+
Expect(err).ToNot(HaveOccurred())
8588

8689
By("verifing that the Deployment has been reconciled back to default values")
8790
Expect(fetchedDeployment.Name).To(Equal(expectedDeployment.Name))
@@ -194,7 +197,8 @@ var _ = Describe("Deployment Test", func() {
194197
It("should cause the existing Deployment to be deleted, and a new Deployment to be created with the updated .spec.selector", func() {
195198

196199
By("create a basic Rollout Deployment")
197-
existingDeployment := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
200+
existingDeployment, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
201+
Expect(err).ToNot(HaveOccurred())
198202

199203
By("assigning a fake UID to the original deployment, so we can detected when it is deleted/recreated")
200204
existingDeployment.ObjectMeta.UID = "original-deployment"
@@ -224,7 +228,8 @@ var _ = Describe("Deployment Test", func() {
224228
Expect(fetchObject(ctx, r.Client, a.Namespace, DefaultArgoRolloutsResourceName, fetchedDeployment)).To(Succeed())
225229
Expect(fetchedDeployment.ObjectMeta.UID).To(Equal(types.UID("")), "UID should be empty, because the original Deployment was deleted and recreated")
226230

227-
expectedDeployment := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", sa.Name, a)
231+
expectedDeployment, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", sa.Name, a)
232+
Expect(err).ToNot(HaveOccurred())
228233

229234
By("verifying that the Deployment has been reconciled back to default labels")
230235
Expect(fetchedDeployment.Name).To(Equal(expectedDeployment.Name))
@@ -240,10 +245,12 @@ var _ = Describe("Deployment Test", func() {
240245
When("the deployment has not changed", func() {
241246
It("should not report any difference, both before and after normalization", func() {
242247

243-
expectedDeployment := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
248+
expectedDeployment, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
249+
Expect(err).ToNot(HaveOccurred())
244250
Expect(identifyDeploymentDifference(*expectedDeployment, *expectedDeployment)).To(Equal(""), "comparing the object with itself should always report no differences")
245251

246-
expectedDeployment = deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
252+
expectedDeployment, err = deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
253+
Expect(err).ToNot(HaveOccurred())
247254
expectedDeploymentNormalized, err := normalizeDeployment(*expectedDeployment, a)
248255
Expect(err).To(Succeed())
249256

@@ -258,7 +265,8 @@ var _ = Describe("Deployment Test", func() {
258265

259266
It("should ensure the user-defiend labels/annotations are be removed by called to normalizeDeployment, while preserving the values contributed by the operation", func() {
260267

261-
originalDeployment := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
268+
originalDeployment, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
269+
Expect(err).ToNot(HaveOccurred())
262270

263271
By("creating a new object with user added labels/annotations")
264272
new := originalDeployment.DeepCopy()
@@ -308,9 +316,11 @@ var _ = Describe("Deployment Test", func() {
308316
DescribeTable("controller should detect and revert the change", func(fxn func(deployment *appsv1.Deployment)) {
309317

310318
By("ensuring that deploymentCR properly detects the change")
311-
defaultDeployment := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
319+
defaultDeployment, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
320+
Expect(err).ToNot(HaveOccurred())
312321

313-
defaultDeploymentModified := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
322+
defaultDeploymentModified, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
323+
Expect(err).ToNot(HaveOccurred())
314324

315325
Expect(identifyDeploymentDifference(*defaultDeployment, *defaultDeploymentModified)).To(BeEmpty(), "they should be the same before one is modified")
316326

@@ -455,7 +465,8 @@ var _ = Describe("Deployment Test", func() {
455465
fetchedDeployment := &appsv1.Deployment{}
456466
Expect(fetchObject(ctx, r.Client, a.Namespace, DefaultArgoRolloutsResourceName, fetchedDeployment)).To(Succeed())
457467

458-
expectedDeployment := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
468+
expectedDeployment, err := deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
469+
Expect(err).ToNot(HaveOccurred())
459470

460471
By("verify that the fetched Deployment matches the desired one")
461472
Expect(fetchedDeployment.Name).To(Equal(expectedDeployment.Name))
@@ -528,7 +539,8 @@ var _ = Describe("generateDesiredRolloutsDeployment tests", func() {
528539

529540
Context("when generating the desired deployment", func() {
530541
It("should set the correct metadata on the deployment", func() {
531-
deployment := generateDesiredRolloutsDeployment(cr, sa)
542+
deployment, err := generateDesiredRolloutsDeployment(cr, sa)
543+
Expect(err).ToNot(HaveOccurred())
532544
Expect(deployment.ObjectMeta.Name).To(Equal(DefaultArgoRolloutsResourceName))
533545
Expect(deployment.ObjectMeta.Namespace).To(Equal(cr.Namespace))
534546

@@ -538,7 +550,9 @@ var _ = Describe("generateDesiredRolloutsDeployment tests", func() {
538550
})
539551

540552
It("should set the NodeSelector and tolerations if NodePlacement is provided", func() {
541-
deployment := generateDesiredRolloutsDeployment(cr, sa)
553+
deployment, err := generateDesiredRolloutsDeployment(cr, sa)
554+
Expect(err).ToNot(HaveOccurred())
555+
542556
Expect(deployment.Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"kubernetes.io/os": "linux", "key1": "value1"}))
543557
Expect(deployment.Spec.Template.Spec.Tolerations).To(ContainElement(corev1.Toleration{
544558
Key: "key1",
@@ -548,18 +562,21 @@ var _ = Describe("generateDesiredRolloutsDeployment tests", func() {
548562

549563
It("should set the default node selector if NodePlacement is not provided", func() {
550564
cr.Spec.NodePlacement = nil
551-
deployment := generateDesiredRolloutsDeployment(cr, sa)
565+
deployment, err := generateDesiredRolloutsDeployment(cr, sa)
566+
Expect(err).ToNot(HaveOccurred())
552567
Expect(deployment.Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"kubernetes.io/os": "linux"}))
553568
Expect(deployment.Spec.Template.Spec.Tolerations).To(BeNil())
554569
})
555570

556571
It("should set the service account name", func() {
557-
deployment := generateDesiredRolloutsDeployment(cr, sa)
572+
deployment, err := generateDesiredRolloutsDeployment(cr, sa)
573+
Expect(err).ToNot(HaveOccurred())
558574
Expect(deployment.Spec.Template.Spec.ServiceAccountName).To(Equal(sa.ObjectMeta.Name))
559575
})
560576

561577
It("should add the correct volumes", func() {
562-
deployment := generateDesiredRolloutsDeployment(cr, sa)
578+
deployment, err := generateDesiredRolloutsDeployment(cr, sa)
579+
Expect(err).ToNot(HaveOccurred())
563580
Expect(deployment.Spec.Template.Spec.Volumes).To(HaveLen(2))
564581
Expect(deployment.Spec.Template.Spec.Volumes).To(ContainElement(corev1.Volume{
565582
Name: "plugin-bin",
@@ -587,7 +604,9 @@ var _ = Describe("normalizeDeployment tests to verify that an error is returned"
587604
a = *makeTestRolloutManager()
588605

589606
// Set up a valid deployment object
590-
deployment = deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
607+
var err error
608+
deployment, err = deploymentCR(DefaultArgoRolloutsResourceName, a.Namespace, DefaultArgoRolloutsResourceName, []string{"plugin-bin", "tmp"}, "linux", DefaultArgoRolloutsResourceName, a)
609+
Expect(err).ToNot(HaveOccurred())
591610
})
592611

593612
DescribeTable("should return an error when",
@@ -790,7 +809,8 @@ var _ = Describe("rolloutsContainer tests", func() {
790809
}
791810

792811
By("Call rolloutsContainer function")
793-
container := rolloutsContainer(cr)
812+
container, err := rolloutsContainer(cr)
813+
Expect(err).ToNot(HaveOccurred())
794814

795815
By("Verify the environment variables")
796816
expectedEnvVars := map[string]string{
@@ -809,7 +829,7 @@ var _ = Describe("rolloutsContainer tests", func() {
809829
})
810830
})
811831

812-
func deploymentCR(name string, namespace string, rolloutsSelectorLabel string, volumeNames []string, nodeSelector string, serviceAccount string, rolloutManager v1alpha1.RolloutManager) *appsv1.Deployment {
832+
func deploymentCR(name string, namespace string, rolloutsSelectorLabel string, volumeNames []string, nodeSelector string, serviceAccount string, rolloutManager v1alpha1.RolloutManager) (*appsv1.Deployment, error) {
813833
runAsNonRoot := true
814834
deploymentCR := &appsv1.Deployment{
815835
ObjectMeta: metav1.ObjectMeta{
@@ -822,6 +842,12 @@ func deploymentCR(name string, namespace string, rolloutsSelectorLabel string, v
822842
if rolloutManager.Spec.HA != nil && rolloutManager.Spec.HA.Enabled {
823843
replicas = 2
824844
}
845+
846+
rolloutsContainer, err := rolloutsContainer(rolloutManager)
847+
if err != nil {
848+
return nil, err
849+
}
850+
825851
deploymentCR.Spec = appsv1.DeploymentSpec{
826852
Replicas: &replicas,
827853
Selector: &metav1.LabelSelector{
@@ -855,7 +881,7 @@ func deploymentCR(name string, namespace string, rolloutsSelectorLabel string, v
855881
"kubernetes.io/os": nodeSelector,
856882
},
857883
Containers: []corev1.Container{
858-
rolloutsContainer(rolloutManager),
884+
rolloutsContainer,
859885
},
860886
ServiceAccountName: serviceAccount,
861887
SecurityContext: &corev1.PodSecurityContext{
@@ -865,6 +891,6 @@ func deploymentCR(name string, namespace string, rolloutsSelectorLabel string, v
865891
},
866892
}
867893

868-
return deploymentCR
894+
return deploymentCR, nil
869895

870896
}

controllers/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
UnsupportedRolloutManagerConfiguration = "when there exists a cluster-scoped RolloutManager on the cluster, there may not exist another: only a single cluster-scoped RolloutManager is supported"
2121
UnsupportedRolloutManagerClusterScoped = "when Subscription has environment variable NAMESPACE_SCOPED_ARGO_ROLLOUTS set to True, there may not exist any cluster-scoped RolloutManagers: in this case, only namespace-scoped RolloutManager resources are supported"
2222
UnsupportedRolloutManagerNamespaceScoped = "when Subscription has environment variable NAMESPACE_SCOPED_ARGO_ROLLOUTS set to False, there may not exist any namespace-scoped RolloutManagers: only a single cluster-scoped RolloutManager is supported"
23-
UnsupportedRolloutManagerClusterScopedNamespace = "Namespace is not specified in CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES environment variable of Subscription resource. If you wish to install a cluster-scoped Argo Rollouts instance outside the default namespace, ensure it is defined in CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES"
23+
UnsupportedRolloutManagerClusterScopedNamespace = "namespace is not specified in CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES environment variable of Subscription resource. If you wish to install a cluster-scoped Argo Rollouts instance outside the default namespace, ensure it is defined in CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES"
2424
)
2525

2626
// pluginItem is a clone of PluginItem from "github.com/argoproj/argo-rollouts/utils/plugin/types"

0 commit comments

Comments
 (0)