Skip to content

Commit c801d50

Browse files
committed
Return error when customizing non-existent containers
Previously, when a user specified container customizations in the provider spec for a container that doesn't exist in the deployment, the operator would silently ignore the configuration. This made it difficult to debug misconfigured provider specs. This change adds error handling to the `customizeContainer` function to return an error when no matching container is found in the deployment. The error message includes both the container name and deployment name to help users quickly identify and fix the issue. This makes the error handling consistent with the existing pattern used for manager container validation.
1 parent 4eaea1d commit c801d50

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

internal/controller/component_customizer.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ func customizeObjectsFn(provider operatorv1.GenericProvider) func(objs []unstruc
128128
func customizeDeployment(dSpec *operatorv1.DeploymentSpec, mSpec *operatorv1.ManagerSpec, d *appsv1.Deployment) error {
129129
// Customize deployment spec first.
130130
if dSpec != nil {
131-
customizeDeploymentSpec(*dSpec, d)
131+
if err := customizeDeploymentSpec(*dSpec, d); err != nil {
132+
return err
133+
}
132134
}
133135

134136
// Run the customizeManagerContainer after, so it overrides anything in the deploymentSpec.
@@ -146,7 +148,7 @@ func customizeDeployment(dSpec *operatorv1.DeploymentSpec, mSpec *operatorv1.Man
146148
return nil
147149
}
148150

149-
func customizeDeploymentSpec(dSpec operatorv1.DeploymentSpec, d *appsv1.Deployment) {
151+
func customizeDeploymentSpec(dSpec operatorv1.DeploymentSpec, d *appsv1.Deployment) error {
150152
if dSpec.Replicas != nil {
151153
replicas := int32(*dSpec.Replicas) //nolint:gosec
152154
d.Spec.Replicas = ptr.To(replicas)
@@ -173,8 +175,12 @@ func customizeDeploymentSpec(dSpec operatorv1.DeploymentSpec, d *appsv1.Deployme
173175
}
174176

175177
for _, pc := range dSpec.Containers {
176-
customizeContainer(pc, d)
178+
if err := customizeContainer(pc, d); err != nil {
179+
return err
180+
}
177181
}
182+
183+
return nil
178184
}
179185

180186
// findManagerContainer finds manager container in the provider deployment.
@@ -299,7 +305,7 @@ func customizeManagerContainer(mSpec *operatorv1.ManagerSpec, c *corev1.Containe
299305
}
300306

301307
// customizeContainer customize provider container base on provider spec input.
302-
func customizeContainer(cSpec operatorv1.ContainerSpec, d *appsv1.Deployment) {
308+
func customizeContainer(cSpec operatorv1.ContainerSpec, d *appsv1.Deployment) error {
303309
for j, c := range d.Spec.Template.Spec.Containers {
304310
if c.Name == cSpec.Name {
305311
for an, av := range cSpec.Args {
@@ -322,10 +328,14 @@ func customizeContainer(cSpec operatorv1.ContainerSpec, d *appsv1.Deployment) {
322328
if cSpec.Command != nil {
323329
c.Command = cSpec.Command
324330
}
325-
}
326331

327-
d.Spec.Template.Spec.Containers[j] = c
332+
d.Spec.Template.Spec.Containers[j] = c
333+
334+
return nil
335+
}
328336
}
337+
338+
return fmt.Errorf("cannot find container %q in deployment %q", cSpec.Name, d.Name)
329339
}
330340

331341
// setArg set container arguments.

internal/controller/component_customizer_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,18 @@ func TestCustomizeDeployment(t *testing.T) {
611611
},
612612
expectedError: true,
613613
},
614+
{
615+
name: "container customization for non-existent container",
616+
inputDeploymentSpec: &operatorv1.DeploymentSpec{
617+
Containers: []operatorv1.ContainerSpec{
618+
{
619+
Name: "NON-EXISTENT",
620+
ImageURL: ptr.To("quay.io/dev/mydns:v3.4.2"),
621+
},
622+
},
623+
},
624+
expectedError: true,
625+
},
614626
}
615627

616628
for _, tc := range tests {

0 commit comments

Comments
 (0)