From a01e6a6c3b2e4dbd344980d0ba663e04d2719371 Mon Sep 17 00:00:00 2001 From: Max Jonas Werner Date: Tue, 8 Aug 2023 15:27:26 +0200 Subject: [PATCH] wip --- cmd/flux/create_image_repository.go | 2 +- cmd/flux/get.go | 8 +++++--- cmd/flux/install.go | 4 ++-- cmd/flux/main.go | 32 ++++++++++++++++++++++++----- cmd/flux/status.go | 2 +- 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/cmd/flux/create_image_repository.go b/cmd/flux/create_image_repository.go index 3a4d351d..87dc600e 100644 --- a/cmd/flux/create_image_repository.go +++ b/cmd/flux/create_image_repository.go @@ -101,7 +101,7 @@ func createImageRepositoryRun(cmd *cobra.Command, args []string) error { var repo = imagev1.ImageRepository{ ObjectMeta: metav1.ObjectMeta{ Name: objectName, - Namespace: *kubeconfigArgs.Namespace, + Namespace: GetDesiredNamespace(kubeconfigArgs), Labels: labels, }, Spec: imagev1.ImageRepositorySpec{ diff --git a/cmd/flux/get.go b/cmd/flux/get.go index fe70006a..4850dfb8 100644 --- a/cmd/flux/get.go +++ b/cmd/flux/get.go @@ -146,9 +146,11 @@ func (get getCommand) run(cmd *cobra.Command, args []string) error { return err } + ns := GetDesiredNamespace(kubeconfigArgs) + var listOpts []client.ListOption if !getArgs.allNamespaces { - listOpts = append(listOpts, client.InNamespace(*kubeconfigArgs.Namespace)) + listOpts = append(listOpts, client.InNamespace(ns)) } if len(args) > 0 { @@ -190,12 +192,12 @@ func (get getCommand) run(cmd *cobra.Command, args []string) error { logger.Failuref("%s object '%s' not found in %s namespace", get.kind, args[0], - namespaceNameOrAny(getArgs.allNamespaces, *kubeconfigArgs.Namespace), + namespaceNameOrAny(getArgs.allNamespaces, ns), ) } else if !getAll { logger.Failuref("no %s objects found in %s namespace", get.kind, - namespaceNameOrAny(getArgs.allNamespaces, *kubeconfigArgs.Namespace), + namespaceNameOrAny(getArgs.allNamespaces, ns), ) } return nil diff --git a/cmd/flux/install.go b/cmd/flux/install.go index 53712021..c25c0135 100644 --- a/cmd/flux/install.go +++ b/cmd/flux/install.go @@ -146,7 +146,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error { opts := install.Options{ BaseURL: installArgs.manifestsPath, Version: installArgs.version, - Namespace: *kubeconfigArgs.Namespace, + Namespace: GetDesiredNamespace(kubeconfigArgs), Components: components, Registry: installArgs.registry, ImagePullSecret: installArgs.imagePullSecret, @@ -181,7 +181,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error { } logger.Successf("manifests build completed") - logger.Actionf("installing components in %s namespace", *kubeconfigArgs.Namespace) + logger.Actionf("installing components in %s namespace", opts.Namespace) applyOutput, err := utils.Apply(ctx, kubeconfigArgs, kubeclientOptions, tmpDir, filepath.Join(tmpDir, manifest.Path)) if err != nil { diff --git a/cmd/flux/main.go b/cmd/flux/main.go index 9485110c..1f590919 100644 --- a/cmd/flux/main.go +++ b/cmd/flux/main.go @@ -105,6 +105,10 @@ Command line utility for assembling Kubernetes CD pipelines the GitOps way.`, return fmt.Errorf("error getting namespace: %w", err) } + if ns == "" { + return nil + } + if e := validation.IsDNS1123Label(ns); len(e) > 0 { return fmt.Errorf("namespace must be a valid DNS label: %q", ns) } @@ -140,7 +144,6 @@ func init() { rootCmd.PersistentFlags().DurationVar(&rootArgs.timeout, "timeout", 5*time.Minute, "timeout for this operation") rootCmd.PersistentFlags().BoolVar(&rootArgs.verbose, "verbose", false, "print generated objects") - configureDefaultNamespace() kubeconfigArgs.APIServer = nil // prevent AddFlags from configuring --server flag kubeconfigArgs.Timeout = nil // prevent AddFlags from configuring --request-timeout flag, we have --timeout instead kubeconfigArgs.AddFlags(rootCmd.PersistentFlags()) @@ -198,8 +201,10 @@ func main() { } } -func configureDefaultNamespace() { - *kubeconfigArgs.Namespace = rootArgs.defaults.Namespace +func GetDesiredNamespace(cfg *genericclioptions.ConfigFlags) string { + if *cfg.Namespace != "" { + return *cfg.Namespace + } fromEnv := os.Getenv("FLUX_SYSTEM_NAMESPACE") if fromEnv != "" { // namespace must be a valid DNS label. Assess against validation @@ -207,11 +212,28 @@ func configureDefaultNamespace() { // may not be actively provided by end-user. if e := validation.IsDNS1123Label(fromEnv); len(e) > 0 { logger.Warningf(" ignoring invalid FLUX_SYSTEM_NAMESPACE: %q", fromEnv) - return + } else { + return fromEnv } + } - kubeconfigArgs.Namespace = &fromEnv + if _, has := os.LookupEnv("FLUX_NS_FOLLOW_KUBECONTEXT"); has { + rawCfg, err := cfg.ToRawKubeConfigLoader().RawConfig() + if err != nil { + logger.Warningf(" failed parsing kubeconfig, ignoring: %q", fromEnv) + } else { + ctx := *cfg.Context + if ctx == "" { + ctx = rawCfg.CurrentContext + } + ns := rawCfg.Contexts[ctx].Namespace + if ns != "" { + return ns + } + } } + + return rootArgs.defaults.Namespace } // readPasswordFromStdin reads a password from stdin and returns the input diff --git a/cmd/flux/status.go b/cmd/flux/status.go index e6c4b5fc..a7944d12 100644 --- a/cmd/flux/status.go +++ b/cmd/flux/status.go @@ -85,7 +85,7 @@ func buildComponentObjectRefs(components ...string) ([]object.ObjMetadata, error var objRefs []object.ObjMetadata for _, deployment := range components { objRefs = append(objRefs, object.ObjMetadata{ - Namespace: *kubeconfigArgs.Namespace, + Namespace: GetDesiredNamespace(kubeconfigArgs), Name: deployment, GroupKind: schema.GroupKind{Group: "apps", Kind: "Deployment"}, })