Skip to content

feat: Preserve user-managed fields when creating namespace #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s crsStrategy) apply(
if err != nil {
return fmt.Errorf("error creating remote cluster client: %w", err)
}
if err = utils.EnsureNamespace(ctx, remoteClient, cluster.Namespace); err != nil {
if err = utils.EnsureNamespaceWithName(ctx, remoteClient, cluster.Namespace); err != nil {
return fmt.Errorf(
"failed to create Namespace in remote cluster: %w",
err,
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/generic/lifecycle/utils/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func CopySecretToRemoteCluster(
return fmt.Errorf("error creating client for remote cluster: %w", err)
}

err = EnsureNamespace(ctx, remoteClient, dstSecretKey.Namespace)
err = EnsureNamespaceWithName(ctx, remoteClient, dstSecretKey.Namespace)
if err != nil {
return fmt.Errorf("error creating namespace on the remote cluster: %w", err)
}
Expand Down
20 changes: 13 additions & 7 deletions pkg/handlers/generic/lifecycle/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func EnsureCRSForClusterFromObjects(
return nil
}

// EnsureNamespace will create the namespece if it does not exist.
func EnsureNamespace(ctx context.Context, c ctrlclient.Client, name string) error {
// EnsureNamespaceWithName will create the namespace with the specified name if it does not exist.
func EnsureNamespaceWithName(ctx context.Context, c ctrlclient.Client, name string) error {
ns := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Expand All @@ -105,12 +105,18 @@ func EnsureNamespace(ctx context.Context, c ctrlclient.Client, name string) erro
},
}

// check if namespace exists and return early if it does
if err := c.Get(ctx, ctrlclient.ObjectKeyFromObject(ns), ns); err == nil {
return nil
}
return EnsureNamespace(ctx, c, ns)
}

err := client.ServerSideApply(ctx, c, ns, client.ForceOwnership)
// EnsureNamespace will create the namespace if it does not exist.
func EnsureNamespace(ctx context.Context, c ctrlclient.Client, ns *corev1.Namespace) error {
if ns.TypeMeta.APIVersion == "" {
ns.TypeMeta.APIVersion = corev1.SchemeGroupVersion.String()
}
if ns.TypeMeta.Kind == "" {
ns.TypeMeta.Kind = "Namespace"
}
err := client.ServerSideApply(ctx, c, ns)
if err != nil {
return fmt.Errorf("failed to server side apply %w", err)
}
Expand Down
97 changes: 97 additions & 0 deletions pkg/handlers/generic/lifecycle/utils/utils_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package utils

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/storage/names"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
)

var _ = Describe("Namespace", func() {
It("creates a new namespace", func(ctx SpecContext) {
c, err := helpers.TestEnv.GetK8sClient()
Expect(err).To(BeNil())

namespaceName := names.SimpleNameGenerator.GenerateName("test-")

Expect(EnsureNamespaceWithName(ctx, c, namespaceName)).To(Succeed())
Expect(c.Delete(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
},
})).To((Succeed()))
})

It(
"updates a namespace with no changes, preserving user-managed fields",
func(ctx SpecContext) {
c, err := helpers.TestEnv.GetK8sClient()
Expect(err).To(BeNil())

namespaceName := names.SimpleNameGenerator.GenerateName("test-")
Expect(c.Create(ctx,
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
Labels: map[string]string{
"userkey": "uservalue",
},
},
})).To(Succeed())

Expect(EnsureNamespaceWithName(ctx, c, namespaceName)).To(Succeed())

ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
},
}
Expect(c.Get(ctx, client.ObjectKeyFromObject(ns), ns)).To(Succeed())
Expect(ns.GetLabels()).To(HaveKeyWithValue("userkey", "uservalue"))
},
)

It(
"updates a namespace by only sending new labels, preserving existing user-managed fields",
func(ctx SpecContext) {
c, err := helpers.TestEnv.GetK8sClient()
Expect(err).To(BeNil())

namespaceName := names.SimpleNameGenerator.GenerateName("test-")
Expect(c.Create(ctx,
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
Labels: map[string]string{
"userkey": "uservalue",
},
},
})).To(Succeed())

Expect(EnsureNamespace(ctx, c, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
Labels: map[string]string{
"newkey": "newvalue",
},
},
})).To(Succeed())

ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
},
}
Expect(c.Get(ctx, client.ObjectKeyFromObject(ns), ns)).To(Succeed())
Expect(ns.GetLabels()).To(HaveKeyWithValue("userkey", "uservalue"))
Expect(ns.GetLabels()).To(HaveKeyWithValue("newkey", "newvalue"))
},
)
})
17 changes: 17 additions & 0 deletions pkg/handlers/generic/lifecycle/utils/utils_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package utils

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

// TestUtils is the entrypoint for integration (envtest) tests.
func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Utils")
}
Loading