forked from nocalhost/nocalhost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cm_watcher.go
179 lines (151 loc) · 4.46 KB
/
cm_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
/*
* 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 cm
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/lsutils/nocalhost/internal/nhctl/app"
"github.com/lsutils/nocalhost/internal/nhctl/appmeta"
"github.com/lsutils/nocalhost/internal/nhctl/common/base"
_const "github.com/lsutils/nocalhost/internal/nhctl/const"
"github.com/lsutils/nocalhost/internal/nhctl/profile"
"github.com/lsutils/nocalhost/internal/nhctl/watcher"
"github.com/lsutils/nocalhost/pkg/nhctl/log"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kblabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"strings"
"sync"
)
var NOT_FOUND = errors.New("Not found")
type CmWatcher struct {
namespace string
clientset *kubernetes.Clientset
cache sync.Map
lock sync.Mutex
quit chan bool
watchController *watcher.Controller
}
func (cmw *CmWatcher) CreateOrUpdate(key string, obj interface{}) error {
if cm, ok := obj.(*corev1.ConfigMap); ok {
appName := appmeta.GetAppNameFromConfigMapName(cm.Name)
if appName == "" {
log.Info(
"cm %s is not standard nocalhost "+
"config (should named with dev.nocalhost.config.${app})", appName,
)
return nil
}
return cmw.join(appName, cm)
} else {
errInfo := fmt.Sprintf("Fetching cm with key %s but could not cast to cm: %v", key, obj)
glog.Error(errInfo)
return fmt.Errorf(errInfo)
}
}
func (cmw *CmWatcher) Delete(key string) error {
var namespace string
var appName string
// if key name has namespace/xxx prefix, prefix is the namespace
if idx := strings.Index(key, "/"); idx > 0 {
if len(key) > idx+1 {
namespace = key[:idx]
cmName := key[idx+1:]
appName = appmeta.GetAppNameFromConfigMapName(cmName)
}
}
if appName != "" && namespace != "" {
cmw.left(appName)
} else {
log.Info(
"[Delete Event] cm %s is not standard nocalhost "+
"config (should named with dev.nocalhost.config.${app})", key,
)
}
return nil
}
func (cmw *CmWatcher) WatcherInfo() string {
return fmt.Sprintf("'Cm'")
}
func (cmw *CmWatcher) join(app string, cm *corev1.ConfigMap) error {
cmw.cache.Store(app, cm)
return nil
}
func (cmw *CmWatcher) left(app string) {
cmw.cache.Delete(app)
}
func NewCmWatcher(clientset *kubernetes.Clientset) *CmWatcher {
return &CmWatcher{
clientset: clientset,
cache: sync.Map{},
quit: make(chan bool),
}
}
func (cmw *CmWatcher) Prepare(namespace string) error {
cmw.namespace = namespace
//create the service account watcher
saWatcher := cache.NewFilteredListWatchFromClient(
cmw.clientset.CoreV1().RESTClient(), "configmaps", namespace,
func(options *metav1.ListOptions) {
options.LabelSelector = kblabels.Set{
_const.NocalhostCmLabelKey: _const.NocalhostCmLabelValue,
}.AsSelector().String()
},
)
controller := watcher.NewController(cmw, saWatcher, &corev1.ConfigMap{})
listOpt := metav1.ListOptions{}
listOpt.LabelSelector = kblabels.Set{
_const.NocalhostCmLabelKey: _const.NocalhostCmLabelValue,
}.AsSelector().String()
if list, err := cmw.clientset.CoreV1().ConfigMaps(namespace).List(
context.TODO(), listOpt,
); err == nil {
for _, item := range list.Items {
if err := cmw.CreateOrUpdate(item.Name, &item); err != nil {
glog.Infof(
"CmCache: load nocalhost cm config in ns: %s error: %s", item.Namespace,
err.Error(),
)
}
}
}
cmw.watchController = controller
return nil
}
// GetNocalhostConfig
// CAUTION: appCfg may nil!
func (cmw *CmWatcher) GetNocalhostConfig(application, svcType, svcName string) (
*profile.NocalHostAppConfigV2, *profile.ServiceConfigV2, error) {
glog.Infof("App: %s, svcType: %s, svcName: %s", application, svcType, svcName)
if load, ok := cmw.cache.Load(application); ok {
if cm, ok := load.(*corev1.ConfigMap); ok {
cfgStr := cm.Data[appmeta.CmConfigKey]
if cfgStr == "" {
return nil, nil, errors.New(
fmt.Sprintf(
"Nocalhost cm %s-%s found, but do not contain %s key in field [Data]",
cmw.namespace, application, appmeta.CmConfigKey,
),
)
}
return app.LoadSvcCfgFromStrIfValid(
cfgStr, svcName,
base.SvcType(strings.ToLower(svcType)),
)
}
}
return nil, nil, NOT_FOUND
}
// this method will block until error occur
func (cmw *CmWatcher) Watch() {
stop := make(chan struct{})
defer close(stop)
go cmw.watchController.Run(1, stop)
<-cmw.quit
}