Skip to content

Commit

Permalink
helm: Add support for remote chart version
Browse files Browse the repository at this point in the history
Currently, the cilium charts are embedded inside cilium cli binary,
which will require a new cilium/charts bump in go.mod and then a new
release for cilium-cli. It's making sense to do so if some other code
changes are required to make installation working for new char version,
but most of the time, only go.mod upgrade is required.

This commit is to add the capability to install new chart version without
the need of releasing or installing new cilium-cli.

Signed-off-by: Tam Mach <[email protected]>
  • Loading branch information
sayboras committed Sep 4, 2022
1 parent 4fc8b22 commit 2b9f0ea
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions internal/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ package helm
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"os"
"path"
"regexp"
"sort"
"strings"
Expand All @@ -30,6 +34,8 @@ import (
corev1 "k8s.io/api/core/v1"
)

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 @@ -186,7 +192,7 @@ func newClient(namespace, k8sVersion string) (*action.Install, error) {
func newChartFromCiliumVersion(ciliumVersion semver2.Version) (*chart.Chart, error) {
helmTgz, err := helm.HelmFS.ReadFile(fmt.Sprintf("cilium-%s.tgz", ciliumVersion))
if err != nil {
return nil, fmt.Errorf("cilium version not found: %s", err)
return nil, fmt.Errorf("cilium version not found: %w", err)
}

// Check chart dependencies to make sure all are present in /charts
Expand All @@ -197,6 +203,33 @@ func newChartFromDirectory(directory string) (*chart.Chart, error) {
return loader.LoadDir(directory)
}

func newChartFromRemote(ciliumVersion semver2.Version) (*chart.Chart, error) {
pull := action.NewPullWithOpts(action.WithConfig(new(action.Configuration)))
pull.Settings = settings
pull.RepoURL = ciliumChart
pull.Version = ciliumVersion.String()

tempDir, err := os.MkdirTemp("", "cilium-cli")
if err != nil {
return nil, err
}
pull.DestDir = tempDir

_, err = pull.Run("cilium")
if err != nil {
return nil, err
}
file := path.Join(pull.DestDir, fmt.Sprintf("cilium-%s.tgz", ciliumVersion))
defer func() {
_ = os.Remove(file)
}()
helmTgz, err := os.ReadFile(file)
if err != nil {
return nil, err
}
return loader.LoadArchive(bytes.NewReader(helmTgz))
}

// GenManifests returns the generated manifests in a map that maps the manifest
// name to its contents.
func GenManifests(
Expand All @@ -218,7 +251,13 @@ func GenManifests(
} else {
helmChart, err = newChartFromCiliumVersion(ciliumVer)
if err != nil {
return nil, err
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
helmChart, err = newChartFromRemote(ciliumVer)
if err != nil {
return nil, err
}
}
}

Expand Down Expand Up @@ -333,7 +372,13 @@ func ResolveHelmChartVersion(versionFlag, chartDirectoryFlag string) (semver2.Ve
return semver2.Version{}, err
}
if _, err = newChartFromCiliumVersion(version); err != nil {
return semver2.Version{}, err
if !errors.Is(err, fs.ErrNotExist) {
return semver2.Version{}, err
}
_, err = newChartFromRemote(version)
if err != nil {
return semver2.Version{}, err
}
}
return version, nil
}
Expand Down

0 comments on commit 2b9f0ea

Please sign in to comment.