diff --git a/cmd/tk/toolCharts.go b/cmd/tk/toolCharts.go index 217234c86..f8fc7d081 100644 --- a/cmd/tk/toolCharts.go +++ b/cmd/tk/toolCharts.go @@ -20,6 +20,7 @@ func chartsCmd() *cli.Command { cmd.AddCommand( chartsInitCmd(), chartsAddCmd(), + chartsAddRepoCmd(), chartsVendorCmd(), chartsConfigCmd(), ) @@ -63,6 +64,28 @@ func chartsAddCmd() *cli.Command { return cmd } +func chartsAddRepoCmd() *cli.Command { + cmd := &cli.Command{ + Use: "add-repo [NAME] [URL]", + Short: "Adds a repository to the chartfile", + Args: cli.ArgsExact(2), + } + + cmd.Run = func(cmd *cli.Command, args []string) error { + c, err := loadChartfile() + if err != nil { + return err + } + + return c.AddRepos(helm.Repo{ + Name: args[0], + URL: args[1], + }) + } + + return cmd +} + func chartsConfigCmd() *cli.Command { cmd := &cli.Command{ Use: "config", diff --git a/docs/docs/helm.mdx b/docs/docs/helm.mdx index 2f6153904..916b86875 100644 --- a/docs/docs/helm.mdx +++ b/docs/docs/helm.mdx @@ -18,7 +18,8 @@ resource definition, you can still consume Helm resources, as described below. Helm support is provided using the [`github.com/grafana/jsonnet-libs/tanka-util`](https://github.com/grafana/jsonnet-libs/tree/master/tanka-util) -library. Install it with: +library. Install it with: + ```bash jb install github.com/grafana/jsonnet-libs/tanka-util ``` @@ -136,8 +137,14 @@ This will also call `tk tool charts vendor`, so that the `charts/` directory is **Adding Repositories:** By default, the `stable` repository is automatically set up for you. If you wish -to add another repository, you currently need modify `chartfile.yaml` directly -to add it: +to add another repository, you can use the `add-repo` command: + +```bash +# Add the official Grafana repository +$ tk tool charts add-repo grafana https://grafana.github.io/helm-charts +``` + +Another way is to modify `chartfile.yaml` directly: ```diff version: 1 diff --git a/pkg/helm/charts.go b/pkg/helm/charts.go index 341e467f7..c9d340e0a 100644 --- a/pkg/helm/charts.go +++ b/pkg/helm/charts.go @@ -142,7 +142,7 @@ func (c Charts) Vendor() error { // Add adds every Chart in reqs to the Manifest after validation, and runs // Vendor afterwards -func (c Charts) Add(reqs []string) error { +func (c *Charts) Add(reqs []string) error { log.Printf("Adding %v Charts ...", len(reqs)) skip := func(s string, err error) { @@ -183,6 +183,29 @@ func (c Charts) Add(reqs []string) error { return c.Vendor() } +func (c *Charts) AddRepos(repos ...Repo) error { + added := 0 + for _, r := range repos { + if c.Manifest.Repositories.Has(r) { + log.Printf("Skipping %s: Already exists", r.Name) + continue + } + + c.Manifest.Repositories = append(c.Manifest.Repositories, r) + } + + // write out + if err := write(c.Manifest, c.ManifestFile()); err != nil { + return err + } + + if added != len(repos) { + return fmt.Errorf("%v Chart(s) were skipped. Please check above logs for details", len(repos)-added) + } + + return nil +} + func InitChartfile(path string) (*Charts, error) { c := Chartfile{ Version: Version, diff --git a/pkg/helm/spec.go b/pkg/helm/spec.go index 23b799234..ab1feabc5 100644 --- a/pkg/helm/spec.go +++ b/pkg/helm/spec.go @@ -23,7 +23,7 @@ type Chartfile struct { Version uint `json:"version"` // Repositories to source from - Repositories []Repo `json:"repositories"` + Repositories Repos `json:"repositories"` // Requires lists Charts expected to be present in the charts folder Requires Requirements `json:"requires"` @@ -43,6 +43,19 @@ type Repo struct { Password string `json:"password,omitempty"` } +type Repos []Repo + +// Has reports whether 'repo' is already part of the repositories +func (r Repos) Has(repo Repo) bool { + for _, x := range r { + if x == repo { + return true + } + } + + return false +} + // Requirement describes a single required Helm Chart. // Both, Chart and Version are required type Requirement struct {