@@ -3,6 +3,7 @@ package labeller
3
3
import (
4
4
"fmt"
5
5
"regexp"
6
+ "strconv"
6
7
7
8
mlp "github.com/caraml-dev/mlp/api/client"
8
9
)
@@ -20,10 +21,13 @@ const (
20
21
orchestratorLabel = "orchestrator"
21
22
// AppLabel refers to application of the Kubernetes Object
22
23
AppLabel = "app"
24
+ // managedLabel mark the kubernetes object as being managed by specific dev
25
+ managedLabel = "managed"
23
26
)
24
27
25
28
var (
26
29
prefix string
30
+ nsPrefix string
27
31
environment string
28
32
)
29
33
@@ -53,8 +57,9 @@ func IsValidLabel(name string) error {
53
57
}
54
58
55
59
// InitKubernetesLabeller builds a new KubernetesLabeller Singleton
56
- func InitKubernetesLabeller (p , e string ) {
60
+ func InitKubernetesLabeller (p , ns , e string ) {
57
61
prefix = p
62
+ nsPrefix = ns
58
63
environment = e
59
64
}
60
65
@@ -71,6 +76,11 @@ func GetLabelName(name string) string {
71
76
return fmt .Sprintf ("%s%s" , prefix , name )
72
77
}
73
78
79
+ // GetNamespaceLabelName prefixes the label with the specified label and returns the formatted label name
80
+ func GetNamespaceLabelName (name string ) string {
81
+ return fmt .Sprintf ("%s%s" , nsPrefix , name )
82
+ }
83
+
74
84
// BuildLabels builds the labels for the Kubernetes object
75
85
// Combines resource labels with project labels
76
86
func BuildLabels (r KubernetesLabelsRequest ) map [string ]string {
@@ -81,6 +91,21 @@ func BuildLabels(r KubernetesLabelsRequest) map[string]string {
81
91
GetLabelName (AppLabel ): r .App ,
82
92
GetLabelName (environmentLabel ): environment ,
83
93
}
94
+ appendFromLabelsRequest (labels , r )
95
+ return labels
96
+ }
97
+
98
+ // BuildNamespaceLabels builds the labels for a Kubernetes namespace.
99
+ // Combines resource labels with project labels
100
+ func BuildNamespaceLabels (r KubernetesLabelsRequest ) map [string ]string {
101
+ labels := map [string ]string {
102
+ GetNamespaceLabelName (managedLabel ): strconv .FormatBool (true ),
103
+ }
104
+ appendFromLabelsRequest (labels , r )
105
+ return labels
106
+ }
107
+
108
+ func appendFromLabelsRequest (labels map [string ]string , r KubernetesLabelsRequest ) {
84
109
for _ , label := range r .Labels {
85
110
// skip label that is trying to override reserved key
86
111
if _ , usingReservedKeys := reservedKeys [prefix + label .Key ]; usingReservedKeys {
@@ -99,5 +124,4 @@ func BuildLabels(r KubernetesLabelsRequest) map[string]string {
99
124
100
125
labels [label .Key ] = label .Value
101
126
}
102
- return labels
103
127
}
0 commit comments