-
Notifications
You must be signed in to change notification settings - Fork 4
/
configmap.go
155 lines (143 loc) · 3.73 KB
/
configmap.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
package configmap
import (
"context"
"errors"
"io/fs"
"log/slog"
"path/filepath"
corev1 "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"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/homedir"
"github.com/mss-boot-io/mss-boot/pkg"
"github.com/mss-boot-io/mss-boot/pkg/config/source"
)
/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2024/4/26 17:19:21
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2024/4/26 17:19:21
*/
// Source is a k8s configmap source
type Source struct {
opt *source.Options
}
func (s *Source) GetExtend() source.Scheme {
return s.opt.Extend
}
// Open a file for reading
func (s *Source) Open(_ string) (fs.File, error) {
return nil, errors.New("method Get not implemented")
}
// ReadFile read file
func (s *Source) ReadFile(name string) (rb []byte, err error) {
ctx, cancel := context.WithTimeout(context.TODO(), s.opt.Timeout)
defer cancel()
cm, err := s.opt.Clientset.CoreV1().
ConfigMaps(s.opt.Namespace).
Get(ctx, s.opt.Configmap, metav1.GetOptions{})
if err != nil {
return nil, err
}
for i := range source.Extends {
if v, ok := cm.Data[name+"."+string(source.Extends[i])]; ok {
s.opt.Extend = source.Extends[i]
return []byte(v), nil
}
}
return nil, err
}
// Watch configmap
func (s *Source) Watch(c source.Entity, unm func([]byte, any) error) error {
watcher, err := s.opt.Clientset.CoreV1().
ConfigMaps(s.opt.Namespace).
Watch(context.TODO(), metav1.ListOptions{
FieldSelector: "metadata.name=" + s.opt.Configmap,
})
if err != nil {
return err
}
go func(sc *Source, cfg source.Entity, w watch.Interface, decoder func([]byte, any) error) {
defer w.Stop()
for event := range w.ResultChan() {
if event.Type == watch.Modified {
cm, ok := event.Object.(*corev1.ConfigMap)
if !ok {
continue
}
for i := range source.Extends {
if v, ok := cm.Data[sc.opt.Name+"."+string(source.Extends[i])]; ok {
if err = decoder([]byte(v), cfg); err != nil {
slog.Error("Failed to decode config", slog.Any("error", err))
continue
}
}
if v, ok := cm.Data[sc.opt.Name+"-"+pkg.GetStage()+"."+string(source.Extends[i])]; ok {
if err = decoder([]byte(v), cfg); err != nil {
slog.Error("Failed to decode config", slog.Any("error", err))
}
}
}
}
cfg.OnChange()
}
}(s, c, watcher, unm)
return nil
}
func (s *Source) getClientset() error {
if s.opt.Clientset == nil {
var err error
var config *rest.Config
if s.opt.KubeconfigPath == "" && s.opt.Kubeconfig == "" {
config, err = rest.InClusterConfig()
if err != nil {
return err
}
} else {
var apiConfig *clientcmdapi.Config
if s.opt.Kubeconfig != "" {
apiConfig, err = clientcmd.Load([]byte(s.opt.Kubeconfig))
if err != nil {
return err
}
} else {
if s.opt.KubeconfigPath == "" {
s.opt.KubeconfigPath = filepath.Join(homedir.HomeDir(), ".kube", "config")
}
apiConfig, err = clientcmd.LoadFromFile(s.opt.KubeconfigPath)
if err != nil {
return err
}
}
config, err = clientcmd.NewDefaultClientConfig(*apiConfig, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return err
}
}
s.opt.Clientset, err = kubernetes.NewForConfig(config)
if err != nil {
return err
}
}
return nil
}
func New(options ...source.Option) (*Source, error) {
s := &Source{
opt: source.DefaultOptions(),
}
for _, opt := range options {
opt(s.opt)
}
err := s.getClientset()
if err != nil {
return nil, err
}
if s.opt.Namespace == "" {
s.opt.Namespace = "default"
}
return s, nil
}