fix: make bootstrap repeatable
This commit is contained in:
parent
d80e3fac02
commit
795a6038f2
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Pingu-Studio/MaidnCLI/internal/config"
|
"github.com/Pingu-Studio/MaidnCLI/internal/config"
|
||||||
"github.com/Pingu-Studio/MaidnCLI/internal/forgejo"
|
"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)
|
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(
|
if err := manager.InitializeAll(
|
||||||
func(dir string) error { return ghrepo.WriteManifestsStructure(dir, r.Config.Flux.ManifestsRepo) },
|
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 {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -66,10 +67,10 @@ func (r Runner) Run() error {
|
||||||
}
|
}
|
||||||
if r.Config.Talos.AutoBootstrap {
|
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))
|
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
|
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
|
return err
|
||||||
}
|
}
|
||||||
if err := utils.RunCommandInDir(generatedDir, "talosctl", "kubeconfig", "--talosconfig=./clusterconfig/talosconfig", "--nodes="+r.Config.Talos.KubeconfigNode, "."); err != nil {
|
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 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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -97,17 +98,6 @@ func ensureRepo(dir, repoURL, ref string) error {
|
||||||
return utils.RunCommandInDir(dir, "git", "pull", "--ff-only", "origin", ref)
|
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 {
|
func ensureTalosConfig(generatedDir string, cfg config.Config) error {
|
||||||
talhelperPath := "talhelper"
|
talhelperPath := "talhelper"
|
||||||
secretPath := filepath.Join(generatedDir, "talsecret.sops.yaml")
|
secretPath := filepath.Join(generatedDir, "talsecret.sops.yaml")
|
||||||
|
|
|
||||||
|
|
@ -13,18 +13,6 @@ import (
|
||||||
|
|
||||||
func BuildFluxConfig(chartOwner, gitURL, manifestsRepo string) string {
|
func BuildFluxConfig(chartOwner, gitURL, manifestsRepo string) string {
|
||||||
return fmt.Sprintf(`---
|
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
|
apiVersion: source.toolkit.fluxcd.io/v1
|
||||||
kind: GitRepository
|
kind: GitRepository
|
||||||
metadata:
|
metadata:
|
||||||
|
|
@ -36,7 +24,7 @@ spec:
|
||||||
ref:
|
ref:
|
||||||
branch: main
|
branch: main
|
||||||
secretRef:
|
secretRef:
|
||||||
name: github-auth
|
name: flux-system
|
||||||
---
|
---
|
||||||
apiVersion: kustomize.toolkit.fluxcd.io/v1
|
apiVersion: kustomize.toolkit.fluxcd.io/v1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
|
|
@ -44,10 +32,6 @@ metadata:
|
||||||
name: namespaces
|
name: namespaces
|
||||||
namespace: flux-system
|
namespace: flux-system
|
||||||
spec:
|
spec:
|
||||||
dependsOn:
|
|
||||||
- name: vault-instance
|
|
||||||
- name: democratic-csi
|
|
||||||
- name: metallb-config
|
|
||||||
interval: 10m
|
interval: 10m
|
||||||
path: ./namespaces
|
path: ./namespaces
|
||||||
prune: true
|
prune: true
|
||||||
|
|
@ -99,7 +83,7 @@ spec:
|
||||||
sourceRef:
|
sourceRef:
|
||||||
kind: GitRepository
|
kind: GitRepository
|
||||||
name: cicd-deployment-manifests
|
name: cicd-deployment-manifests
|
||||||
`, chartOwner, gitURL)
|
`, gitURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RepoManager struct {
|
type RepoManager struct {
|
||||||
|
|
@ -127,7 +111,7 @@ func WriteManifestsStructure(baseDir, repoName string) error {
|
||||||
for _, env := range []string{"staging", "production", "previews"} {
|
for _, env := range []string{"staging", "production", "previews"} {
|
||||||
dirPath := filepath.Join(baseDir, "apps", env)
|
dirPath := filepath.Join(baseDir, "apps", env)
|
||||||
os.MkdirAll(dirPath, 0755)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue