-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (143 loc) · 4.25 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
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"reflect"
"strconv"
"syscall"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func readJsonFile(name string) (map[string]interface{}, error) {
data, err := os.ReadFile(name)
if err != nil {
return nil, errors.New(fmt.Sprintf("error: '%v' open file: %v", err, name))
}
v := make(map[string]any)
err = json.Unmarshal(data, &v)
if err != nil {
return nil, errors.New(fmt.Sprintf("error: '%v' parse json file: %v", err, name))
}
return v, nil
}
func writeJsonFile(name string, v map[string]interface{}) error {
out, err := json.MarshalIndent(v, "", " ")
if err != nil {
return errors.New(fmt.Sprintf("error: '%v' encode file: %v", err, name))
}
err = os.WriteFile(name, out, os.ModePerm)
if err != nil {
return errors.New(fmt.Sprintf("error: '%v' write file: %v", err, name))
}
return nil
}
func getNatsPid(name string) (int, error) {
pidData, err := os.ReadFile(name)
if err != nil {
return 0, errors.New(fmt.Sprintf("error: '%v' read pid file: '%v' ", err, name))
}
pid, err := strconv.Atoi(string(pidData))
if err != nil {
return 0, errors.New(fmt.Sprintf("error: '%v' parse pid file: '%v' ", err, name))
}
return pid, nil
}
var Version = "0.0.0"
var Hash = "xxxxxxxx"
var BuildDate = ""
func buildInfo() string {
return fmt.Sprintf("version: %v, build date: %v, hash: %v", Version, BuildDate, Hash)
}
func main() {
log.Printf("started nats-configurator %v", buildInfo())
defer log.Printf("stopped nats-configurator %v", buildInfo())
podIp := flag.String("pod-ip", "", "pod ip address")
podName := flag.String("pod-name", "", "pod name")
nameSpace := flag.String("namespace", "default", "namespace")
podLabel := flag.String("pod-label", "", "each pod with this have this label, will be included in mesh")
natsRoutesPort := flag.Int("nats-routes-port", 6222, "port for internal cluster data exchange between nats nodes")
natsConfigTemplate := flag.String("nats-config-template", "", "nats-config file template")
natsConfigFileName := flag.String("nats-config", "", "nats-config")
natsPidFile := flag.String("nats-pid-file", "", "nats pid file, for signal about reload config")
refreshInterval := flag.Duration("refresh-interval", 3*time.Second, "pod list refresh interval")
flag.Parse()
natsConfig, err := readJsonFile(*natsConfigTemplate)
if err != nil {
log.Fatal(err)
}
natsConfig["server_name"] = *podName
err = writeJsonFile(*natsConfigFileName, natsConfig)
if err != nil {
log.Fatal(err)
}
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
routesPrev := make([]string, 0)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
ticker := time.NewTicker(*refreshInterval)
for {
select {
case <-interrupt:
return
case <-ticker.C:
pid, err := getNatsPid(*natsPidFile)
if err != nil {
log.Print(err)
break
}
// kubectl get pods -n nats -lapp=nats-configurator -o wide
pods, err := clientset.
CoreV1().
Pods(*nameSpace).
List(context.TODO(), metav1.ListOptions{LabelSelector: *podLabel})
if err != nil {
log.Printf("error listing pods: %v", err)
break
}
routes := make([]string, 0)
for _, pod := range pods.Items {
if len(pod.Status.PodIP) == 0 || pod.Status.PodIP == *podIp {
continue
}
routes = append(routes, fmt.Sprintf("nats://%v:%v", pod.Status.PodIP, *natsRoutesPort))
}
cluster := natsConfig["cluster"]
if val, ok := cluster.(map[string]any); ok {
if reflect.DeepEqual(routesPrev, routes) {
time.Sleep(*refreshInterval)
continue
} else {
val["routes"] = routes
routesPrev = routes
log.Printf("updating routes: %v", routes)
}
} else {
log.Fatalf("error modify routes: '%v'", err)
}
err = writeJsonFile(*natsConfigFileName, natsConfig)
if err != nil {
log.Fatal(err)
}
err = syscall.Kill(pid, syscall.SIGHUP)
if err != nil {
log.Fatalf("error send SIGHUP to nats-server: %v with pid: %v", err, pid)
}
}
}
}