-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (59 loc) · 1.42 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"github.com/davidhong1/k8s-image-credential-helper/conf"
"github.com/davidhong1/k8s-image-credential-helper/service"
"github.com/golang/glog"
)
const (
httpHealthCheckPort = "HTTP_HEALTH_CHECK_PORT"
defaultHttpHealthCheckPort = "8080"
initConfig = "INIT_CONFIG"
defaultInitConfig = "environment"
)
func main() {
ctx := context.Background()
var err error
ic := strings.TrimSpace(os.Getenv(initConfig))
if ic == "" {
ic = defaultInitConfig
}
var ici *conf.ImageCredentialInfo
switch ic {
case defaultInitConfig:
envLoader := conf.EnvImageCredentialInfoLoader{}
ici, err = envLoader.Load()
if err != nil {
glog.Fatal(err.Error())
}
}
if ici == nil {
glog.Fatal("ImageCredentialInfo is nil")
}
namespaceWatcher, err := service.InitNamespaceWatcher(ctx, ici)
if err != nil {
glog.Fatal(err)
}
err = namespaceWatcher.Watch(context.Background())
if err != nil {
glog.Fatal(err)
}
// 监听健康检查接口
httpPort := strings.TrimSpace(os.Getenv(httpHealthCheckPort))
if httpPort == "" {
httpPort = defaultHttpHealthCheckPort
}
http.HandleFunc("/pong", pongHandler)
err = http.ListenAndServe(":"+httpPort, nil)
if err != nil {
glog.Fatal("init health http failed. err: %v", err)
}
}
func pongHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "ping")
}