-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.go
More file actions
200 lines (169 loc) · 4.32 KB
/
Copy pathgenerator.go
File metadata and controls
200 lines (169 loc) · 4.32 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"context"
"fmt"
"os"
"strconv"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)
const (
// TODO: Remove it!
configMapName = "podtinytidyid-counter"
defaultValue = "0"
)
// getNext gets the next Identifier for a podSet.
func GetNext(podSet string, bitsInID int) string {
// Get the configmap
var configmap *corev1.ConfigMap
var nextId string
var err error
isIdValid := false
klog.Infof("GetNext for: %s", podSet)
for !isIdValid {
configmap, err = getConfigMap()
if err != nil {
klog.Errorf("getting configmap: %s", err)
continue
}
// Use the configmap variable here
nextId, err = getNextInConfigmap(configmap, podSet, bitsInID)
if err != nil {
klog.Errorf("getting next: %s", err)
continue
}
// Check if there is a pod with hte nextId as Id.
// Get the pod with labelId
err = podDoesNotExist(labelSet, podSet, getLabelId(podSet), nextId)
if err != nil {
klog.Errorf("getting pod: %s", err)
continue
}
isIdValid = true
}
return nextId
}
func getConfigMap() (*corev1.ConfigMap, error) {
// Create kubernetes client
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
// Get current namespace
namespace, err := getCurrentNamespace()
if err != nil {
return nil, err
}
// Try to get existing configmap
cm, err := clientset.CoreV1().ConfigMaps(namespace).Get(
context.TODO(),
configMapName,
metav1.GetOptions{},
)
if err == nil {
return cm, nil
}
// Create new configmap if not exists
newCm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
},
Data: map[string]string{},
}
cm, err = clientset.CoreV1().ConfigMaps(namespace).Create(
context.TODO(),
newCm,
metav1.CreateOptions{},
)
if err != nil {
return nil, err
}
return cm, nil
}
func getCurrentNamespace() (string, error) {
// Read namespace from pod
data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return "", err
}
return string(data), nil
}
func getNextInConfigmap(configmap *corev1.ConfigMap, podSet string, bitsInID int) (string, error) {
//Get current value or set default
currentVal := defaultValue
if val, exists := configmap.Data[podSet]; exists {
currentVal = val
}
// Convert to int and increment
counter, err := strconv.Atoi(currentVal)
if err != nil {
return "", fmt.Errorf("error converting counter to int: %w", err)
}
counter = (counter + 1) % (1 << bitsInID)
// Update configmap with new value
if configmap.Data == nil {
configmap.Data = make(map[string]string)
}
configmap.Data[podSet] = strconv.Itoa(counter)
// Create kubernetes client
config, err := rest.InClusterConfig()
if err != nil {
return "", fmt.Errorf("error getting cluster config: %w", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return "", fmt.Errorf("error creating clientset: %w", err)
}
// Get current namespace
namespace, err := getCurrentNamespace()
if err != nil {
return "", fmt.Errorf("error getting namespace: %w", err)
}
// Update configmap
_, err = clientset.CoreV1().ConfigMaps(namespace).Update(
context.TODO(),
configmap,
metav1.UpdateOptions{},
)
if err != nil {
return "", fmt.Errorf("error updating configmap: %w", err)
}
return strconv.Itoa(counter), nil
}
func podDoesNotExist(label1Name, label1Value, label2Name, label2Value string) error {
// Create kubernetes client
config, err := rest.InClusterConfig()
if err != nil {
return fmt.Errorf("error getting cluster config: %w", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("error creating clientset: %w", err)
}
// Create label selector string
labelSelector := fmt.Sprintf("%s=%s,%s=%s",
label1Name, label1Value,
label2Name, label2Value)
// List pods in all namespaces with labels
pods, err := clientset.CoreV1().Pods("").List(
context.TODO(),
metav1.ListOptions{
LabelSelector: labelSelector,
},
)
if err != nil {
return fmt.Errorf("error listing pods: %w", err)
}
// Check if any pods found
if len(pods.Items) > 0 {
return fmt.Errorf("pod with labels %s already exists", labelSelector)
}
return nil
}