/
delete.go
67 lines (54 loc) · 1.91 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package deleteinfrarefs
import (
"context"
"fmt"
"github.com/giantswarm/microerror"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/giantswarm/cluster-operator/v3/pkg/label"
"github.com/giantswarm/cluster-operator/v3/service/controller/key"
)
func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
if r.provider != label.ProviderAWS {
r.logger.Debugf(ctx, "provider is %q, only supported provider for %q resource is aws", r.provider, r.Name())
r.logger.Debugf(ctx, "canceling resource")
return nil
}
cr, err := meta.Accessor(obj)
if err != nil {
return microerror.Mask(err)
}
or, err := r.toObjRef(obj)
if err != nil {
return microerror.Mask(err)
}
var ir *unstructured.Unstructured
{
r.logger.Debugf(ctx, "finding infrastructure reference")
ir = &unstructured.Unstructured{}
ir.SetAPIVersion(or.APIVersion)
ir.SetKind(or.Kind)
err = r.k8sClient.CtrlClient().Get(ctx, key.ObjRefToNamespacedName(or), ir)
if apierrors.IsNotFound(err) {
// At this point the runtime object linked in the infrastructure reference
// does not exist anymore, which means the deletion of the parent can
// continue now.
r.logger.Debugf(ctx, "did not find infrastructure reference")
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "found infrastructure reference")
}
{
r.logger.Debugf(ctx, "deleting object %#q of type %T for tenant cluster %#q", fmt.Sprintf("%s/%s", or.Namespace, or.Name), or.Kind, key.ClusterID(cr))
err = r.k8sClient.CtrlClient().Delete(ctx, ir)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "deleted object %#q of type %T for tenant cluster %#q", fmt.Sprintf("%s/%s", or.Namespace, or.Name), or.Kind, key.ClusterID(cr))
}
return nil
}