Pull charts
This commit is contained in:
		@@ -15,6 +15,7 @@ import (
 | 
			
		||||
	"git.badhouseplants.net/allanger/shoebill/pkg/config"
 | 
			
		||||
	"git.badhouseplants.net/allanger/shoebill/pkg/lockfile"
 | 
			
		||||
	"git.badhouseplants.net/allanger/shoebill/pkg/release"
 | 
			
		||||
	"github.com/go-logr/logr"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func ReadTheConfig(path string) (*config.Config, error) {
 | 
			
		||||
@@ -36,6 +37,11 @@ type SyncOptions struct {
 | 
			
		||||
type SyncController struct{}
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
	configPath := filepath.Dir(opts.Config.ConfigPath)
 | 
			
		||||
	// Prepare helm repositories
 | 
			
		||||
@@ -109,7 +115,12 @@ func Sync(ctx context.Context, opts *SyncOptions) error {
 | 
			
		||||
 | 
			
		||||
		releaseObj := release.FindReleaseByNames(cluster.Releases, opts.Config.Releases)
 | 
			
		||||
		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)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package helmhelper
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
 | 
			
		||||
	"github.com/sirupsen/logrus"
 | 
			
		||||
	"gopkg.in/yaml.v2"
 | 
			
		||||
@@ -11,9 +12,7 @@ import (
 | 
			
		||||
	"helm.sh/helm/v3/pkg/chartutil"
 | 
			
		||||
	"helm.sh/helm/v3/pkg/cli"
 | 
			
		||||
	"helm.sh/helm/v3/pkg/engine"
 | 
			
		||||
	"helm.sh/helm/v3/pkg/getter"
 | 
			
		||||
	"helm.sh/helm/v3/pkg/registry"
 | 
			
		||||
	"helm.sh/helm/v3/pkg/repo"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Helm struct{}
 | 
			
		||||
@@ -27,8 +26,7 @@ func getDownloadDirPath(workdirPath string) 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) {
 | 
			
		||||
@@ -53,29 +51,21 @@ func (h *Helm) PullChart(workdirPath string, release *ReleaseData) (path string,
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		client := action.NewPullWithOpts(action.WithConfig(config))
 | 
			
		||||
		var path string
 | 
			
		||||
		var chartRemote string
 | 
			
		||||
		// Download the chart to the workdir
 | 
			
		||||
		if release.RepositoryKind != "oci" {
 | 
			
		||||
			r, err := repo.NewChartRepository(&repo.Entry{
 | 
			
		||||
				Name: release.RepositoryName,
 | 
			
		||||
				URL:  release.RepositoryURL,
 | 
			
		||||
			}, getter.All(cl))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return "", err
 | 
			
		||||
			}
 | 
			
		||||
			path = r.Config.Name
 | 
			
		||||
 | 
			
		||||
			client.RepoURL = release.RepositoryURL
 | 
			
		||||
			chartRemote = fmt.Sprintf(release.Chart)
 | 
			
		||||
		} else {
 | 
			
		||||
			path = release.RepositoryURL
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		client := action.NewPullWithOpts(action.WithConfig(config))
 | 
			
		||||
			chartRemote = fmt.Sprintf("%s/%s", path, release.Chart)
 | 
			
		||||
		client.SetRegistryClient(registry)
 | 
			
		||||
		client.DestDir = chartDir
 | 
			
		||||
		}
 | 
			
		||||
		client.Settings = cl
 | 
			
		||||
 | 
			
		||||
		chartRemote := fmt.Sprintf("%s/%s", path, release.Chart)
 | 
			
		||||
		logrus.Infof("trying to pull: %s", chartRemote)
 | 
			
		||||
		client.Untar = true
 | 
			
		||||
		client.UntarDir = chartDir
 | 
			
		||||
		if _, err = client.Run(chartRemote); err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
@@ -160,7 +150,17 @@ func (h *Helm) RenderChart(workdirPath string, release *ReleaseData) 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)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	} else if len(files) == 0 {
 | 
			
		||||
 
 | 
			
		||||
@@ -100,6 +100,7 @@ func (r *Release) VersionHandler(dir string, hh helmhelper.Helmhelper) error {
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		logrus.Info(version)
 | 
			
		||||
		r.Version = version
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -64,6 +64,7 @@ clusters:
 | 
			
		||||
          - age:
 | 
			
		||||
            - age1hcpgy4yy4psp6y2jt8waemzgg7crtlpxf3a48l6jvl6zmxll3vjsxj75vu
 | 
			
		||||
    provider: flux
 | 
			
		||||
    reconcileRef: main
 | 
			
		||||
    releases:
 | 
			
		||||
      - metrics-server
 | 
			
		||||
      - istio-base
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user