Skip to content

Commit

Permalink
Fix cli to work on windows (#1139)
Browse files Browse the repository at this point in the history
* Fix cli to work on windows

Previously would error about missing file `consul\Chart.yaml`. This
is because the go embedded filesystem must be accessed using `/`
delimiters but we were using `filepath` functions which on windows
use `\`.
  • Loading branch information
lkysow committed Apr 4, 2022
1 parent b79e029 commit 7341255
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ BUG FIXES:
* Helm
* Don't set TTL for server certificates when using Vault as the secrets backend. [[GH-1104](https://github.com/hashicorp/consul-k8s/pull/1104)]
* Fix PodSecurityPolicies for clients/mesh gateways when hostNetwork is used. [[GH-1090](https://github.com/hashicorp/consul-k8s/pull/1090)]
* CLI
* Fix `install` and `upgrade` commands for Windows. [[GH-1139](https://github.com/hashicorp/consul-k8s/pull/1139)]

## 0.41.1 (February 24, 2022)

Expand Down
21 changes: 12 additions & 9 deletions cli/helm/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package helm

import (
"embed"
"path/filepath"
"path"
"strings"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -34,17 +35,23 @@ func LoadChart(chart embed.FS, chartDirName string) (*chart.Chart, error) {
func ReadChartFiles(chart embed.FS, chartDirName string) ([]*loader.BufferedFile, error) {
var chartFiles []*loader.BufferedFile

// NOTE: Because we're using the embedded filesystem, we must use path.* functions,
// *not* filepath.* functions. This is because the embedded filesystem always uses
// linux-style separators, even if this code is running on Windows. If we use
// filepath.* functions, then Go on Windows will try to use `\` delimiters to access
// the embedded filesystem, which will then fail.

// Load Chart.yaml and values.yaml first.
for _, f := range []string{chartFileName, valuesFileName} {
file, err := readFile(chart, filepath.Join(chartDirName, f), chartDirName)
file, err := readFile(chart, path.Join(chartDirName, f), chartDirName)
if err != nil {
return nil, err
}
chartFiles = append(chartFiles, file)
}

// Now load everything under templates/.
dirs, err := chart.ReadDir(filepath.Join(chartDirName, templatesDirName))
dirs, err := chart.ReadDir(path.Join(chartDirName, templatesDirName))
if err != nil {
return nil, err
}
Expand All @@ -54,7 +61,7 @@ func ReadChartFiles(chart embed.FS, chartDirName string) ([]*loader.BufferedFile
continue
}

file, err := readFile(chart, filepath.Join(chartDirName, templatesDirName, f.Name()), chartDirName)
file, err := readFile(chart, path.Join(chartDirName, templatesDirName, f.Name()), chartDirName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,11 +93,7 @@ func readFile(chart embed.FS, f string, pathPrefix string) (*loader.BufferedFile
if err != nil {
return nil, err
}
// Remove the path prefix.
rel, err := filepath.Rel(pathPrefix, f)
if err != nil {
return nil, err
}
rel := strings.TrimPrefix(f, pathPrefix+"/")
return &loader.BufferedFile{
Name: rel,
Data: bytes,
Expand Down

0 comments on commit 7341255

Please sign in to comment.