feat: added retry mechanism

This commit is contained in:
eding 2025-10-26 16:13:15 +01:00
parent 4fe103eeb7
commit 10a79d6870
2 changed files with 37 additions and 20 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
"github.com/Pingu-Studio/MaidnCLI/internal/k8s" "github.com/Pingu-Studio/MaidnCLI/internal/k8s"
"github.com/Pingu-Studio/MaidnCLI/internal/ui" "github.com/Pingu-Studio/MaidnCLI/internal/ui"
@ -72,18 +73,37 @@ func runCreateDockerSecret(cmd *cobra.Command, args []string) {
os.Exit(1) os.Exit(1)
} }
if err := k8s.CreateDockerHubExternalSecret(); err != nil { // First attempt to create the secret
// Call our new universal handler for the specific cache error. err = k8s.CreateDockerHubExternalSecret()
utils.HandleKubectlCacheError(err) if err == nil {
// Success on the first try
return
}
// Provide a generic warning for all other potential errors. // If it failed, check for the specific cache error
if !strings.Contains(err.Error(), "no matches for kind") { if strings.Contains(err.Error(), "no matches for kind") {
fmt.Printf("[WARNING] The secret was stored in Vault, but failed to create the ExternalSecret: %v\n", err) // Handle the cache error interactively
cacheCleared := utils.HandleKubectlCacheError(err)
// If the user agreed and the cache was cleared, retry the operation
if cacheCleared {
fmt.Println("\n[INFO] Retrying to apply the ExternalSecret after clearing cache...")
time.Sleep(1 * time.Second) // Give the system a moment to breathe
err = k8s.CreateDockerHubExternalSecret()
if err == nil {
fmt.Println("[SUCCESS] ExternalSecret applied successfully on the second attempt.")
return
} }
os.Exit(1) // Exit after handling the error to prompt the user to re-run.
} }
} }
// If we reach here, either the error was not the cache issue, or the retry failed.
fmt.Printf("\n[ERROR] Operation failed. The secret is saved in Vault, but the ExternalSecret could not be applied to Kubernetes.\n")
fmt.Printf("Final error: %v\n", err)
os.Exit(1)
}
func runCreateGhcrSecret(cmd *cobra.Command, args []string) { func runCreateGhcrSecret(cmd *cobra.Command, args []string) {
vm, err := vault.NewVaultManager() vm, err := vault.NewVaultManager()
if err != nil { if err != nil {

View file

@ -48,10 +48,9 @@ func RunCommandInDir(dir string, name string, args ...string) error {
} }
// HandleKubectlCacheError detects a specific kubectl cache error and offers an interactive fix. // HandleKubectlCacheError detects a specific kubectl cache error and offers an interactive fix.
func HandleKubectlCacheError(err error) { func HandleKubectlCacheError(err error) bool {
// Guard clause: Only proceed if the error is the one we're looking for.
if err == nil || !strings.Contains(err.Error(), "no matches for kind") { if err == nil || !strings.Contains(err.Error(), "no matches for kind") {
return return false
} }
fmt.Println("\n[DIAGNOSIS] A common kubectl cache issue was detected.") fmt.Println("\n[DIAGNOSIS] A common kubectl cache issue was detected.")
@ -63,24 +62,21 @@ func HandleKubectlCacheError(err error) {
if strings.TrimSpace(strings.ToLower(response)) != "y" { if strings.TrimSpace(strings.ToLower(response)) != "y" {
fmt.Println("[INFO] Operation cancelled by user.") fmt.Println("[INFO] Operation cancelled by user.")
return return false
} }
var cmd *exec.Cmd var cmd *exec.Cmd
var shell string var shell string
// Determine the correct command based on the operating system.
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
shell = "PowerShell" shell = "PowerShell"
// Using -NoProfile and -NonInteractive for cleaner execution
cmd = exec.Command("powershell", "-NoProfile", "-NonInteractive", "-Command", "Remove-Item -Path $env:USERPROFILE\\.kube\\cache -Recurse -Force") cmd = exec.Command("powershell", "-NoProfile", "-NonInteractive", "-Command", "Remove-Item -Path $env:USERPROFILE\\.kube\\cache -Recurse -Force")
} else { } else {
shell = "shell" shell = "shell"
// The user's home directory can be fetched reliably.
homeDir, err := os.UserHomeDir() homeDir, err := os.UserHomeDir()
if err != nil { if err != nil {
fmt.Printf("[ERROR] Could not determine home directory to clear cache: %v\n", err) fmt.Printf("[ERROR] Could not determine home directory to clear cache: %v\n", err)
return return false
} }
cachePath := fmt.Sprintf("%s/.kube/cache", homeDir) cachePath := fmt.Sprintf("%s/.kube/cache", homeDir)
cmd = exec.Command("rm", "-rf", cachePath) cmd = exec.Command("rm", "-rf", cachePath)
@ -92,8 +88,9 @@ func HandleKubectlCacheError(err error) {
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
fmt.Printf("[ERROR] Failed to clear kubectl cache: %v\n", err) fmt.Printf("[ERROR] Failed to clear kubectl cache: %v\n", err)
} else { return false
}
fmt.Println("[SUCCESS] Kubectl cache cleared successfully.") fmt.Println("[SUCCESS] Kubectl cache cleared successfully.")
fmt.Println("[INFO] Please try running the previous command again.") return true
}
} }