Skip to content

Commit 9c0402a

Browse files
committed
Support HCP labels
1 parent 1aba998 commit 9c0402a

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

pkg/driver/common/operator/hooks.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewDefaultOperatorControllerConfig(flavour generator.ClusterFlavour, c *cli
3939
cfg.AddDeploymentHookBuilders(c, withClusterWideProxy, withStandaloneReplicas)
4040
} else {
4141
// HyperShift
42-
cfg.AddDeploymentHookBuilders(c, withHyperShiftReplicas, withHyperShiftNodeSelector, withHyperShiftControlPlaneImages, withHyperShiftCustomTolerations)
42+
cfg.AddDeploymentHookBuilders(c, withHyperShiftReplicas, withHyperShiftNodeSelector, withHyperShiftLabels, withHyperShiftControlPlaneImages, withHyperShiftCustomTolerations)
4343
}
4444

4545
return cfg
@@ -141,6 +141,49 @@ func getHostedControlPlaneTolerations(hostedControlPlaneLister hypev1beta1lister
141141
return tolerations, nil
142142
}
143143

144+
// withHyperShiftLabels sets Deployment labels on a HyperShift hosted control-plane.
145+
func withHyperShiftLabels(c *clients.Clients) (dc.DeploymentHookFunc, []factory.Informer) {
146+
hook := func(_ *opv1.OperatorSpec, deployment *appsv1.Deployment) error {
147+
labels, err := getHostedControlLabels(
148+
c.ControlPlaneHypeInformer.Hypershift().V1beta1().HostedControlPlanes().Lister(),
149+
c.ControlPlaneNamespace)
150+
if err != nil {
151+
return err
152+
}
153+
154+
if deployment.Spec.Template.Labels == nil {
155+
deployment.Spec.Template.Labels = map[string]string{}
156+
}
157+
158+
for key, value := range labels {
159+
// don't replace existing labels as they are used in the deployment's labelSelector.
160+
if _, exist := deployment.Spec.Template.Labels[key]; !exist {
161+
deployment.Spec.Template.Labels[key] = value
162+
}
163+
}
164+
return nil
165+
}
166+
informers := []factory.Informer{
167+
c.ControlPlaneHypeInformer.Hypershift().V1beta1().HostedControlPlanes().Informer(),
168+
}
169+
return hook, informers
170+
}
171+
172+
// getHostedControlLabels returns the labels from the HostedControlPlane CR.
173+
func getHostedControlLabels(hostedControlPlaneLister hypev1beta1listers.HostedControlPlaneLister, namespace string) (map[string]string, error) {
174+
hcp, err := getHostedControlPlane(hostedControlPlaneLister, namespace)
175+
if err != nil {
176+
return nil, err
177+
}
178+
labels := hcp.Spec.Labels
179+
if len(labels) == 0 {
180+
return nil, nil
181+
}
182+
klog.V(4).Infof("Using labels %v", labels)
183+
return labels, nil
184+
185+
}
186+
144187
// getHostedControlPlane returns the HostedControlPlane CR.
145188
func getHostedControlPlane(hostedControlPlaneLister hypev1beta1listers.HostedControlPlaneLister, namespace string) (*hypev1beta1api.HostedControlPlane, error) {
146189
list, err := hostedControlPlaneLister.List(labels.Everything())

pkg/driver/common/operator/hooks_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,63 @@ func Test_WithHyperShiftNodeSelector(t *testing.T) {
146146
}
147147
}
148148

149+
func Test_WithHyperShiftLabels(t *testing.T) {
150+
tests := []struct {
151+
name string
152+
hcp *hypev1beta1api.HostedControlPlane
153+
expectedLabels map[string]string
154+
}{
155+
{
156+
name: "no labels",
157+
hcp: getTestHostedControlPlane("hcp_no_labels.yaml"),
158+
expectedLabels: nil,
159+
},
160+
{
161+
name: "labels",
162+
hcp: getTestHostedControlPlane("hcp_labels.yaml"),
163+
expectedLabels: map[string]string{
164+
"foo": "bar",
165+
"baz": "",
166+
},
167+
},
168+
{
169+
name: "existing labels should not be replaced",
170+
hcp: getTestHostedControlPlane("hcp_no_labels.yaml"),
171+
expectedLabels: map[string]string{
172+
"app": "aws-ebs-csi-driver-controller",
173+
"hypershift.openshift.io/hosted-control-plane": "clusters-test",
174+
},
175+
},
176+
}
177+
for _, tt := range tests {
178+
t.Run(tt.name, func(t *testing.T) {
179+
cr := clients.GetFakeOperatorCR()
180+
c := clients.NewFakeClients("clusters-test", cr)
181+
// Arrange: inject HostedControlPlane to the clients
182+
c.ControlPlaneHypeClient.(*fakehype.Clientset).Tracker().Add(tt.hcp)
183+
184+
hook, _ := withHyperShiftLabels(c)
185+
deployment := getTestDeployment()
186+
// Sync the informers with the client as the last step, withHyperShiftLabels()
187+
// must create necessary informers before.
188+
clients.SyncFakeInformers(t, c)
189+
190+
// Act
191+
err := hook(&cr.Spec.OperatorSpec, deployment)
192+
if err != nil {
193+
t.Fatalf("unexpected hook error: %v", err)
194+
}
195+
// Assert
196+
for key, expectedValue := range tt.expectedLabels {
197+
value, exist := deployment.Spec.Template.Labels[key]
198+
if !exist || value != expectedValue {
199+
t.Errorf("expected labels %s to exist with value %s", key, expectedValue)
200+
}
201+
}
202+
})
203+
}
204+
}
205+
149206
func Test_WithHyperShiftControlPlaneImages(t *testing.T) {
150207
tests := []struct {
151208
name string
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: hypershift.openshift.io/v1beta1
2+
kind: HostedControlPlane
3+
metadata:
4+
name: test
5+
namespace: clusters-test
6+
spec:
7+
labels:
8+
foo: bar
9+
baz: ""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: hypershift.openshift.io/v1beta1
2+
kind: HostedControlPlane
3+
metadata:
4+
name: test
5+
namespace: clusters-test
6+
spec: {}

0 commit comments

Comments
 (0)