-
Notifications
You must be signed in to change notification settings - Fork 22
/
delete.go
76 lines (60 loc) · 2.05 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
package service
import (
"context"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/v7/pkg/resource/crud"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/giantswarm/aws-operator/v14/service/controller/key"
)
func (r *Resource) ApplyDeleteChange(ctx context.Context, obj, deleteChange interface{}) error {
cr, err := key.ToCluster(ctx, obj)
if err != nil {
return microerror.Mask(err)
}
serviceToDelete, err := toService(deleteChange)
if err != nil {
return microerror.Mask(err)
}
if serviceToDelete != nil {
r.logger.Debugf(ctx, "deleting Kubernetes service")
namespace := key.ClusterNamespace(cr)
err := r.k8sClient.CoreV1().Services(namespace).Delete(ctx, serviceToDelete.Name, metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
// fall through
} else if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "deleting Kubernetes service: deleted")
} else {
r.logger.Debugf(ctx, "deleting Kubernetes service: already deleted")
}
return nil
}
func (r *Resource) NewDeletePatch(ctx context.Context, obj, currentState, desiredState interface{}) (*crud.Patch, error) {
delete, err := r.newDeleteChange(ctx, obj, currentState, desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
patch := crud.NewPatch()
patch.SetDeleteChange(delete)
return patch, nil
}
func (r *Resource) newDeleteChange(ctx context.Context, obj, currentState, desiredState interface{}) (interface{}, error) {
currentService, err := toService(currentState)
if err != nil {
return nil, microerror.Mask(err)
}
desiredService, err := toService(desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
r.logger.Debugf(ctx, "finding out if the service has to be deleted")
var serviceToDelete *corev1.Service
if currentService != nil && desiredService.Name == currentService.Name {
serviceToDelete = desiredService
}
r.logger.Debugf(ctx, "found out if the service has to be deleted")
return serviceToDelete, nil
}