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_persister.go
86 lines (70 loc) · 2.82 KB
/
azure_machinepool_subnet_persister.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"
capz "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
capzexp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v7/pkg/helpers"
)
type AzureMachinePoolSubnetPersisterConfig struct {
CtrlClient client.Client
Logger micrologger.Logger
}
// AzureMachinePoolSubnetPersister is a Persister implementation that saves a
// subnet allocated for a node pool by adding it to AzureCluster CR.
type AzureMachinePoolSubnetPersister struct {
ctrlClient client.Client
logger micrologger.Logger
}
func NewAzureMachinePoolSubnetPersister(config AzureMachinePoolSubnetPersisterConfig) (*AzureMachinePoolSubnetPersister, 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 := &AzureMachinePoolSubnetPersister{
ctrlClient: config.CtrlClient,
logger: config.Logger,
}
return p, nil
}
// Persist functions takes a subnet CIDR allocated for the specified
// AzureMachinePool (namespace/ name) and adds it to Subnets array in the
// corresponding AzureCluster CR that owns the specified AzureMachinePool.
func (p *AzureMachinePoolSubnetPersister) Persist(ctx context.Context, subnet net.IPNet, namespace string, name string) error {
p.logger.Debugf(ctx, "persisting allocated subnet in AzureCluster CR")
azureMachinePool := &capzexp.AzureMachinePool{}
err := p.ctrlClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, azureMachinePool)
if err != nil {
return microerror.Mask(err)
}
err = p.addSubnetToAzureCluster(ctx, subnet, azureMachinePool)
if err != nil {
return microerror.Mask(err)
}
p.logger.Debugf(ctx, "persisted allocated subnet in AzureCluster CR")
return nil
}
func (p *AzureMachinePoolSubnetPersister) addSubnetToAzureCluster(ctx context.Context, subnet net.IPNet, azureMachinePool *capzexp.AzureMachinePool) error {
azureCluster, err := helpers.GetAzureClusterFromMetadata(ctx, p.ctrlClient, azureMachinePool.ObjectMeta)
if err != nil {
errorMessage := "error while getting AzureCluster CR from AzureMachinePool CR metadata"
p.logger.LogCtx(ctx, "level", "warning", "message", errorMessage)
return microerror.Mask(err)
}
azureMachinePoolSubnet := capz.SubnetSpec{
Role: capz.SubnetNode,
Name: azureMachinePool.Name,
CIDRBlocks: []string{subnet.String()},
}
azureCluster.Spec.NetworkSpec.Subnets = append(azureCluster.Spec.NetworkSpec.Subnets, azureMachinePoolSubnet)
err = p.ctrlClient.Update(ctx, azureCluster)
if err != nil {
return microerror.Mask(err)
}
return nil
}