A lot of changes
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
@ -24,51 +25,49 @@ func ReadTheConfig(path string) (*config.Config, error) {
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// func cloneSnapshoot(gh githelper.Githelper, snapshotDir, snapshotBranch string) error {
|
||||
// if err := gh.CloneRepo(snapshotBranch, snapshotUrl, false); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
type SyncOptions struct {
|
||||
Workdir string
|
||||
SSHKey string
|
||||
Dry bool
|
||||
Config *config.Config
|
||||
SopsBin string
|
||||
}
|
||||
|
||||
func Sync(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool, diffArg string) error {
|
||||
type SyncController struct{}
|
||||
|
||||
func Sync(ctx context.Context, opts *SyncOptions) error {
|
||||
// Start by creating a directory where everything should be happening
|
||||
configPath := filepath.Dir(conf.ConfigPath)
|
||||
workdirPath, err := workdir.CreateWorkdir(definedWorkdirPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configPath := filepath.Dir(opts.Config.ConfigPath)
|
||||
// Prepare helm repositories
|
||||
for _, repository := range conf.Repositories {
|
||||
for _, repository := range opts.Config.Repositories {
|
||||
if err := repository.KindFromUrl(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Configure a git client
|
||||
gh := githelper.NewGit(sshKeyPath)
|
||||
gh := githelper.NewGit(opts.SSHKey)
|
||||
// if len(diffArg) > 0 {
|
||||
// snapshotDir := fmt.Sprint("%s/.snapshot", workdirPath)
|
||||
// cloneSnapshoot(gh, snapshotDir, diffArg)
|
||||
// snapshotDir := fmt.Sprint("%s/.snapshot", workdirPath)
|
||||
// cloneSnapshoot(gh, snapshotDir, diffArg)
|
||||
// }
|
||||
|
||||
// The main logic starts here
|
||||
for _, cluster := range conf.Clusters {
|
||||
for _, cluster := range opts.Config.Clusters {
|
||||
// Create a dir for the cluster git repo
|
||||
clusterWorkdirPath := fmt.Sprintf("%s/%s", workdirPath, cluster.Name)
|
||||
clusterWorkdirPath := fmt.Sprintf("%s/%s", opts.Workdir, cluster.Name)
|
||||
|
||||
// Init a gitops provider (Currently onle flux is supported)
|
||||
provider, err := providers.NewProvider(cluster.Provider, clusterWorkdirPath, conf.SopsBin, gh)
|
||||
provider, err := providers.NewProvider(cluster.Provider, clusterWorkdirPath, opts.SopsBin, gh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cluster.CloneRepo(gh, clusterWorkdirPath, dry); err != nil {
|
||||
if err := cluster.CloneRepo(gh, clusterWorkdirPath, opts.Dry); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cluster.BootstrapRepo(gh, clusterWorkdirPath, dry); err != nil {
|
||||
if err := cluster.BootstrapRepo(gh, clusterWorkdirPath, opts.Dry); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -83,7 +82,7 @@ func Sync(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool,
|
||||
return err
|
||||
}
|
||||
|
||||
if err := conf.Releases.PopulateRepositories(conf.Repositories); err != nil {
|
||||
if err := opts.Config.Releases.PopulateRepositories(opts.Config.Repositories); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -93,23 +92,12 @@ func Sync(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool,
|
||||
// Init the sops client
|
||||
sops := sopshelper.NewSops()
|
||||
|
||||
for _, release := range conf.Releases {
|
||||
err := release.VersionHandler(workdirPath, hh)
|
||||
for _, release := range opts.Config.Releases {
|
||||
err := release.VersionHandler(opts.Workdir, 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 err := release.ValuesHandler(configPath); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -119,20 +107,14 @@ func Sync(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool,
|
||||
}
|
||||
}
|
||||
|
||||
releaseObj := release.FindReleaseByNames(cluster.Releases, conf.Releases)
|
||||
releaseObj := release.FindReleaseByNames(cluster.Releases, opts.Config.Releases)
|
||||
cluster.PopulateReleases(releaseObj)
|
||||
|
||||
releasesCurrent, err := release.ReleasesFromLockfile(lockfileData, conf.Repositories)
|
||||
releasesCurrent, err := release.ReleasesFromLockfile(lockfileData, opts.Config.Repositories)
|
||||
if err != nil {
|
||||
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 {
|
||||
@ -161,15 +143,15 @@ func Sync(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool,
|
||||
if _, err := gh.AddAllAndCommit(clusterWorkdirPath, "Update the lockfile"); err != nil {
|
||||
return err
|
||||
}
|
||||
if !dry {
|
||||
if !opts.Dry {
|
||||
if err := gh.Push(clusterWorkdirPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if !dry {
|
||||
if err := workdir.RemoveWorkdir(workdirPath); err != nil {
|
||||
if !opts.Dry {
|
||||
if err := workdir.RemoveWorkdir(opts.Workdir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,19 @@
|
||||
package workdir
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
func CreateWorkdir(path string) (workdir string, err error) {
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
func CreateWorkdir(ctx context.Context, path string) (workdir string, err error) {
|
||||
log, err := logr.FromContext(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(path) > 0 {
|
||||
log.Info("Creating a new directory", "path", path)
|
||||
// Create a dir using the path
|
||||
if err := os.Mkdir(path, 0777); err != nil {
|
||||
return path, err
|
||||
@ -11,6 +21,7 @@ func CreateWorkdir(path string) (workdir string, err error) {
|
||||
// TODO(@allanger): I've got a feeling that it doesn't have to look that bad
|
||||
workdir = path
|
||||
} else {
|
||||
log.Info("Path is not set, creating a temporary directory")
|
||||
// Create a temporary dir
|
||||
workdir, err = os.MkdirTemp("", "shoebill")
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user