Skip to content

Commit a68ffe8

Browse files
Merge pull request #223 from muraee/hcp-labels
OCPBUGS-45073: Support HCP labels
2 parents 9dceffb + 0878bc0 commit a68ffe8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pkg/operator/starter.go

+34
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ func RunOperator(ctx context.Context, controllerConfig *controllercmd.Controller
263263
hyperShiftControlPlaneIsolationHook(hcpInformer.Lister(), controlPlaneNamespace),
264264
hyperShiftColocationHook(controlPlaneNamespace),
265265
hyperShiftNodeSelectorHook(hcpInformer.Lister(), controlPlaneNamespace),
266+
hyperShiftLabelsHook(hcpInformer.Lister(), controlPlaneNamespace),
266267
hyperShiftSetSecurityContext(),
267268
}
268269

@@ -420,6 +421,39 @@ func getHostedControlPlaneNodeSelector(hostedControlPlaneLister cache.GenericLis
420421
return nodeSelector, nil
421422
}
422423

424+
func hyperShiftLabelsHook(hcpLister cache.GenericLister, controlPlaneNamespace string) dc.DeploymentHookFunc {
425+
return func(_ *operatorv1.OperatorSpec, d *appsv1.Deployment) error {
426+
labels, err := getHostedControlPlaneLabels(hcpLister, controlPlaneNamespace)
427+
if err != nil {
428+
return err
429+
}
430+
431+
for key, value := range labels {
432+
// don't replace existing labels as they are used in the deployment's labelSelector.
433+
if _, exist := d.Spec.Template.Labels[key]; !exist {
434+
d.Spec.Template.Labels[key] = value
435+
}
436+
}
437+
return nil
438+
}
439+
}
440+
441+
func getHostedControlPlaneLabels(hostedControlPlaneLister cache.GenericLister, namespace string) (map[string]string, error) {
442+
hcp, err := getHostedControlPlane(hostedControlPlaneLister, namespace)
443+
if err != nil {
444+
return nil, err
445+
}
446+
labels, exists, err := unstructured.NestedStringMap(hcp.UnstructuredContent(), "spec", "labels")
447+
if !exists {
448+
return nil, nil
449+
}
450+
if err != nil {
451+
return nil, err
452+
}
453+
klog.V(4).Infof("Using labels %v", labels)
454+
return labels, nil
455+
}
456+
423457
func getHostedControlPlaneTolerations(hostedControlPlaneLister cache.GenericLister, namespace string) ([]corev1.Toleration, error) {
424458
hcp, err := getHostedControlPlane(hostedControlPlaneLister, namespace)
425459
if err != nil {

0 commit comments

Comments
 (0)