forked from nocalhost/nocalhost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_secret_watcher.go
209 lines (175 loc) · 5.05 KB
/
application_secret_watcher.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
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
* This source code is licensed under the Apache License Version 2.0.
*/
package appmeta_manager
import (
"context"
"fmt"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/flowcontrol"
"k8s.io/client-go/tools/clientcmd"
"nocalhost/internal/nhctl/appmeta"
"nocalhost/internal/nhctl/watcher"
"nocalhost/pkg/nhctl/log"
"sync"
)
type applicationSecretWatcher struct {
// todo recreate ASW if kubeConfig changed
configBytes []byte
ns string
applicationMetas map[string]*appmeta.ApplicationMeta
lock sync.Mutex
quit chan bool
watchController *watcher.Controller
}
func (asw *applicationSecretWatcher) CreateOrUpdate(key string, obj interface{}) error {
if secret, ok := obj.(*v1.Secret); ok {
return asw.join(secret)
} else {
errInfo := fmt.Sprintf(
"Fetching secret with key %s but "+
"could not cast to secret: %v", key, obj,
)
log.Error(errInfo)
return fmt.Errorf(errInfo)
}
}
func (asw *applicationSecretWatcher) Delete(key string) error {
appName, err := appmeta.GetApplicationName(key)
if err != nil {
return err
}
asw.left(appName)
return nil
}
func (asw *applicationSecretWatcher) WatcherInfo() string {
return fmt.Sprintf("'Secret - ns:%s'", asw.ns)
}
func (asw *applicationSecretWatcher) join(secret *v1.Secret) error {
devMetaBefore := appmeta.ApplicationDevMeta{}
devMetaCurrent := appmeta.ApplicationDevMeta{}
asw.lock.Lock()
defer asw.lock.Unlock()
current, err := appmeta.Decode(secret)
if err != nil {
return err
}
appName := current.Application
if before, ok := asw.applicationMetas[appName]; ok && before != nil {
devMetaBefore = before.GetApplicationDevMeta()
}
devMetaCurrent = current.DevMeta
asw.applicationMetas[appName] = current
for _, event := range *devMetaBefore.Events(devMetaCurrent) {
EventPush(
&ApplicationEventPack{
Event: event,
//Nid: current.NamespaceId,
Ns: asw.ns,
AppName: appName,
KubeConfigBytes: asw.configBytes,
},
)
}
return nil
}
func (asw *applicationSecretWatcher) left(appName string) {
devMetaBefore := appmeta.ApplicationDevMeta{}
devMetaCurrent := appmeta.ApplicationDevMeta{}
asw.lock.Lock()
defer asw.lock.Unlock()
if before, ok := asw.applicationMetas[appName]; ok {
devMetaBefore = before.GetApplicationDevMeta()
}
//m := asw.applicationMetas[appName]
delete(asw.applicationMetas, appName)
for _, event := range *devMetaBefore.Events(devMetaCurrent) {
EventPush(
&ApplicationEventPack{
Event: event,
Ns: asw.ns,
//Nid: m.NamespaceId,
AppName: appName,
KubeConfigBytes: asw.configBytes,
},
)
}
}
func NewApplicationSecretWatcher(configBytes []byte, ns string) *applicationSecretWatcher {
return &applicationSecretWatcher{
configBytes: configBytes,
ns: ns,
applicationMetas: map[string]*appmeta.ApplicationMeta{},
quit: make(chan bool),
}
}
func (asw *applicationSecretWatcher) GetApplicationMetas() (result []*appmeta.ApplicationMeta) {
asw.lock.Lock()
defer asw.lock.Unlock()
for _, meta := range asw.applicationMetas {
result = append(result, meta)
}
return
}
// prevent other func change the application meta
// caution!!!!!
func (asw *applicationSecretWatcher) GetApplicationMeta(application string) *appmeta.ApplicationMeta {
asw.lock.Lock()
defer asw.lock.Unlock()
return asw.applicationMetas[application]
}
func (asw *applicationSecretWatcher) Quit() {
asw.quit <- true
}
func (asw *applicationSecretWatcher) Prepare() error {
c, err := clientcmd.RESTConfigFromKubeConfig(asw.configBytes)
if err != nil {
return err
}
// creates the clientset
c.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(10000, 10000)
clientset, err := kubernetes.NewForConfig(c)
if err != nil {
return err
}
// create the secret watcher
listWatcher := cache.NewListWatchFromClient(
clientset.CoreV1().RESTClient(), "secrets", asw.ns,
fields.OneTermEqualSelector("type", appmeta.SecretType),
)
controller := watcher.NewController(asw, listWatcher, &v1.Secret{})
asw.watchController = controller
// first get all nocalhost secrets for initial
// ignore error prevent kubeconfig has not permission for get secret
// ignore fail
list, err := clientset.CoreV1().Secrets(asw.ns).List(
context.TODO(), metav1.ListOptions{
FieldSelector: fields.OneTermEqualSelector("type", appmeta.SecretType).String(),
},
)
if err != nil {
log.ErrorE(err, "")
return nil
}
for _, v := range list.Items {
// https://medium.com/swlh/use-pointer-of-for-range-loop-variable-in-go-3d3481f7ffc9
x := v
if err = asw.join(&x); err != nil {
return err
}
}
return nil
}
// todo stop while Ns deleted
// this method will block until error occur
func (asw *applicationSecretWatcher) Watch() {
stop := make(chan struct{})
defer close(stop)
go asw.watchController.Run(1, stop)
<-asw.quit
}