-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
233 lines (195 loc) · 6.18 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
var (
configMapName string
configMapNamespace string
labelSelector string
watchKey string
mainContainerName string
clientset *kubernetes.Clientset
lock sync.Mutex
)
func init() {
// Define command-line flags
flag.StringVar(&configMapName, "configmap-name", "watch-namespace-config", "Name of the ConfigMap to update")
flag.StringVar(&configMapNamespace, "configmap-namespace", "vault", "Namespace of the ConfigMap")
flag.StringVar(&labelSelector, "label-selector", "foo=bar", "Label selector for namespaces to watch")
flag.StringVar(&watchKey, "watch-key", "WATCH_NAMESPACE", "Key in the ConfigMap to store namespace list")
flag.StringVar(&mainContainerName, "main-container", "main-container", "Name of the main container to restart")
// Parse flags
flag.Parse()
}
func main() {
config, err := getKubeConfig()
if err != nil {
fmt.Printf("Error getting Kubernetes config: %v\n", err)
os.Exit(1)
}
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
fmt.Printf("Error creating Kubernetes client: %v\n", err)
os.Exit(1)
}
// Ensure the ConfigMap is created/updated at startup
updateConfigMap()
// Set up signal handling for graceful shutdown
stopCh := make(chan struct{})
signalCh := make(chan os.Signal, 1)
// Listen for termination signals
signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT)
// Listen for termination signals
signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-signalCh
fmt.Println("Received termination signal. Shutting down gracefully...")
close(stopCh) // Stop the namespace watcher
os.Exit(0) // Exit the program
}()
// Start namespace watcher
go watchNamespaces(stopCh)
// Keep the program running
<-stopCh
}
// getKubeConfig tries in-cluster config first, then falls back to local kubeconfig
func getKubeConfig() (*rest.Config, error) {
// Try in-cluster config
config, err := rest.InClusterConfig()
if err == nil {
fmt.Println("Using in-cluster configuration")
return config, nil
}
// Fall back to local kubeconfig
fmt.Println("In-cluster config failed, falling back to local kubeconfig")
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
if envKubeconfig := os.Getenv("KUBECONFIG"); envKubeconfig != "" {
kubeconfig = envKubeconfig
}
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, fmt.Errorf("failed to load kubeconfig: %w", err)
}
fmt.Println("Using local kubeconfig")
return config, nil
}
// watchNamespaces monitors namespace creation and deletion
func watchNamespaces(stopCh chan struct{}) {
informerFactory := informers.NewSharedInformerFactoryWithOptions(clientset, time.Minute, informers.WithTweakListOptions(func(opts *metav1.ListOptions) {
opts.LabelSelector = labelSelector
}))
namespaceInformer := informerFactory.Core().V1().Namespaces().Informer()
namespaceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { updateConfigMap() },
UpdateFunc: func(oldObj, newObj interface{}) { updateConfigMap() },
DeleteFunc: func(obj interface{}) { updateConfigMap() },
})
informerFactory.Start(stopCh)
informerFactory.WaitForCacheSync(stopCh)
}
// updateConfigMap updates the ConfigMap with the latest namespace list
func updateConfigMap() {
lock.Lock()
defer lock.Unlock()
namespaces, err := getFilteredNamespaces()
if err != nil {
fmt.Printf("Error getting namespaces: %v\n", err)
return
}
newValue := strings.Join(namespaces, ",")
// Fetch the existing ConfigMap
cm, err := clientset.CoreV1().ConfigMaps(configMapNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{})
if err != nil {
// If ConfigMap doesn't exist, create it
createConfigMap(newValue)
return
}
// Check if the value has changed
if cm.Data[watchKey] == newValue {
return
}
// Update ConfigMap
cm.Data[watchKey] = newValue
_, err = clientset.CoreV1().ConfigMaps(configMapNamespace).Update(context.TODO(), cm, metav1.UpdateOptions{})
if err != nil {
fmt.Printf("Error updating ConfigMap: %v\n", err)
return
}
// Trigger restart of the main container
killMainContainer()
}
// getFilteredNamespaces retrieves namespaces with the specified label
func getFilteredNamespaces() ([]string, error) {
namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{
LabelSelector: labelSelector,
})
if err != nil {
return nil, err
}
var nsList []string
for _, ns := range namespaces.Items {
nsList = append(nsList, ns.Name)
}
return nsList, nil
}
// createConfigMap creates a new ConfigMap
func createConfigMap(value string) {
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: configMapNamespace,
},
Data: map[string]string{
watchKey: value,
},
}
_, err := clientset.CoreV1().ConfigMaps(configMapNamespace).Create(context.TODO(), cm, metav1.CreateOptions{})
if err != nil {
fmt.Printf("Error creating ConfigMap: %v\n", err)
}
}
// killMainContainer kills the main container to reload the ConfigMap
func killMainContainer() {
fmt.Println("Restarting main container...")
// Find the main container's PID (assumes PID namespace is shared)
pid, err := getMainContainerPID()
if err != nil {
fmt.Printf("Error getting main container PID: %v\n", err)
return
}
// Send SIGTERM to the main container
err = exec.Command("kill", "-SIGTERM", pid).Run()
if err != nil {
fmt.Printf("Error sending SIGTERM to main container: %v\n", err)
}
}
// getMainContainerPID finds the process ID of the main container
func getMainContainerPID() (string, error) {
out, err := exec.Command("pgrep", "-f", mainContainerName).Output()
if err != nil {
return "", err
}
// Return the first PID found
pidList := strings.Fields(string(out))
if len(pidList) > 0 {
return pidList[0], nil
}
return "", fmt.Errorf("main container PID not found")
}