-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
71 lines (59 loc) · 1.75 KB
/
root.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
package cmd
import (
"fmt"
cfg "github.com/marcosQuesada/k8s-swarm/pkg/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)
const appID = "k8s-swarm-controller"
var (
config *cfg.Workload
namespace string
watchLabel string
workersConfigMapName string
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "root",
Short: "root controller command",
Long: `root controller command`,
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if err := cfg.BuildLogger(appID); err != nil {
log.Fatalf("unable to unMarshall config, error %v", err)
}
viper.AddConfigPath(cfg.ConfigFilePath)
viper.SetConfigName(cfg.ConfigFile)
viper.SetConfigType("yml")
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("unable to read config, error %v", err)
}
if err := viper.Unmarshal(&config); err != nil {
log.Fatalf("unable to unMarshall config, error %v", err)
}
}
func init() {
cobra.OnInitialize(initConfig)
cfg.SetCoreFlags(rootCmd, "controller")
rootCmd.PersistentFlags().StringVar(&namespace, "namespace", "swarm", "namespace to listen")
if p := os.Getenv("NAMESPACE"); p != "" {
namespace = p
}
rootCmd.PersistentFlags().StringVar(&watchLabel, "label", "swarm-worker", "label to watch statefulsets and pods")
if p := os.Getenv("WATCHED_LABEL"); p != "" {
watchLabel = p
}
rootCmd.PersistentFlags().StringVar(&workersConfigMapName, "configmap", "swarm-worker-config", "workers configmap name")
if p := os.Getenv("WORKERS_CONFIGMAP_NAME"); p != "" {
workersConfigMapName = p
}
}