forked from harvester/harvester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (160 loc) · 4.59 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
//go:generate go run pkg/codegen/cleanup/main.go
//go:generate /bin/rm -rf pkg/generated
//go:generate go run pkg/codegen/main.go
//go:generate /bin/bash scripts/generate-manifest
//go:generate /bin/bash scripts/generate-openapi
package main
import (
"context"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"github.com/ehazlett/simplelog"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/harvester/harvester/pkg/config"
"github.com/harvester/harvester/pkg/server"
"github.com/harvester/harvester/pkg/version"
)
var (
profileAddress = "localhost:6060"
KubeConfig string
)
func main() {
var options config.Options
app := cli.NewApp()
app.Name = "harvester"
app.Version = version.FriendlyVersion()
app.Usage = ""
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "kubeconfig",
EnvVar: "KUBECONFIG",
Usage: "Kube config for accessing k8s cluster",
Destination: &KubeConfig,
},
cli.BoolFlag{
Name: "debug",
EnvVar: "HARVESTER_DEBUG",
Usage: "Enable debug logs",
Destination: &options.Debug,
},
cli.BoolFlag{
Name: "trace",
EnvVar: "HARVESTER_TRACE",
Usage: "Enable trace logs",
Destination: &options.Trace,
},
cli.IntFlag{
Name: "threadiness",
EnvVar: "THREADINESS",
Usage: "Specify controller threads",
Value: 10,
Destination: &options.Threadiness,
},
cli.IntFlag{
Name: "http-port",
EnvVar: "HARVESTER_SERVER_HTTP_PORT",
Usage: "HTTP listen port",
Value: 8080,
Destination: &options.HTTPListenPort,
},
cli.IntFlag{
Name: "https-port",
EnvVar: "HARVESTER_SERVER_HTTPS_PORT",
Usage: "HTTPS listen port",
Value: 8443,
Destination: &options.HTTPSListenPort,
},
cli.StringFlag{
Name: "namespace",
EnvVar: "NAMESPACE",
Destination: &options.Namespace,
Usage: "The default namespace to store management resources",
Required: true,
},
cli.BoolFlag{
Name: "skip-authentication",
EnvVar: "SKIP_AUTHENTICATION",
Usage: "Define whether to skip auth login or not, default to false",
Destination: &options.SkipAuthentication,
},
cli.StringFlag{
Name: "authentication-mode",
EnvVar: "HARVESTER_AUTHENTICATION_MODE",
Usage: "Define authentication mode, kubernetesCredentials, localUser and rancher are supported, could config more than one mode, separated by comma",
},
cli.BoolFlag{
Name: "hci-mode",
EnvVar: "HCI_MODE",
Usage: "Enable HCI mode. Additional controllers are registered in HCI mode",
Destination: &options.HCIMode,
},
cli.StringFlag{
Name: "profile-listen-address",
Value: "0.0.0.0:6060",
Usage: "Address to listen on for profiling",
Destination: &profileAddress,
},
cli.BoolFlag{
Name: "rancher-embedded",
EnvVar: "RANCHER_EMBEDDED",
Usage: "Specify whether the Harvester is running with embedded Rancher mode, default to false",
Destination: &options.RancherEmbedded,
},
cli.StringFlag{
Name: "rancher-server-url",
EnvVar: "RANCHER_SERVER_URL",
Usage: "Specify the URL to connect to the Rancher server",
Destination: &options.RancherURL,
},
}
app.Action = func(c *cli.Context) error {
// enable profiler
if profileAddress != "" {
go func() {
log.Println(http.ListenAndServe(profileAddress, nil))
}()
}
initLogs(c, options)
return run(c, options)
}
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
func initLogs(c *cli.Context, options config.Options) {
switch c.String("log-format") {
case "simple":
logrus.SetFormatter(&simplelog.StandardFormatter{})
case "text":
logrus.SetFormatter(&logrus.TextFormatter{})
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
}
logrus.SetOutput(os.Stdout)
if options.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debugf("Loglevel set to [%v]", logrus.DebugLevel)
}
if options.Trace {
logrus.SetLevel(logrus.TraceLevel)
logrus.Tracef("Loglevel set to [%v]", logrus.TraceLevel)
}
}
func run(c *cli.Context, options config.Options) error {
logrus.Info("Starting controller")
ctx := signals.SetupSignalHandler(context.Background())
kubeConfig, err := server.GetConfig(KubeConfig)
if err != nil {
return fmt.Errorf("failed to find kubeconfig: %v", err)
}
harv, err := server.New(ctx, kubeConfig, options)
if err != nil {
return fmt.Errorf("failed to create harvester server: %v", err)
}
return harv.ListenAndServe(nil, options)
}