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/config"
|
|
|
|
"git.badhouseplants.net/allanger/shoebill/internal/config/release"
|
|
|
|
"git.badhouseplants.net/allanger/shoebill/internal/lockfile"
|
|
|
|
"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"
|
|
|
|
"git.badhouseplants.net/allanger/shoebill/internal/utils/workdir"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func Reconcile(workdirPath, sshKeyPath string, conf *config.Config, dry bool) error {
|
|
|
|
dir, err := workdir.CreateWorkdir(workdirPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
// Prepare repositories
|
2023-07-20 09:26:25 +00:00
|
|
|
for _, repository := range conf.Repositories {
|
|
|
|
if err := repository.ValidateURL(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := repository.KindFromUrl(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-07-20 09:26:25 +00:00
|
|
|
gh := githelper.NewGit(sshKeyPath)
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-07-20 09:26:25 +00:00
|
|
|
for _, cluster := range conf.Clusters {
|
|
|
|
fullPath := fmt.Sprintf("%s/%s", dir, cluster.Name)
|
|
|
|
provider, err := providers.NewProvider(cluster.Provider, fullPath, gh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cluster.CloneRepo(gh, fullPath, dry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-07-20 09:26:25 +00:00
|
|
|
err = cluster.BootstrapRepo(gh, fullPath, dry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-07-20 09:26:25 +00:00
|
|
|
lockfileData, err := lockfile.NewFromFile(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reposExisting, err := lockfileData.ReposFromLockfile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := conf.Releases.PopulateRepositories(conf.Repositories); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hh := helmhelper.NewHelm()
|
2023-09-22 11:02:56 +00:00
|
|
|
|
2023-07-20 09:26:25 +00:00
|
|
|
for _, release := range conf.Releases {
|
|
|
|
err := release.VersionHandler(workdirPath, hh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 11:02:56 +00:00
|
|
|
release.ValuesHandler(filepath.Dir(conf.ConfigPath))
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rsObj := release.FindReleaseByNames(cluster.Releases, conf.Releases)
|
|
|
|
cluster.PopulateReleases(rsObj)
|
|
|
|
releasesCurrent, err := release.ReleasesFromLockfile(lockfileData, conf.Repositories)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
diffRls, err := diff.DiffReleases(releasesCurrent, cluster.ReleasesObj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lockfile, err := diffRls.Resolve(reposExisting, fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := provider.SyncState(*diffRls); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := kustomize.Generate(fullPath, gh); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := lockfile.WriteToFile(fullPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := gh.AddAllAndCommit(fullPath, "Update the lockfile"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !dry {
|
|
|
|
if err := gh.Push(fullPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if !dry {
|
|
|
|
if err := workdir.RemoveWorkdir(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|