Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helm mode: Make Helm chart repository configurable #1560

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ const (
HelmChartVersionSecretKeyName = "io.cilium.chart-version"

CiliumNoScheduleLabel = "cilium.io/no-schedule"

// HelmRepository specifies Helm repository to download Cilium charts from.
HelmRepository = "https://helm.cilium.io"
)

var (
Expand Down
5 changes: 4 additions & 1 deletion install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ type Parameters struct {
// DryRunHelmValues writes non-default Helm values to stdout without performing the actual installation.
// For Helm installation mode only.
DryRunHelmValues bool

// HelmRepository specifies the Helm repository to download Cilium Helm charts from.
HelmRepository string
}

type rollbackStep func(context.Context)
Expand Down Expand Up @@ -453,7 +456,7 @@ func NewK8sInstaller(client k8sInstallerImplementation, p Parameters) (*K8sInsta
}

cm := certs.NewCertManager(client, certs.Parameters{Namespace: p.Namespace})
chartVersion, helmChart, err := helm.ResolveHelmChartVersion(p.Version, p.HelmChartDirectory)
chartVersion, helmChart, err := helm.ResolveHelmChartVersion(p.Version, p.HelmChartDirectory, p.HelmRepository)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ cilium install --context kind-cluster1 --helm-set cluster.id=1 --helm-set cluste
addCommonHelmFlags(cmd, &params)
cmd.Flags().BoolVar(&params.DryRun, "dry-run", false, "Write resources to be installed to stdout without actually installing them")
cmd.Flags().BoolVar(&params.DryRunHelmValues, "dry-run-helm-values", false, "Write non-default Helm values to stdout without performing the actual installation")
cmd.Flags().StringVar(&params.HelmRepository, "repository", defaults.HelmRepository, "Helm chart repository to download Cilium charts from")
return cmd
}

Expand Down Expand Up @@ -374,5 +375,6 @@ cilium upgrade --helm-set cluster.id=1 --helm-set cluster.name=cluster1
"Write resources to be installed to stdout without actually installing them")
cmd.Flags().BoolVar(&params.DryRunHelmValues, "dry-run-helm-values", false,
"Write non-default Helm values to stdout; without performing the actual upgrade")
cmd.Flags().StringVar(&params.HelmRepository, "repository", defaults.HelmRepository, "Helm chart repository to download Cilium charts from")
return cmd
}
38 changes: 22 additions & 16 deletions internal/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

const ciliumChart = "https://helm.cilium.io"

var settings = cli.New()

// State contains Helm state for the current Cilium installation. Cilium CLI retrieves this
Expand Down Expand Up @@ -217,7 +215,7 @@ func newChartFromDirectory(directory string) (*chart.Chart, error) {

// newChartFromRemoteWithCache fetches the chart from remote repository, the chart file
// is then stored in the local cache directory for future usage.
func newChartFromRemoteWithCache(ciliumVersion semver2.Version) (*chart.Chart, error) {
func newChartFromRemoteWithCache(ciliumVersion semver2.Version, repository string) (*chart.Chart, error) {
cacheDir, err := ciliumCacheDir()
if err != nil {
return nil, err
Expand All @@ -232,7 +230,7 @@ func newChartFromRemoteWithCache(ciliumVersion semver2.Version) (*chart.Chart, e
// Download the chart from remote repository
pull := action.NewPullWithOpts(action.WithConfig(new(action.Configuration)))
pull.Settings = settings
pull.RepoURL = ciliumChart
pull.RepoURL = repository
pull.Version = ciliumVersion.String()
pull.DestDir = cacheDir

Expand Down Expand Up @@ -289,7 +287,8 @@ func GenManifests(
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
helmChart, err = newChartFromRemoteWithCache(ciliumVer)
// Helm repository is not configurable in the classic mode. Always use the default Helm repository.
helmChart, err = newChartFromRemoteWithCache(ciliumVer, defaults.HelmRepository)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -420,11 +419,15 @@ func ListVersions() ([]string, error) {
return versions, nil
}

// ResolveHelmChartVersion resolves Helm chart version based on --version and --chart-directory flags.
func ResolveHelmChartVersion(versionFlag, chartDirectoryFlag string) (semver2.Version, *chart.Chart, error) {
// ResolveHelmChartVersion resolves Helm chart version based on --version, --chart-directory, and --repository flags.
func ResolveHelmChartVersion(versionFlag, chartDirectoryFlag, repository string) (semver2.Version, *chart.Chart, error) {
// If repository is empty, set it to the default Helm repository ("https://helm.cilium.io") for backward compatibility.
if repository == "" {
repository = defaults.HelmRepository
}
if chartDirectoryFlag == "" {
// If --chart-directory flag is not specified, use the version specified with --version flag.
return resolveChartVersion(versionFlag)
return resolveChartVersion(versionFlag, repository)
}

// Get the chart version from the local Helm chart specified with --chart-directory flag.
Expand All @@ -435,22 +438,25 @@ func ResolveHelmChartVersion(versionFlag, chartDirectoryFlag string) (semver2.Ve
return versioncheck.MustVersion(localChart.Metadata.Version), localChart, nil
}

func resolveChartVersion(versionFlag string) (semver2.Version, *chart.Chart, error) {
func resolveChartVersion(versionFlag string, repository string) (semver2.Version, *chart.Chart, error) {
version, err := utils.ParseCiliumVersion(versionFlag)
if err != nil {
return semver2.Version{}, nil, err
}

helmChart, err := newChartFromEmbeddedFile(version)
if err == nil {
return version, helmChart, nil
}
// If the repository is the default repository ("https://helm.cilium.io"), check embedded charts first.
if repository == defaults.HelmRepository {
helmChart, err := newChartFromEmbeddedFile(version)
if err == nil {
return version, helmChart, nil
}

if !errors.Is(err, fs.ErrNotExist) {
return semver2.Version{}, nil, err
if !errors.Is(err, fs.ErrNotExist) {
return semver2.Version{}, nil, err
}
}

helmChart, err = newChartFromRemoteWithCache(version)
helmChart, err := newChartFromRemoteWithCache(version, repository)
if err != nil {
return semver2.Version{}, nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion internal/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"testing"

"github.com/cilium/cilium-cli/defaults"

"github.com/blang/semver/v4"
)

Expand Down Expand Up @@ -51,7 +53,7 @@ func TestResolveHelmChartVersion(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _, err := ResolveHelmChartVersion(tt.args.versionFlag, tt.args.chartDirectoryFlag)
got, _, err := ResolveHelmChartVersion(tt.args.versionFlag, tt.args.chartDirectoryFlag, defaults.HelmRepository)
if (err != nil) != tt.wantErr {
t.Errorf("ResolveHelmChartVersion() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down