Skip to content

Commit

Permalink
Merge pull request #239 from yashvardhan-kukreja/yashvardhan/namespac…
Browse files Browse the repository at this point in the history
…e-labels-and-annotations-support

[MTSRE-691] | feat: Addon API supporting .spec.namespaces.labels and .spec.namespaces.annotations
  • Loading branch information
openshift-merge-robot authored Sep 15, 2022
2 parents edd14dd + 12a2af7 commit 39bc82b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 13 deletions.
8 changes: 8 additions & 0 deletions apis/addons/v1alpha1/addons_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ type AddonNamespace struct {
// Name of the KubernetesNamespace.
// +kubebuilder:validation:MinLength=1
Name string `json:"name"`

// Labels to be added to the namespace
// +optional
Labels map[string]string `json:"labels,omitempty"`

// Annotations to be added to the namespace
// +optional
Annotations map[string]string `json:"annotations,omitempty"`
}

const (
Expand Down
18 changes: 17 additions & 1 deletion apis/addons/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions config/deploy/addons.managed.openshift.io_addons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ spec:
existing Namespaces being adopted.
items:
properties:
annotations:
additionalProperties:
type: string
description: Annotations to be added to the namespace
type: object
labels:
additionalProperties:
type: string
description: Labels to be added to the namespace
type: object
name:
description: Name of the KubernetesNamespace.
minLength: 1
Expand Down
10 changes: 10 additions & 0 deletions config/openshift/manifests/addons.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ spec:
existing Namespaces being adopted.
items:
properties:
annotations:
additionalProperties:
type: string
description: Annotations to be added to the namespace
type: object
labels:
additionalProperties:
type: string
description: Labels to be added to the namespace
type: object
name:
description: Name of the KubernetesNamespace.
minLength: 1
Expand Down
2 changes: 2 additions & 0 deletions docs/api-reference/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ AddonInstallSpec defines the desired Addon installation type.
| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| name | Name of the KubernetesNamespace. | string | true |
| labels | Labels to be added to the namespace | map[string]string | false |
| annotations | Annotations to be added to the namespace | map[string]string | false |
[Back to Group]()
Expand Down
17 changes: 17 additions & 0 deletions internal/controllers/addon/namespace_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package addon

import corev1 "k8s.io/api/core/v1"

type NamespaceOpts func(*corev1.Namespace)

func WithNamespaceLabels(labels map[string]string) NamespaceOpts {
return func(n *corev1.Namespace) {
n.ObjectMeta.Labels = labels
}
}

func WithNamespaceAnnotations(annotations map[string]string) NamespaceOpts {
return func(n *corev1.Namespace) {
n.ObjectMeta.Annotations = annotations
}
}
20 changes: 11 additions & 9 deletions internal/controllers/addon/namespace_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (r *namespaceReconciler) ensureWantedNamespaces(
var unreadyNamespaces []string

for _, namespace := range addon.Spec.Namespaces {
ensuredNamespace, err := r.ensureNamespace(ctx, addon, namespace.Name)
ensuredNamespace, err := r.ensureNamespace(ctx, addon, namespace.Name, WithNamespaceLabels(namespace.Labels), WithNamespaceAnnotations(namespace.Annotations))
if err != nil {
return err
}
Expand All @@ -135,18 +135,16 @@ func (r *namespaceReconciler) ensureWantedNamespaces(
}

// Ensure a single Namespace for the given Addon resource
func (r *namespaceReconciler) ensureNamespace(ctx context.Context, addon *addonsv1alpha1.Addon, name string) (*corev1.Namespace, error) {
return r.ensureNamespaceWithLabels(ctx, addon, name, map[string]string{})
}

// Ensure a single Namespace with a set of labels for the given Addon resource
func (r *namespaceReconciler) ensureNamespaceWithLabels(ctx context.Context, addon *addonsv1alpha1.Addon, name string, labels map[string]string) (*corev1.Namespace, error) {
func (r *namespaceReconciler) ensureNamespace(ctx context.Context, addon *addonsv1alpha1.Addon, name string, namespaceOpts ...NamespaceOpts) (*corev1.Namespace, error) {
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
Name: name,
},
}
for _, opt := range namespaceOpts {
opt := opt
opt(namespace)
}
controllers.AddCommonLabels(namespace, addon)
err := controllerutil.SetControllerReference(addon, namespace, r.scheme)
if err != nil {
Expand Down Expand Up @@ -174,5 +172,9 @@ func reconcileNamespace(ctx context.Context, c client.Client, namespace *corev1.
newLabels := labels.Merge(currentLabels, labels.Set(namespace.Labels))
currentNamespace.Labels = newLabels

currentAnnotations := labels.Set(currentNamespace.Annotations)
newAnnotations := labels.Merge(currentAnnotations, labels.Set(namespace.Annotations))
currentNamespace.Annotations = newAnnotations

return currentNamespace, c.Update(ctx, currentNamespace)
}
16 changes: 13 additions & 3 deletions internal/controllers/addon/namespace_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,23 @@ func TestEnsureNamespace_Create(t *testing.T) {
}

ctx := context.Background()
ensuredNamespace, err := r.ensureNamespace(ctx, addon, addon.Spec.Namespaces[0].Name)
namespace := addon.Spec.Namespaces[0]
ensuredNamespace, err := r.ensureNamespace(ctx, addon, namespace.Name)
c.AssertExpectations(t)
require.NoError(t, err)
require.NotNil(t, ensuredNamespace)
}

func TestEnsureNamespace_CreateWithLabels(t *testing.T) {
func TestEnsureNamespace_CreateWithLabelsAndAnnotations(t *testing.T) {
addon := testutil.NewTestAddonWithSingleNamespace()
labels := map[string]string{
"foo": "bar",
"baz": "qux",
}
annotations := map[string]string{
"cache": "invalidation",
"naming": "variables",
}

c := testutil.NewClient()
c.On("Get", testutil.IsContext, testutil.IsObjectKey, testutil.IsCoreV1NamespacePtr).Return(testutil.NewTestErrNotFound())
Expand All @@ -207,6 +212,10 @@ func TestEnsureNamespace_CreateWithLabels(t *testing.T) {
for key, value := range labels {
assert.Equal(t, value, ns.Labels[key])
}
for key, value := range annotations {
assert.Equal(t, value, ns.Annotations[key])
}

}).
Return(nil)

Expand All @@ -217,7 +226,8 @@ func TestEnsureNamespace_CreateWithLabels(t *testing.T) {

ctx := context.Background()

ensuredNamespace, err := r.ensureNamespaceWithLabels(ctx, addon, addon.Spec.Namespaces[0].Name, labels)
namespace := addon.Spec.Namespaces[0]
ensuredNamespace, err := r.ensureNamespace(ctx, addon, namespace.Name, WithNamespaceLabels(labels), WithNamespaceAnnotations(annotations))
c.AssertExpectations(t)
require.NoError(t, err)
require.NotNil(t, ensuredNamespace)
Expand Down

0 comments on commit 39bc82b

Please sign in to comment.