172 lines
4.4 KiB
Go
172 lines
4.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"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/sopshelper"
|
|
"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"
|
|
"github.com/go-logr/logr"
|
|
)
|
|
|
|
func ReadTheConfig(path string) (*config.Config, error) {
|
|
conf, err := config.NewConfigFromFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return conf, nil
|
|
}
|
|
|
|
type SyncOptions struct {
|
|
Workdir string
|
|
SSHKey string
|
|
Dry bool
|
|
Config *config.Config
|
|
SopsBin string
|
|
}
|
|
|
|
type SyncController struct{}
|
|
|
|
func Sync(ctx context.Context, opts *SyncOptions) error {
|
|
log, err := logr.FromContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Start by creating a directory where everything should be happening
|
|
configPath := filepath.Dir(opts.Config.ConfigPath)
|
|
// Prepare helm repositories
|
|
for _, repository := range opts.Config.Repositories {
|
|
if err := repository.KindFromUrl(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Configure a git client
|
|
gh := githelper.NewGit(opts.SSHKey)
|
|
// if len(diffArg) > 0 {
|
|
// snapshotDir := fmt.Sprint("%s/.snapshot", workdirPath)
|
|
// cloneSnapshoot(gh, snapshotDir, diffArg)
|
|
// }
|
|
|
|
// The main logic starts here
|
|
for _, cluster := range opts.Config.Clusters {
|
|
// Create a dir for the cluster git repo
|
|
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, opts.SopsBin, gh)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := cluster.CloneRepo(gh, clusterWorkdirPath, opts.Dry); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := cluster.BootstrapRepo(gh, clusterWorkdirPath, opts.Dry); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Read the lockfile generated by the shoebill
|
|
lockfileData, err := lockfile.NewFromFile(clusterWorkdirPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
currentRepositories, err := lockfileData.ReposFromLockfile()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := opts.Config.Releases.PopulateRepositories(opts.Config.Repositories); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Init the helm client
|
|
hh := helmhelper.NewHelm()
|
|
|
|
// Init the sops client
|
|
sops := sopshelper.NewSops()
|
|
|
|
for _, release := range opts.Config.Releases {
|
|
err := release.VersionHandler(opts.Workdir, hh)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := release.ValuesHandler(configPath); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := release.SecretsHandler(configPath, sops); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
releaseObj := release.FindReleaseByNames(cluster.Releases, opts.Config.Releases)
|
|
cluster.PopulateReleases(releaseObj)
|
|
for _, oneRelease := range releaseObj {
|
|
log.Info("Pullin a helm chart to the git repo", "chart", oneRelease.Chart)
|
|
if _, err := hh.PullChart(clusterWorkdirPath, oneRelease.ToHelmReleaseData()); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
releasesCurrent, err := release.ReleasesFromLockfile(lockfileData, opts.Config.Repositories)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Compare releases from the lockfile to ones from the current cluster config
|
|
diffReleases, err := diff.DiffReleases(releasesCurrent, cluster.ReleasesObj)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
lockfile, diffRepos, err := diffReleases.Resolve(currentRepositories, clusterWorkdirPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
hashesPerRelease, err := provider.SyncState(diffReleases, diffRepos)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := kustomize.Generate(clusterWorkdirPath, gh); err != nil {
|
|
return err
|
|
}
|
|
|
|
lockfile.AddHashes(hashesPerRelease)
|
|
|
|
if err := lockfile.WriteToFile(clusterWorkdirPath); err != nil {
|
|
return err
|
|
}
|
|
if _, err := gh.AddAllAndCommit(clusterWorkdirPath, "Update the lockfile"); err != nil {
|
|
return err
|
|
}
|
|
if !opts.Dry {
|
|
if err := gh.Push(clusterWorkdirPath); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
}
|
|
if !opts.Dry {
|
|
if err := workdir.RemoveWorkdir(opts.Workdir); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|