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_checker.go
88 lines (73 loc) · 2.64 KB
/
azure_machinepool_subnet_checker.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
87
88
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/v6/pkg/helpers"
)
type AzureMachinePoolSubnetCheckerConfig struct {
CtrlClient client.Client
Logger micrologger.Logger
}
// AzureMachinePoolSubnetChecker is a Checker implementation that checks if a subnet is allocated for the
// node pool specified in Check function.
type AzureMachinePoolSubnetChecker struct {
ctrlClient client.Client
logger micrologger.Logger
}
func NewAzureMachinePoolSubnetChecker(config AzureMachinePoolSubnetCheckerConfig) (*AzureMachinePoolSubnetChecker, 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)
}
c := &AzureMachinePoolSubnetChecker{
ctrlClient: config.CtrlClient,
logger: config.Logger,
}
return c, nil
}
// Check function checks if a subnet is allocated for the specified AzureMachinePool. It is
// checking if the allocated subnet is set in the corresponding Cluster CR that owns specified
// AzureMachinePool. Returns allocated subnet or nil.
func (c *AzureMachinePoolSubnetChecker) Check(ctx context.Context, namespace string, name string) (*net.IPNet, error) {
c.logger.Debugf(ctx, "checking if node pool subnet has to be allocated")
var err error
var azureMachinePool *capzexp.AzureMachinePool
{
objectKey := client.ObjectKey{
Namespace: namespace,
Name: name,
}
azureMachinePool = &capzexp.AzureMachinePool{}
err = c.ctrlClient.Get(ctx, objectKey, azureMachinePool)
if err != nil {
return nil, microerror.Mask(err)
}
}
var azureCluster *capz.AzureCluster
{
azureCluster, err = helpers.GetAzureClusterFromMetadata(ctx, c.ctrlClient, azureMachinePool.ObjectMeta)
if err != nil {
return nil, microerror.Mask(err)
}
}
// In case there is no subnet tracked so far, we want to proceed with the allocation process.
for _, subnet := range azureCluster.Spec.NetworkSpec.Subnets {
if subnet.Name == azureMachinePool.Name && len(subnet.CIDRBlocks) > 0 {
_, subnet, err := net.ParseCIDR(subnet.CIDRBlocks[0])
if err != nil {
return nil, microerror.Mask(err)
}
c.logger.Debugf(ctx, "found existing node pool subnet")
return subnet, nil
}
}
c.logger.Debugf(ctx, "node pool subnet not found, new subnet has to be allocated")
return nil, nil
}