shoebill/internal/controller/controller.go

191 lines
4.9 KiB
Go
Raw Normal View History

2023-07-20 09:26:25 +00:00
package controller
import (
"fmt"
2023-09-22 11:02:56 +00:00
"path/filepath"
2023-07-20 09:26:25 +00:00
2023-08-02 15:00:34 +00:00
"git.badhouseplants.net/allanger/shoebill/internal/providers"
"git.badhouseplants.net/allanger/shoebill/internal/utils/diff"
"git.badhouseplants.net/allanger/shoebill/internal/utils/githelper"
"git.badhouseplants.net/allanger/shoebill/internal/utils/helmhelper"
"git.badhouseplants.net/allanger/shoebill/internal/utils/kustomize"
2023-10-11 12:14:20 +00:00
"git.badhouseplants.net/allanger/shoebill/internal/utils/sopshelper"
2023-08-02 15:00:34 +00:00
"git.badhouseplants.net/allanger/shoebill/internal/utils/workdir"
"git.badhouseplants.net/allanger/shoebill/pkg/config"
"git.badhouseplants.net/allanger/shoebill/pkg/lockfile"
"git.badhouseplants.net/allanger/shoebill/pkg/release"
2023-12-20 16:36:14 +00:00
"github.com/sirupsen/logrus"
2023-07-20 09:26:25 +00:00
)
func ReadTheConfig(path string) (*config.Config, error) {
conf, err := config.NewConfigFromFile(path)
if err != nil {
return nil, err
}
return conf, nil
}
2023-10-20 11:31:30 +00:00
// func cloneSnapshoot(gh githelper.Githelper, snapshotDir, snapshotBranch string) error {
// if err := gh.CloneRepo(snapshotBranch, snapshotUrl, false); err != nil {
// return err
// }
// return nil
// }
func Sync(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool, diffArg string) error {
2023-10-12 11:23:50 +00:00
// Start by creating a directory where everything should be happening
configPath := filepath.Dir(conf.ConfigPath)
workdirPath, err := workdir.CreateWorkdir(definedWorkdirPath)
2023-07-20 09:26:25 +00:00
if err != nil {
return err
}
2023-10-12 11:23:50 +00:00
// Prepare helm repositories
2023-07-20 09:26:25 +00:00
for _, repository := range conf.Repositories {
if err := repository.KindFromUrl(); err != nil {
return err
}
}
2023-12-20 16:36:14 +00:00
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 {
2023-12-20 21:19:08 +00:00
ch.ExtensionsHandler(configPath)
2023-12-20 16:36:14 +00:00
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")
}
2023-09-22 11:02:56 +00:00
2023-10-12 11:23:50 +00:00
// Configure a git client
2023-07-20 09:26:25 +00:00
gh := githelper.NewGit(sshKeyPath)
2023-10-20 11:31:30 +00:00
// if len(diffArg) > 0 {
// snapshotDir := fmt.Sprint("%s/.snapshot", workdirPath)
// cloneSnapshoot(gh, snapshotDir, diffArg)
// }
2023-09-22 11:02:56 +00:00
2023-10-12 11:23:50 +00:00
// The main logic starts here
2023-07-20 09:26:25 +00:00
for _, cluster := range conf.Clusters {
2023-10-12 11:23:50 +00:00
// Create a dir for the cluster git repo
clusterWorkdirPath := fmt.Sprintf("%s/%s", workdirPath, cluster.Name)
// Init a gitops provider (Currently onle flux is supported)
provider, err := providers.NewProvider(cluster.Provider, clusterWorkdirPath, conf.SopsBin, gh)
2023-07-20 09:26:25 +00:00
if err != nil {
return err
}
2023-10-12 11:23:50 +00:00
if err := cluster.CloneRepo(gh, clusterWorkdirPath, dry); err != nil {
2023-07-20 09:26:25 +00:00
return err
}
2023-09-22 11:02:56 +00:00
2023-10-12 11:23:50 +00:00
if err := cluster.BootstrapRepo(gh, clusterWorkdirPath, dry); err != nil {
2023-07-20 09:26:25 +00:00
return err
}
2023-09-22 11:02:56 +00:00
2023-10-12 11:23:50 +00:00
// Read the lockfile generated by the shoebill
lockfileData, err := lockfile.NewFromFile(clusterWorkdirPath)
2023-07-20 09:26:25 +00:00
if err != nil {
return err
}
2023-10-12 11:23:50 +00:00
currentRepositories, err := lockfileData.ReposFromLockfile()
2023-07-20 09:26:25 +00:00
if err != nil {
return err
}
2023-12-20 21:19:08 +00:00
if err := conf.Releases.PopulateRepositories(conf.Repositories, conf.Mirrors); err != nil {
2023-07-20 09:26:25 +00:00
return err
}
2023-10-12 11:23:50 +00:00
// Init the sops client
2023-10-11 12:14:20 +00:00
sops := sopshelper.NewSops()
2023-07-20 09:26:25 +00:00
for _, release := range conf.Releases {
2023-10-12 11:23:50 +00:00
2024-01-18 10:44:44 +00:00
// 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
// }
//
// }
2023-10-13 16:02:11 +00:00
2023-10-12 16:02:14 +00:00
if err := release.ValuesHandler(configPath); err != nil {
return err
}
2023-10-12 11:23:50 +00:00
if err := release.SecretsHandler(configPath, sops); err != nil {
2023-10-11 12:14:20 +00:00
return err
}
2023-07-20 09:26:25 +00:00
}
2023-10-12 11:23:50 +00:00
releaseObj := release.FindReleaseByNames(cluster.Releases, conf.Releases)
cluster.PopulateReleases(releaseObj)
2023-07-20 09:26:25 +00:00
releasesCurrent, err := release.ReleasesFromLockfile(lockfileData, conf.Repositories)
if err != nil {
return err
}
2023-10-12 11:23:50 +00:00
// Compare releases from the lockfile to ones from the current cluster config
diffReleases, err := diff.DiffReleases(releasesCurrent, cluster.ReleasesObj)
2023-07-20 09:26:25 +00:00
if err != nil {
return err
}
2023-10-12 11:23:50 +00:00
2023-10-12 16:02:14 +00:00
lockfile, diffRepos, err := diffReleases.Resolve(currentRepositories, clusterWorkdirPath)
2023-07-20 09:26:25 +00:00
if err != nil {
return err
}
2023-10-20 11:31:30 +00:00
hashesPerRelease, err := provider.SyncState(diffReleases, diffRepos)
if err != nil {
2023-07-20 09:26:25 +00:00
return err
}
2023-10-12 11:23:50 +00:00
if err := kustomize.Generate(clusterWorkdirPath, gh); err != nil {
2023-07-20 09:26:25 +00:00
return err
}
2023-10-20 11:31:30 +00:00
lockfile.AddHashes(hashesPerRelease)
2023-10-12 11:23:50 +00:00
if err := lockfile.WriteToFile(clusterWorkdirPath); err != nil {
2023-07-20 09:26:25 +00:00
return err
}
2023-10-20 11:31:30 +00:00
if _, err := gh.AddAllAndCommit(clusterWorkdirPath, "Update the lockfile"); err != nil {
2023-07-20 09:26:25 +00:00
return err
}
if !dry {
2023-10-12 11:23:50 +00:00
if err := gh.Push(clusterWorkdirPath); err != nil {
2023-07-20 09:26:25 +00:00
return err
}
}
}
if !dry {
2023-10-12 11:23:50 +00:00
if err := workdir.RemoveWorkdir(workdirPath); err != nil {
2023-07-20 09:26:25 +00:00
return err
}
}
return nil
}