Skip to content

Commit d423563

Browse files
committed
fix: customize only provider deployments
If there are several deployments in the manifest file, operator tries to apply provider changes to all of them, which leads to a failure. In order to prevent this, we will customize only deployments with "cluster.x-k8s.io/provider" label.
1 parent 8fc9605 commit d423563

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

internal/controller/component_customizer.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ import (
2929
"k8s.io/client-go/kubernetes/scheme"
3030
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
3131
"k8s.io/utils/pointer"
32-
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
32+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3333
"sigs.k8s.io/cluster-api/util"
34+
35+
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
3436
)
3537

3638
const (
@@ -95,6 +97,11 @@ func customizeObjectsFn(provider operatorv1.GenericProvider) func(objs []unstruc
9597

9698
// customizeDeployment customize provider deployment base on provider spec input.
9799
func customizeDeployment(pSpec operatorv1.ProviderSpec, d *appsv1.Deployment) error {
100+
// Ensure that we customize a manager deployment. It must contain "cluster.x-k8s.io/provider" label.
101+
if _, ok := d.GetLabels()[clusterv1.ProviderNameLabel]; !ok {
102+
return nil
103+
}
104+
98105
// Customize deployment spec first.
99106
if pSpec.Deployment != nil {
100107
customizeDeploymentSpec(pSpec, d)

internal/controller/component_customizer_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/util/intstr"
3030
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
3131
"k8s.io/utils/pointer"
32+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3233

3334
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
3435
)
@@ -40,6 +41,9 @@ func TestCustomizeDeployment(t *testing.T) {
4041
ObjectMeta: metav1.ObjectMeta{
4142
Name: "manager",
4243
Namespace: metav1.NamespaceSystem,
44+
Labels: map[string]string{
45+
clusterv1.ProviderNameLabel: "infra-provider",
46+
},
4347
},
4448
Spec: appsv1.DeploymentSpec{
4549
Template: corev1.PodTemplateSpec{
@@ -83,6 +87,7 @@ func TestCustomizeDeployment(t *testing.T) {
8387
inputDeploymentSpec *operatorv1.DeploymentSpec
8488
inputManagerSpec *operatorv1.ManagerSpec
8589
expectedDeploymentSpec func(*appsv1.DeploymentSpec) (*appsv1.DeploymentSpec, bool)
90+
noDeployementLabels bool
8691
}{
8792
{
8893
name: "empty",
@@ -550,6 +555,16 @@ func TestCustomizeDeployment(t *testing.T) {
550555
return expectedDS, reflect.DeepEqual(inputDS.Template.Spec.Containers[0], expectedDS.Template.Spec.Containers[0])
551556
},
552557
},
558+
{
559+
name: "no provider label on the deployment",
560+
inputDeploymentSpec: &operatorv1.DeploymentSpec{
561+
NodeSelector: map[string]string{"a": "b"},
562+
},
563+
expectedDeploymentSpec: func(inputDS *appsv1.DeploymentSpec) (*appsv1.DeploymentSpec, bool) {
564+
return &managerDepl.Spec, true
565+
},
566+
noDeployementLabels: true,
567+
},
553568
}
554569

555570
for _, tc := range tests {

0 commit comments

Comments
 (0)