forked from rancher/kubectld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (102 loc) · 2.82 KB
/
Copy pathmain.go
File metadata and controls
117 lines (102 loc) · 2.82 KB
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
package main
import (
"fmt"
"os"
"github.com/PastureStack/kubectl-service/events"
platformhelm "github.com/PastureStack/kubectl-service/helm"
"github.com/codegangsta/cli"
"github.com/rancher/swarm-agent/healthcheck"
"github.com/sirupsen/logrus"
)
var VERSION = "dev"
var (
startHealthCheck = healthcheck.StartHealthCheck
startEventHandler = events.StartEventHandler
logFatalf = logrus.Fatalf
)
func newApp() *cli.App {
app := cli.NewApp()
app.Name = "kubectl-service"
app.Version = VERSION
app.Action = launch
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "platform-url",
Usage: "URL for the control-platform API",
EnvVar: "PLATFORM_URL,CATTLE_URL",
},
cli.StringFlag{
Name: "platform-access-key",
Usage: "Control-platform API access key",
EnvVar: "PLATFORM_ACCESS_KEY,CATTLE_ACCESS_KEY",
},
cli.StringFlag{
Name: "platform-secret-key",
Usage: "Control-platform API secret key",
EnvVar: "PLATFORM_SECRET_KEY,CATTLE_SECRET_KEY",
},
cli.IntFlag{
Name: "worker-count",
Value: 50,
Usage: "Number of workers for handling events",
EnvVar: "WORKER_COUNT",
},
cli.IntFlag{
Name: "health-check-port",
Value: 10240,
Usage: "Port to configure an HTTP health check listener on",
EnvVar: "HEALTH_CHECK_PORT",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable debug logs",
EnvVar: "DEBUG",
},
cli.StringFlag{
Name: "locale",
Value: "en-US",
Usage: "Operator message locale: en-US or zh-TW",
EnvVar: "PASTURESTACK_LOCALE",
},
cli.StringFlag{
Name: "helm-backend",
Value: platformhelm.LegacyHelm2BackendName,
Usage: "Helm release backend: legacy-helm2 or helm4",
EnvVar: "PASTURESTACK_HELM_BACKEND",
},
}
return app
}
func main() {
if err := newApp().Run(os.Args); err != nil {
logFatalf("Fatal exit: %v", err)
}
}
func launch(ctx *cli.Context) error {
hcPort := ctx.Int("health-check-port")
locale := ctx.String("locale")
if locale != "en-US" && locale != "zh-TW" {
return fmt.Errorf("unsupported locale %q; use en-US or zh-TW", locale)
}
if err := platformhelm.ConfigureBackend(ctx.String("helm-backend")); err != nil {
return err
}
url := ctx.String("platform-url")
accessKey := ctx.String("platform-access-key")
secretKey := ctx.String("platform-secret-key")
workers := ctx.Int("worker-count")
if ctx.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
go func() {
logFatalf("%s: %v", operatorMessage(locale, "healthcheck-exit"), startHealthCheck(hcPort))
}()
return startEventHandler(url, accessKey, secretKey, workers)
}
func operatorMessage(locale, key string) string {
messages := map[string]map[string]string{
"en-US": {"healthcheck-exit": "Health check exited with an error"},
"zh-TW": {"healthcheck-exit": "健康檢查因錯誤而停止"},
}
return messages[locale][key]
}