From 75d676029908034b031f5889c22b6f795c757296 Mon Sep 17 00:00:00 2001 From: Marcel Jacek <72880145+marceljk@users.noreply.github.com> Date: Thu, 30 Jan 2025 11:43:18 +0100 Subject: [PATCH] fix: kubeconfig state will not be removed if the cluster does not exists anymore (#659) * fix: kubeconfig state is not removed if the cluster no longer exists and kubeconfig can't be found (status code 404) --- stackit/internal/services/ske/kubeconfig/resource.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/stackit/internal/services/ske/kubeconfig/resource.go b/stackit/internal/services/ske/kubeconfig/resource.go index 07bd5e3f..90b420a3 100644 --- a/stackit/internal/services/ske/kubeconfig/resource.go +++ b/stackit/internal/services/ske/kubeconfig/resource.go @@ -3,6 +3,7 @@ package ske import ( "context" "fmt" + "net/http" "strconv" "strings" "time" @@ -23,6 +24,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" "github.com/stackitcloud/stackit-sdk-go/services/ske" ) @@ -259,6 +261,11 @@ func (r *kubeconfigResource) Read(ctx context.Context, req resource.ReadRequest, cluster, err := r.client.GetClusterExecute(ctx, projectId, clusterName) if err != nil { + oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped + if ok && oapiErr.StatusCode == http.StatusNotFound { + resp.State.RemoveResource(ctx) + return + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading kubeconfig", fmt.Sprintf("Could not get cluster(%s): %v", clusterName, err)) return }