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

How to initialize NewClientFromKubeConf on Google Cloud's GKE #89

Closed
masus04 opened this issue May 19, 2022 · 5 comments
Closed

How to initialize NewClientFromKubeConf on Google Cloud's GKE #89

masus04 opened this issue May 19, 2022 · 5 comments

Comments

@masus04
Copy link

masus04 commented May 19, 2022

I am trying to create a Client using the NewClientFromKubeConf factory but keep getting errors.

If I try to simply pass an empty KubeConfClientOptions I get the following error:

panic: kubeconfig missing

Even though my kubeconfig is located under ~/.kube/config and $KUBECONFIG is set accordingly.

So instead I tried passing the KubeConfig parameter to KubeConfClientOptions:

kubeConfig, err := os.ReadFile(kubeConfigPath)
checkErr(err)

clientOptions := &helmclient.KubeConfClientOptions{
    KubeConfig: kubeConfig,
}

kubeConfig properly contains the kubeconfig file as []byte, but I receive the following error anyway:

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x243b3d6]

With a StackTrace that points to this line:

client, err := helmclient.NewClientFromKubeConf(clientOptions)

Am I misunderstanding something?

@zvonkok
Copy link
Contributor

zvonkok commented May 19, 2022

You haven't created a helmclient.Options object. Since it is a pointer a dereference to read its values will fail. Start with a full example and remove parts that you do not need. https://pkg.go.dev/github.com/mittwald/go-helm-client@v0.9.0#example-NewClientFromKubeConf

This is how I use it:

        // Either provide path or read from default location
	if path == "" {
		homeDir, err := os.UserHomeDir()
		if err != nil {
			return errors.Wrap(err, "GetClientWithKubeConf: cannot read user home dir")
		}
		path = homeDir + "/.kube/config"
	}

	kubeConfig, err := os.ReadFile(path)
	if err != nil {
		return errors.Wrapf(err, "GetClientWithKubeConf cannot read kubeConfig from path %s:%v", path, err)
	}

	opt := &helmclient.KubeConfClientOptions{
		Options: &helmclient.Options{
			Namespace:        h.ChartSpec.Namespace, // Change this to the namespace you wish to install the chart in.
			RepositoryCache:  "/tmp/.helmcache",
			RepositoryConfig: "/tmp/.helmrepo",
			Debug:            true,
			Linting:          false, // Change this to false if you don't want linting.
			DebugLog:         klog.Infof,
		},
		KubeContext: kubeContext,
		KubeConfig:  kubeConfig,
	}

	h.HelmClient, err = helmclient.NewClientFromKubeConf(opt)
	if err != nil {
		return errors.Wrap(err, "GetClientWithKubeConf cannot create client from kubeConfig")
	}
	return nil

@masus04
Copy link
Author

masus04 commented May 19, 2022

@zvonkok That did the trick, thank you 👍

Now I'm running into the next error that I don't know how to solve:

panic: Kubernetes cluster unreachable: no Auth Provider found for name "gcp"

Is there any chance you have a solution for that as well?

@masus04 masus04 changed the title How to initialize NewClientFromKubeConf()? How to initialize NewClientFromKubeConf on Google Cloud's GKE May 19, 2022
@zvonkok
Copy link
Contributor

zvonkok commented May 19, 2022

Add this to your imports:

_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // register GCP auth provider

@masus04
Copy link
Author

masus04 commented May 19, 2022

Great, thank you very much!

@masus04 masus04 closed this as completed May 19, 2022
@masus04
Copy link
Author

masus04 commented May 19, 2022

I think error handling messages could be slightly improved: Similar to how a readable error is thrown when KubeConfig is not passed, the same could be donw for Options. Or simply use a reasonable default instead of the current error that is hard to interpret.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants