diff --git a/api/v1/types.go b/api/v1/types.go index 3721dc89..c6fc3ce1 100644 --- a/api/v1/types.go +++ b/api/v1/types.go @@ -10,6 +10,7 @@ const ( ) var DefaultAnnotations = []string{"scheduler.alpha.kubernetes.io/defaultTolerations", "openshift.io/node-selector"} +var DefaultLabels = []string{"environment"} const ( Missing Phase = "Missing" diff --git a/internal/namespace/nsutils/nsutils.go b/internal/namespace/nsutils/nsutils.go index 2cf449cc..061aa355 100644 --- a/internal/namespace/nsutils/nsutils.go +++ b/internal/namespace/nsutils/nsutils.go @@ -67,6 +67,8 @@ func LabelsBasedOnParent(parentNS *objectcontext.ObjectContext, nsName string) m parentDisplayNameSliced := DisplayNameSlice(parentNS) labels := make(map[string]string) + defaultLabels(parentNS, labels) + labels[danav1.Parent] = parentNS.Object.(*corev1.Namespace).Name labels[danav1.Hns] = "true" @@ -116,6 +118,15 @@ func defaultAnnotations(ns *objectcontext.ObjectContext, annotations map[string] } } +// defaultLabels updates the map of the ns labels with the DefaultLabels. +func defaultLabels(ns *objectcontext.ObjectContext, labels map[string]string) { + for key, value := range ns.Object.GetLabels() { + if slices.Contains(danav1.DefaultLabels, key) { + labels[key] = value + } + } +} + // Depth returns the depth of a namespace from its annotation. func Depth(namespace client.Object) int { if ownerNamespaceDepth, err := strconv.Atoi(namespace.(*corev1.Namespace).Annotations[danav1.Depth]); err != nil { diff --git a/test/e2e_tests/subnamespace_test.go b/test/e2e_tests/subnamespace_test.go index ef75e7ef..d05ae32a 100644 --- a/test/e2e_tests/subnamespace_test.go +++ b/test/e2e_tests/subnamespace_test.go @@ -276,16 +276,22 @@ var _ = Describe("Subnamespaces", func() { FieldShouldContain("subnamespace", nsA, nsB, ".status.total.free.pods", "10") }) - It("should update the child namespace with the default annotations of its parent", func() { + It("should update the child namespace with the default annotations and labels of its parent", func() { nsA := GenerateE2EName("a", testPrefix, randPrefix) nsB := GenerateE2EName("b", testPrefix, randPrefix) CreateSubnamespace(nsA, nsRoot, randPrefix, false, storage, "50Gi", cpu, "50", memory, "50Gi", pods, "50", gpu, "50") - AnnotateNSDefaultAnnotation(nsA) + AnnotateNSDefaultAnnotations(nsA) + LabelNSDefaultLabels(nsA) + CreateSubnamespace(nsB, nsA, randPrefix, false, storage, "25Gi", cpu, "25", memory, "25Gi", pods, "25", gpu, "25") for i := range danav1.DefaultAnnotations { FieldShouldContain("namespace", "", nsB, ".metadata.annotations", danav1.DefaultAnnotations[i]) } + + for i := range danav1.DefaultLabels { + FieldShouldContain("namespace", "", nsB, ".metadata.labels", danav1.DefaultLabels[i]) + } }) It("should fail when deleting a subnamespace directly", func() { nsA := GenerateE2EName("a", testPrefix, randPrefix) diff --git a/test/testutils/composeutils.go b/test/testutils/composeutils.go index 3642bac5..e2e092ba 100644 --- a/test/testutils/composeutils.go +++ b/test/testutils/composeutils.go @@ -70,12 +70,17 @@ func AnnotateNSSecondaryRoot(ns string) { MustRun("kubectl annotate --overwrite ns", ns, danav1.IsSecondaryRoot+"="+danav1.True) } -// AnnotateNSDefaultAnnotation annotates a namespace with the default annotations. -func AnnotateNSDefaultAnnotation(ns string) { +// AnnotateNSDefaultAnnotations annotates a namespace with the default annotations. +func AnnotateNSDefaultAnnotations(ns string) { MustRun("kubectl annotate --overwrite ns", ns, danav1.DefaultAnnotations[0]+"='[{\"key\":\"test\",\"value\":\"true\",\"effect\":\"NoSchedule\"}]'") MustRun("kubectl annotate --overwrite ns", ns, danav1.DefaultAnnotations[1]+"="+"testServer") } +// LabelNSDefaultLabels labels a namespace with the default labels. +func LabelNSDefaultLabels(ns string) { + MustRun("kubectl label --overwrite ns", ns, danav1.DefaultLabels[0]+"=test") +} + // CreateRootNS creates/updates a root name with a given name // and with the required labels. func CreateRootNS(nm, randPrefix string, rqDepth int) {