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

Running TemplateChart breaks GetRelease #207

Open
ConnorsApps opened this issue May 15, 2024 · 0 comments
Open

Running TemplateChart breaks GetRelease #207

ConnorsApps opened this issue May 15, 2024 · 0 comments

Comments

@ConnorsApps
Copy link

ConnorsApps commented May 15, 2024

The Issue

Running Client.TemplateChart breaks Client.GetRelease.

  1. GetRelease("my-release") Works ✅
  2. TemplateChart(....) Works ✅
  3. GetRelease("my-release") Fails 🛑 release: not found

Cause of problem

The issue is from this line in the upstream helm repo. helm/pkg/action/install.go#L269

mem := driver.NewMemory()
mem.SetNamespace(i.Namespace)
i.cfg.Releases = storage.Init(mem)

Possible Workaround

mittwald/go-helm-client/client.go#L466

import helmStorage "helm.sh/helm/v3/pkg/storage"

func (c *HelmClient) TemplateChart(....) ([]byte, error) {
// Copy as is
ogReleases := c.ActionConfig.Releases
// Set temporary empty new storage
c.ActionConfig.Releases = helmStorage.Init(ogReleases.Driver)
defer func() {
	// Set back to value it was to start with
	c.ActionConfig.Releases = ogReleases
}()

Test to reproduce issue

package helmclient

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"helm.sh/helm/v3/pkg/chartutil"
	"helm.sh/helm/v3/pkg/repo"
	"k8s.io/client-go/discovery"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
)

func must(err error) {
	if err != nil {
		panic(err)
	}
}

func mustGetClusterKubeVersion(restConfig *rest.Config) *chartutil.KubeVersion {
	discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig)
	must(err)
	information, err := discoveryClient.ServerVersion()
	must(err)
	kubeVersion, err := chartutil.ParseKubeVersion(information.GitVersion)
	must(err)
	return kubeVersion
}

func mustMakeHelmClient(kubeConfigPath, kubeContextName, namespace string) (*rest.Config, Client) {
	restConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
		&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfigPath},
		&clientcmd.ConfigOverrides{CurrentContext: kubeContextName},
	).ClientConfig()
	must(err)
	client, err := NewClientFromRestConf(&RestConfClientOptions{
		RestConfig: restConfig,
		Options: &Options{
			Namespace: namespace,
		},
	})
	must(err)
	return restConfig, client
}

func TestTemplateThenGetRelease(_ *testing.T) {
	var (
		kubeContextName = "some-context-name"
		myReleaseName   = "my-release"
		namespace       = "my-namespace"
		kubeConfigPath  = filepath.Join(os.Getenv("HOME"), ".kube", "config")
	)

	restConfig, client := mustMakeHelmClient(kubeConfigPath, kubeContextName, namespace)

	release, err := client.GetRelease(myReleaseName)
	must(err)
	fmt.Println("Successfully got release: check 1", release.Name)

	err = client.AddOrUpdateChartRepo(repo.Entry{
		Name: "nginx-stable",
		URL:  "https://helm.nginx.com/stable",
	})
	must(err)

	_, err = client.TemplateChart(&ChartSpec{
		Namespace:   namespace,
		ChartName:   "nginx-stable/nginx-ingress",
		ReleaseName: "nginx-ingress",
	}, &HelmTemplateOptions{KubeVersion: mustGetClusterKubeVersion(restConfig)})
	must(err)

	fmt.Println("Templated chart")

	release, err = client.GetRelease(myReleaseName) // Failing here  🛑 
	must(err)

	fmt.Println("Successfully got release: check 2", release.Name)
}

Before I make a pull request, anyone have any thoughts?

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

1 participant