This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
delete.go
86 lines (70 loc) · 2.48 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package azureclusterconfig
import (
"context"
corev1alpha1 "github.com/giantswarm/apiextensions/v6/pkg/apis/core/v1alpha1"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/v7/pkg/controller/context/finalizerskeptcontext"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v7/service/controller/key"
)
func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
azureCluster, err := key.ToAzureCluster(obj)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "ensuring AzureClusterConfig deletion")
var azureClusterConfig corev1alpha1.AzureClusterConfig
{
nsName := types.NamespacedName{
Name: clusterConfigName(key.ClusterID(&azureCluster)),
Namespace: metav1.NamespaceDefault,
}
err = r.ctrlClient.Get(ctx, nsName, &azureClusterConfig)
if errors.IsNotFound(err) {
// Done. AzureClusterConfig is gone and finalizer can be released.
r.logger.Debugf(ctx, "AzureClusterConfig deleted")
return nil
} else if err != nil {
return microerror.Mask(err)
}
}
// Wait until AzureClusterConfig is gone.
finalizerskeptcontext.SetKept(ctx)
if key.IsDeleted(&azureClusterConfig) {
r.logger.Debugf(ctx, "AzureClusterConfig deletion in progress")
err = r.ensureFinalizersRemoved(ctx, azureClusterConfig)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "canceling resource")
return nil
}
err = r.ctrlClient.Delete(ctx, &azureClusterConfig)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "ensured AzureClusterConfig deletion")
err = r.ensureFinalizersRemoved(ctx, azureClusterConfig)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (r *Resource) ensureFinalizersRemoved(ctx context.Context, azureClusterConfig corev1alpha1.AzureClusterConfig) error {
// There is no operator reconciling AzureClusterConfig CR any more.
// I remove all finalizers to avoid deletion being stuck.
if len(azureClusterConfig.Finalizers) > 0 {
r.logger.Debugf(ctx, "clearing finalizers from AzureClusterConfig")
patch := client.MergeFrom(azureClusterConfig.DeepCopy())
azureClusterConfig.Finalizers = make([]string, 0)
err := r.ctrlClient.Patch(ctx, &azureClusterConfig, patch)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "cleared finalizers from AzureClusterConfig")
}
return nil
}