Skip to content

Commit

Permalink
Make bits of kctrl more configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Soumik Majumder <[email protected]>
  • Loading branch information
100mik committed Jan 3, 2024
1 parent 55d7a38 commit d4125e1
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 19 deletions.
1 change: 1 addition & 0 deletions cli/pkg/kctrl/cmd/app/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cli/pkg/kctrl/cmd/app/kick.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions cli/pkg/kctrl/cmd/core/config_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type ConfigFactoryImpl struct {

qps float32
burst int

defaultKubeconfigOverridePath string
defaultKubeconfigOverrideContext string
}

var _ ConfigFactory = &ConfigFactoryImpl{}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions cli/pkg/kctrl/cmd/core/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ type PackageCommandTreeOpts struct {

Color bool
JSON bool

DefaultKubeconfigOverridePath string
DefaultKubeconfigOverrideContext string

DefaultServiceAcccountName string
WaitByDefault bool
AllowSharedNamespaces bool
}

type Example struct {
Expand Down
4 changes: 2 additions & 2 deletions cli/pkg/kctrl/cmd/core/secure_namespace_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions cli/pkg/kctrl/cmd/core/wait_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
3 changes: 2 additions & 1 deletion cli/pkg/kctrl/cmd/kctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions cli/pkg/kctrl/cmd/package/installed/create_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand All @@ -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")
Expand All @@ -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)

Expand All @@ -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)")
Expand All @@ -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")
Expand All @@ -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)

Expand All @@ -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")
Expand All @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions cli/pkg/kctrl/cmd/package/installed/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cli/pkg/kctrl/cmd/package/installed/pause_or_kick.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions cli/pkg/kctrl/cmd/package/repository/add_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand All @@ -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
Expand All @@ -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)")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cli/pkg/kctrl/cmd/package/repository/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cli/pkg/kctrl/cmd/package/repository/kick.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d4125e1

Please sign in to comment.