Skip to content

Commit

Permalink
Add support for local Helm repos
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Rascao <luis.rascao@gmail.com>
  • Loading branch information
lrascao committed Nov 21, 2023
1 parent 4fe6978 commit d30c0b4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
47 changes: 35 additions & 12 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)

Check failure on line 213 in pkg/kubernetes/kubernetes.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

Comment should end in a period (godot)
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", "")
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 431 in utils/utils.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

Comment should end in a period (godot)
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

Check failure on line 437 in utils/utils.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

Comment should end in a period (godot)
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)
}

0 comments on commit d30c0b4

Please sign in to comment.