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

New Helm Definition #288

Merged
merged 13 commits into from
Mar 11, 2024
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

### Image Definition Changes

* Added the ability to configure Helm charts under `kubernetes/helmCharts`
* Added the ability to configure Helm charts under `kubernetes/helm`

### Image Configuration Directory Changes

Expand Down
11 changes: 6 additions & 5 deletions pkg/cli/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Run(_ *cli.Context) error {

appendElementalRPMs(ctx)

appendHelmCharts(ctx)
appendHelm(ctx)

if !bootstrapDependencyServices(ctx, args.RootBuildDir) {
os.Exit(1)
Expand Down Expand Up @@ -278,10 +278,11 @@ func appendRPMs(ctx *image.Context, repository image.AddRepo, packages ...string
ctx.ImageDefinition.OperatingSystem.Packages.AdditionalRepos = repositories
}

func appendHelmCharts(ctx *image.Context) {
componentCharts := combustion.ComponentHelmCharts(ctx)
func appendHelm(ctx *image.Context) {
componentCharts, componentRepos := combustion.ComponentHelmCharts(ctx)

ctx.ImageDefinition.Kubernetes.HelmCharts = append(ctx.ImageDefinition.Kubernetes.HelmCharts, componentCharts...)
ctx.ImageDefinition.Kubernetes.Helm.Charts = append(ctx.ImageDefinition.Kubernetes.Helm.Charts, componentCharts...)
ctx.ImageDefinition.Kubernetes.Helm.Repositories = append(ctx.ImageDefinition.Kubernetes.Helm.Repositories, componentRepos...)
}

// If the image definition requires it, starts the necessary services, displaying appropriate messages
Expand All @@ -306,7 +307,7 @@ func bootstrapDependencyServices(ctx *image.Context, rootDir string) bool {
}

if combustion.IsEmbeddedArtifactRegistryConfigured(ctx) {
ctx.Helm = helm.New(ctx.BuildDir)
ctx.HelmClient = helm.New(ctx.BuildDir)
}

if ctx.ImageDefinition.Kubernetes.Version != "" {
Expand Down
23 changes: 16 additions & 7 deletions pkg/combustion/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package combustion

import "github.com/suse-edge/edge-image-builder/pkg/image"

func ComponentHelmCharts(ctx *image.Context) []image.HelmChart {
func ComponentHelmCharts(ctx *image.Context) ([]image.HelmChart, []image.HelmRepository) {
if ctx.ImageDefinition.Kubernetes.Version == "" {
return nil
return nil, nil
}

const (
suseEdgeRepository = "https://suse-edge.github.io/charts"
installationNamespace = "kube-system"
suseEdgeRepositoryName = "suse-edge"
suseEdgeRepositoryURL = "https://suse-edge.github.io/charts"
installationNamespace = "kube-system"
)

var charts []image.HelmChart
var repos []image.HelmRepository

if ctx.ImageDefinition.Kubernetes.Network.APIVIP != "" {
metalLBChart := image.HelmChart{
Name: "metallb",
Repo: suseEdgeRepository,
RepositoryName: suseEdgeRepositoryName,
TargetNamespace: "metallb-system",
CreateNamespace: true,
InstallationNamespace: installationNamespace,
Expand All @@ -26,15 +28,22 @@ func ComponentHelmCharts(ctx *image.Context) []image.HelmChart {

endpointCopierOperatorChart := image.HelmChart{
Name: "endpoint-copier-operator",
Repo: suseEdgeRepository,
RepositoryName: suseEdgeRepositoryName,
TargetNamespace: "endpoint-copier-operator",
CreateNamespace: true,
InstallationNamespace: installationNamespace,
Version: "0.2.0",
}

charts = append(charts, metalLBChart, endpointCopierOperatorChart)

suseEdgeRepo := image.HelmRepository{
Name: suseEdgeRepositoryName,
URL: suseEdgeRepositoryURL,
}

repos = append(repos, suseEdgeRepo)
}

return charts
return charts, repos
}
6 changes: 3 additions & 3 deletions pkg/combustion/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func createRegistryCommand(ctx *image.Context, commandName string, args []string
func IsEmbeddedArtifactRegistryConfigured(ctx *image.Context) bool {
return len(ctx.ImageDefinition.EmbeddedArtifactRegistry.ContainerImages) != 0 ||
len(ctx.ImageDefinition.Kubernetes.Manifests.URLs) != 0 ||
len(ctx.ImageDefinition.Kubernetes.HelmCharts) != 0 ||
len(ctx.ImageDefinition.Kubernetes.Helm.Charts) != 0 ||
isComponentConfigured(ctx, filepath.Join(K8sDir, k8sManifestsDir))
}

Expand Down Expand Up @@ -320,7 +320,7 @@ func parseManifests(ctx *image.Context) ([]string, error) {
}

func parseHelmCharts(ctx *image.Context) ([]*registry.HelmChart, error) {
if len(ctx.ImageDefinition.Kubernetes.HelmCharts) == 0 {
if len(ctx.ImageDefinition.Kubernetes.Helm.Charts) == 0 {
return nil, nil
}

Expand All @@ -335,7 +335,7 @@ func parseHelmCharts(ctx *image.Context) ([]*registry.HelmChart, error) {

helmValuesDir := filepath.Join(ctx.ImageConfigDir, K8sDir, HelmDir, ValuesDir)

return registry.HelmCharts(ctx.ImageDefinition.Kubernetes.HelmCharts, helmValuesDir, buildDir, ctx.ImageDefinition.Kubernetes.Version, ctx.Helm)
return registry.HelmCharts(&ctx.ImageDefinition.Kubernetes.Helm, helmValuesDir, buildDir, ctx.ImageDefinition.Kubernetes.Version, ctx.HelmClient)
}

func storeHelmCharts(ctx *image.Context, helmCharts []*registry.HelmChart) error {
Expand Down
26 changes: 15 additions & 11 deletions pkg/combustion/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ func TestIsEmbeddedArtifactRegistryConfigured(t *testing.T) {
"https://k8s.io/examples/application/nginx-app.yaml",
},
},
HelmCharts: []image.HelmChart{
{
Name: "apache",
Repo: "oci://registry-1.docker.io/bitnamicharts/apache",
Version: "10.7.0",
Helm: image.Helm{
Charts: []image.HelmChart{
{
Name: "apache",
RepositoryName: "apache-repo",
Version: "10.7.0",
},
},
},
},
Expand Down Expand Up @@ -194,11 +196,13 @@ func TestIsEmbeddedArtifactRegistryConfigured(t *testing.T) {
ctx: &image.Context{
ImageDefinition: &image.Definition{
Kubernetes: image.Kubernetes{
HelmCharts: []image.HelmChart{
{
Name: "apache",
Repo: "oci://registry-1.docker.io/bitnamicharts/apache",
Version: "10.7.0",
Helm: image.Helm{
Charts: []image.HelmChart{
{
Name: "apache",
RepositoryName: "apache-repo",
Version: "10.7.0",
},
},
},
},
Expand Down Expand Up @@ -312,7 +316,7 @@ func TestStoreHelmCharts(t *testing.T) {

helmChart := &image.HelmChart{
Name: "apache",
Repo: "oci://registry-1.docker.io/bitnamicharts/apache",
RepositoryName: "apache-repo",
TargetNamespace: "web",
CreateNamespace: true,
InstallationNamespace: "kube-system",
Expand Down
29 changes: 13 additions & 16 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/suse-edge/edge-image-builder/pkg/fileio"
"github.com/suse-edge/edge-image-builder/pkg/image"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
Expand All @@ -31,21 +32,17 @@ func New(outputDir string) *Helm {
}
}

func tempRepo(chart string) string {
return fmt.Sprintf("repo-%s", chart)
}

func repositoryName(repoURL, chart string) string {
func repositoryName(repoName, repoURL, chart string) string {
if strings.HasPrefix(repoURL, "http") {
return fmt.Sprintf("%s/%s", tempRepo(chart), chart)
return fmt.Sprintf("%s/%s", repoName, chart)
}

return repoURL
}

func (h *Helm) AddRepo(chart, repository string) error {
if !strings.HasPrefix(repository, "http") {
zap.S().Infof("Skipping 'helm repo add' for non-http(s) repository: %s", repository)
func (h *Helm) AddRepo(repo *image.HelmRepository) error {
if !strings.HasPrefix(repo.URL, "http") {
zap.S().Infof("Skipping 'helm repo add' for non-http(s) repository: %s", repo.Name)
return nil
}

Expand All @@ -61,7 +58,7 @@ func (h *Helm) AddRepo(chart, repository string) error {
}
}()

cmd := addRepoCommand(chart, repository, file)
cmd := addRepoCommand(repo, file)

if _, err = fmt.Fprintf(file, "command: %s\n", cmd); err != nil {
return fmt.Errorf("writing command prefix to log file: %w", err)
Expand All @@ -70,9 +67,9 @@ func (h *Helm) AddRepo(chart, repository string) error {
return cmd.Run()
}

func addRepoCommand(chart, repository string, output io.Writer) *exec.Cmd {
func addRepoCommand(repo *image.HelmRepository, output io.Writer) *exec.Cmd {
var args []string
args = append(args, "repo", "add", tempRepo(chart), repository)
args = append(args, "repo", "add", repo.Name, repo.URL)

cmd := exec.Command("helm", args...)
cmd.Stdout = output
Expand All @@ -81,7 +78,7 @@ func addRepoCommand(chart, repository string, output io.Writer) *exec.Cmd {
return cmd
}

func (h *Helm) Pull(chart, repository, version, destDir string) (string, error) {
func (h *Helm) Pull(chart string, repo *image.HelmRepository, version, destDir string) (string, error) {
logFile := filepath.Join(h.outputDir, pullLogFileName)

file, err := os.OpenFile(logFile, outputFileFlags, fileio.NonExecutablePerms)
Expand All @@ -94,7 +91,7 @@ func (h *Helm) Pull(chart, repository, version, destDir string) (string, error)
}
}()

cmd := pullCommand(chart, repository, version, destDir, file)
cmd := pullCommand(chart, repo, version, destDir, file)

if _, err = fmt.Fprintf(file, "command: %s\n", cmd); err != nil {
return "", fmt.Errorf("writing command prefix to log file: %w", err)
Expand All @@ -117,8 +114,8 @@ func (h *Helm) Pull(chart, repository, version, destDir string) (string, error)
return chartPath, nil
}

func pullCommand(chart, repository, version, destDir string, output io.Writer) *exec.Cmd {
repository = repositoryName(repository, chart)
func pullCommand(chart string, repo *image.HelmRepository, version, destDir string, output io.Writer) *exec.Cmd {
repository := repositoryName(repo.Name, repo.URL, chart)

var args []string
args = append(args, "pull", repository)
Expand Down
Loading
Loading