diff --git a/pkg/controller/infrastructure/actuator_delete.go b/pkg/controller/infrastructure/actuator_delete.go index 7c00a158..e967c0dc 100644 --- a/pkg/controller/infrastructure/actuator_delete.go +++ b/pkg/controller/infrastructure/actuator_delete.go @@ -19,6 +19,7 @@ package infrastructure import ( "context" + "encoding/hex" "github.com/23technologies/gardener-extension-provider-ionos/pkg/controller/infrastructure/ensurer" "github.com/23technologies/gardener-extension-provider-ionos/pkg/ionos/apis/transcoder" @@ -58,6 +59,29 @@ func (a *actuator) delete(ctx context.Context, infra *extensionsv1alpha1.Infrast if err != nil { return err } + + labels, _, err := client.LabelApi.DatacentersLabelsGet(ctx, infraStatus.DatacenterID).Depth(1).Execute() + if nil != err { + return err + } + + for _, label := range *labels.Items { + if nil != label.Properties && "cluster" == *label.Properties.Key { + clusterValue, err := hex.DecodeString(*label.Properties.Value) + if nil != err { + continue + } + + if infra.Namespace == string(clusterValue) { + err = ensurer.EnsureDatacenterDeleted(ctx, client, infraStatus.DatacenterID) + if err != nil { + return err + } + + break + } + } + } } return a.updateProviderStatus(ctx, infra, nil) diff --git a/pkg/controller/infrastructure/ensurer/datacenter.go b/pkg/controller/infrastructure/ensurer/datacenter.go index dc8ff93d..4acdc467 100644 --- a/pkg/controller/infrastructure/ensurer/datacenter.go +++ b/pkg/controller/infrastructure/ensurer/datacenter.go @@ -19,6 +19,7 @@ package ensurer import ( "context" + "encoding/hex" "fmt" "strings" @@ -60,6 +61,11 @@ func EnsureDatacenter(ctx context.Context, client *ionossdk.APIClient, zone, nam return "", err } + err = ionosapiwrapper.AddLabelToDatacenter(ctx, client, datacenterID, "cluster", hex.EncodeToString([]byte(namespace))) + if nil != err { + return "", err + } + return datacenterID, nil } diff --git a/pkg/ionos/apis/mock/infrastructure.go b/pkg/ionos/apis/mock/infrastructure.go index 011bd78d..af280b69 100644 --- a/pkg/ionos/apis/mock/infrastructure.go +++ b/pkg/ionos/apis/mock/infrastructure.go @@ -450,6 +450,38 @@ func SetupTestDatacenterEndpointOnMux(mux *http.ServeMux) { } }) + mux.HandleFunc(fmt.Sprintf("%s/labels", baseURL), func(res http.ResponseWriter, req *http.Request) { + res.Header().Add("Content-Type", "application/json; charset=utf-8") + + if (strings.ToLower(req.Method) == "get") { + res.WriteHeader(http.StatusOK) + res.Write([]byte(fmt.Sprintf(` +{ +"id": %q, +"type": "collection", +"href": "", +"items": [ ] +} + `, uuid.NewString()))) + } else if (strings.ToLower(req.Method) == "post") { + res.WriteHeader(http.StatusCreated) + + jsonData := make([]byte, req.ContentLength) + req.Body.Read(jsonData) + + var data map[string]interface{} + + jsonErr := json.Unmarshal(jsonData, &data) + if jsonErr != nil { + panic(jsonErr) + } + + res.Write([]byte(jsonData)) + } else { + panic("Unsupported HTTP method call") + } + }) + mux.HandleFunc(fmt.Sprintf("%s/lans", baseURL), func(res http.ResponseWriter, req *http.Request) { res.Header().Add("Content-Type", "application/json; charset=utf-8")