This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
root.go
122 lines (95 loc) · 3.09 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
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
package cmd
import (
golog "log"
"os"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // from https://github.com/kubernetes/client-go/issues/345
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"code.cloudfoundry.org/quarks-job/pkg/kube/operator"
"code.cloudfoundry.org/quarks-job/version"
"code.cloudfoundry.org/quarks-utils/pkg/cmd"
"code.cloudfoundry.org/quarks-utils/pkg/config"
"code.cloudfoundry.org/quarks-utils/pkg/ctxlog"
"code.cloudfoundry.org/quarks-utils/pkg/logger"
)
var log *zap.SugaredLogger
func wrapError(err error, msg string) error {
return errors.Wrapf(err, "quarks-job command failed. %s", msg)
}
var rootCmd = &cobra.Command{
Use: "quarks-job",
Short: "quarks-job starts the operator",
RunE: func(_ *cobra.Command, args []string) error {
log = logger.NewControllerLogger(cmd.LogLevel())
defer log.Sync()
restConfig, err := cmd.KubeConfig(log)
if err != nil {
return wrapError(err, "")
}
cfg := config.NewDefaultConfig(afero.NewOsFs())
cmd.MonitoredID(cfg)
log.Infof("Starting quarks-job %s, monitoring namespaces labeled with '%s'", version.Version, cfg.MonitoredID)
err = cmd.DockerImage()
if err != nil {
return wrapError(err, "")
}
log.Infof("quarks-job docker image: %s", config.GetOperatorDockerImage())
cfg.MaxQuarksJobWorkers = viper.GetInt("max-workers")
cmd.CtxTimeOut(cfg)
cmd.Meltdown(cfg)
ctx := ctxlog.NewParentContext(log)
err = cmd.ApplyCRDs(ctx, operator.ApplyCRDs, restConfig)
if err != nil {
return wrapError(err, "Couldn't apply CRDs.")
}
mgr, err := operator.NewManager(ctx, cfg, restConfig, manager.Options{
MetricsBindAddress: "0",
LeaderElection: false,
})
if err != nil {
return wrapError(err, "Failed to create new manager.")
}
ctxlog.Info(ctx, "Waiting for Quarks job resources...")
err = mgr.Start(signals.SetupSignalHandler())
if err != nil {
return wrapError(err, "Failed to start quarks-job manager.")
}
return nil
},
TraverseChildren: true,
}
// NewOperatorCommand returns the `quarks-job` command.
func NewOperatorCommand() *cobra.Command {
return rootCmd
}
// Execute the root command, runs the server
func Execute() {
if err := rootCmd.Execute(); err != nil {
golog.Fatal(err)
os.Exit(1)
}
}
func init() {
pf := rootCmd.PersistentFlags()
argToEnv := map[string]string{}
cmd.CtxTimeOutFlags(pf, argToEnv)
cmd.MonitoredIDFlags(pf, argToEnv)
cmd.KubeConfigFlags(pf, argToEnv)
cmd.LoggerFlags(pf, argToEnv)
cmd.DockerImageFlags(pf, argToEnv, "quarks-job", version.Version)
cmd.ApplyCRDsFlags(pf, argToEnv)
cmd.MeltdownFlags(pf, argToEnv)
pf.Int("max-workers", 1, "Maximum number of workers concurrently running the controller")
viper.BindPFlag("max-workers", pf.Lookup("max-workers"))
argToEnv["max-workers"] = "MAX_WORKERS"
// Add env variables to help
cmd.AddEnvToUsage(rootCmd, argToEnv)
// Do not display cmd usage and errors
rootCmd.SilenceUsage = true
rootCmd.SilenceErrors = true
}