From 85e0a700cb0c4818419c70fe0bac135305a76bad Mon Sep 17 00:00:00 2001 From: eding Date: Wed, 19 Nov 2025 08:36:17 +0100 Subject: [PATCH] chore: cache clearning if CRDs do not exist --- cmd/vault.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cmd/vault.go b/cmd/vault.go index 11b3c27..e54fd28 100644 --- a/cmd/vault.go +++ b/cmd/vault.go @@ -105,6 +105,11 @@ func runCreateDockerSecret(cmd *cobra.Command, args []string) { } func runCreateGhcrSecret(cmd *cobra.Command, args []string) { + if err := k8s.CheckExternalSecretCRD(); err != nil { + fmt.Printf("[ERROR] Prerequisite check failed: %v\n", err) + os.Exit(1) + } + vm, err := vault.NewVaultManager() if err != nil { fmt.Printf("[ERROR] Failed to initialize Vault manager: %v\n", err) @@ -134,6 +139,30 @@ func runCreateGhcrSecret(cmd *cobra.Command, args []string) { fmt.Printf("[ERROR] Failed to store organization management PAT in Vault: %v\n", err) os.Exit(1) } + + err = k8s.CreateGhcrExternalSecret() + if err == nil { + return + } + + if strings.Contains(err.Error(), "no matches for kind") { + cacheCleared := utils.HandleKubectlCacheError(err) + + if cacheCleared { + fmt.Println("\n[INFO] Retrying to apply the ExternalSecret after clearing cache...") + time.Sleep(1 * time.Second) + + err = k8s.CreateGhcrExternalSecret() + if err == nil { + fmt.Println("[SUCCESS] ExternalSecret applied successfully on the second attempt.") + return + } + } + } + + fmt.Printf("\n[ERROR] Operation failed. The secrets are saved in Vault, but the ExternalSecret for GHCR could not be applied to Kubernetes.\n") + fmt.Printf("Final error: %v\n", err) + os.Exit(1) } func runCreateGithubSecret(cmd *cobra.Command, args []string) {