diff --git a/apis/core/v1beta1/openstackcontrolplane_webhook.go b/apis/core/v1beta1/openstackcontrolplane_webhook.go index 2e497052f..84a7de45f 100644 --- a/apis/core/v1beta1/openstackcontrolplane_webhook.go +++ b/apis/core/v1beta1/openstackcontrolplane_webhook.go @@ -18,6 +18,7 @@ package v1beta1 import ( "fmt" + "strings" "github.com/openstack-k8s-operators/lib-common/modules/common/route" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -315,6 +316,7 @@ var _ webhook.Defaulter = &OpenStackControlPlane{} func (r *OpenStackControlPlane) Default() { openstackcontrolplanelog.Info("default", "name", r.Name) + r.DefaultLabel() r.DefaultServices() } @@ -420,3 +422,17 @@ func (r *OpenStackControlPlane) DefaultServices() { // Barbican r.Spec.Barbican.Template.Default() } + +// DefaultLabel - adding default label to the OpenStackControlPlane +func (r *OpenStackControlPlane) DefaultLabel() { + // adds map[string]string{"core.openstack.org/openstackcontrolplane": r.name>} to the + // instance, if not already provided in the CR. With this ctlplane object can be + // queried using the default label. + typeLabel := strings.ToLower(r.GroupVersionKind().Group + "/" + r.Kind) + if _, ok := r.Labels[typeLabel]; !ok { + if r.Labels == nil { + r.Labels = map[string]string{} + } + r.Labels[typeLabel] = "" + } +} diff --git a/tests/functional/openstackoperator_controller_test.go b/tests/functional/openstackoperator_controller_test.go index 22d19c453..2452fc887 100644 --- a/tests/functional/openstackoperator_controller_test.go +++ b/tests/functional/openstackoperator_controller_test.go @@ -563,6 +563,43 @@ var _ = Describe("OpenStackOperator controller", func() { var _ = Describe("OpenStackOperator Webhook", func() { + It("Adds default label via defaulting webhook", func() { + spec := GetDefaultOpenStackControlPlaneSpec() + DeferCleanup( + th.DeleteInstance, + CreateOpenStackControlPlane(names.OpenStackControlplaneName, spec), + ) + + OSCtlplane := GetOpenStackControlPlane(names.OpenStackControlplaneName) + Expect(OSCtlplane.Labels).Should(Not(BeNil())) + Expect(OSCtlplane.Labels).Should(HaveKeyWithValue("core.openstack.org/openstackcontrolplane", "")) + }) + + It("Does not override default label via defaulting webhook when provided", func() { + spec := GetDefaultOpenStackControlPlaneSpec() + raw := map[string]interface{}{ + "apiVersion": "core.openstack.org/v1beta1", + "kind": "OpenStackControlPlane", + "metadata": map[string]interface{}{ + "name": "openstack", + "namespace": namespace, + "labels": map[string]interface{}{ + "core.openstack.org/openstackcontrolplane": "foo", + }, + }, + "spec": spec, + } + unstructuredObj := &unstructured.Unstructured{Object: raw} + _, err := controllerutil.CreateOrPatch( + ctx, k8sClient, unstructuredObj, func() error { return nil }) + + Expect(err).ShouldNot(HaveOccurred()) + + OSCtlplane := GetOpenStackControlPlane(types.NamespacedName{Name: "openstack", Namespace: namespace}) + Expect(OSCtlplane.Labels).Should(Not(BeNil())) + Expect(OSCtlplane.Labels).Should(HaveKeyWithValue("core.openstack.org/openstackcontrolplane", "foo")) + }) + It("calls placement validation webhook", func() { spec := GetDefaultOpenStackControlPlaneSpec() spec["placement"] = map[string]interface{}{