ref(*): improve initializing helm clients

pull/3945/head
Adam Reese 6 years ago
parent 68c0b6a24a
commit a78aff8d39
No known key found for this signature in database
GPG Key ID: 06F35E60A7A18DD6

@ -22,8 +22,6 @@ import (
"strings"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
// Import to initialize client auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth"
@ -136,28 +134,6 @@ func checkArgsLength(argsReceived int, requiredArgs ...string) error {
return nil
}
// configForContext creates a Kubernetes REST client configuration for a given kubeconfig context.
func configForContext(context string) (*rest.Config, error) {
config, err := kube.GetConfig(context).ClientConfig()
if err != nil {
return nil, fmt.Errorf("could not get Kubernetes config for context %q: %s", context, err)
}
return config, nil
}
// getKubeClient creates a Kubernetes config and client for a given kubeconfig context.
func getKubeClient(context string) (*rest.Config, kubernetes.Interface, error) {
config, err := configForContext(context)
if err != nil {
return nil, nil, err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, fmt.Errorf("could not get Kubernetes client: %s", err)
}
return config, client, nil
}
// ensureHelmClient returns a new helm client impl. if h is not nil.
func ensureHelmClient(h helm.Interface) helm.Interface {
if h != nil {
@ -167,15 +143,18 @@ func ensureHelmClient(h helm.Interface) helm.Interface {
}
func newClient() helm.Interface {
_, clientset, err := getKubeClient(settings.KubeContext)
cfg := kube.GetConfig(settings.KubeContext)
kc := kube.New(cfg)
clientset, err := kc.KubernetesClientSet()
if err != nil {
// TODO return error
panic(err)
}
// TODO add other backends
cfgmaps := driver.NewConfigMaps(clientset.Core().ConfigMaps(settings.TillerNamespace))
cfgmaps := driver.NewConfigMaps(clientset.CoreV1().ConfigMaps(settings.TillerNamespace))
return helm.NewClient(
helm.KubeClient(kc),
helm.Driver(cfgmaps),
helm.Discovery(clientset.Discovery()),
)

@ -18,7 +18,6 @@ package helm // import "k8s.io/helm/pkg/helm"
import (
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/kube"
"k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/proto/hapi/release"
rls "k8s.io/helm/pkg/proto/hapi/services"
@ -42,9 +41,7 @@ func NewClient(opts ...Option) *Client {
func (c *Client) init() *Client {
env := environment.New()
env.Releases = storage.Init(c.opts.driver)
// TODO
env.KubeClient = kube.New(nil)
env.KubeClient = c.opts.kubeClient
c.tiller = tiller.NewReleaseServer(env, c.opts.discovery)
return c

@ -24,6 +24,7 @@ import (
"k8s.io/helm/pkg/proto/hapi/release"
rls "k8s.io/helm/pkg/proto/hapi/services"
"k8s.io/helm/pkg/storage/driver"
"k8s.io/helm/pkg/tiller/environment"
)
// Option allows specifying various settings configurable by
@ -68,8 +69,9 @@ type options struct {
// release test options are applied directly to the test release history request
testReq rls.TestReleaseRequest
driver driver.Driver
discovery discovery.DiscoveryInterface
driver driver.Driver
kubeClient environment.KubeClient
discovery discovery.DiscoveryInterface
}
func (opts *options) runBefore(msg proto.Message) error {
@ -378,6 +380,12 @@ func Driver(d driver.Driver) Option {
}
}
func KubeClient(kc environment.KubeClient) Option {
return func(opts *options) {
opts.kubeClient = kc
}
}
func Discovery(dc discovery.DiscoveryInterface) Option {
return func(opts *options) {
opts.discovery = dc

Loading…
Cancel
Save