This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
azure_config_persister.go
72 lines (58 loc) · 2.21 KB
/
azure_config_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
package ipam
import (
"context"
"net"
"github.com/giantswarm/apiextensions/v6/pkg/apis/provider/v1alpha1"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v6/service/network"
)
type AzureConfigPersisterConfig struct {
CtrlClient client.Client
Logger micrologger.Logger
}
type AzureConfigPersister struct {
ctrlClient client.Client
logger micrologger.Logger
}
func NewAzureConfigPersister(config AzureConfigPersisterConfig) (*AzureConfigPersister, 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 := &AzureConfigPersister{
ctrlClient: config.CtrlClient,
logger: config.Logger,
}
return p, nil
}
func (p *AzureConfigPersister) Persist(ctx context.Context, vnet net.IPNet, namespace string, name string) error {
azureConfig := &v1alpha1.AzureConfig{}
err := p.ctrlClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, azureConfig)
if err != nil {
return microerror.Mask(err)
}
azureNetwork, err := network.Compute(vnet)
if err != nil {
return microerror.Mask(err)
}
azureConfig.Spec.Azure.VirtualNetwork.CIDR = vnet.String()
azureConfig.Spec.Azure.VirtualNetwork.CalicoSubnetCIDR = azureNetwork.Calico.String()
azureConfig.Spec.Azure.VirtualNetwork.MasterSubnetCIDR = azureNetwork.Master.String()
azureConfig.Spec.Azure.VirtualNetwork.WorkerSubnetCIDR = azureNetwork.Worker.String()
// TODO ensure if this fields are used or can be removed.
// NOTE in Azure we disable Calico right now. This is due to a transitioning
// phase. The k8scloudconfig templates require certain calico valus to be set
// nonetheless. So we set them here. Later when the Calico setup is
// straightened out we can improve the handling here.
azureConfig.Spec.Cluster.Calico.Subnet = azureNetwork.Calico.IP.String()
azureConfig.Spec.Cluster.Calico.CIDR, _ = azureNetwork.Calico.Mask.Size()
err = p.ctrlClient.Update(ctx, azureConfig)
if err != nil {
return microerror.Mask(err)
}
return nil
}