From d5e78b9f803ddeeef79e8d1ba7c143a55acc7ae5 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Sun, 13 Sep 2020 10:38:15 +0300 Subject: [PATCH] Add watch all namespaces flag --- cmd/gotk/bootstrap.go | 18 +++++--- cmd/gotk/install.go | 71 ++++++++++++++++++++----------- docs/cmd/gotk_bootstrap.md | 1 + docs/cmd/gotk_bootstrap_github.md | 1 + docs/cmd/gotk_bootstrap_gitlab.md | 1 + docs/cmd/gotk_install.md | 1 + 6 files changed, 61 insertions(+), 32 deletions(-) diff --git a/cmd/gotk/bootstrap.go b/cmd/gotk/bootstrap.go index 5802cf26..3eccb843 100644 --- a/cmd/gotk/bootstrap.go +++ b/cmd/gotk/bootstrap.go @@ -45,12 +45,13 @@ var bootstrapCmd = &cobra.Command{ } var ( - bootstrapVersion string - bootstrapComponents []string - bootstrapRegistry string - bootstrapImagePullSecret string - bootstrapArch string - bootstrapBranch string + bootstrapVersion string + bootstrapComponents []string + bootstrapRegistry string + bootstrapImagePullSecret string + bootstrapArch string + bootstrapBranch string + bootstrapWatchAllNamespaces bool ) const ( @@ -74,6 +75,8 @@ func init() { bootstrapCmd.PersistentFlags().StringVar(&bootstrapBranch, "branch", bootstrapDefaultBranch, "default branch (for GitHub this must match the default branch setting for the organization)") rootCmd.AddCommand(bootstrapCmd) + 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") } func generateInstallManifests(targetPath, namespace, tmpDir string) (string, error) { @@ -84,7 +87,8 @@ func generateInstallManifests(targetPath, namespace, tmpDir string) (string, err return "", fmt.Errorf("generating manifests failed: %w", err) } - if err := genInstallManifests(bootstrapVersion, namespace, bootstrapComponents, bootstrapRegistry, bootstrapImagePullSecret, bootstrapArch, gotkDir); err != nil { + if err := genInstallManifests(bootstrapVersion, namespace, bootstrapComponents, + bootstrapWatchAllNamespaces, bootstrapRegistry, bootstrapImagePullSecret, bootstrapArch, gotkDir); err != nil { return "", fmt.Errorf("generating manifests failed: %w", err) } diff --git a/cmd/gotk/install.go b/cmd/gotk/install.go index 6a7edad8..91e9e01f 100644 --- a/cmd/gotk/install.go +++ b/cmd/gotk/install.go @@ -55,14 +55,15 @@ If a previous version is installed, then an in-place upgrade will be performed.` } var ( - installExport bool - installDryRun bool - installManifestsPath string - installVersion string - installComponents []string - installRegistry string - installImagePullSecret string - installArch string + installExport bool + installDryRun bool + installManifestsPath string + installVersion string + installComponents []string + installRegistry string + installImagePullSecret string + installArch string + installWatchAllNamespaces bool ) func init() { @@ -82,6 +83,8 @@ func init() { "Kubernetes secret name used for pulling the toolkit images from a private registry") installCmd.Flags().StringVar(&installArch, "arch", "amd64", "arch can be amd64 or arm64") + 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") rootCmd.AddCommand(installCmd) } @@ -111,7 +114,8 @@ func installCmdRun(cmd *cobra.Command, args []string) error { logger.Generatef("generating manifests") } if kustomizePath == "" { - err = genInstallManifests(installVersion, namespace, installComponents, installRegistry, installImagePullSecret, installArch, tmpDir) + err = genInstallManifests(installVersion, namespace, installComponents, + installWatchAllNamespaces, installRegistry, installImagePullSecret, installArch, tmpDir) if err != nil { return fmt.Errorf("install failed: %w", err) } @@ -199,6 +203,7 @@ fieldSpecs: var kustomizationTmpl = `--- {{- $eventsAddr := .EventsAddr }} +{{- $watchAllNamespaces := .WatchAllNamespaces }} {{- $registry := .Registry }} {{- $arch := .Arch }} apiVersion: kustomize.config.k8s.io/v1beta1 @@ -223,7 +228,17 @@ patches: patchesJson6902: {{- range $i, $component := .Components }} -{{- if ne $component "notification-controller" }} +{{- if eq $component "notification-controller" }} +- target: + group: apps + version: v1 + kind: Deployment + name: {{$component}} + patch: |- + - op: replace + path: /spec/template/spec/containers/0/args/0 + value: --watch-all-namespaces={{$watchAllNamespaces}} +{{- else }} - target: group: apps version: v1 @@ -233,6 +248,9 @@ patchesJson6902: - op: replace path: /spec/template/spec/containers/0/args/0 value: --events-addr={{$eventsAddr}} + - op: replace + path: /spec/template/spec/containers/0/args/1 + value: --watch-all-namespaces={{$watchAllNamespaces}} {{- end }} {{- end }} @@ -308,28 +326,31 @@ func downloadManifests(version string, tmpDir string) error { return nil } -func genInstallManifests(version string, namespace string, components []string, registry, imagePullSecret, arch, tmpDir string) error { +func genInstallManifests(version string, namespace string, components []string, + watchAllNamespaces bool, registry, imagePullSecret, arch, tmpDir string) error { eventsAddr := "" if utils.containsItemString(components, defaultNotification) { eventsAddr = fmt.Sprintf("http://%s/", defaultNotification) } model := struct { - Version string - Namespace string - Components []string - EventsAddr string - Registry string - ImagePullSecret string - Arch string + Version string + Namespace string + Components []string + EventsAddr string + Registry string + ImagePullSecret string + Arch string + WatchAllNamespaces bool }{ - Version: version, - Namespace: namespace, - Components: components, - EventsAddr: eventsAddr, - Registry: registry, - ImagePullSecret: imagePullSecret, - Arch: arch, + Version: version, + Namespace: namespace, + Components: components, + EventsAddr: eventsAddr, + Registry: registry, + ImagePullSecret: imagePullSecret, + Arch: arch, + WatchAllNamespaces: watchAllNamespaces, } if err := downloadManifests(version, tmpDir); err != nil { diff --git a/docs/cmd/gotk_bootstrap.md b/docs/cmd/gotk_bootstrap.md index de373453..1b87380f 100644 --- a/docs/cmd/gotk_bootstrap.md +++ b/docs/cmd/gotk_bootstrap.md @@ -16,6 +16,7 @@ The bootstrap sub-commands bootstrap the toolkit components on the targeted Git --image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry --registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd") -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) ``` ### Options inherited from parent commands diff --git a/docs/cmd/gotk_bootstrap_github.md b/docs/cmd/gotk_bootstrap_github.md index 5bca06e9..650a8d43 100644 --- a/docs/cmd/gotk_bootstrap_github.md +++ b/docs/cmd/gotk_bootstrap_github.md @@ -67,6 +67,7 @@ gotk bootstrap github [flags] --timeout duration timeout for this operation (default 5m0s) --verbose print generated objects -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) ``` ### SEE ALSO diff --git a/docs/cmd/gotk_bootstrap_gitlab.md b/docs/cmd/gotk_bootstrap_gitlab.md index 7c7805ab..be8c6399 100644 --- a/docs/cmd/gotk_bootstrap_gitlab.md +++ b/docs/cmd/gotk_bootstrap_gitlab.md @@ -64,6 +64,7 @@ gotk bootstrap gitlab [flags] --timeout duration timeout for this operation (default 5m0s) --verbose print generated objects -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) ``` ### SEE ALSO diff --git a/docs/cmd/gotk_install.md b/docs/cmd/gotk_install.md index 97d629d4..1cdc400a 100644 --- a/docs/cmd/gotk_install.md +++ b/docs/cmd/gotk_install.md @@ -40,6 +40,7 @@ gotk install [flags] --manifests string path to the manifest directory, dev only --registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd") -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) ``` ### Options inherited from parent commands