Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow modification of host name via package command tree while attaching package command tree #1438

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 23 additions & 2 deletions cli/pkg/kctrl/cmd/core/config_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ConfigFactory interface {
ConfigureContextResolver(func() (string, error))
ConfigureYAMLResolver(func() (string, error))
ConfigureClient(float32, int)
RESTConfig() (*rest.Config, error)
RESTConfig(*ConfigOpts) (*rest.Config, error)
DefaultNamespace() (string, error)
}

Expand All @@ -29,6 +29,13 @@ type ConfigFactoryImpl struct {

qps float32
burst int

defaultKubeconfigOverridePath string
defaultKubeconfigOverrideContext string
}

type ConfigOpts struct {
HostNameMod func(string) string
}

var _ ConfigFactory = &ConfigFactoryImpl{}
Expand All @@ -49,12 +56,17 @@ 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
}

func (f *ConfigFactoryImpl) RESTConfig() (*rest.Config, error) {
func (f *ConfigFactoryImpl) RESTConfig(opts *ConfigOpts) (*rest.Config, error) {
isExplicitYAMLConfig, config, err := f.clientConfig()
if err != nil {
return nil, err
Expand All @@ -78,6 +90,10 @@ func (f *ConfigFactoryImpl) RESTConfig() (*rest.Config, error) {
return nil, fmt.Errorf("Building Kubernetes config%s: %s%s", prefixMsg, err, hintMsg)
}

if opts != nil && opts.HostNameMod != nil {
restConfig.Host = opts.HostNameMod(restConfig.Host)
}

if f.qps > 0.0 {
restConfig.QPS = f.qps
restConfig.Burst = f.burst
Expand Down Expand Up @@ -123,6 +139,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
14 changes: 7 additions & 7 deletions cli/pkg/kctrl/cmd/core/deps_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DepsFactory interface {
DynamicClient(opts DynamicClientOpts) (dynamic.Interface, error)
CoreClient() (kubernetes.Interface, error)
KappCtrlClient() (kcclient.Interface, error)
PackageClient() (pkgclient.Interface, error)
PackageClient(*ConfigOpts) (pkgclient.Interface, error)
}

type DepsFactoryImpl struct {
Expand All @@ -44,15 +44,15 @@ type DynamicClientOpts struct{}

// RESTHost ideally should be on ConfigFactory (TODO remove)
func (f *DepsFactoryImpl) RESTHost() (string, error) {
config, err := f.configFactory.RESTConfig()
config, err := f.configFactory.RESTConfig(nil)
if err != nil {
return "", err
}
return config.Host, nil
}

func (f *DepsFactoryImpl) DynamicClient(opts DynamicClientOpts) (dynamic.Interface, error) {
config, err := f.configFactory.RESTConfig()
config, err := f.configFactory.RESTConfig(nil)
if err != nil {
return nil, err
}
Expand All @@ -72,7 +72,7 @@ func (f *DepsFactoryImpl) DynamicClient(opts DynamicClientOpts) (dynamic.Interfa
}

func (f *DepsFactoryImpl) CoreClient() (kubernetes.Interface, error) {
config, err := f.configFactory.RESTConfig()
config, err := f.configFactory.RESTConfig(nil)
if err != nil {
return nil, err
}
Expand All @@ -88,7 +88,7 @@ func (f *DepsFactoryImpl) CoreClient() (kubernetes.Interface, error) {
}

func (f *DepsFactoryImpl) KappCtrlClient() (kcclient.Interface, error) {
config, err := f.configFactory.RESTConfig()
config, err := f.configFactory.RESTConfig(nil)
if err != nil {
return nil, err
}
Expand All @@ -103,8 +103,8 @@ func (f *DepsFactoryImpl) KappCtrlClient() (kcclient.Interface, error) {
return clientset, nil
}

func (f *DepsFactoryImpl) PackageClient() (pkgclient.Interface, error) {
config, err := f.configFactory.RESTConfig()
func (f *DepsFactoryImpl) PackageClient(configOpts *ConfigOpts) (pkgclient.Interface, error) {
config, err := f.configFactory.RESTConfig(configOpts)
if err != nil {
return nil, err
}
Expand Down
8 changes: 8 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,14 @@ type PackageCommandTreeOpts struct {

Color bool
JSON bool

DefaultKubeconfigOverridePath string
DefaultKubeconfigOverrideContext string
KubeconfigHostNameMod func(string) 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
4 changes: 3 additions & 1 deletion cli/pkg/kctrl/cmd/package/available/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func (o *GetOptions) Run(args []string) error {
return fmt.Errorf("Package name should be of the format 'name' or 'name/version'")
}

client, err := o.depsFactory.PackageClient()
client, err := o.depsFactory.PackageClient(&cmdcore.ConfigOpts{
HostNameMod: o.pkgCmdTreeOpts.KubeconfigHostNameMod,
})
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions cli/pkg/kctrl/cmd/package/available/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ func (o *ListOptions) listPackageMetadatas() error {
nsHeader.Hidden = false
}

client, err := o.depsFactory.PackageClient()
client, err := o.depsFactory.PackageClient(&cmdcore.ConfigOpts{
HostNameMod: o.pkgCmdTreeOpts.KubeconfigHostNameMod,
})
if err != nil {
return err
}
Expand Down Expand Up @@ -159,7 +161,9 @@ func (o *ListOptions) listPackages() error {
nsHeader.Hidden = false
}

client, err := o.depsFactory.PackageClient()
client, err := o.depsFactory.PackageClient(&cmdcore.ConfigOpts{
HostNameMod: o.pkgCmdTreeOpts.KubeconfigHostNameMod,
})
if err != nil {
return err
}
Expand Down
17 changes: 10 additions & 7 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 @@ -234,7 +237,7 @@ func (o *CreateOrUpdateOptions) RunCreate(args []string) error {
}

if len(o.version) == 0 {
pkgClient, err := o.depsFactory.PackageClient()
pkgClient, err := o.depsFactory.PackageClient(nil)
if err != nil {
return err
}
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
Loading