Add mirrors
This commit is contained in:
@ -14,6 +14,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/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func ReadTheConfig(path string) (*config.Config, error) {
|
||||
@ -45,6 +46,29 @@ func Sync(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool,
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := conf.Charts.PopulateRepositories(conf.Repositories); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := conf.Releases.PopulateCharts(conf.Charts); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Init the helm client
|
||||
hh := helmhelper.NewHelm()
|
||||
for _, ch := range conf.Charts {
|
||||
err := ch.VersionHandler(workdirPath, hh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Info("BEFORE")
|
||||
if err := ch.SyncMirrors(workdirPath, conf.Mirrors, hh); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Info("AFTER")
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Configure a git client
|
||||
gh := githelper.NewGit(sshKeyPath)
|
||||
@ -87,28 +111,21 @@ func Sync(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool,
|
||||
return err
|
||||
}
|
||||
|
||||
// Init the helm client
|
||||
hh := helmhelper.NewHelm()
|
||||
|
||||
// Init the sops client
|
||||
sops := sopshelper.NewSops()
|
||||
|
||||
for _, release := range conf.Releases {
|
||||
err := release.VersionHandler(workdirPath, hh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(diffArg) > 0 {
|
||||
_, err := hh.PullChart(workdirPath, release.ToHelmReleaseData())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := hh.RenderChart(workdirPath, release.ToHelmReleaseData()); err != nil {
|
||||
return err
|
||||
}
|
||||
// if len(diffArg) > 0 {
|
||||
// _, err := hh.PullChart(workdirPath, release.ToHelmReleaseData())
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// if err := hh.RenderChart(workdirPath, release.ToHelmReleaseData()); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
if err := release.ValuesHandler(configPath); err != nil {
|
||||
return err
|
||||
@ -127,12 +144,6 @@ func Sync(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool,
|
||||
return err
|
||||
}
|
||||
|
||||
if len(diffArg) > 0 {
|
||||
for _, releaseCurrent := range releasesCurrent {
|
||||
hh.PullChart(workdirPath, releaseCurrent.ToHelmReleaseData())
|
||||
}
|
||||
}
|
||||
|
||||
// Compare releases from the lockfile to ones from the current cluster config
|
||||
diffReleases, err := diff.DiffReleases(releasesCurrent, cluster.ReleasesObj)
|
||||
if err != nil {
|
||||
|
@ -188,12 +188,14 @@ func (f *Flux) SyncState(releasesDiffs diff.ReleasesDiffs, repoDiffs diff.Reposi
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown action is requests: %s", release.Action)
|
||||
}
|
||||
hashPerRelease := &lockfile.HashPerRelease{
|
||||
Release: release.Wished.Release,
|
||||
Namespace: release.Wished.Namespace,
|
||||
CommitHash: hash,
|
||||
}
|
||||
hashesPerReleases = append(hashesPerReleases, hashPerRelease)
|
||||
if release.Wished != nil {
|
||||
hashPerRelease := &lockfile.HashPerRelease{
|
||||
Release: release.Wished.Release,
|
||||
Namespace: release.Wished.Namespace,
|
||||
CommitHash: hash,
|
||||
}
|
||||
hashesPerReleases = append(hashesPerReleases, hashPerRelease)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -3,14 +3,12 @@ package helmhelper
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v2"
|
||||
"helm.sh/helm/v3/pkg/action"
|
||||
"helm.sh/helm/v3/pkg/chart/loader"
|
||||
"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"
|
||||
@ -26,12 +24,12 @@ func getDownloadDirPath(workdirPath string) string {
|
||||
return fmt.Sprintf("%s/.charts", workdirPath)
|
||||
}
|
||||
|
||||
func getChartDirPath(downloadDirPath string, release *ReleaseData) string {
|
||||
return fmt.Sprintf("%s/%s-%s-%s", downloadDirPath, release.RepositoryName, release.Chart, release.Version)
|
||||
func getChartDirPath(downloadDirPath string, release *ChartData) string {
|
||||
return fmt.Sprintf("%s/%s-%s-%s", downloadDirPath, release.RepositoryName, release.Name, release.Version)
|
||||
|
||||
}
|
||||
|
||||
func (h *Helm) PullChart(workdirPath string, release *ReleaseData) (path string, err error) {
|
||||
func (h *Helm) PullChart(workdirPath string, release *ChartData) (path string, err error) {
|
||||
downloadDirPath := getDownloadDirPath(workdirPath)
|
||||
if err := os.MkdirAll(downloadDirPath, 0777); err != nil {
|
||||
return "", err
|
||||
@ -71,10 +69,11 @@ func (h *Helm) PullChart(workdirPath string, release *ReleaseData) (path string,
|
||||
|
||||
client := action.NewPullWithOpts(action.WithConfig(config))
|
||||
client.SetRegistryClient(registry)
|
||||
client.Untar = true
|
||||
client.DestDir = chartDir
|
||||
client.Settings = cl
|
||||
|
||||
chartRemote := fmt.Sprintf("%s/%s", path, release.Chart)
|
||||
chartRemote := fmt.Sprintf("%s/%s", path, release.Name)
|
||||
logrus.Infof("trying to pull: %s", chartRemote)
|
||||
if _, err = client.Run(chartRemote); err != nil {
|
||||
return "", err
|
||||
@ -88,7 +87,7 @@ func (h *Helm) PullChart(workdirPath string, release *ReleaseData) (path string,
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (h *Helm) FindLatestVersion(workdirPath string, release *ReleaseData) (version string, err error) {
|
||||
func (h *Helm) FindLatestVersion(workdirPath string, release *ChartData) (version string, err error) {
|
||||
downloadDirPath := getDownloadDirPath(workdirPath)
|
||||
if err := os.MkdirAll(downloadDirPath, 0777); err != nil {
|
||||
return "", err
|
||||
@ -117,47 +116,95 @@ func (h *Helm) FindLatestVersion(workdirPath string, release *ReleaseData) (vers
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
logrus.Infof("the latest version of %s is %s", release.Chart, chartData.Version)
|
||||
logrus.Infof("the latest version of %s is %s", release.Name, chartData.Version)
|
||||
release.Version = chartData.Version
|
||||
versionedChartDir := getChartDirPath(downloadDirPath, release)
|
||||
os.Rename(chartDir, versionedChartDir)
|
||||
if err := os.Rename(chartDir, versionedChartDir); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return chartData.Version, err
|
||||
}
|
||||
|
||||
func (h *Helm) RenderChart(workdirPath string, release *ReleaseData) error {
|
||||
func (h *Helm) PushChart(workdirPath string, server, prefix, username, password string, chartdata *ChartData) (err error) {
|
||||
downloadDirPath := getDownloadDirPath(workdirPath)
|
||||
chartDirPath := getChartDirPath(downloadDirPath, release)
|
||||
chartPath, err := getChartPathFromDir(chartDirPath)
|
||||
if err != nil {
|
||||
chartDir := getChartDirPath(downloadDirPath, chartdata)
|
||||
_, err = os.Stat(chartDir)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
} else if os.IsNotExist(err) {
|
||||
if err := os.Mkdir(chartDir, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
regclient, err := registry.NewClient()
|
||||
options := registry.LoginOptBasicAuth("allanger", "")
|
||||
tls := registry.LoginOptInsecure(true)
|
||||
serverClean := strings.ReplaceAll(server, "oci://", "")
|
||||
|
||||
if err :=regclient.Login(serverClean, options, tls); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Info(fmt.Sprintf("%s/%s", chartDirPath, chartPath))
|
||||
chartObj, err := loader.Load(fmt.Sprintf("%s/%s", chartDirPath, chartPath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
values := chartutil.Values{}
|
||||
values["Values"] = chartObj.Values
|
||||
values["Release"] = map[string]string{
|
||||
"Name": release.Name,
|
||||
"Namespace": release.Namespace,
|
||||
}
|
||||
values["Capabilities"] = map[string]map[string]string{
|
||||
"KubeVersion": {
|
||||
"Version": "v1.27.9",
|
||||
"GitVersion": "v1.27.9",
|
||||
},
|
||||
}
|
||||
files, err := engine.Engine{Strict: false}.Render(chartObj, values)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Info(files)
|
||||
for file, data := range files {
|
||||
logrus.Infof("%s - %s", file, data)
|
||||
}
|
||||
logrus.Info("I'm here")
|
||||
return nil
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
versionedChartDir := fmt.Sprintf("%s/%s", getChartDirPath(downloadDirPath, chartdata), chartdata.Name)
|
||||
|
||||
tar := action.NewPackage()
|
||||
tar.Destination = downloadDirPath
|
||||
logrus.Info(versionedChartDir)
|
||||
logrus.Info(chartDir)
|
||||
tarname, err := tar.Run(versionedChartDir, nil)
|
||||
// tarpath := fmt.Sprintf("%s/%s", versionedChartDir, tarname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client := action.NewPushWithOpts(action.WithPushConfig(&action.Configuration{}))
|
||||
logrus.Infof("trying to push: %s", tarname)
|
||||
client.Settings = &cli.EnvSettings{}
|
||||
smth, err := client.Run(tarname, fmt.Sprintf("%s/%s", server, prefix))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Info(smth)
|
||||
return nil
|
||||
}
|
||||
func (h *Helm) RenderChart(workdirPath string, release *ReleaseData) error {
|
||||
return nil
|
||||
// downloadDirPath := getDownloadDirPath(workdirPath)
|
||||
// chartDirPath := getChartDirPath(downloadDirPath, release)
|
||||
// chartPath, err := getChartPathFromDir(chartDirPath)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// logrus.Info(fmt.Sprintf("%s/%s", chartDirPath, chartPath))
|
||||
// chartObj, err := loader.Load(fmt.Sprintf("%s/%s", chartDirPath, chartPath))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// values := chartutil.Values{}
|
||||
// values["Values"] = chartObj.Values
|
||||
// values["Release"] = map[string]string{
|
||||
// "Name": release.Name,
|
||||
// "Namespace": release.Namespace,
|
||||
// }
|
||||
// values["Capabilities"] = map[string]map[string]string{
|
||||
// "KubeVersion": {
|
||||
// "Version": "v1.27.9",
|
||||
// "GitVersion": "v1.27.9",
|
||||
// },
|
||||
// }
|
||||
// files, err := engine.Engine{Strict: false}.Render(chartObj, values)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// logrus.Info(files)
|
||||
// for file, data := range files {
|
||||
// logrus.Infof("%s - %s", file, data)
|
||||
// }
|
||||
// logrus.Info("I'm here")
|
||||
// return nil
|
||||
}
|
||||
|
||||
|
||||
func getChartPathFromDir(downloadDir string) (file string, err error) {
|
||||
files, err := os.ReadDir(downloadDir)
|
||||
@ -171,8 +218,8 @@ func getChartPathFromDir(downloadDir string) (file string, err error) {
|
||||
return files[0].Name(), nil
|
||||
}
|
||||
|
||||
func chartFromString(info string) (*ReleaseData, error) {
|
||||
releaseData := new(ReleaseData)
|
||||
func chartFromString(info string) (*ChartData, error) {
|
||||
releaseData := new(ChartData)
|
||||
if err := yaml.Unmarshal([]byte(info), &releaseData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -11,14 +11,18 @@ func NewHelmMock() Helmhelper {
|
||||
return &Mock{}
|
||||
}
|
||||
|
||||
func (h *Mock) FindLatestVersion(workdir string, release *ReleaseData) (version string, err error) {
|
||||
func (h *Mock) FindLatestVersion(workdir string, release *ChartData) (version string, err error) {
|
||||
return MOCK_LATEST_VERSION, nil
|
||||
}
|
||||
|
||||
func (h *Mock) PullChart(workdirPath string, release *ReleaseData) (path string, err error) {
|
||||
func (h *Mock) PullChart(workdirPath string, release *ChartData) (path string, err error) {
|
||||
return MOCK_CHART_PATH, nil
|
||||
}
|
||||
|
||||
func (h *Mock) RenderChart(workdirPath string, release *ReleaseData) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Mock) PushChart(chartDir string, server, prefix, username, password string, chartdata *ChartData) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package helmhelper
|
||||
|
||||
type Helmhelper interface {
|
||||
FindLatestVersion(workdirPath string, release *ReleaseData) (string, error)
|
||||
PullChart(workdirPath string, release *ReleaseData) (string, error)
|
||||
FindLatestVersion(workdirPath string, release *ChartData) (string, error)
|
||||
PullChart(workdirPath string, release *ChartData) (string, error)
|
||||
RenderChart(workdirPath string, release *ReleaseData) error
|
||||
PushChart(chartDir, server, prefix, username, password string, chart *ChartData) error
|
||||
}
|
||||
|
||||
type ReleaseData struct {
|
||||
@ -16,3 +17,12 @@ type ReleaseData struct {
|
||||
RepositoryKind string
|
||||
ValuesData string
|
||||
}
|
||||
|
||||
|
||||
type ChartData struct {
|
||||
Name string
|
||||
Version string
|
||||
RepositoryName string
|
||||
RepositoryURL string
|
||||
RepositoryKind string
|
||||
}
|
||||
|
Reference in New Issue
Block a user