Skip to content

Commit

Permalink
introduce default labels that are propagted from parent
Browse files Browse the repository at this point in the history
This PR adds a new label, with key `environment` which is propgated
from the parent down the hierarchy, if it exists

Signed-off-by: mzeevi <meytar80@gmail.com>
  • Loading branch information
mzeevi committed Apr 14, 2024
1 parent 12f4e1b commit 6db45c2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -61,6 +62,7 @@ const (
DisplayName = MetaGroup + "display-name"
Description = MetaGroup + "description"
OpenShiftDisplayName = "openshift.io/display-name"
Environment = "environment"
)

const (
Expand Down
11 changes: 11 additions & 0 deletions internal/namespace/nsutils/nsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions test/e2e_tests/subnamespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,21 @@ 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)
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])
}

LabelNSDefaultLabels(nsA)
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)
Expand Down
7 changes: 6 additions & 1 deletion test/testutils/composeutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ func AnnotateNSSecondaryRoot(ns string) {
}

// AnnotateNSDefaultAnnotation annotates a namespace with the default annotations.
func AnnotateNSDefaultAnnotation(ns string) {
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) {
Expand Down

0 comments on commit 6db45c2

Please sign in to comment.