Skip to content

Commit

Permalink
Allow modifying host via opts while getting and listing data packagin…
Browse files Browse the repository at this point in the history
…g resources
  • Loading branch information
100mik committed Jan 9, 2024
1 parent a796c7a commit d80a83c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
12 changes: 10 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 @@ -34,6 +34,10 @@ type ConfigFactoryImpl struct {
defaultKubeconfigOverrideContext string
}

type ConfigOpts struct {
HostNameMod func(string) string
}

var _ ConfigFactory = &ConfigFactoryImpl{}

func NewConfigFactoryImpl() *ConfigFactoryImpl {
Expand Down Expand Up @@ -62,7 +66,7 @@ func (f *ConfigFactoryImpl) ConfigureClient(qps float32, burst int) {
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 @@ -86,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
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
1 change: 1 addition & 0 deletions cli/pkg/kctrl/cmd/core/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type PackageCommandTreeOpts struct {

DefaultKubeconfigOverridePath string
DefaultKubeconfigOverrideContext string
KubeconfigHostNameMod func(string) string

DefaultServiceAcccountName string
WaitByDefault bool
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
2 changes: 1 addition & 1 deletion cli/pkg/kctrl/cmd/package/installed/create_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,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

0 comments on commit d80a83c

Please sign in to comment.