-
Notifications
You must be signed in to change notification settings - Fork 6
/
azure.go
168 lines (145 loc) · 4.67 KB
/
azure.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package provider
import (
"context"
"fmt"
"io"
"strconv"
"text/template"
"github.com/giantswarm/k8sclient/v7/pkg/k8sclient"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
capzexp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
capi "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/k8smetadata/pkg/annotation"
"github.com/giantswarm/k8smetadata/pkg/label"
"github.com/giantswarm/microerror"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/reference"
"sigs.k8s.io/yaml"
"github.com/giantswarm/kubectl-gs/v3/internal/key"
"github.com/giantswarm/kubectl-gs/v3/pkg/scheme"
)
func WriteAzureTemplate(ctx context.Context, client k8sclient.Interface, out io.Writer, config NodePoolCRsConfig) error {
return WriteGSAzureTemplate(ctx, client, out, config)
}
func WriteGSAzureTemplate(ctx context.Context, client k8sclient.Interface, out io.Writer, config NodePoolCRsConfig) error {
var err error
config.ReleaseComponents, err = key.GetReleaseComponents(ctx, client.CtrlClient(), config.ReleaseVersion)
if err != nil {
return microerror.Mask(err)
}
var azureMachinePoolCRYaml, machinePoolCRYaml, sparkCRYaml []byte
{
azureMachinePoolCR := newAzureMachinePoolCR(config)
azureMachinePoolCRYaml, err = yaml.Marshal(azureMachinePoolCR)
if err != nil {
return microerror.Mask(err)
}
sparkCR := newSparkCR(config)
sparkCRYaml, err = yaml.Marshal(sparkCR)
if err != nil {
return microerror.Mask(err)
}
infrastructureRef := newCAPZMachinePoolInfraRef(azureMachinePoolCR)
bootstrapConfigRef := newSparkCRRef(sparkCR)
machinePoolCR := newcapiMachinePoolCR(config, infrastructureRef, bootstrapConfigRef)
{
machinePoolCR.GetAnnotations()[annotation.NodePoolMinSize] = strconv.Itoa(config.NodesMin)
machinePoolCR.GetAnnotations()[annotation.NodePoolMaxSize] = strconv.Itoa(config.NodesMax)
}
machinePoolCRYaml, err = yaml.Marshal(machinePoolCR)
if err != nil {
return microerror.Mask(err)
}
}
data := struct {
ProviderMachinePoolCR string
MachinePoolCR string
SparkCR string
}{
ProviderMachinePoolCR: string(azureMachinePoolCRYaml),
MachinePoolCR: string(machinePoolCRYaml),
SparkCR: string(sparkCRYaml),
}
t := template.Must(template.New(config.FileName).Parse(key.MachinePoolAzureCRsTemplate))
err = t.Execute(out, data)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func newAzureMachinePoolCR(config NodePoolCRsConfig) *capzexp.AzureMachinePool {
var spot *v1beta1.SpotVMOptions
if config.AzureUseSpotVms {
var maxPrice resource.Quantity
if config.AzureSpotMaxPrice > 0 {
maxPrice = resource.MustParse(fmt.Sprintf("%f", config.AzureSpotMaxPrice))
} else {
maxPrice = resource.MustParse("-1")
}
spot = &v1beta1.SpotVMOptions{
MaxPrice: &maxPrice,
}
}
azureMp := &capzexp.AzureMachinePool{
TypeMeta: metav1.TypeMeta{
Kind: "AzureMachinePool",
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: config.NodePoolName,
Namespace: config.Namespace,
Labels: map[string]string{
label.Cluster: config.ClusterName,
capi.ClusterNameLabel: config.ClusterName,
label.MachinePool: config.NodePoolName,
label.Organization: config.Organization,
label.AzureOperatorVersion: config.ReleaseComponents["azure-operator"],
label.ReleaseVersion: config.ReleaseVersion,
},
},
Spec: capzexp.AzureMachinePoolSpec{
Template: capzexp.AzureMachinePoolMachineTemplate{
VMSize: config.VMSize,
OSDisk: v1beta1.OSDisk{
ManagedDisk: &v1beta1.ManagedDiskParameters{
StorageAccountType: "",
},
},
SSHPublicKey: "",
SpotVMOptions: spot,
},
},
}
return azureMp
}
func newCAPZMachinePoolInfraRef(obj client.Object) *corev1.ObjectReference {
var infrastructureCRRef *corev1.ObjectReference
{
s, err := scheme.NewScheme()
if err != nil {
panic(microerror.Pretty(err, true))
}
infrastructureCRRef, err = reference.GetReference(s, obj)
if err != nil {
panic(fmt.Sprintf("cannot create reference to infrastructure CR: %q", err))
}
}
return infrastructureCRRef
}
func newSparkCRRef(obj client.Object) *corev1.ObjectReference {
var infrastructureCRRef *corev1.ObjectReference
{
s, err := scheme.NewScheme()
if err != nil {
panic(microerror.Pretty(err, true))
}
infrastructureCRRef, err = reference.GetReference(s, obj)
if err != nil {
panic(fmt.Sprintf("cannot create reference to infrastructure CR: %q", err))
}
}
return infrastructureCRRef
}