From e61ba4901865298a03c75a50cdcb2373f700370f Mon Sep 17 00:00:00 2001 From: Volodymyr Zotov Date: Thu, 28 Aug 2025 13:17:27 -0500 Subject: [PATCH] Add `namespace` package --- go.mod | 2 +- pkg/testhelper/kube/kube.go | 8 +++++++ pkg/testhelper/kube/namespace.go | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 pkg/testhelper/kube/namespace.go diff --git a/go.mod b/go.mod index 941b1a8..bcb22b7 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/onsi/gomega v1.36.1 github.com/stretchr/testify v1.10.0 k8s.io/api v0.33.0 + k8s.io/apiextensions-apiserver v0.33.0 k8s.io/apimachinery v0.33.0 k8s.io/client-go v0.33.0 k8s.io/kubectl v0.29.0 @@ -102,7 +103,6 @@ require ( gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - k8s.io/apiextensions-apiserver v0.33.0 // indirect k8s.io/apiserver v0.33.0 // indirect k8s.io/component-base v0.33.0 // indirect k8s.io/klog/v2 v2.130.1 // indirect diff --git a/pkg/testhelper/kube/kube.go b/pkg/testhelper/kube/kube.go index 1af2046..77bbe68 100644 --- a/pkg/testhelper/kube/kube.go +++ b/pkg/testhelper/kube/kube.go @@ -127,6 +127,14 @@ func (k *Kube) Pod(selector map[string]string) *Pod { } } +func (k *Kube) Namespace(name string) *Namespace { + return &Namespace{ + client: k.Client, + config: k.Config, + name: name, + } +} + // ApplyOnePasswordItem applies a OnePasswordItem manifest. func (k *Kube) ApplyOnePasswordItem(ctx context.Context, fileName string) { By("Applying " + fileName) diff --git a/pkg/testhelper/kube/namespace.go b/pkg/testhelper/kube/namespace.go new file mode 100644 index 0000000..08aec1f --- /dev/null +++ b/pkg/testhelper/kube/namespace.go @@ -0,0 +1,41 @@ +package kube + +import ( + "context" + //nolint:staticcheck // ST1001 + . "github.com/onsi/ginkgo/v2" + //nolint:staticcheck // ST1001 + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/labels" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +type Namespace struct { + client client.Client + config *Config + name string +} + +// LabelNamespace applies the given labels to the specified namespace +func (n *Namespace) LabelNamespace(ctx context.Context, labelsMap map[string]string) { + if labelsMap == nil || len(labelsMap) == 0 { + return + } + + By("Setting labelsMap " + labels.Set(labelsMap).String() + " to namespace/" + n.name) + ns := &corev1.Namespace{} + err := n.client.Get(ctx, client.ObjectKey{Name: n.name}, ns) + Expect(err).NotTo(HaveOccurred()) + + if ns.Labels == nil { + ns.Labels = map[string]string{} + } + + for k, v := range labelsMap { + ns.Labels[k] = v + } + + err = n.client.Update(ctx, ns) + Expect(err).NotTo(HaveOccurred()) +}