Pull charts

This commit is contained in:
Nikolai Rodionov 2024-07-26 09:04:03 +02:00
parent 5b6a503fd1
commit 7e0050b02c
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
4 changed files with 34 additions and 21 deletions

View File

@ -15,6 +15,7 @@ import (
"git.badhouseplants.net/allanger/shoebill/pkg/config" "git.badhouseplants.net/allanger/shoebill/pkg/config"
"git.badhouseplants.net/allanger/shoebill/pkg/lockfile" "git.badhouseplants.net/allanger/shoebill/pkg/lockfile"
"git.badhouseplants.net/allanger/shoebill/pkg/release" "git.badhouseplants.net/allanger/shoebill/pkg/release"
"github.com/go-logr/logr"
) )
func ReadTheConfig(path string) (*config.Config, error) { func ReadTheConfig(path string) (*config.Config, error) {
@ -36,6 +37,11 @@ type SyncOptions struct {
type SyncController struct{} type SyncController struct{}
func Sync(ctx context.Context, opts *SyncOptions) error { func Sync(ctx context.Context, opts *SyncOptions) error {
log, err := logr.FromContext(ctx)
if err != nil {
return err
}
// Start by creating a directory where everything should be happening // Start by creating a directory where everything should be happening
configPath := filepath.Dir(opts.Config.ConfigPath) configPath := filepath.Dir(opts.Config.ConfigPath)
// Prepare helm repositories // Prepare helm repositories
@ -109,7 +115,12 @@ func Sync(ctx context.Context, opts *SyncOptions) error {
releaseObj := release.FindReleaseByNames(cluster.Releases, opts.Config.Releases) releaseObj := release.FindReleaseByNames(cluster.Releases, opts.Config.Releases)
cluster.PopulateReleases(releaseObj) cluster.PopulateReleases(releaseObj)
for _, oneRelease := range releaseObj {
log.Info("Pullin a helm chart to the git repo", "chart", oneRelease.Chart)
if _, err := hh.PullChart(clusterWorkdirPath, oneRelease.ToHelmReleaseData()); err != nil {
return err
}
}
releasesCurrent, err := release.ReleasesFromLockfile(lockfileData, opts.Config.Repositories) releasesCurrent, err := release.ReleasesFromLockfile(lockfileData, opts.Config.Repositories)
if err != nil { if err != nil {
return err return err

View File

@ -3,6 +3,7 @@ package helmhelper
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -11,9 +12,7 @@ import (
"helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/engine" "helm.sh/helm/v3/pkg/engine"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/registry" "helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/repo"
) )
type Helm struct{} type Helm struct{}
@ -27,8 +26,7 @@ func getDownloadDirPath(workdirPath string) string {
} }
func getChartDirPath(downloadDirPath string, release *ReleaseData) string { func getChartDirPath(downloadDirPath string, release *ReleaseData) string {
return fmt.Sprintf("%s/%s-%s-%s", downloadDirPath, release.RepositoryName, release.Chart, release.Version) return fmt.Sprintf("%s/%s-%s", downloadDirPath, release.Chart, release.Name)
} }
func (h *Helm) PullChart(workdirPath string, release *ReleaseData) (path string, err error) { func (h *Helm) PullChart(workdirPath string, release *ReleaseData) (path string, err error) {
@ -53,29 +51,21 @@ func (h *Helm) PullChart(workdirPath string, release *ReleaseData) (path string,
return "", err return "", err
} }
client := action.NewPullWithOpts(action.WithConfig(config))
var path string var path string
var chartRemote string
// Download the chart to the workdir // Download the chart to the workdir
if release.RepositoryKind != "oci" { if release.RepositoryKind != "oci" {
r, err := repo.NewChartRepository(&repo.Entry{ client.RepoURL = release.RepositoryURL
Name: release.RepositoryName, chartRemote = fmt.Sprintf(release.Chart)
URL: release.RepositoryURL,
}, getter.All(cl))
if err != nil {
return "", err
}
path = r.Config.Name
} else { } else {
path = release.RepositoryURL path = release.RepositoryURL
} chartRemote = fmt.Sprintf("%s/%s", path, release.Chart)
client := action.NewPullWithOpts(action.WithConfig(config))
client.SetRegistryClient(registry) client.SetRegistryClient(registry)
client.DestDir = chartDir }
client.Settings = cl client.Settings = cl
client.Untar = true
chartRemote := fmt.Sprintf("%s/%s", path, release.Chart) client.UntarDir = chartDir
logrus.Infof("trying to pull: %s", chartRemote)
if _, err = client.Run(chartRemote); err != nil { if _, err = client.Run(chartRemote); err != nil {
return "", err return "", err
} }
@ -160,7 +150,17 @@ func (h *Helm) RenderChart(workdirPath string, release *ReleaseData) error {
} }
func getChartPathFromDir(downloadDir string) (file string, err error) { func getChartPathFromDir(downloadDir string) (file string, err error) {
filesRM, err := filepath.Glob(fmt.Sprintf("%s/*.tgz", downloadDir))
if err != nil {
return "", err
}
for _, f := range filesRM {
if err := os.Remove(f); err != nil {
return "", err
}
}
files, err := os.ReadDir(downloadDir) files, err := os.ReadDir(downloadDir)
if err != nil { if err != nil {
return "", err return "", err
} else if len(files) == 0 { } else if len(files) == 0 {

View File

@ -100,6 +100,7 @@ func (r *Release) VersionHandler(dir string, hh helmhelper.Helmhelper) error {
if err != nil { if err != nil {
return err return err
} }
logrus.Info(version)
r.Version = version r.Version = version
} }

View File

@ -64,6 +64,7 @@ clusters:
- age: - age:
- age1hcpgy4yy4psp6y2jt8waemzgg7crtlpxf3a48l6jvl6zmxll3vjsxj75vu - age1hcpgy4yy4psp6y2jt8waemzgg7crtlpxf3a48l6jvl6zmxll3vjsxj75vu
provider: flux provider: flux
reconcileRef: main
releases: releases:
- metrics-server - metrics-server
- istio-base - istio-base