-
Notifications
You must be signed in to change notification settings - Fork 9
/
root.go
136 lines (118 loc) · 5.25 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cmd
import (
"os"
"strings"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "kubechecks",
Short: "Argo Git Hooks",
Long: `A Kubernetes controller and webhook server for integration of ArgoCD applications into CI`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(RootCmd.Execute())
}
const envPrefix = "kubechecks"
var envKeyReplacer = strings.NewReplacer("-", "_")
func init() {
// allows environment variables to use _ instead of -
viper.SetEnvKeyReplacer(envKeyReplacer) // sync-provider becomes SYNC_PROVIDER
viper.SetEnvPrefix(envPrefix) // port becomes KUBECHECKS_PORT
viper.AutomaticEnv() // read in environment variables that match
flags := RootCmd.PersistentFlags()
stringFlag(flags, "log-level", "Set the log output level.",
newStringOpts().
withChoices(
zerolog.LevelErrorValue,
zerolog.LevelWarnValue,
zerolog.LevelInfoValue,
zerolog.LevelDebugValue,
zerolog.LevelTraceValue,
).
withDefault("info").
withShortHand("l"),
)
boolFlag(flags, "persist-log-level", "Persists the set log level down to other module loggers.")
stringFlag(flags, "vcs-base-url", "VCS base url, useful if self hosting gitlab, enterprise github, etc.")
stringFlag(flags, "vcs-type", "VCS type. One of gitlab or github. Defaults to gitlab.",
newStringOpts().
withChoices("github", "gitlab").
withDefault("gitlab"))
stringFlag(flags, "vcs-token", "VCS API token.")
stringFlag(flags, "argocd-api-token", "ArgoCD API token.")
stringFlag(flags, "argocd-api-server-addr", "ArgoCD API Server Address.", newStringOpts().withDefault("argocd-server"))
boolFlag(flags, "argocd-api-insecure", "Enable to use insecure connections to the ArgoCD API server.")
stringFlag(flags, "kubernetes-config", "Path to your kubernetes config file, used to monitor applications.")
stringFlag(flags, "otel-collector-port", "The OpenTelemetry collector port.")
stringFlag(flags, "otel-collector-host", "The OpenTelemetry collector host.")
boolFlag(flags, "otel-enabled", "Enable OpenTelemetry.")
stringFlag(flags, "tidy-outdated-comments-mode", "Sets the mode to use when tidying outdated comments.",
newStringOpts().
withChoices("hide", "delete").
withDefault("hide"))
stringSliceFlag(flags, "schemas-location", "Sets schema locations to be used for every check request. Can be common paths inside the repos being checked or git urls in either git or http(s) format.",
newStringSliceOpts().
withDefault([]string{"./schemas"}))
boolFlag(flags, "enable-conftest", "Set to true to enable conftest policy checking of manifests.")
stringSliceFlag(flags, "policies-location", "Sets rego policy locations to be used for every check request. Can be common path inside the repos being checked or git urls in either git or http(s) format.",
newStringSliceOpts().
withDefault([]string{"./policies"}))
stringFlag(flags, "worst-conftest-state", "The worst state that can be returned from conftest.",
newStringOpts().
withDefault("panic"))
boolFlag(flags, "enable-kubeconform", "Enable kubeconform checks.",
newBoolOpts().
withDefault(true))
stringFlag(flags, "worst-kubeconform-state", "The worst state that can be returned from kubeconform.",
newStringOpts().
withDefault("panic"))
boolFlag(flags, "enable-preupgrade", "Enable preupgrade checks.",
newBoolOpts().
withDefault(true))
stringFlag(flags, "worst-preupgrade-state", "The worst state that can be returned from preupgrade checks.",
newStringOpts().
withDefault("panic"))
int64Flag(flags, "max-queue-size", "Size of app diff check queue.",
newInt64Opts().
withDefault(1024))
int64Flag(flags, "max-concurrenct-checks", "Number of concurrent checks to run.",
newInt64Opts().
withDefault(32))
boolFlag(flags, "enable-hooks-renderer", "Render hooks.", newBoolOpts().withDefault(true))
stringFlag(flags, "worst-hooks-state", "The worst state that can be returned from the hooks renderer.",
newStringOpts().
withDefault("panic"))
panicIfError(viper.BindPFlags(flags))
setupLogOutput()
}
func setupLogOutput() {
output := zerolog.ConsoleWriter{Out: os.Stdout}
log.Logger = log.Output(output)
// Default level is info, unless debug flag is present
levelFlag := viper.GetString("log-level")
level, _ := zerolog.ParseLevel(levelFlag)
zerolog.SetGlobalLevel(level)
log.Debug().Msg("Debug level logging enabled.")
log.Trace().Msg("Trace level logging enabled.")
log.Info().Msg("Initialized logger.")
// set logrus log level to overwrite the logs exporting from argo-cd package
logrusLevel := logrus.ErrorLevel
if viper.GetBool("persist_log_level") {
if log.Debug().Enabled() {
logrusLevel = logrus.DebugLevel
}
if log.Trace().Enabled() {
logrusLevel = logrus.TraceLevel
}
}
logrus.StandardLogger().Level = logrusLevel
log.Info().Str("log_level", logrus.StandardLogger().Level.String()).Msg("setting logrus log level")
}