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
How do I pass custom kube config to helm3 go client? #6910
Comments
Related: #6024 |
clientcmd also has a canonical example for loading your own ClientConfig which implements the RESTClientGetter interface. https://godoc.org/k8s.io/client-go/tools/clientcmd |
Thank you for the links :D I went through the resource links. This might be a silly question but I could not find anything in the second link which implemented Also, I am building |
@vadasambar the following worked for me to install a helm chart programatically with helm3. package main
import (
"fmt"
"os"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/kube"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
func main() {
chartPath := "/tmp/my-chart-0.1.0.tgz"
chart, err := loader.Load(chartPath)
if err != nil {
panic(err)
}
kubeconfigPath := "/tmp/my-kubeconfig"
releaseName := "my-release"
releaseNamespace := "default"
actionConfig := new(action.Configuration)
if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", releaseNamespace), releaseNamespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
fmt.Sprintf(format, v)
}); err != nil {
panic(err)
}
iCli := action.NewInstall(actionConfig)
iCli.Namespace = releaseNamespace
iCli.ReleaseName = releaseName
rel, err := iCli.Run(chart, nil)
if err != nil {
panic(err)
}
fmt.Println("Successfully installed release: ", rel.Name)
} |
Hey @turkenh ! Sorry for the late reply. Thank you for the example :D |
@vadasambar Closing as the issue is answered and example given. Thanks for the example @turkenh |
mark |
@vadasambar hi, I am searching for the RESTClientGetter object. Could you please teel me how to get an object which implements RESTClientGetter interface? Thank you so much. |
How would I initialize an |
e.g. restClientGetter := helmapi.NewRESTClientGetter(namespace, kubeConfig)
configuration := new(action.Configuration)
if err := configuration.Init(restClientGetter, namespace, os.Getenv("HELM_DRIVER"), util.FakeDebugLog); err != nil {
return nil, err
} func NewRESTClientGetter(namespace, kubeConfig string) *SimpleRESTClientGetter {
return &SimpleRESTClientGetter{
Namespace: namespace,
KubeConfig: kubeConfig,
}
}
|
@liuming-tech I'm sorry, but what pkg is |
Oh sorry, func (c *SimpleRESTClientGetter) ToRESTConfig() (*rest.Config, error) {
config, err := clientcmd.RESTConfigFromKubeConfig([]byte(c.KubeConfig))
if err != nil {
return nil, err
}
return config, nil
} |
@bahlo Have a look at: https://helm.sh/docs/topics/advanced/#simple-example |
@hickeyma That's getting the kubeconfig path via env var, I want to pass in the kubeconfig itself. |
@liuming-tech Thanks, do you have the other three methods available as well? Otherwise I'll try to implement them myself. |
import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
)
type SimpleRESTClientGetter struct {
Namespace string
KubeConfig string
}
func NewRESTClientGetter(namespace, kubeConfig string) *SimpleRESTClientGetter {
return &SimpleRESTClientGetter{
Namespace: namespace,
KubeConfig: kubeConfig,
}
}
func (c *SimpleRESTClientGetter) ToRESTConfig() (*rest.Config, error) {
config, err := clientcmd.RESTConfigFromKubeConfig([]byte(c.KubeConfig))
if err != nil {
return nil, err
}
return config, nil
}
func (c *SimpleRESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
config, err := c.ToRESTConfig()
if err != nil {
return nil, err
}
// The more groups you have, the more discovery requests you need to make.
// given 25 groups (our groups + a few custom conf) with one-ish version each, discovery needs to make 50 requests
// double it just so we don't end up here again for a while. This config is only used for discovery.
config.Burst = 100
discoveryClient, _ := discovery.NewDiscoveryClientForConfig(config)
return memory.NewMemCacheClient(discoveryClient), nil
}
func (c *SimpleRESTClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
discoveryClient, err := c.ToDiscoveryClient()
if err != nil {
return nil, err
}
mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
expander := restmapper.NewShortcutExpander(mapper, discoveryClient)
return expander, nil
}
func (c *SimpleRESTClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// use the standard defaults for this client command
// DEPRECATED: remove and replace with something more accurate
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}
overrides.Context.Namespace = c.Namespace
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)
} If there are any improvement, tell me. |
@liuming-tech Thanks a lot, that helps! I think in general a |
Hi, is there a way to pass custom kube config to helm go client? I want to use
helm 3
client to programmatically install a helm chart in a remote cluster. I want to create a kube config in my code and pass it to the client so that I can install the chart in my remote Kubernetes cluster.I went through the code and seems like I can pass
genericclioptions.RESTClientGetter
toclient.New
helm/pkg/kube/client.go
Line 77 in 5519308
But I am not sure about how I would go about creating my own
genericclioptions.RESTClientGetter
so that it also contains the custom kube config that I want to pass.Thank you :D
The text was updated successfully, but these errors were encountered: