Add log level flag

pull/229/head
stefanprodan 4 years ago
parent 97ff225bc0
commit 2bac5aabee

@ -52,6 +52,7 @@ var (
bootstrapArch string bootstrapArch string
bootstrapBranch string bootstrapBranch string
bootstrapWatchAllNamespaces bool bootstrapWatchAllNamespaces bool
bootstrapLogLevel string
) )
const ( const (
@ -77,6 +78,19 @@ func init() {
rootCmd.AddCommand(bootstrapCmd) rootCmd.AddCommand(bootstrapCmd)
bootstrapCmd.PersistentFlags().BoolVar(&bootstrapWatchAllNamespaces, "watch-all-namespaces", true, bootstrapCmd.PersistentFlags().BoolVar(&bootstrapWatchAllNamespaces, "watch-all-namespaces", true,
"watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed") "watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed")
bootstrapCmd.PersistentFlags().StringVar(&bootstrapLogLevel, "log-level", "info", "set the controllers log level")
}
func bootstrapValidate() error {
if !utils.containsItemString(supportedArch, bootstrapArch) {
return fmt.Errorf("arch %s is not supported, can be %v", bootstrapArch, supportedArch)
}
if !utils.containsItemString(supportedLogLevels, bootstrapLogLevel) {
return fmt.Errorf("log level %s is not supported, can be %v", bootstrapLogLevel, supportedLogLevels)
}
return nil
} }
func generateInstallManifests(targetPath, namespace, tmpDir string) (string, error) { func generateInstallManifests(targetPath, namespace, tmpDir string) (string, error) {
@ -88,7 +102,8 @@ func generateInstallManifests(targetPath, namespace, tmpDir string) (string, err
} }
if err := genInstallManifests(bootstrapVersion, namespace, bootstrapComponents, if err := genInstallManifests(bootstrapVersion, namespace, bootstrapComponents,
bootstrapWatchAllNamespaces, bootstrapRegistry, bootstrapImagePullSecret, bootstrapArch, gotkDir); err != nil { bootstrapWatchAllNamespaces, bootstrapRegistry, bootstrapImagePullSecret,
bootstrapArch, bootstrapLogLevel, gotkDir); err != nil {
return "", fmt.Errorf("generating manifests failed: %w", err) return "", fmt.Errorf("generating manifests failed: %w", err)
} }

@ -100,8 +100,8 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("%s environment variable not found", git.GitHubTokenName) return fmt.Errorf("%s environment variable not found", git.GitHubTokenName)
} }
if !utils.containsItemString(supportedArch, bootstrapArch) { if err := bootstrapValidate(); err != nil {
return fmt.Errorf("arch %s is not supported, can be %v", bootstrapArch, supportedArch) return err
} }
repository, err := git.NewRepository(ghRepository, ghOwner, ghHostname, ghToken, "gotk", ghOwner+"@users.noreply.github.com") repository, err := git.NewRepository(ghRepository, ghOwner, ghHostname, ghToken, "gotk", ghOwner+"@users.noreply.github.com")

@ -89,8 +89,8 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("%s environment variable not found", git.GitLabTokenName) return fmt.Errorf("%s environment variable not found", git.GitLabTokenName)
} }
if !utils.containsItemString(supportedArch, bootstrapArch) { if err := bootstrapValidate(); err != nil {
return fmt.Errorf("arch %s is not supported, can be %v", bootstrapArch, supportedArch) return err
} }
repository, err := git.NewRepository(glRepository, glOwner, glHostname, glToken, "gotk", glOwner+"@users.noreply.gitlab.com") repository, err := git.NewRepository(glRepository, glOwner, glHostname, glToken, "gotk", glOwner+"@users.noreply.gitlab.com")

@ -64,6 +64,7 @@ var (
installImagePullSecret string installImagePullSecret string
installArch string installArch string
installWatchAllNamespaces bool installWatchAllNamespaces bool
installLogLevel string
) )
func init() { func init() {
@ -85,6 +86,7 @@ func init() {
"arch can be amd64 or arm64") "arch can be amd64 or arm64")
installCmd.Flags().BoolVar(&installWatchAllNamespaces, "watch-all-namespaces", true, installCmd.Flags().BoolVar(&installWatchAllNamespaces, "watch-all-namespaces", true,
"watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed") "watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed")
installCmd.Flags().StringVar(&installLogLevel, "log-level", "info", "set the controllers log level")
rootCmd.AddCommand(installCmd) rootCmd.AddCommand(installCmd)
} }
@ -93,6 +95,10 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("arch %s is not supported, can be %v", installArch, supportedArch) return fmt.Errorf("arch %s is not supported, can be %v", installArch, supportedArch)
} }
if !utils.containsItemString(supportedLogLevels, installLogLevel) {
return fmt.Errorf("log level %s is not supported, can be %v", bootstrapLogLevel, installLogLevel)
}
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel() defer cancel()
@ -115,7 +121,8 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
} }
if kustomizePath == "" { if kustomizePath == "" {
err = genInstallManifests(installVersion, namespace, installComponents, err = genInstallManifests(installVersion, namespace, installComponents,
installWatchAllNamespaces, installRegistry, installImagePullSecret, installArch, tmpDir) installWatchAllNamespaces, installRegistry, installImagePullSecret,
installArch, installLogLevel, tmpDir)
if err != nil { if err != nil {
return fmt.Errorf("install failed: %w", err) return fmt.Errorf("install failed: %w", err)
} }
@ -206,6 +213,7 @@ var kustomizationTmpl = `---
{{- $watchAllNamespaces := .WatchAllNamespaces }} {{- $watchAllNamespaces := .WatchAllNamespaces }}
{{- $registry := .Registry }} {{- $registry := .Registry }}
{{- $arch := .Arch }} {{- $arch := .Arch }}
{{- $logLevel := .LogLevel }}
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
namespace: {{.Namespace}} namespace: {{.Namespace}}
@ -238,6 +246,9 @@ patchesJson6902:
- op: replace - op: replace
path: /spec/template/spec/containers/0/args/0 path: /spec/template/spec/containers/0/args/0
value: --watch-all-namespaces={{$watchAllNamespaces}} value: --watch-all-namespaces={{$watchAllNamespaces}}
- op: replace
path: /spec/template/spec/containers/0/args/1
value: --log-level={{$logLevel}}
{{- else }} {{- else }}
- target: - target:
group: apps group: apps
@ -251,6 +262,9 @@ patchesJson6902:
- op: replace - op: replace
path: /spec/template/spec/containers/0/args/1 path: /spec/template/spec/containers/0/args/1
value: --watch-all-namespaces={{$watchAllNamespaces}} value: --watch-all-namespaces={{$watchAllNamespaces}}
- op: replace
path: /spec/template/spec/containers/0/args/2
value: --log-level={{$logLevel}}
{{- end }} {{- end }}
{{- end }} {{- end }}
@ -327,7 +341,7 @@ func downloadManifests(version string, tmpDir string) error {
} }
func genInstallManifests(version string, namespace string, components []string, func genInstallManifests(version string, namespace string, components []string,
watchAllNamespaces bool, registry, imagePullSecret, arch, tmpDir string) error { watchAllNamespaces bool, registry, imagePullSecret, arch, logLevel, tmpDir string) error {
eventsAddr := "" eventsAddr := ""
if utils.containsItemString(components, defaultNotification) { if utils.containsItemString(components, defaultNotification) {
eventsAddr = fmt.Sprintf("http://%s/", defaultNotification) eventsAddr = fmt.Sprintf("http://%s/", defaultNotification)
@ -342,6 +356,7 @@ func genInstallManifests(version string, namespace string, components []string,
ImagePullSecret string ImagePullSecret string
Arch string Arch string
WatchAllNamespaces bool WatchAllNamespaces bool
LogLevel string
}{ }{
Version: version, Version: version,
Namespace: namespace, Namespace: namespace,
@ -351,6 +366,7 @@ func genInstallManifests(version string, namespace string, components []string,
ImagePullSecret: imagePullSecret, ImagePullSecret: imagePullSecret,
Arch: arch, Arch: arch,
WatchAllNamespaces: watchAllNamespaces, WatchAllNamespaces: watchAllNamespaces,
LogLevel: logLevel,
} }
if err := downloadManifests(version, tmpDir); err != nil { if err := downloadManifests(version, tmpDir); err != nil {

@ -113,6 +113,7 @@ var (
supportedArch = []string{"arm64", "amd64"} supportedArch = []string{"arm64", "amd64"}
supportedDecryptionProviders = []string{"sops"} supportedDecryptionProviders = []string{"sops"}
supportedHelmChartSourceKinds = []string{sourcev1.HelmRepositoryKind, sourcev1.GitRepositoryKind} supportedHelmChartSourceKinds = []string{sourcev1.HelmRepositoryKind, sourcev1.GitRepositoryKind}
supportedLogLevels = []string{"debug", "info", "error"}
) )
func init() { func init() {

@ -14,6 +14,7 @@ The bootstrap sub-commands bootstrap the toolkit components on the targeted Git
--components strings list of components, accepts comma-separated values (default [source-controller,kustomize-controller,helm-controller,notification-controller]) --components strings list of components, accepts comma-separated values (default [source-controller,kustomize-controller,helm-controller,notification-controller])
-h, --help help for bootstrap -h, --help help for bootstrap
--image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry --image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry
--log-level string set the controllers log level (default "info")
--registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd") --registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd")
-v, --version string toolkit version (default "latest") -v, --version string toolkit version (default "latest")
--watch-all-namespaces watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed (default true) --watch-all-namespaces watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed (default true)

@ -62,6 +62,7 @@ gotk bootstrap github [flags]
--components strings list of components, accepts comma-separated values (default [source-controller,kustomize-controller,helm-controller,notification-controller]) --components strings list of components, accepts comma-separated values (default [source-controller,kustomize-controller,helm-controller,notification-controller])
--image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry --image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry
--kubeconfig string path to the kubeconfig file (default "~/.kube/config") --kubeconfig string path to the kubeconfig file (default "~/.kube/config")
--log-level string set the controllers log level (default "info")
--namespace string the namespace scope for this operation (default "gitops-system") --namespace string the namespace scope for this operation (default "gitops-system")
--registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd") --registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd")
--timeout duration timeout for this operation (default 5m0s) --timeout duration timeout for this operation (default 5m0s)

@ -59,6 +59,7 @@ gotk bootstrap gitlab [flags]
--components strings list of components, accepts comma-separated values (default [source-controller,kustomize-controller,helm-controller,notification-controller]) --components strings list of components, accepts comma-separated values (default [source-controller,kustomize-controller,helm-controller,notification-controller])
--image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry --image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry
--kubeconfig string path to the kubeconfig file (default "~/.kube/config") --kubeconfig string path to the kubeconfig file (default "~/.kube/config")
--log-level string set the controllers log level (default "info")
--namespace string the namespace scope for this operation (default "gitops-system") --namespace string the namespace scope for this operation (default "gitops-system")
--registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd") --registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd")
--timeout duration timeout for this operation (default 5m0s) --timeout duration timeout for this operation (default 5m0s)

@ -37,6 +37,7 @@ gotk install [flags]
--export write the install manifests to stdout and exit --export write the install manifests to stdout and exit
-h, --help help for install -h, --help help for install
--image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry --image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry
--log-level string set the controllers log level (default "info")
--manifests string path to the manifest directory, dev only --manifests string path to the manifest directory, dev only
--registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd") --registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd")
-v, --version string toolkit version (default "latest") -v, --version string toolkit version (default "latest")

Loading…
Cancel
Save