Skip to content

Commit

Permalink
Add type label to osctlplane object
Browse files Browse the repository at this point in the history
Adds map[string]string{"core.openstack.org/openstackcontrolplane": ""}
to the instance, if not already provided in the CR. With this
ctlplane object can be queried using the default label. Which
is useful e.g. in tests to query the ctlplane in a generic way
without the object name.
  • Loading branch information
stuggi committed Apr 17, 2024
1 parent 4e2085c commit 8ffd3ef
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions apis/core/v1beta1/openstackcontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -315,6 +316,7 @@ var _ webhook.Defaulter = &OpenStackControlPlane{}
func (r *OpenStackControlPlane) Default() {
openstackcontrolplanelog.Info("default", "name", r.Name)

r.DefaultLabel()
r.DefaultServices()
}

Expand Down Expand Up @@ -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] = ""
}
}
37 changes: 37 additions & 0 deletions tests/functional/openstackoperator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down

0 comments on commit 8ffd3ef

Please sign in to comment.