fix: make bootstrap repeatable
This commit is contained in:
parent
d80e3fac02
commit
795a6038f2
|
|
@ -5,7 +5,6 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Pingu-Studio/MaidnCLI/internal/config"
|
||||
"github.com/Pingu-Studio/MaidnCLI/internal/forgejo"
|
||||
|
|
@ -34,7 +33,9 @@ func (r Runner) Run() error {
|
|||
manager := forgejo.NewRepoManager(r.Config.Git.BaseURL, r.Config.Git.Token, r.Config.Git.Owner, r.Config.Git.Username, r.Config.Flux.ManifestsRepo, r.Config.Flux.RepoName)
|
||||
if err := manager.InitializeAll(
|
||||
func(dir string) error { return ghrepo.WriteManifestsStructure(dir, r.Config.Flux.ManifestsRepo) },
|
||||
func(dir string) error { return ghrepo.WriteFluxStructure(dir, r.Config.Flux.RepoName, r.Config.Flux.ClusterPath, fluxConfig) },
|
||||
func(dir string) error {
|
||||
return ghrepo.WriteFluxStructure(dir, r.Config.Flux.RepoName, r.Config.Flux.ClusterPath, fluxConfig)
|
||||
},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -66,10 +67,10 @@ func (r Runner) Run() error {
|
|||
}
|
||||
if r.Config.Talos.AutoBootstrap {
|
||||
configFile := filepath.Join("clusterconfig", fmt.Sprintf("%s-%s.yaml", r.Config.Talos.Cluster.Name, r.Config.Talos.Nodes[0].Name))
|
||||
if err := retryTalosCommand(generatedDir, "apply-config", "--insecure", "--nodes="+r.Config.Talos.BootstrapNode, "--endpoints="+r.Config.Talos.BootstrapEndpoint, "--file="+configFile); err != nil {
|
||||
if err := utils.RunCommandInDir(generatedDir, "talosctl", "apply-config", "--insecure", "--nodes="+r.Config.Talos.BootstrapNode, "--endpoints="+r.Config.Talos.BootstrapEndpoint, "--file="+configFile); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := retryTalosCommand(generatedDir, "bootstrap", "--talosconfig=./clusterconfig/talosconfig", "--endpoints="+r.Config.Talos.BootstrapEndpoint, "--nodes="+r.Config.Talos.BootstrapNode); err != nil {
|
||||
if err := utils.RunCommandInDir(generatedDir, "talosctl", "bootstrap", "--talosconfig=./clusterconfig/talosconfig", "--endpoints="+r.Config.Talos.BootstrapEndpoint, "--nodes="+r.Config.Talos.BootstrapNode); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := utils.RunCommandInDir(generatedDir, "talosctl", "kubeconfig", "--talosconfig=./clusterconfig/talosconfig", "--nodes="+r.Config.Talos.KubeconfigNode, "."); err != nil {
|
||||
|
|
@ -77,7 +78,7 @@ func (r Runner) Run() error {
|
|||
}
|
||||
}
|
||||
if r.Config.Talos.AutoBootstrapFlux {
|
||||
if err := utils.RunCommand("flux", "bootstrap", "git", "--url="+forgejo.CloneURL(r.Config.Git.BaseURL, r.Config.Git.Owner, r.Config.Flux.RepoName), "--branch="+r.Config.Flux.Branch, "--path="+r.Config.Flux.ClusterPath); err != nil {
|
||||
if err := utils.RunCommandInDir(generatedDir, "flux", "bootstrap", "git", "--url="+forgejo.CloneURL(r.Config.Git.BaseURL, r.Config.Git.Owner, r.Config.Flux.RepoName), "--branch="+r.Config.Flux.Branch, "--path="+r.Config.Flux.ClusterPath, "--cluster-domain="+r.Config.Flux.ClusterDomain, "--username="+r.Config.Git.Username, "--password="+r.Config.Git.Token, "--token-auth", "--kubeconfig=kubeconfig"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -97,17 +98,6 @@ func ensureRepo(dir, repoURL, ref string) error {
|
|||
return utils.RunCommandInDir(dir, "git", "pull", "--ff-only", "origin", ref)
|
||||
}
|
||||
|
||||
func retryTalosCommand(dir string, args ...string) error {
|
||||
var err error
|
||||
for range 120 {
|
||||
if err = utils.RunCommandInDir(dir, "talosctl", args...); err == nil {
|
||||
return nil
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func ensureTalosConfig(generatedDir string, cfg config.Config) error {
|
||||
talhelperPath := "talhelper"
|
||||
secretPath := filepath.Join(generatedDir, "talsecret.sops.yaml")
|
||||
|
|
|
|||
|
|
@ -13,18 +13,6 @@ import (
|
|||
|
||||
func BuildFluxConfig(chartOwner, gitURL, manifestsRepo string) string {
|
||||
return fmt.Sprintf(`---
|
||||
apiVersion: source.toolkit.fluxcd.io/v1beta2
|
||||
kind: HelmRepository
|
||||
metadata:
|
||||
name: ghcr-charts
|
||||
namespace: flux-system
|
||||
spec:
|
||||
interval: 5m
|
||||
type: oci
|
||||
url: oci://ghcr.io/%s
|
||||
secretRef:
|
||||
name: ghcr-auth
|
||||
---
|
||||
apiVersion: source.toolkit.fluxcd.io/v1
|
||||
kind: GitRepository
|
||||
metadata:
|
||||
|
|
@ -36,7 +24,7 @@ spec:
|
|||
ref:
|
||||
branch: main
|
||||
secretRef:
|
||||
name: github-auth
|
||||
name: flux-system
|
||||
---
|
||||
apiVersion: kustomize.toolkit.fluxcd.io/v1
|
||||
kind: Kustomization
|
||||
|
|
@ -44,10 +32,6 @@ metadata:
|
|||
name: namespaces
|
||||
namespace: flux-system
|
||||
spec:
|
||||
dependsOn:
|
||||
- name: vault-instance
|
||||
- name: democratic-csi
|
||||
- name: metallb-config
|
||||
interval: 10m
|
||||
path: ./namespaces
|
||||
prune: true
|
||||
|
|
@ -99,7 +83,7 @@ spec:
|
|||
sourceRef:
|
||||
kind: GitRepository
|
||||
name: cicd-deployment-manifests
|
||||
`, chartOwner, gitURL)
|
||||
`, gitURL)
|
||||
}
|
||||
|
||||
type RepoManager struct {
|
||||
|
|
@ -127,7 +111,7 @@ func WriteManifestsStructure(baseDir, repoName string) error {
|
|||
for _, env := range []string{"staging", "production", "previews"} {
|
||||
dirPath := filepath.Join(baseDir, "apps", env)
|
||||
os.MkdirAll(dirPath, 0755)
|
||||
utils.WriteFile(filepath.Join(dirPath, ".gitkeep"), "")
|
||||
utils.WriteFile(filepath.Join(dirPath, "kustomization.yaml"), "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue