From d30c0b4e5c66a1d9020f655ea5286f157d4d6d88 Mon Sep 17 00:00:00 2001 From: Luis Rascao Date: Tue, 21 Nov 2023 11:08:34 +0000 Subject: [PATCH] Add support for local Helm repos Signed-off-by: Luis Rascao --- README.md | 9 +++++++ pkg/kubernetes/kubernetes.go | 47 +++++++++++++++++++++++++++--------- utils/utils.go | 16 ++++++++++++ 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index cbe310e2b..916fecff8 100644 --- a/README.md +++ b/README.md @@ -369,6 +369,15 @@ export DAPR_HELM_REPO_PASSWORD="passwd_xxx" Setting the above parameters will allow `dapr init -k` to install Dapr images from the configured Helm repository. +A local Helm repo is also supported: + +export DAPR_HELM_REPO_URL="/home/user/dapr/helm-charts" + +To directly use HEAD helm charts, create two local symlinks under `DAPR_HELM_REPO_URL` that point to the `charts` folder of each repo: + * `dapr-dashboard-latest` -> `https://github.com/dapr/dashboard/tree/master/chart/dapr-dashboard` + * `dapr-latest` -> `https://github.com/dapr/dapr/tree/master/charts/dapr` + + ### Launch Dapr and your app The Dapr CLI lets you debug easily by launching both Dapr and your app. diff --git a/pkg/kubernetes/kubernetes.go b/pkg/kubernetes/kubernetes.go index f6bd78e5a..f9f4c22ca 100644 --- a/pkg/kubernetes/kubernetes.go +++ b/pkg/kubernetes/kubernetes.go @@ -190,12 +190,15 @@ func getVersion(releaseName string, version string) (string, error) { return actualVersion, nil } -func createTempDir() (string, error) { +func createTempDir() (string, func(), error) { dir, err := os.MkdirTemp("", "dapr") if err != nil { - return "", fmt.Errorf("error creating temp dir: %w", err) + return "", func() {}, fmt.Errorf("error creating temp dir: %w", err) } - return dir, nil + cleanup := func() { + os.RemoveAll(dir) + } + return dir, cleanup, nil } func locateChartFile(dirPath string) (string, error) { @@ -206,7 +209,12 @@ func locateChartFile(dirPath string) (string, error) { return filepath.Join(dirPath, files[0].Name()), nil } -func getHelmChart(version, releaseName, helmRepo string, config *helm.Configuration) (*chart.Chart, error) { +func pullHelmChart(version, releaseName, helmRepo string, config *helm.Configuration) (string, func(), error) { + // is helmRepo already a directory path? (ie. /home/user/dapr/helm-charts) + if localPath, err := utils.DiscoverHelmPath(helmRepo, releaseName, version); err == nil { + return localPath, func() {}, nil + } + pull := helm.NewPullWithOpts(helm.WithConfig(config)) pull.RepoURL = helmRepo pull.Username = utils.GetEnv("DAPR_HELM_REPO_USERNAME", "") @@ -218,24 +226,39 @@ func getHelmChart(version, releaseName, helmRepo string, config *helm.Configurat pull.Version = chartVersion(version) } - dir, err := createTempDir() + dir, cleanup, err := createTempDir() if err != nil { - return nil, err + return "", nil, fmt.Errorf("unable to create temp dir: %w", err) } - defer os.RemoveAll(dir) pull.DestDir = dir _, err = pull.Run(releaseName) if err != nil { - return nil, err + return "", cleanup, fmt.Errorf("unable to pull chart from repo: %w", err) } chartPath, err := locateChartFile(dir) if err != nil { - return nil, err + return "", cleanup, fmt.Errorf("unable to locate chart: %w", err) } - return loader.Load(chartPath) + + return chartPath, cleanup, nil +} + +func getHelmChart(version, releaseName, helmRepo string, config *helm.Configuration) (*chart.Chart, error) { + chartPath, cleanup, err := pullHelmChart(version, releaseName, helmRepo, config) + defer cleanup() + if err != nil { + return nil, fmt.Errorf("unable to pull helm chart: %w", err) + } + + chart, err := loader.Load(chartPath) + if err != nil { + return nil, fmt.Errorf("unable to load chart from path: %w", err) + } + + return chart, nil } func daprChartValues(config InitConfiguration, version string) (map[string]interface{}, error) { @@ -461,8 +484,8 @@ spec: zipkin: endpointAddress: "http://dapr-dev-zipkin.default.svc.cluster.local:9411/api/v2/spans" ` - tempDirPath, err := createTempDir() - defer os.RemoveAll(tempDirPath) + tempDirPath, cleanup, err := createTempDir() + defer cleanup() if err != nil { return err } diff --git a/utils/utils.go b/utils/utils.go index 716c37fa7..2e1541cf8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -426,3 +426,19 @@ func AttachJobObjectToProcess(pid string, proc *os.Process) { func GetJobObjectNameFromPID(pid string) string { return pid + "-" + windowsDaprAppProcJobName } + +func DiscoverHelmPath(helmPath, release, version string) (string, error) { + // first try for a local directory path + dirPath := filepath.Join(helmPath, fmt.Sprintf("%s-%s", release, version)) + if ValidatePath(dirPath) == nil { + return dirPath, nil + } + + // not a dir, try a .tgz file instead + filePath := filepath.Join(helmPath, fmt.Sprintf("%s-%s.tgz", release, version)) + if ValidatePath(filePath) == nil { + return filePath, nil + } + + return "", fmt.Errorf("unable to find a helm path in either %s or %s", dirPath, filePath) +}