-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
254 lines (220 loc) · 7.02 KB
/
client.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"context"
"fmt"
"log"
"strings"
"sync"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// client represents the wrapper of Kubernetes API client
type client struct {
clientset *kubernetes.Clientset
// origin is K8NSKEL_ORIGIN environment value
origin string
// ignoreDest is K8NSKEL_IGNORE_DEST environment value
ignoreDests map[string]int
// excludeSecrets contains excluding Secrets name.
// this value is from K8NSKEL_EXCLUDE_SECRETS environment value.
excludeSecrets map[string]struct{}
}
// newClient creates client object
func newClient(origin, ignoreDestCSV, excludeSecretsCSV string) (*client, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("failed to load in-cluster config: %s", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("failed to load clientset: %s", err)
}
sv := strings.Split(ignoreDestCSV, ",")
ignoreDests := map[string]int{
origin: 0, // ignore origin
}
for _, dest := range sv {
ignoreDests[dest] = 0 // meaningless value
}
es := strings.Split(excludeSecretsCSV, ",")
excludeSecrets := make(map[string]struct{}, len(es))
for _, secret := range es {
excludeSecrets[secret] = struct{}{}
}
return &client{
clientset: clientset,
origin: origin,
ignoreDests: ignoreDests,
excludeSecrets: excludeSecrets,
}, nil
}
func (c *client) watchNamespaceEvents(ctx context.Context, wg *sync.WaitGroup) (stop bool, err error) {
wg.Add(1)
defer wg.Done()
watcher, err := c.clientset.CoreV1().Namespaces().Watch(ctx, metav1.ListOptions{})
if err != nil {
return true, fmt.Errorf("failed to create watch interface: %s", err)
}
// ADDED events are also notified for namespaces that already exist at startup.
// Therefore, necessary to ignore this notification only at startup.
namespaces, err := c.clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return true, fmt.Errorf("failed to receive a namespace list: %s", err)
}
existingNS := map[string]int{}
for _, ns := range namespaces.Items {
existingNS[ns.ObjectMeta.Name] = 0 // meaningless value
}
// Event loop
for {
select {
case <-ctx.Done():
watcher.Stop()
return true, nil
case ev := <-watcher.ResultChan():
if ev.Object == nil {
// Closed because of timeout
return false, nil
}
if ev.Type != watch.Added {
continue
}
ns := ev.Object.(*apiv1.Namespace)
newNS := ns.ObjectMeta.Name
// Skip existing namaspaces only at startup
if _, ok := existingNS[newNS]; ok {
delete(existingNS, newNS)
continue
}
secrets, err := c.clientset.CoreV1().Secrets(c.origin).List(ctx, metav1.ListOptions{})
if err != nil {
log.Printf("failed to receive a secret list from '%s': %s", c.origin, err)
continue
}
// Copy secret from K8NSKEL_ORIGIN
for _, secret := range secrets.Items {
// Skip the default token
if strings.HasPrefix(secret.Name, defaultTokenPrefix) && secret.Type == apiv1.SecretTypeServiceAccountToken {
continue
}
_, err := c.clientset.CoreV1().Secrets(newNS).Create(ctx, &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secret.ObjectMeta.Name,
Namespace: newNS,
},
Type: secret.Type,
Data: secret.Data,
}, metav1.CreateOptions{})
if err != nil {
log.Printf("failed to copy a secret '%s' from '%s' to %s: %s", secret.ObjectMeta.Name, c.origin, newNS, err)
continue
}
}
log.Printf("all secrets were copied from '%s' to '%s'", c.origin, newNS)
}
}
}
func (c *client) watchSecretEvents(ctx context.Context, wg *sync.WaitGroup) (stop bool, err error) {
wg.Add(1)
defer wg.Done()
watcher, err := c.clientset.CoreV1().Secrets(c.origin).Watch(ctx, metav1.ListOptions{})
if err != nil {
return true, fmt.Errorf("failed to create watch interface: %s", err)
}
// ADDED events are also notified for secrets that already exist at startup.
// Therefore, necessary to ignore this notification only at startup.
secrets, err := c.clientset.CoreV1().Secrets(c.origin).List(ctx, metav1.ListOptions{})
if err != nil {
return true, fmt.Errorf("failed to receive a secret list: %s", err)
}
existingSecrets := map[string]int{}
for _, secret := range secrets.Items {
if _, ok := c.excludeSecrets[secret.ObjectMeta.Name]; ok {
continue
}
existingSecrets[secret.ObjectMeta.Name] = 0 // meaningless value
}
// Event loop
for {
select {
case <-ctx.Done():
watcher.Stop()
return true, nil
case ev := <-watcher.ResultChan():
if ev.Object == nil {
// Closed because of timeout
return false, nil
}
secret := ev.Object.(*apiv1.Secret)
// Skip to sync a secret when contains excluding lists
if _, ok := c.excludeSecrets[secret.Name]; ok {
continue
}
namespaces, err := c.clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
log.Printf("failed to receive a namespace list: %s", err)
continue
}
dests := []string{}
for _, ns := range namespaces.Items {
if _, ok := c.ignoreDests[ns.ObjectMeta.Name]; !ok {
dests = append(dests, ns.ObjectMeta.Name)
}
}
switch ev.Type {
case watch.Added:
// Skip existing secrets only at startup
if _, ok := existingSecrets[secret.Name]; ok {
delete(existingSecrets, secret.Name)
continue
}
log.Printf("secret '%s' ADDED in '%s'", secret.Name, c.origin)
for _, dest := range dests {
_, err := c.clientset.CoreV1().Secrets(dest).Create(ctx, &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secret.Name,
Namespace: dest,
},
Type: secret.Type,
Data: secret.Data,
}, metav1.CreateOptions{})
if err != nil {
log.Printf("failed to copy a secret '%s' from '%s' to '%s': %s", secret.Name, c.origin, dest, err)
continue
}
log.Printf("secret '%s' was copied from '%s' to '%s'", secret.Name, c.origin, dest)
}
case watch.Modified:
log.Printf("secret '%s' MODIFIED in '%s'", secret.Name, c.origin)
for _, dest := range dests {
_, err := c.clientset.CoreV1().Secrets(dest).Update(ctx, &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secret.Name,
Namespace: dest,
},
Type: secret.Type,
Data: secret.Data,
}, metav1.UpdateOptions{})
if err != nil {
log.Printf("failed to update a secret '%s' in '%s': %s", secret.Name, dest, err)
continue
}
log.Printf("secret '%s' was updated in '%s'", secret.Name, dest)
}
case watch.Deleted:
log.Printf("secret '%s' DELETED in'%s'", secret.Name, c.origin)
for _, dest := range dests {
err := c.clientset.CoreV1().Secrets(dest).Delete(ctx, secret.Name, metav1.DeleteOptions{})
if err != nil {
log.Printf("failed to delete a secret '%s' in '%s': %s", secret.Name, dest, err)
continue
}
log.Printf("secret '%s' was deleted in '%s'", secret.Name, dest)
}
}
}
}
}