From c801d5072767c7f4dd08490c4b7cb0a1d5fa09af Mon Sep 17 00:00:00 2001 From: Mikhail Fedosin Date: Tue, 21 Oct 2025 12:27:36 +0200 Subject: [PATCH] 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. --- internal/controller/component_customizer.go | 22 ++++++++++++++----- .../controller/component_customizer_test.go | 12 ++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/internal/controller/component_customizer.go b/internal/controller/component_customizer.go index 478afb63f..bbec97823 100644 --- a/internal/controller/component_customizer.go +++ b/internal/controller/component_customizer.go @@ -128,7 +128,9 @@ func customizeObjectsFn(provider operatorv1.GenericProvider) func(objs []unstruc func customizeDeployment(dSpec *operatorv1.DeploymentSpec, mSpec *operatorv1.ManagerSpec, d *appsv1.Deployment) error { // Customize deployment spec first. if dSpec != nil { - customizeDeploymentSpec(*dSpec, d) + if err := customizeDeploymentSpec(*dSpec, d); err != nil { + return err + } } // Run the customizeManagerContainer after, so it overrides anything in the deploymentSpec. @@ -146,7 +148,7 @@ func customizeDeployment(dSpec *operatorv1.DeploymentSpec, mSpec *operatorv1.Man return nil } -func customizeDeploymentSpec(dSpec operatorv1.DeploymentSpec, d *appsv1.Deployment) { +func customizeDeploymentSpec(dSpec operatorv1.DeploymentSpec, d *appsv1.Deployment) error { if dSpec.Replicas != nil { replicas := int32(*dSpec.Replicas) //nolint:gosec d.Spec.Replicas = ptr.To(replicas) @@ -173,8 +175,12 @@ func customizeDeploymentSpec(dSpec operatorv1.DeploymentSpec, d *appsv1.Deployme } for _, pc := range dSpec.Containers { - customizeContainer(pc, d) + if err := customizeContainer(pc, d); err != nil { + return err + } } + + return nil } // findManagerContainer finds manager container in the provider deployment. @@ -299,7 +305,7 @@ func customizeManagerContainer(mSpec *operatorv1.ManagerSpec, c *corev1.Containe } // customizeContainer customize provider container base on provider spec input. -func customizeContainer(cSpec operatorv1.ContainerSpec, d *appsv1.Deployment) { +func customizeContainer(cSpec operatorv1.ContainerSpec, d *appsv1.Deployment) error { for j, c := range d.Spec.Template.Spec.Containers { if c.Name == cSpec.Name { for an, av := range cSpec.Args { @@ -322,10 +328,14 @@ func customizeContainer(cSpec operatorv1.ContainerSpec, d *appsv1.Deployment) { if cSpec.Command != nil { c.Command = cSpec.Command } - } - d.Spec.Template.Spec.Containers[j] = c + d.Spec.Template.Spec.Containers[j] = c + + return nil + } } + + return fmt.Errorf("cannot find container %q in deployment %q", cSpec.Name, d.Name) } // setArg set container arguments. diff --git a/internal/controller/component_customizer_test.go b/internal/controller/component_customizer_test.go index a95f2b235..936554c9b 100644 --- a/internal/controller/component_customizer_test.go +++ b/internal/controller/component_customizer_test.go @@ -611,6 +611,18 @@ func TestCustomizeDeployment(t *testing.T) { }, expectedError: true, }, + { + name: "container customization for non-existent container", + inputDeploymentSpec: &operatorv1.DeploymentSpec{ + Containers: []operatorv1.ContainerSpec{ + { + Name: "NON-EXISTENT", + ImageURL: ptr.To("quay.io/dev/mydns:v3.4.2"), + }, + }, + }, + expectedError: true, + }, } for _, tc := range tests {