From d4125e1cb2b3fb5b0b58a2b032e5dd13491297d0 Mon Sep 17 00:00:00 2001 From: Soumik Majumder Date: Fri, 15 Dec 2023 16:23:22 +0530 Subject: [PATCH] Make bits of kctrl more configurable Signed-off-by: Soumik Majumder --- cli/pkg/kctrl/cmd/app/delete.go | 1 + cli/pkg/kctrl/cmd/app/kick.go | 1 + cli/pkg/kctrl/cmd/core/config_factory.go | 13 +++++++++++++ cli/pkg/kctrl/cmd/core/examples.go | 7 +++++++ cli/pkg/kctrl/cmd/core/secure_namespace_flags.go | 4 ++-- cli/pkg/kctrl/cmd/core/wait_flags.go | 7 ++++--- cli/pkg/kctrl/cmd/kctrl.go | 3 ++- .../cmd/package/installed/create_or_update.go | 15 +++++++++------ cli/pkg/kctrl/cmd/package/installed/delete.go | 12 +++++++----- .../kctrl/cmd/package/installed/pause_or_kick.go | 1 + .../kctrl/cmd/package/repository/add_or_update.go | 8 ++++++-- cli/pkg/kctrl/cmd/package/repository/delete.go | 1 + cli/pkg/kctrl/cmd/package/repository/kick.go | 1 + 13 files changed, 55 insertions(+), 19 deletions(-) diff --git a/cli/pkg/kctrl/cmd/app/delete.go b/cli/pkg/kctrl/cmd/app/delete.go index 5e3dee82c..04f48cac9 100644 --- a/cli/pkg/kctrl/cmd/app/delete.go +++ b/cli/pkg/kctrl/cmd/app/delete.go @@ -57,6 +57,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co AllowDisableWait: true, DefaultInterval: 1 * time.Second, DefaultTimeout: 5 * time.Minute, + WaitByDefault: true, }) return cmd diff --git a/cli/pkg/kctrl/cmd/app/kick.go b/cli/pkg/kctrl/cmd/app/kick.go index 93b83424c..a223fad28 100644 --- a/cli/pkg/kctrl/cmd/app/kick.go +++ b/cli/pkg/kctrl/cmd/app/kick.go @@ -50,6 +50,7 @@ func NewKickCmd(o *KickOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman AllowDisableWait: true, DefaultInterval: 1 * time.Second, DefaultTimeout: 5 * time.Minute, + WaitByDefault: true, }) return cmd diff --git a/cli/pkg/kctrl/cmd/core/config_factory.go b/cli/pkg/kctrl/cmd/core/config_factory.go index 5e98d0e4b..bb643041b 100644 --- a/cli/pkg/kctrl/cmd/core/config_factory.go +++ b/cli/pkg/kctrl/cmd/core/config_factory.go @@ -29,6 +29,9 @@ type ConfigFactoryImpl struct { qps float32 burst int + + defaultKubeconfigOverridePath string + defaultKubeconfigOverrideContext string } var _ ConfigFactory = &ConfigFactoryImpl{} @@ -49,6 +52,11 @@ func (f *ConfigFactoryImpl) ConfigureYAMLResolver(resolverFunc func() (string, e f.yamlResolverFunc = resolverFunc } +func (f *ConfigFactoryImpl) ConfigureKubeconfigOverrides(defaultKubeconfigOverridePath string, defaultKubeconfigOverrideContext string) { + f.defaultKubeconfigOverridePath = defaultKubeconfigOverridePath + f.defaultKubeconfigOverrideContext = defaultKubeconfigOverrideContext +} + func (f *ConfigFactoryImpl) ConfigureClient(qps float32, burst int) { f.qps = qps f.burst = burst @@ -123,6 +131,11 @@ func (f *ConfigFactoryImpl) clientConfig() (bool, clientcmd.ClientConfig, error) loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() overrides := &clientcmd.ConfigOverrides{} + if len(path) == 0 && len(context) == 0 && f.defaultKubeconfigOverrideContext != "" && f.defaultKubeconfigOverridePath != "" { + path = f.defaultKubeconfigOverridePath + context = f.defaultKubeconfigOverrideContext + } + if len(path) > 0 { loadingRules.ExplicitPath = path } diff --git a/cli/pkg/kctrl/cmd/core/examples.go b/cli/pkg/kctrl/cmd/core/examples.go index 1405e976d..fadf87826 100644 --- a/cli/pkg/kctrl/cmd/core/examples.go +++ b/cli/pkg/kctrl/cmd/core/examples.go @@ -14,6 +14,13 @@ type PackageCommandTreeOpts struct { Color bool JSON bool + + DefaultKubeconfigOverridePath string + DefaultKubeconfigOverrideContext string + + DefaultServiceAcccountName string + WaitByDefault bool + AllowSharedNamespaces bool } type Example struct { diff --git a/cli/pkg/kctrl/cmd/core/secure_namespace_flags.go b/cli/pkg/kctrl/cmd/core/secure_namespace_flags.go index 303a3d3ba..2566c80e9 100644 --- a/cli/pkg/kctrl/cmd/core/secure_namespace_flags.go +++ b/cli/pkg/kctrl/cmd/core/secure_namespace_flags.go @@ -18,8 +18,8 @@ type SecureNamespaceFlags struct { AllowedSharedNamespaces bool } -func (s *SecureNamespaceFlags) Set(cmd *cobra.Command) { - cmd.Flags().BoolVar(&s.AllowedSharedNamespaces, "dangerous-allow-use-of-shared-namespace", false, "Allow use of shared namespaces") +func (s *SecureNamespaceFlags) Set(cmd *cobra.Command, defaultVal bool) { + cmd.Flags().BoolVar(&s.AllowedSharedNamespaces, "dangerous-allow-use-of-shared-namespace", defaultVal, "Allow use of shared namespaces") } func (s *SecureNamespaceFlags) CheckForDisallowedSharedNamespaces(namespace string) error { diff --git a/cli/pkg/kctrl/cmd/core/wait_flags.go b/cli/pkg/kctrl/cmd/core/wait_flags.go index 76a240ebb..b4e8865d6 100644 --- a/cli/pkg/kctrl/cmd/core/wait_flags.go +++ b/cli/pkg/kctrl/cmd/core/wait_flags.go @@ -19,13 +19,14 @@ type WaitFlagsOpts struct { AllowDisableWait bool DefaultInterval time.Duration DefaultTimeout time.Duration + WaitByDefault bool } func (f *WaitFlags) Set(cmd *cobra.Command, flagsFactory FlagsFactory, opts *WaitFlagsOpts) { - if opts.AllowDisableWait { - cmd.Flags().BoolVar(&f.Enabled, "wait", true, "Wait for reconciliation to complete") + if opts.AllowDisableWait || !opts.WaitByDefault { + cmd.Flags().BoolVar(&f.Enabled, "wait", opts.WaitByDefault, "Wait for reconciliation to complete") } - f.Enabled = true + f.Enabled = opts.WaitByDefault cmd.Flags().DurationVar(&f.CheckInterval, "wait-check-interval", opts.DefaultInterval, "Amount of time to sleep between checks while waiting") cmd.Flags().DurationVar(&f.Timeout, "wait-timeout", opts.DefaultTimeout, "Maximum amount of time to wait in wait phase") } diff --git a/cli/pkg/kctrl/cmd/kctrl.go b/cli/pkg/kctrl/cmd/kctrl.go index eef306703..bab14fcbf 100644 --- a/cli/pkg/kctrl/cmd/kctrl.go +++ b/cli/pkg/kctrl/cmd/kctrl.go @@ -76,7 +76,7 @@ func NewKctrlCmd(o *KctrlOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comm cmdcore.RestOfCommandsHelpGroup, })) - pkgOpts := cmdcore.PackageCommandTreeOpts{BinaryName: "kctrl", PositionalArgs: false, Color: true, JSON: true} + pkgOpts := cmdcore.PackageCommandTreeOpts{BinaryName: "kctrl", Color: true, JSON: true, WaitByDefault: true} setGlobalFlags(o, cmd, flagsFactory, pkgOpts) @@ -227,6 +227,7 @@ func AttachGlobalFlags(o *KctrlOptions, cmd *cobra.Command, flagsFactory cmdcore func AttachKctrlPackageCommandTree(cmd *cobra.Command, confUI *ui.ConfUI, opts cmdcore.PackageCommandTreeOpts) { configFactory := cmdcore.NewConfigFactoryImpl() + configFactory.ConfigureKubeconfigOverrides(opts.DefaultKubeconfigOverridePath, opts.DefaultKubeconfigOverrideContext) depsFactory := cmdcore.NewDepsFactoryImpl(configFactory, confUI) options := NewKctrlOptions(confUI, configFactory, depsFactory) flagsFactory := cmdcore.NewFlagsFactory(configFactory, depsFactory) diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index 1ab1a6092..ebf075118 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -88,7 +88,7 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * cmdcore.PackageManagementCommandsHelpGroup.Key: cmdcore.PackageManagementCommandsHelpGroup.Value}, } o.NamespaceFlags.SetWithPackageCommandTreeOpts(cmd, flagsFactory, o.pkgCmdTreeOpts) - o.SecureNamespaceFlags.Set(cmd) + o.SecureNamespaceFlags.Set(cmd, o.pkgCmdTreeOpts.AllowSharedNamespaces) if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") @@ -99,7 +99,7 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * cmd.Flags().StringVarP(&o.packageName, "package", "p", "", "Set package name (required)") cmd.Flags().StringVarP(&o.version, "version", "v", "", "Set package version (required)") - cmd.Flags().StringVar(&o.serviceAccountName, "service-account-name", "", "Name of an existing service account used to install underlying package contents, optional") + cmd.Flags().StringVar(&o.serviceAccountName, "service-account-name", o.pkgCmdTreeOpts.DefaultServiceAcccountName, "Name of an existing service account used to install underlying package contents, optional") cmd.Flags().StringVar(&o.valuesFile, "values-file", "", "The path to the configuration values file, optional") cmd.Flags().BoolVar(&o.values, "values", true, "Add or keep values supplied to package install, optional") cmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "Print YAML for resources being applied to the cluster without applying them, optional") @@ -108,6 +108,7 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * AllowDisableWait: true, DefaultInterval: 1 * time.Second, DefaultTimeout: 30 * time.Minute, + WaitByDefault: o.pkgCmdTreeOpts.WaitByDefault, }) o.YttOverlayFlags.Set(cmd) @@ -134,7 +135,7 @@ func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) cmdcore.PackageManagementCommandsHelpGroup.Key: cmdcore.PackageManagementCommandsHelpGroup.Value}, } o.NamespaceFlags.SetWithPackageCommandTreeOpts(cmd, flagsFactory, o.pkgCmdTreeOpts) - o.SecureNamespaceFlags.Set(cmd) + o.SecureNamespaceFlags.Set(cmd, o.pkgCmdTreeOpts.AllowSharedNamespaces) if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name (required)") @@ -145,7 +146,7 @@ func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) cmd.Flags().StringVarP(&o.packageName, "package", "p", "", "Set package name (required)") cmd.Flags().StringVarP(&o.version, "version", "v", "", "Set package version (required)") - cmd.Flags().StringVar(&o.serviceAccountName, "service-account-name", "", "Name of an existing service account used to install underlying package contents, optional") + cmd.Flags().StringVar(&o.serviceAccountName, "service-account-name", o.pkgCmdTreeOpts.DefaultServiceAcccountName, "Name of an existing service account used to install underlying package contents, optional") cmd.Flags().StringVar(&o.valuesFile, "values-file", "", "The path to the configuration values file, optional") cmd.Flags().BoolVar(&o.values, "values", true, "Add or keep values supplied to package install, optional") cmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "Print YAML for resources being applied to the cluster without applying them, optional") @@ -154,6 +155,7 @@ func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) AllowDisableWait: true, DefaultInterval: 1 * time.Second, DefaultTimeout: 30 * time.Minute, + WaitByDefault: o.pkgCmdTreeOpts.WaitByDefault, }) o.YttOverlayFlags.Set(cmd) @@ -179,7 +181,7 @@ func NewUpdateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * cmdcore.PackageManagementCommandsHelpGroup.Key: cmdcore.PackageManagementCommandsHelpGroup.Value}, } o.NamespaceFlags.SetWithPackageCommandTreeOpts(cmd, flagsFactory, o.pkgCmdTreeOpts) - o.SecureNamespaceFlags.Set(cmd) + o.SecureNamespaceFlags.Set(cmd, o.pkgCmdTreeOpts.AllowSharedNamespaces) if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "package-install", "i", "", "Set installed package name") @@ -197,6 +199,7 @@ func NewUpdateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * AllowDisableWait: true, DefaultInterval: 1 * time.Second, DefaultTimeout: 30 * time.Minute, + WaitByDefault: o.pkgCmdTreeOpts.WaitByDefault, }) o.YttOverlayFlags.Set(cmd) @@ -1072,10 +1075,10 @@ func (o *CreateOrUpdateOptions) showVersions(client pkgclient.Interface) error { } func (o *CreateOrUpdateOptions) createOrUpdateYttOverlaySecrets(pkgi *kcpkgv1alpha1.PackageInstall, client kubernetes.Interface) (*corev1.Secret, error) { - o.statusUI.PrintMessage("Creating overlay secrets") if len(o.YttOverlayFlags.yttOverlayFiles) == 0 { return nil, nil } + o.statusUI.PrintMessage("Creating overlay secrets") if pkgi != nil { for annotation := range pkgi.Annotations { diff --git a/cli/pkg/kctrl/cmd/package/installed/delete.go b/cli/pkg/kctrl/cmd/package/installed/delete.go index 08b1942a3..4a18a6097 100644 --- a/cli/pkg/kctrl/cmd/package/installed/delete.go +++ b/cli/pkg/kctrl/cmd/package/installed/delete.go @@ -75,6 +75,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co AllowDisableWait: false, DefaultInterval: 1 * time.Second, DefaultTimeout: 5 * time.Minute, + WaitByDefault: o.pkgCmdTreeOpts.WaitByDefault, }) return cmd @@ -136,11 +137,12 @@ func (o *DeleteOptions) Run(args []string) error { return err } - o.statusUI.PrintMessagef("Waiting for deletion of package install '%s' from namespace '%s'", o.Name, o.NamespaceFlags.Name) - - err = o.waitForResourceDelete(kcClient) - if err != nil { - return err + if o.WaitFlags.Enabled { + o.statusUI.PrintMessagef("Waiting for deletion of package install '%s' from namespace '%s'", o.Name, o.NamespaceFlags.Name) + err = o.waitForResourceDelete(kcClient) + if err != nil { + return err + } } return o.deleteInstallCreatedResources(pkgi, dynamicClient) diff --git a/cli/pkg/kctrl/cmd/package/installed/pause_or_kick.go b/cli/pkg/kctrl/cmd/package/installed/pause_or_kick.go index f42c83303..087ff5305 100644 --- a/cli/pkg/kctrl/cmd/package/installed/pause_or_kick.go +++ b/cli/pkg/kctrl/cmd/package/installed/pause_or_kick.go @@ -96,6 +96,7 @@ func NewKickCmd(o *PauseOrKickOptions, flagsFactory cmdcore.FlagsFactory) *cobra AllowDisableWait: true, DefaultInterval: 2 * time.Second, DefaultTimeout: 5 * time.Minute, + WaitByDefault: o.pkgCmdTreeOpts.WaitByDefault, }) return cmd diff --git a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go index f9ede874f..3bb164f7b 100644 --- a/cli/pkg/kctrl/cmd/package/repository/add_or_update.go +++ b/cli/pkg/kctrl/cmd/package/repository/add_or_update.go @@ -65,7 +65,7 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra. } o.NamespaceFlags.SetWithPackageCommandTreeOpts(cmd, flagsFactory, o.pkgCmdTreeOpts) - o.SecureNamespaceFlags.Set(cmd) + o.SecureNamespaceFlags.Set(cmd, o.pkgCmdTreeOpts.AllowSharedNamespaces) if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") @@ -84,6 +84,7 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra. AllowDisableWait: true, DefaultInterval: 1 * time.Second, DefaultTimeout: 5 * time.Minute, + WaitByDefault: o.pkgCmdTreeOpts.WaitByDefault, }) o.CreateRepository = true @@ -106,7 +107,7 @@ func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cob } o.NamespaceFlags.SetWithPackageCommandTreeOpts(cmd, flagsFactory, o.pkgCmdTreeOpts) - o.SecureNamespaceFlags.Set(cmd) + o.SecureNamespaceFlags.Set(cmd, o.pkgCmdTreeOpts.AllowSharedNamespaces) if !o.pkgCmdTreeOpts.PositionalArgs { cmd.Flags().StringVarP(&o.Name, "repository", "r", "", "Set package repository name (required)") @@ -121,6 +122,7 @@ func NewUpdateCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cob AllowDisableWait: true, DefaultInterval: 1 * time.Second, DefaultTimeout: 5 * time.Minute, + WaitByDefault: o.pkgCmdTreeOpts.WaitByDefault, }) return cmd @@ -194,6 +196,7 @@ func (o *AddOrUpdateOptions) Run(args []string) error { return NewRepoTailer(o.NamespaceFlags.Name, o.Name, o.ui, client, RepoTailerOpts{PrintCurrentState: true}).TailRepoStatus() } + o.statusUI.PrintMessagef("Updating package repository resource '%s' in namespace '%s'", o.Name, o.NamespaceFlags.Name) pkgRepository, err := o.updateExistingPackageRepository(existingRepository) if err != nil { return err @@ -218,6 +221,7 @@ func (o *AddOrUpdateOptions) add(client kcclient.Interface) error { return err } + o.statusUI.PrintMessagef("Creating package repository resource '%s' in namespace '%s'", o.Name, o.NamespaceFlags.Name) _, err = client.PackagingV1alpha1().PackageRepositories(o.NamespaceFlags.Name).Create( context.Background(), pkgRepository, metav1.CreateOptions{}) if err != nil { diff --git a/cli/pkg/kctrl/cmd/package/repository/delete.go b/cli/pkg/kctrl/cmd/package/repository/delete.go index 6f06f67ac..3d5ffd011 100644 --- a/cli/pkg/kctrl/cmd/package/repository/delete.go +++ b/cli/pkg/kctrl/cmd/package/repository/delete.go @@ -62,6 +62,7 @@ func NewDeleteCmd(o *DeleteOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Co AllowDisableWait: true, DefaultInterval: 1 * time.Second, DefaultTimeout: 5 * time.Minute, + WaitByDefault: o.pkgCmdTreeOpts.WaitByDefault, }) return cmd diff --git a/cli/pkg/kctrl/cmd/package/repository/kick.go b/cli/pkg/kctrl/cmd/package/repository/kick.go index 5d33030d2..9b013a6e9 100644 --- a/cli/pkg/kctrl/cmd/package/repository/kick.go +++ b/cli/pkg/kctrl/cmd/package/repository/kick.go @@ -65,6 +65,7 @@ func NewKickCmd(o *KickOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman AllowDisableWait: true, DefaultInterval: 1 * time.Second, DefaultTimeout: 5 * time.Minute, + WaitByDefault: o.pkgCmdTreeOpts.WaitByDefault, }) return cmd