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
/
azure_machinepool_subnet_releaser.go
86 lines (70 loc) · 2.75 KB
/
azure_machinepool_subnet_releaser.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 ipam
import (
"context"
"net"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
apierrors "k8s.io/apimachinery/pkg/api/errors"
capzexp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v6/pkg/helpers"
)
type AzureMachinePoolSubnetReleaserConfig struct {
CtrlClient client.Client
Logger micrologger.Logger
}
// AzureMachinePoolSubnetReleaser is a Releaser implementation that releases an
// allocated subnet for a node pool by removing it from AzureCluster CR.
type AzureMachinePoolSubnetReleaser struct {
ctrlClient client.Client
logger micrologger.Logger
}
func NewAzureMachinePoolSubnetReleaser(config AzureMachinePoolSubnetReleaserConfig) (*AzureMachinePoolSubnetReleaser, error) {
if config.CtrlClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.CtrlClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
p := &AzureMachinePoolSubnetReleaser{
ctrlClient: config.CtrlClient,
logger: config.Logger,
}
return p, nil
}
func (r *AzureMachinePoolSubnetReleaser) Release(ctx context.Context, subnet net.IPNet, namespace, name string) error {
r.logger.Debugf(ctx, "releasing allocated subnet from AzureCluster CR")
azureMachinePool := &capzexp.AzureMachinePool{}
err := r.ctrlClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, azureMachinePool)
if err != nil {
return microerror.Mask(err)
}
err = r.removeSubnetFromAzureCluster(ctx, subnet, azureMachinePool)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "released allocated subnet from AzureCluster CR")
return nil
}
func (r *AzureMachinePoolSubnetReleaser) removeSubnetFromAzureCluster(ctx context.Context, subnet net.IPNet, azureMachinePool *capzexp.AzureMachinePool) error {
azureCluster, err := helpers.GetAzureClusterFromMetadata(ctx, r.ctrlClient, azureMachinePool.ObjectMeta)
if err != nil {
errorMessage := "error while getting AzureCluster CR from AzureMachinePool CR metadata"
r.logger.LogCtx(ctx, "level", "warning", "message", errorMessage)
return microerror.Mask(err)
}
for i, subnet := range azureCluster.Spec.NetworkSpec.Subnets {
if subnet.Name == azureMachinePool.Name {
azureCluster.Spec.NetworkSpec.Subnets = append(azureCluster.Spec.NetworkSpec.Subnets[:i], azureCluster.Spec.NetworkSpec.Subnets[i+1:]...)
break
}
}
err = r.ctrlClient.Update(ctx, azureCluster)
if apierrors.IsConflict(err) {
r.logger.Debugf(ctx, "conflict trying to save object in k8s API concurrently")
r.logger.Debugf(ctx, "cancelling resource")
} else if err != nil {
return microerror.Mask(err)
}
return nil
}