Skip to content

Commit

Permalink
Merge pull request #91 from izturn/RESTClient-options
Browse files Browse the repository at this point in the history
add options for customize the Discovery client's config
  • Loading branch information
elenz97 authored Jul 20, 2022
2 parents a48adbe + 0d11cad commit d5e6611
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func New(options *Options) (Client, error) {
return newClient(options, settings.RESTClientGetter(), settings)
}

// NewClientFromKubeConf returns a new Helm client constructed with the provided kubeconfig options.
func NewClientFromKubeConf(options *KubeConfClientOptions) (Client, error) {
// NewClientFromKubeConf returns a new Helm client constructed with the provided kubeconfig & RESTClient (optional) options.
func NewClientFromKubeConf(options *KubeConfClientOptions, restClientOpts ...RESTClientOption) (Client, error) {
settings := cli.New()
if options.KubeConfig == nil {
return nil, fmt.Errorf("kubeconfig missing")
}

clientGetter := NewRESTClientGetter(options.Namespace, options.KubeConfig, nil)
clientGetter := NewRESTClientGetter(options.Namespace, options.KubeConfig, nil, restClientOpts...)

if options.KubeContext != "" {
settings.KubeContext = options.KubeContext
Expand Down
8 changes: 7 additions & 1 deletion client_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
// NewRESTClientGetter returns a RESTClientGetter using the provided 'namespace', 'kubeConfig' and 'restConfig'.
//
// source: https://github.com/helm/helm/issues/6910#issuecomment-601277026
func NewRESTClientGetter(namespace string, kubeConfig []byte, restConfig *rest.Config) *RESTClientGetter {
func NewRESTClientGetter(namespace string, kubeConfig []byte, restConfig *rest.Config, opts ...RESTClientOption) *RESTClientGetter {
return &RESTClientGetter{
namespace: namespace,
kubeConfig: kubeConfig,
restConfig: restConfig,
opts: opts,
}
}

Expand All @@ -27,6 +28,7 @@ func (c *RESTClientGetter) ToRESTConfig() (*rest.Config, error) {
}

return clientcmd.RESTConfigFromKubeConfig(c.kubeConfig)

}

// ToDiscoveryClient returns a CachedDiscoveryInterface that can be used as a discovery client.
Expand All @@ -41,6 +43,10 @@ func (c *RESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterfa
// This setting is only used for discovery.
config.Burst = 100

for _, fn := range c.opts {
fn(config)
}

discoveryClient, _ := discovery.NewDiscoveryClientForConfig(config)
return memory.NewMemCacheClient(discoveryClient), nil
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func ExampleNewClientFromKubeConf() {
KubeConfig: []byte{},
}

helmClient, err := NewClientFromKubeConf(opt)
helmClient, err := NewClientFromKubeConf(opt, Burst(100), Timeout(10e9))
if err != nil {
panic(err)
}
Expand Down
23 changes: 23 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,34 @@ type Options struct {
Output io.Writer
}

// RESTClientOption is a function that can be used to set the RESTClientOptions of a HelmClient.
type RESTClientOption func(*rest.Config)

// Timeout specifies the timeout for a RESTClient as a RESTClientOption.
// The default (if unspecified) is 32 seconds.
// See [1] for reference.
// [^1]: https://github.com/kubernetes/client-go/blob/c6bd30b9ec5f668df191bc268c6f550c37726edb/discovery/discovery_client.go#L52
func Timeout(d time.Duration) RESTClientOption {
return func(r *rest.Config) {
r.Timeout = d
}
}

// Maximum burst for throttle
// the created RESTClient will use DefaultBurst: 100.
func Burst(v int) RESTClientOption {
return func(r *rest.Config) {
r.Burst = v
}
}

// RESTClientGetter defines the values of a helm REST client.
type RESTClientGetter struct {
namespace string
kubeConfig []byte
restConfig *rest.Config

opts []RESTClientOption
}

// HelmClient Client defines the values of a helm client.
Expand Down

0 comments on commit d5e6611

Please sign in to comment.