This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
deleteold.go
71 lines (62 loc) · 2.67 KB
/
deleteold.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
package migration
import (
"context"
oldcapzexpv1alpha3 "github.com/giantswarm/apiextensions/v6/pkg/apis/capzexp/v1alpha3"
"github.com/giantswarm/k8smetadata/pkg/label"
"github.com/giantswarm/microerror"
capzexp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
)
var (
pauseAnnotations = map[string]string{
"cluster.x-k8s.io/paused": "true",
"operatorkit.giantswarm.io/paused": "true",
}
)
func (r *Resource) deleteOldAzureMachinePool(ctx context.Context, oldAzureMachinePool *oldcapzexpv1alpha3.AzureMachinePool, newAzureMachinePool *capzexp.AzureMachinePool) error {
// First we manually remove all finalizers, because new CR is replacing old
// CR, so the new one will have all required finalizers, and we don't want
// those finalizers to block the deletion of the old AzureMachinePool.
update := false
r.logger.Debugf(ctx, "Deleting old AzureMachinePool %s/%s", oldAzureMachinePool.Namespace, oldAzureMachinePool.Name)
if len(oldAzureMachinePool.ObjectMeta.Finalizers) > 0 {
r.logger.Debugf(ctx, "Removing all old AzureMachinePool %s/%s finalizers", oldAzureMachinePool.Namespace, oldAzureMachinePool.Name)
oldAzureMachinePool.ObjectMeta.SetFinalizers([]string{})
update = true
}
// We then update release label, in order to stop reconciling old AzureMachinePool
// from the old azure-operator version
if oldAzureMachinePool.Labels[label.ReleaseVersion] != newAzureMachinePool.Labels[label.ReleaseVersion] {
r.logger.Debugf(ctx,
"Updating release label on old AzureMachinePool %s/%s from %s to %s",
oldAzureMachinePool.Namespace,
oldAzureMachinePool.Name,
oldAzureMachinePool.Labels[label.ReleaseVersion],
newAzureMachinePool.Labels[label.ReleaseVersion])
oldAzureMachinePool.Labels[label.ReleaseVersion] = newAzureMachinePool.Labels[label.ReleaseVersion]
update = true
}
// Ensure pause annotations are in place.
for k, v := range pauseAnnotations {
if oldAzureMachinePool.GetAnnotations()[k] != v {
if oldAzureMachinePool.Annotations == nil {
oldAzureMachinePool.Annotations = make(map[string]string)
}
oldAzureMachinePool.Annotations[k] = v
update = true
}
}
if update {
err := r.ctrlClient.Update(ctx, oldAzureMachinePool)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Removed finalizers and ensured pause annotations on old AzureMachinePool %s/%s", oldAzureMachinePool.Namespace, oldAzureMachinePool.Name)
}
// Finally, delete the old AzureMachinePool
err := r.ctrlClient.Delete(ctx, oldAzureMachinePool)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Deleted old AzureMachinePool %s/%s", oldAzureMachinePool.Namespace, oldAzureMachinePool.Name)
return nil
}