-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
238 lines (200 loc) · 6.12 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
234
235
236
237
238
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync/atomic"
"time"
"encoding/json"
"strconv"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type key int
const (
requestIDKey key = 0
)
var (
listenAddr string
healthy int32
logger = log.New(os.Stdout, "http: ", log.LstdFlags)
zonalNameMap = make(map[string]string)
)
func main() {
initialize()
smokeTest()
router := http.NewServeMux()
router.Handle("/", root())
router.Handle("/healthz", healthz())
router.Handle("/cluster/discovery", discovery())
startServer(router, 5700)
}
func root() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, " hi. try /healthz or /cluster/discovery ")
})
}
func healthz() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if atomic.LoadInt32(&healthy) == 1 {
w.WriteHeader(http.StatusNoContent)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
})
}
func discovery() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(getMap())
})
}
//-------------------------------
func initialize() {
err := json.Unmarshal([]byte(os.Getenv("zonalNameMap")), &zonalNameMap)
if err != nil {
logger.Panicf("FAILED TO Unmarshal zonalNameMap. Err: %v", err)
}
}
func smokeTest() {
clientSet := getLocalK8sClientSet()
nodeList, err := clientSet.CoreV1().Nodes().List(metav1.ListOptions{})
nodes := nodeList.Items
if err != nil || len(nodes) < 1 {
logger.Panicf("FAILED TO GET NODES. Err: %v", err)
}
podList, err := clientSet.CoreV1().Pods(os.Getenv("imdgNamespace")).List(metav1.ListOptions{LabelSelector: os.Getenv("imdgPodSelector")})
pods := podList.Items
if err != nil || len(pods) < 1 {
logger.Panicf("FAILED TO GET IMDG PODS. Err: %v", err)
}
}
func startServer(router *http.ServeMux, port int) {
flag.StringVar(&listenAddr, "listen-addr", ":"+strconv.Itoa(port), "server listen address")
flag.Parse()
logger.Println("Server is starting...")
nextRequestID := func() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
server := &http.Server{
Addr: listenAddr,
Handler: tracing(nextRequestID)(logging(logger)(router)),
ErrorLog: logger,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
done := make(chan bool)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
logger.Println("Server is shutting down...")
atomic.StoreInt32(&healthy, 0)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
logger.Fatalf("Could not gracefully shutdown the server: %v\n", err)
}
close(done)
}()
logger.Println("Server is ready to handle requests at", listenAddr)
atomic.StoreInt32(&healthy, 1)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("Could not listen on %s: %v\n", listenAddr, err)
}
<-done
logger.Println("Server stopped")
}
func logging(logger *log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
requestID, ok := r.Context().Value(requestIDKey).(string)
if !ok {
requestID = "unknown"
}
logger.Println(requestID, r.Method, r.URL.Path, r.RemoteAddr, r.UserAgent())
}()
next.ServeHTTP(w, r)
})
}
}
func tracing(nextRequestID func() string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get("X-Request-Id")
if requestID == "" {
requestID = nextRequestID()
}
ctx := context.WithValue(r.Context(), requestIDKey, requestID)
w.Header().Set("X-Request-Id", requestID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
func getMap() interface{} {
clientSet := getLocalK8sClientSet()
nodeIP_zone_map := make(map[string]string)
nodeList, _ := clientSet.CoreV1().Nodes().List(metav1.ListOptions{})
nodes := nodeList.Items
for _, node := range nodes {
nodeIP_zone_map[node.Status.Addresses[0].Address] = node.Labels["failure-domain.beta.kubernetes.io/zone"]
}
podList, _ := clientSet.CoreV1().Pods(os.Getenv("imdgNamespace")).List(metav1.ListOptions{LabelSelector: os.Getenv("imdgPodSelector")})
pods := podList.Items
// podIPmap := make(map[string]string)
podIPkey := os.Getenv("PRIVATE_ADDRESS_PROPERTY")
if podIPkey == "" {
podIPkey = "private-address"
}
zonalNameKey := os.Getenv("PUBLIC_ADDRESS_PROPERTY")
if zonalNameKey == "" {
zonalNameKey = "public-address"
}
fatMap := make([]map[string]string, 0, 0)
for _, pod := range pods {
zone := nodeIP_zone_map[pod.Status.HostIP]
ipNport := pod.Status.PodIP + ":5701"
pvtlnNport := zonalNameMap[zone] + ":" + getPort(pod.Status.PodIP)
// podIPmap[ipNport] = pvtlnNport
var buffer = make(map[string]string)
buffer[podIPkey] = ipNport
buffer[zonalNameKey] = pvtlnNport
fatMap = append(fatMap, buffer)
}
//return podIPmap
return fatMap
}
func getPort(ip string) string {
d := strings.Split(ip, ".")
ipD3, _ := strconv.Atoi(d[2])
ipD4, _ := strconv.Atoi(d[3])
return strconv.Itoa(ipD3*256 + ipD4)
}
func getLocalK8sClientSet() *kubernetes.Clientset {
config, err := rest.InClusterConfig()
if err != nil {
panic(fmt.Sprintf("Error occurred while creating kubernetes config. Err: %v", err))
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
panic(fmt.Sprintf("Error occurred while connecting to kubernetes cluster. Err: %v", err))
}
return clientSet
}