shoebill/internal/controller/controller.go

148 lines
3.7 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-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-12 11:23:50 +00:00
func Reconcile(definedWorkdirPath, sshKeyPath string, conf *config.Config, dry bool) error {
// 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-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-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
}
if err := conf.Releases.PopulateRepositories(conf.Repositories); err != nil {
return err
}
2023-10-12 11:23:50 +00:00
// Init the helm client
2023-07-20 09:26:25 +00:00
hh := helmhelper.NewHelm()
2023-09-22 11:02:56 +00:00
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-11 12:14:20 +00:00
release.InitRelease()
2023-10-12 11:23:50 +00:00
err := release.VersionHandler(workdirPath, hh)
2023-07-20 09:26:25 +00:00
if err != nil {
return err
}
2023-10-12 11:23:50 +00:00
release.ValuesHandler(configPath)
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
lockfile, err := diffReleases.Resolve(currentRepositories, clusterWorkdirPath)
2023-07-20 09:26:25 +00:00
if err != nil {
return err
}
2023-10-12 11:23:50 +00:00
if err := provider.SyncState(*diffReleases); 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-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-12 11:23:50 +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
}