-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
109 lines (94 loc) · 2.95 KB
/
main.go
File metadata and controls
109 lines (94 loc) · 2.95 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"time"
"strings"
"encoding/json"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/apimachinery/pkg/types"
)
type patchStringValue struct {
Op string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
}
const nameFieldManager = "node-labels-copier"
const labelSystemWorkload = "system-workload"
func main() {
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
for {
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for _, n := range nodes.Items {
nodeName := n.ObjectMeta.Name
role := ""
if _, ok := n.ObjectMeta.Labels["role"]; !ok {
continue
}
role, _ = n.ObjectMeta.Labels["role"]
nodeRole := ""
for lKey, _ := range n.ObjectMeta.Labels {
if strings.Contains(lKey, "node-role.kubernetes.io") {
split := strings.Split(lKey, "/")
if len(split) == 2 {
nodeRole = split[1]
}
}
}
if role != nodeRole {
fmt.Printf("copy label for node: %s, role: %s => node-role.kubernetes.io/%s\n", nodeName, role, role)
payload := []patchStringValue{{
Op: "replace",
Path: fmt.Sprintf("/metadata/labels/node-role.kubernetes.io~1%s", role),
Value: "",
}}
payloadBytes, _ := json.Marshal(payload)
_ , err = clientset.CoreV1().Nodes().Patch(context.TODO(), nodeName, types.JSONPatchType, payloadBytes, metav1.PatchOptions{FieldManager: nameFieldManager})
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
if role == labelSystemWorkload {
if _, ok := n.ObjectMeta.Labels[labelSystemWorkload]; !ok {
fmt.Printf("create label for node: %s, label: %s\n", nodeName, labelSystemWorkload)
payload := []patchStringValue{{
Op: "add",
Path: fmt.Sprintf("/metadata/labels/%s", labelSystemWorkload),
Value: "",
}}
payloadBytes, _ := json.Marshal(payload)
_ , err = clientset.CoreV1().Nodes().Patch(context.TODO(), nodeName, types.JSONPatchType, payloadBytes, metav1.PatchOptions{FieldManager: nameFieldManager})
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
}
}
time.Sleep(300 * time.Second)
}
}