Skip to content

Commit

Permalink
Enable datacenter deletion if it has been labeled as created by us
Browse files Browse the repository at this point in the history
  • Loading branch information
NotTheEvilOne committed Nov 23, 2021
1 parent 6c7b999 commit 45c713a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/controller/infrastructure/actuator_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/infrastructure/ensurer/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package ensurer

import (
"context"
"encoding/hex"
"fmt"
"strings"

Expand Down Expand Up @@ -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
}

Expand Down
32 changes: 32 additions & 0 deletions pkg/ionos/apis/mock/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 45c713a

Please sign in to comment.