forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labels.go
72 lines (60 loc) · 1.77 KB
/
labels.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package nslabels
import (
"fmt"
"strings"
typescorev1 "github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
)
const (
ProjectIDFieldLabel = "field.cattle.io/projectId"
)
type namespaceHandler struct {
nsClient typescorev1.NamespaceInterface
}
func Register(cluster *config.UserContext) {
logrus.Infof("Registering namespaceHandler for adding labels ")
nsh := &namespaceHandler{
cluster.Core.Namespaces(""),
}
cluster.Core.Namespaces("").AddHandler("namespaceHandler", nsh.Sync)
}
func (nsh *namespaceHandler) Sync(key string, ns *corev1.Namespace) error {
if ns == nil {
return nil
}
logrus.Debugf("namespaceHandler: Sync: key=%v, ns=%+v", key, *ns)
field, ok := ns.Annotations[ProjectIDFieldLabel]
if !ok {
return nil
}
splits := strings.Split(field, ":")
if len(splits) != 2 {
return nil
}
projectID := splits[1]
logrus.Debugf("namespaceHandler: Sync: projectID=%v", projectID)
if err := nsh.addProjectIDLabelToNamespace(ns, projectID); err != nil {
logrus.Errorf("namespaceHandler: Sync: error adding project id label to namespace err=%v", err)
return nil
}
return nil
}
func (nsh *namespaceHandler) addProjectIDLabelToNamespace(ns *corev1.Namespace, projectID string) error {
if ns == nil {
return fmt.Errorf("cannot add label to nil namespace")
}
if ns.Labels[ProjectIDFieldLabel] != projectID {
logrus.Infof("namespaceHandler: addProjectIDLabelToNamespace: adding label %v=%v to namespace=%v", ProjectIDFieldLabel, projectID, ns.Name)
nscopy := ns.DeepCopy()
if nscopy.Labels == nil {
nscopy.Labels = map[string]string{}
}
nscopy.Labels[ProjectIDFieldLabel] = projectID
if _, err := nsh.nsClient.Update(nscopy); err != nil {
return err
}
}
return nil
}