Add namespace package

This commit is contained in:
Volodymyr Zotov
2025-08-28 13:17:27 -05:00
parent 6492b3cf34
commit e61ba49018
3 changed files with 50 additions and 1 deletions

2
go.mod
View File

@@ -12,6 +12,7 @@ require (
github.com/onsi/gomega v1.36.1 github.com/onsi/gomega v1.36.1
github.com/stretchr/testify v1.10.0 github.com/stretchr/testify v1.10.0
k8s.io/api v0.33.0 k8s.io/api v0.33.0
k8s.io/apiextensions-apiserver v0.33.0
k8s.io/apimachinery v0.33.0 k8s.io/apimachinery v0.33.0
k8s.io/client-go v0.33.0 k8s.io/client-go v0.33.0
k8s.io/kubectl v0.29.0 k8s.io/kubectl v0.29.0
@@ -102,7 +103,6 @@ require (
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.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/apiserver v0.33.0 // indirect
k8s.io/component-base v0.33.0 // indirect k8s.io/component-base v0.33.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect k8s.io/klog/v2 v2.130.1 // indirect

View File

@@ -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. // ApplyOnePasswordItem applies a OnePasswordItem manifest.
func (k *Kube) ApplyOnePasswordItem(ctx context.Context, fileName string) { func (k *Kube) ApplyOnePasswordItem(ctx context.Context, fileName string) {
By("Applying " + fileName) By("Applying " + fileName)

View File

@@ -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())
}