fix: recover externally removed Talos VMs #29

Merged
eding merged 5 commits from fix/rebuild-absent-vm into main 2026-09-06 10:55:18 +02:00
2 changed files with 48 additions and 0 deletions
Showing only changes of commit 54a2ae2838 - Show all commits

View file

@ -1406,6 +1406,10 @@ func terraformReconcileTargets(cfg config.Config) []string {
func (r Runner) rebuildTalosVMs(terraformDir string, environment []string) error { func (r Runner) rebuildTalosVMs(terraformDir string, environment []string) error {
if err := r.importConfiguredTalosVMs(terraformDir, environment); err != nil { if err := r.importConfiguredTalosVMs(terraformDir, environment); err != nil {
var apiErr proxmox.APIError
if errors.As(err, &apiErr) && apiErr.StatusCode == 404 {
return nil
}
return err return err
} }
return destroyTalosVMs(terraformDir, environment) return destroyTalosVMs(terraformDir, environment)

View file

@ -4,6 +4,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -17,6 +18,7 @@ import (
"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"
"github.com/Pingu-Studio/MaidnCLI/internal/openbao" "github.com/Pingu-Studio/MaidnCLI/internal/openbao"
"github.com/Pingu-Studio/MaidnCLI/internal/proxmox"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -1278,6 +1280,48 @@ func TestRebuildTerraformRetainsFullTalosVMLifecycle(t *testing.T) {
} }
} }
func TestRebuildTerraformSkipsAlreadyAbsentTalosVM(t *testing.T) {
originalVerify := verifyTalosVMs
originalRun := runTerraform
originalDestroy := destroyTalosVMs
t.Cleanup(func() {
verifyTalosVMs = originalVerify
runTerraform = originalRun
destroyTalosVMs = originalDestroy
})
cfg := config.Config{ClusterID: "test-cluster", Talos: config.TalosConfig{
Nodes: []config.TalosNode{{Name: "cp-01", ProxmoxNode: "pve", VMID: 100, Role: "controlplane"}},
}}
var events []string
verifyTalosVMs = func(config.Config) error {
return fmt.Errorf("inspect configured Talos VM: %w", proxmox.APIError{StatusCode: 404})
}
destroyTalosVMs = func(string, []string) error {
t.Fatal("destroy ran for an already-absent VM")
return nil
}
runTerraform = func(_ string, _ []string, name string, args ...string) error {
if name != "terraform" {
t.Fatalf("unexpected command %q", name)
}
switch args[0] {
case "init", "plan", "apply":
events = append(events, args[0])
default:
t.Fatalf("unexpected Terraform operation %q", args[0])
}
return nil
}
if err := (Runner{Config: cfg, Mode: Rebuild}).reconcileTerraform(t.TempDir()); err != nil {
t.Fatal(err)
}
if strings.Join(events, ",") != "init,plan,apply" {
t.Fatalf("Terraform phase order = %q", events)
}
}
func TestEnsureLifecycleIdentityStoresMetadataUnderGeneratedDirectory(t *testing.T) { func TestEnsureLifecycleIdentityStoresMetadataUnderGeneratedDirectory(t *testing.T) {
repo := t.TempDir() repo := t.TempDir()
terraformDir := filepath.Join(repo, "terraform") terraformDir := filepath.Join(repo, "terraform")