package controller import ( "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" ) 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 } // Prepare repositories for _, repository := range conf.Repositories { if err := repository.ValidateURL(); err != nil { return err } if err := repository.KindFromUrl(); err != nil { return err } } gh := githelper.NewGit(sshKeyPath) for _, cluster := range conf.Clusters { fullPath := fmt.Sprintf("%s/%s", dir, cluster.Name) provider, err := providers.NewProvider(cluster.Provider, fullPath, conf.SopsBin, gh) if err != nil { return err } if err := cluster.CloneRepo(gh, fullPath, dry); err != nil { return err } err = cluster.BootstrapRepo(gh, fullPath, dry) if err != nil { return err } 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() sops := sopshelper.NewSops() for _, release := range conf.Releases { release.InitRelease() err := release.VersionHandler(dir, hh) if err != nil { return err } release.ValuesHandler(filepath.Dir(conf.ConfigPath)) if err := release.SecretsHandler(filepath.Dir(conf.ConfigPath), sops); err != nil { return err } } 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 }