Skip to content

Commit

Permalink
Add ability to add helm chart repositories (#203)
Browse files Browse the repository at this point in the history
**What this PR does / why we need it**:

**Which issue(s) this PR fixes**:

Fixes #166

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
-->
```release-note
NONE
```

This PR was merged by Kapetanios.
  • Loading branch information
nghialv committed Jun 25, 2020
1 parent 41e735b commit eaf5ff9
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 25 deletions.
12 changes: 12 additions & 0 deletions pkg/app/piped/chartrepo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["chartrepo.go"],
importpath = "github.com/pipe-cd/pipe/pkg/app/piped/chartrepo",
visibility = ["//visibility:public"],
deps = [
"//pkg/config:go_default_library",
"@org_uber_go_zap//:go_default_library",
],
)
54 changes: 54 additions & 0 deletions pkg/app/piped/chartrepo/chartrepo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package chartrepo manages a list of configured helm repositories.
package chartrepo

import (
"context"
"fmt"
"os/exec"

"go.uber.org/zap"

"github.com/pipe-cd/pipe/pkg/config"
)

type registry interface {
Helm(ctx context.Context, version string) (string, bool, error)
}

// Add installs all specified Helm Chart repositories.
// https://helm.sh/docs/topics/chart_repository/
// helm repo add fantastic-charts https://fantastic-charts.storage.googleapis.com
// helm repo add fantastic-charts https://fantastic-charts.storage.googleapis.com --username my-username --password my-password
func Add(ctx context.Context, repos []config.HelmChartRepository, reg registry, logger *zap.Logger) error {
helm, _, err := reg.Helm(ctx, "")
if err != nil {
return fmt.Errorf("failed to find helm to add repos (%v)", err)
}
for _, repo := range repos {
args := []string{"repo", "add", repo.Address}
if repo.Username != "" || repo.Password != "" {
args = append(args, "--username", repo.Username, "--password", repo.Password)
}
cmd := exec.CommandContext(ctx, helm, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to add chart repository %s: %s (%w)", repo.Name, string(out), err)
}
logger.Info(fmt.Sprintf("successfully added chart repository %s", repo.Name))
}
return nil
}
1 change: 1 addition & 0 deletions pkg/app/piped/cmd/piped/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//pkg/app/piped/apistore/applicationstore:go_default_library",
"//pkg/app/piped/apistore/commandstore:go_default_library",
"//pkg/app/piped/apistore/deploymentstore:go_default_library",
"//pkg/app/piped/chartrepo:go_default_library",
"//pkg/app/piped/controller:go_default_library",
"//pkg/app/piped/driftdetector:go_default_library",
"//pkg/app/piped/executor/registry:go_default_library",
Expand Down
9 changes: 9 additions & 0 deletions pkg/app/piped/cmd/piped/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/pipe-cd/pipe/pkg/app/piped/apistore/applicationstore"
"github.com/pipe-cd/pipe/pkg/app/piped/apistore/commandstore"
"github.com/pipe-cd/pipe/pkg/app/piped/apistore/deploymentstore"
"github.com/pipe-cd/pipe/pkg/app/piped/chartrepo"
"github.com/pipe-cd/pipe/pkg/app/piped/controller"
"github.com/pipe-cd/pipe/pkg/app/piped/driftdetector"
"github.com/pipe-cd/pipe/pkg/app/piped/livestatereporter"
Expand Down Expand Up @@ -122,6 +123,14 @@ func (p *piped) run(ctx context.Context, t cli.Telemetry) error {
return err
}

// Add configured Helm chart repositories.
if len(cfg.ChartRepositories) > 0 {
if err := chartrepo.Add(ctx, cfg.ChartRepositories, toolregistry.DefaultRegistry(), t.Logger); err != nil {
t.Logger.Error("failed to add configured chart repositories", zap.Error(err))
return err
}
}

// Make gRPC client and connect to the API.
apiClient, err := p.createAPIClient(ctx, cfg.ProjectID, cfg.PipedID, cfg.PipedKeyFile, t.Logger)
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions pkg/app/piped/helmrepo/BUILD.bazel

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/app/piped/helmrepo/helmrepo.go

This file was deleted.

11 changes: 10 additions & 1 deletion pkg/config/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ type PipedSpec struct {
// Git configuration needed for git commands.
Git PipedGit `json:"git"`
// List of git repositories this piped will handle.
Repositories []PipedRepository `json:"repositories"`
Repositories []PipedRepository `json:"repositories"`
// List of helm chart repositories that should be added while starting up.
ChartRepositories []HelmChartRepository `json:"chartRepositories"`
CloudProviders []PipedCloudProvider `json:"cloudProviders"`
AnalysisProviders []PipedAnalysisProvider `json:"analysisProviders"`
}
Expand Down Expand Up @@ -149,6 +151,13 @@ type PipedRepository struct {
Branch string `json:"branch"`
}

type HelmChartRepository struct {
Name string `json:"name"`
Address string `json:"address"`
Username string `json:"username"`
Password string `json:"password"`
}

type PipedCloudProvider struct {
Name string
Type model.CloudProviderType
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/piped_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ func TestPipedConfig(t *testing.T) {
Branch: "master",
},
},
ChartRepositories: []HelmChartRepository{
{
Name: "fantastic-charts",
Address: "https://fantastic-charts.storage.googleapis.com",
},
{
Name: "private-charts",
Address: "https://private-charts.com",
Username: "basic-username",
Password: "basic-password",
},
},
CloudProviders: []PipedCloudProvider{
{
Name: "kubernetes-default",
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/testdata/piped/piped-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ spec:
remote: git@github.com:org/repo2.git
branch: master

chartRepositories:
- name: fantastic-charts
address: https://fantastic-charts.storage.googleapis.com
- name: private-charts
address: https://private-charts.com
username: basic-username
password: basic-password

cloudProviders:
- name: kubernetes-default
type: KUBERNETES
Expand Down

0 comments on commit eaf5ff9

Please sign in to comment.