Skip to content

Commit a591b23

Browse files
committed
correctly bootstrap the helm home path
1 parent b8f947d commit a591b23

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

helm/init.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ import (
44
"fmt"
55
"os"
66

7+
"k8s.io/helm/pkg/getter"
78
"k8s.io/helm/pkg/helm/helmpath"
89
"k8s.io/helm/pkg/repo"
910
)
1011

12+
const (
13+
stableRepository = "stable"
14+
stableRepositoryURL = "https://kubernetes-charts.storage.googleapis.com"
15+
)
16+
1117
// Init makes sure the Helm home path exists and the required subfolders.
1218
func Init() error {
1319
if err := ensureDirectories(settings.Home); err != nil {
1420
return err
1521
}
1622

23+
if err := ensureDefaultRepos(settings.Home); err != nil {
24+
return err
25+
}
26+
1727
if err := ensureRepoFileFormat(settings.Home.RepositoryFile()); err != nil {
1828
return err
1929
}
@@ -27,6 +37,7 @@ func ensureDirectories(home helmpath.Home) error {
2737
home.Repository(),
2838
home.Cache(),
2939
home.Plugins(),
40+
home.Starters(),
3041
}
3142

3243
for _, p := range configDirectories {
@@ -52,3 +63,43 @@ func ensureRepoFileFormat(file string) error {
5263

5364
return nil
5465
}
66+
67+
func ensureDefaultRepos(home helmpath.Home) error {
68+
repoFile := home.RepositoryFile()
69+
if fi, err := os.Stat(repoFile); err != nil {
70+
f := repo.NewRepoFile()
71+
sr, err := initStableRepo(home.CacheIndex(stableRepository))
72+
if err != nil {
73+
return err
74+
}
75+
76+
f.Add(sr)
77+
78+
if err := f.WriteFile(repoFile, 0644); err != nil {
79+
return err
80+
}
81+
} else if fi.IsDir() {
82+
return fmt.Errorf("%s must be a file, not a directory", repoFile)
83+
}
84+
return nil
85+
}
86+
87+
func initStableRepo(cacheFile string) (*repo.Entry, error) {
88+
c := repo.Entry{
89+
Name: stableRepository,
90+
URL: stableRepositoryURL,
91+
Cache: cacheFile,
92+
}
93+
r, err := repo.NewChartRepository(&c, getter.All(settings))
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
// In this case, the cacheFile is always absolute. So passing empty string
99+
// is safe.
100+
if err := r.DownloadIndexFile(""); err != nil {
101+
return nil, fmt.Errorf("Looks like %q is not a valid chart repository or cannot be reached: %s", stableRepositoryURL, err.Error())
102+
}
103+
104+
return &c, nil
105+
}

main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ func main() {
1919
os.Exit(1)
2020
}
2121

22+
if err = helm.Init(); err != nil {
23+
fmt.Fprintf(os.Stderr, "error initialising helm: \n\n%s\n", err)
24+
os.Exit(1)
25+
}
26+
27+
if err = helm.AddRepository("stable", "https://kubernetes-charts.storage.googleapis.com"); err != nil {
28+
fmt.Fprintf(os.Stderr, "error adding repository: \n\n%s\n", err)
29+
os.Exit(1)
30+
}
31+
2232
if cli["--repo"] != nil {
2333
for _, r := range strings.Split(cli["--repo"].(string), ",") {
2434
p := strings.SplitN(r, "=", 2)
@@ -38,16 +48,6 @@ func main() {
3848
os.Exit(1)
3949
}
4050

41-
if err = helm.Init(); err != nil {
42-
fmt.Fprintf(os.Stderr, "error initialising helm: \n\n%s\n", err)
43-
os.Exit(1)
44-
}
45-
46-
if err = helm.AddRepository("stable", "https://kubernetes-charts.storage.googleapis.com"); err != nil {
47-
fmt.Fprintf(os.Stderr, "error adding repository: \n\n%s\n", err)
48-
os.Exit(1)
49-
}
50-
5151
cc, err := chartsconfig.NewChartsConfiguration(cfg)
5252
if err != nil {
5353
fmt.Fprintf(os.Stderr, "charts config parsing error: \n\n%s\n", err)

0 commit comments

Comments
 (0)