diff --git a/cmd/vault.go b/cmd/vault.go index 07918db..11b3c27 100644 --- a/cmd/vault.go +++ b/cmd/vault.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "strings" + "time" "github.com/Pingu-Studio/MaidnCLI/internal/k8s" "github.com/Pingu-Studio/MaidnCLI/internal/ui" @@ -72,16 +73,35 @@ func runCreateDockerSecret(cmd *cobra.Command, args []string) { os.Exit(1) } - if err := k8s.CreateDockerHubExternalSecret(); err != nil { - // Call our new universal handler for the specific cache error. - utils.HandleKubectlCacheError(err) - - // Provide a generic warning for all other potential errors. - 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) - } - os.Exit(1) // Exit after handling the error to prompt the user to re-run. + // First attempt to create the secret + err = k8s.CreateDockerHubExternalSecret() + if err == nil { + // Success on the first try + return } + + // If it failed, check for the specific cache error + if strings.Contains(err.Error(), "no matches for kind") { + // 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 + } + } + } + + // 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) { diff --git a/internal/utils/helpers.go b/internal/utils/helpers.go index d1171e9..eedbd35 100644 --- a/internal/utils/helpers.go +++ b/internal/utils/helpers.go @@ -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. -func HandleKubectlCacheError(err error) { - // Guard clause: Only proceed if the error is the one we're looking for. +func HandleKubectlCacheError(err error) bool { 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.") @@ -63,24 +62,21 @@ func HandleKubectlCacheError(err error) { if strings.TrimSpace(strings.ToLower(response)) != "y" { fmt.Println("[INFO] Operation cancelled by user.") - return + return false } var cmd *exec.Cmd var shell string - // Determine the correct command based on the operating system. if runtime.GOOS == "windows" { 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") } else { shell = "shell" - // The user's home directory can be fetched reliably. homeDir, err := os.UserHomeDir() if err != nil { fmt.Printf("[ERROR] Could not determine home directory to clear cache: %v\n", err) - return + return false } cachePath := fmt.Sprintf("%s/.kube/cache", homeDir) cmd = exec.Command("rm", "-rf", cachePath) @@ -92,8 +88,9 @@ func HandleKubectlCacheError(err error) { if err := cmd.Run(); err != nil { fmt.Printf("[ERROR] Failed to clear kubectl cache: %v\n", err) - } else { - fmt.Println("[SUCCESS] Kubectl cache cleared successfully.") - fmt.Println("[INFO] Please try running the previous command again.") + return false } + + fmt.Println("[SUCCESS] Kubectl cache cleared successfully.") + return true }