forked from kubernetes-sigs/cluster-api-provider-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
managedmachinepool.go
325 lines (278 loc) · 13.4 KB
/
managedmachinepool.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scope
import (
"context"
"fmt"
"strings"
"github.com/Azure/go-autorest/autorest/to"
"github.com/pkg/errors"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/agentpools"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/util/futures"
"sigs.k8s.io/cluster-api-provider-azure/util/maps"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ManagedMachinePoolScopeParams defines the input parameters used to create a new managed
// control plane.
type ManagedMachinePoolScopeParams struct {
ManagedMachinePool
Client client.Client
Cluster *clusterv1.Cluster
ControlPlane *infrav1exp.AzureManagedControlPlane
ManagedControlPlaneScope azure.ManagedClusterScoper
}
// ManagedMachinePool defines the scope interface for a managed machine pool.
type ManagedMachinePool struct {
InfraMachinePool *infrav1exp.AzureManagedMachinePool
MachinePool *expv1.MachinePool
}
// NewManagedMachinePoolScope creates a new Scope from the supplied parameters.
// This is meant to be called for each reconcile iteration.
func NewManagedMachinePoolScope(ctx context.Context, params ManagedMachinePoolScopeParams) (*ManagedMachinePoolScope, error) {
_, _, done := tele.StartSpanWithLogger(ctx, "scope.NewManagedMachinePoolScope")
defer done()
if params.Cluster == nil {
return nil, errors.New("failed to generate new scope from nil Cluster")
}
if params.ControlPlane == nil {
return nil, errors.New("failed to generate new scope from nil ControlPlane")
}
helper, err := patch.NewHelper(params.InfraMachinePool, params.Client)
if err != nil {
return nil, errors.Wrap(err, "failed to init patch helper")
}
capiMachinePoolPatchHelper, err := patch.NewHelper(params.MachinePool, params.Client)
if err != nil {
return nil, errors.Wrap(err, "failed to init patch helper")
}
return &ManagedMachinePoolScope{
Client: params.Client,
Cluster: params.Cluster,
ControlPlane: params.ControlPlane,
MachinePool: params.MachinePool,
InfraMachinePool: params.InfraMachinePool,
patchHelper: helper,
capiMachinePoolPatchHelper: capiMachinePoolPatchHelper,
ManagedClusterScoper: params.ManagedControlPlaneScope,
}, nil
}
// ManagedMachinePoolScope defines the basic context for an actuator to operate upon.
type ManagedMachinePoolScope struct {
Client client.Client
patchHelper *patch.Helper
capiMachinePoolPatchHelper *patch.Helper
azure.ManagedClusterScoper
Cluster *clusterv1.Cluster
MachinePool *expv1.MachinePool
ControlPlane *infrav1exp.AzureManagedControlPlane
InfraMachinePool *infrav1exp.AzureManagedMachinePool
}
// PatchObject persists the cluster configuration and status.
func (s *ManagedMachinePoolScope) PatchObject(ctx context.Context) error {
ctx, _, done := tele.StartSpanWithLogger(ctx, "scope.ManagedMachinePoolScope.PatchObject")
defer done()
conditions.SetSummary(s.InfraMachinePool)
return s.patchHelper.Patch(
ctx,
s.InfraMachinePool,
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
clusterv1.ReadyCondition,
}})
}
// Close closes the current scope persisting the cluster configuration and status.
func (s *ManagedMachinePoolScope) Close(ctx context.Context) error {
ctx, _, done := tele.StartSpanWithLogger(ctx, "scope.ManagedMachinePoolScope.Close")
defer done()
return s.PatchObject(ctx)
}
// AgentPoolAnnotations returns a map of annotations for the infra machine pool.
func (s *ManagedMachinePoolScope) AgentPoolAnnotations() map[string]string {
return s.InfraMachinePool.Annotations
}
// Name returns the name of the infra machine pool.
func (s *ManagedMachinePoolScope) Name() string {
return s.InfraMachinePool.Name
}
// AgentPoolSpec returns an azure.ResourceSpecGetter for currently reconciled AzureManagedMachinePool.
func (s *ManagedMachinePoolScope) AgentPoolSpec() azure.ResourceSpecGetter {
return buildAgentPoolSpec(s.ControlPlane, s.MachinePool, s.InfraMachinePool, s.AgentPoolAnnotations())
}
func buildAgentPoolSpec(managedControlPlane *infrav1exp.AzureManagedControlPlane,
machinePool *expv1.MachinePool,
managedMachinePool *infrav1exp.AzureManagedMachinePool,
agentPoolAnnotations map[string]string) azure.ResourceSpecGetter {
var normalizedVersion *string
if machinePool.Spec.Template.Spec.Version != nil {
v := strings.TrimPrefix(*machinePool.Spec.Template.Spec.Version, "v")
normalizedVersion = &v
}
replicas := int32(1)
if machinePool.Spec.Replicas != nil {
replicas = *machinePool.Spec.Replicas
}
agentPoolSpec := &agentpools.AgentPoolSpec{
Name: to.String(managedMachinePool.Spec.Name),
ResourceGroup: managedControlPlane.Spec.ResourceGroupName,
Cluster: managedControlPlane.Name,
SKU: managedMachinePool.Spec.SKU,
Replicas: replicas,
Version: normalizedVersion,
OSType: managedMachinePool.Spec.OSType,
VnetSubnetID: azure.SubnetID(
managedControlPlane.Spec.SubscriptionID,
managedControlPlane.Spec.VirtualNetwork.ResourceGroup,
managedControlPlane.Spec.VirtualNetwork.Name,
managedControlPlane.Spec.VirtualNetwork.Subnet.Name,
),
Mode: managedMachinePool.Spec.Mode,
MaxPods: managedMachinePool.Spec.MaxPods,
AvailabilityZones: managedMachinePool.Spec.AvailabilityZones,
OsDiskType: managedMachinePool.Spec.OsDiskType,
EnableUltraSSD: managedMachinePool.Spec.EnableUltraSSD,
Headers: maps.FilterByKeyPrefix(agentPoolAnnotations, azure.CustomHeaderPrefix),
EnableNodePublicIP: managedMachinePool.Spec.EnableNodePublicIP,
NodePublicIPPrefixID: managedMachinePool.Spec.NodePublicIPPrefixID,
ScaleSetPriority: managedMachinePool.Spec.ScaleSetPriority,
AdditionalTags: managedMachinePool.Spec.AdditionalTags,
KubeletDiskType: managedMachinePool.Spec.KubeletDiskType,
}
if managedMachinePool.Spec.OSDiskSizeGB != nil {
agentPoolSpec.OSDiskSizeGB = *managedMachinePool.Spec.OSDiskSizeGB
}
if len(managedMachinePool.Spec.Taints) > 0 {
nodeTaints := make([]string, 0, len(managedMachinePool.Spec.Taints))
for _, t := range managedMachinePool.Spec.Taints {
nodeTaints = append(nodeTaints, fmt.Sprintf("%s=%s:%s", t.Key, t.Value, t.Effect))
}
agentPoolSpec.NodeTaints = nodeTaints
}
if managedMachinePool.Spec.Scaling != nil {
agentPoolSpec.EnableAutoScaling = to.BoolPtr(true)
agentPoolSpec.MaxCount = managedMachinePool.Spec.Scaling.MaxSize
agentPoolSpec.MinCount = managedMachinePool.Spec.Scaling.MinSize
}
if len(managedMachinePool.Spec.NodeLabels) > 0 {
agentPoolSpec.NodeLabels = make(map[string]*string, len(managedMachinePool.Spec.NodeLabels))
for k, v := range managedMachinePool.Spec.NodeLabels {
agentPoolSpec.NodeLabels[k] = to.StringPtr(v)
}
}
if managedMachinePool.Spec.KubeletConfig != nil {
agentPoolSpec.KubeletConfig = &agentpools.KubeletConfig{
CPUManagerPolicy: (*string)(managedMachinePool.Spec.KubeletConfig.CPUManagerPolicy),
CPUCfsQuota: managedMachinePool.Spec.KubeletConfig.CPUCfsQuota,
CPUCfsQuotaPeriod: managedMachinePool.Spec.KubeletConfig.CPUCfsQuotaPeriod,
ImageGcHighThreshold: managedMachinePool.Spec.KubeletConfig.ImageGcHighThreshold,
ImageGcLowThreshold: managedMachinePool.Spec.KubeletConfig.ImageGcLowThreshold,
TopologyManagerPolicy: (*string)(managedMachinePool.Spec.KubeletConfig.TopologyManagerPolicy),
FailSwapOn: managedMachinePool.Spec.KubeletConfig.FailSwapOn,
ContainerLogMaxSizeMB: managedMachinePool.Spec.KubeletConfig.ContainerLogMaxSizeMB,
ContainerLogMaxFiles: managedMachinePool.Spec.KubeletConfig.ContainerLogMaxFiles,
PodMaxPids: managedMachinePool.Spec.KubeletConfig.PodMaxPids,
}
if len(managedMachinePool.Spec.KubeletConfig.AllowedUnsafeSysctls) > 0 {
agentPoolSpec.KubeletConfig.AllowedUnsafeSysctls = &managedMachinePool.Spec.KubeletConfig.AllowedUnsafeSysctls
}
}
return agentPoolSpec
}
// SetAgentPoolProviderIDList sets a list of agent pool's Azure VM IDs.
func (s *ManagedMachinePoolScope) SetAgentPoolProviderIDList(providerIDs []string) {
s.InfraMachinePool.Spec.ProviderIDList = providerIDs
}
// SetAgentPoolReplicas sets the number of agent pool replicas.
func (s *ManagedMachinePoolScope) SetAgentPoolReplicas(replicas int32) {
s.InfraMachinePool.Status.Replicas = replicas
}
// SetAgentPoolReady sets the flag that indicates if the agent pool is ready or not.
func (s *ManagedMachinePoolScope) SetAgentPoolReady(ready bool) {
s.InfraMachinePool.Status.Ready = ready
}
// SetLongRunningOperationState will set the future on the AzureManagedMachinePool status to allow the resource to continue
// in the next reconciliation.
func (s *ManagedMachinePoolScope) SetLongRunningOperationState(future *infrav1.Future) {
futures.Set(s.InfraMachinePool, future)
}
// GetLongRunningOperationState will get the future on the AzureManagedMachinePool status.
func (s *ManagedMachinePoolScope) GetLongRunningOperationState(name, service, futureType string) *infrav1.Future {
return futures.Get(s.InfraMachinePool, name, service, futureType)
}
// DeleteLongRunningOperationState will delete the future from the AzureManagedMachinePool status.
func (s *ManagedMachinePoolScope) DeleteLongRunningOperationState(name, service, futureType string) {
futures.Delete(s.InfraMachinePool, name, service, futureType)
}
// UpdateDeleteStatus updates a condition on the AzureManagedControlPlane status after a DELETE operation.
func (s *ManagedMachinePoolScope) UpdateDeleteStatus(condition clusterv1.ConditionType, service string, err error) {
switch {
case err == nil:
conditions.MarkFalse(s.InfraMachinePool, condition, infrav1.DeletedReason, clusterv1.ConditionSeverityInfo, "%s successfully deleted", service)
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.InfraMachinePool, condition, infrav1.DeletingReason, clusterv1.ConditionSeverityInfo, "%s deleting", service)
default:
conditions.MarkFalse(s.InfraMachinePool, condition, infrav1.DeletionFailedReason, clusterv1.ConditionSeverityError, "%s failed to delete. err: %s", service, err.Error())
}
}
// UpdatePutStatus updates a condition on the AzureManagedMachinePool status after a PUT operation.
func (s *ManagedMachinePoolScope) UpdatePutStatus(condition clusterv1.ConditionType, service string, err error) {
switch {
case err == nil:
conditions.MarkTrue(s.InfraMachinePool, condition)
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.InfraMachinePool, condition, infrav1.CreatingReason, clusterv1.ConditionSeverityInfo, "%s creating or updating", service)
default:
conditions.MarkFalse(s.InfraMachinePool, condition, infrav1.FailedReason, clusterv1.ConditionSeverityError, "%s failed to create or update. err: %s", service, err.Error())
}
}
// UpdatePatchStatus updates a condition on the AzureManagedMachinePool status after a PATCH operation.
func (s *ManagedMachinePoolScope) UpdatePatchStatus(condition clusterv1.ConditionType, service string, err error) {
switch {
case err == nil:
conditions.MarkTrue(s.InfraMachinePool, condition)
case azure.IsOperationNotDoneError(err):
conditions.MarkFalse(s.InfraMachinePool, condition, infrav1.UpdatingReason, clusterv1.ConditionSeverityInfo, "%s updating", service)
default:
conditions.MarkFalse(s.InfraMachinePool, condition, infrav1.FailedReason, clusterv1.ConditionSeverityError, "%s failed to update. err: %s", service, err.Error())
}
}
// PatchCAPIMachinePoolObject persists the capi machinepool configuration and status.
func (s *ManagedMachinePoolScope) PatchCAPIMachinePoolObject(ctx context.Context) error {
return s.capiMachinePoolPatchHelper.Patch(
ctx,
s.MachinePool,
)
}
// SetCAPIMachinePoolReplicas sets the associated MachinePool replica count.
func (s *ManagedMachinePoolScope) SetCAPIMachinePoolReplicas(replicas *int32) {
s.MachinePool.Spec.Replicas = replicas
}
// SetCAPIMachinePoolAnnotation sets the specified annotation on the associated MachinePool.
func (s *ManagedMachinePoolScope) SetCAPIMachinePoolAnnotation(key, value string) {
s.MachinePool.Annotations[key] = value
}
// RemoveCAPIMachinePoolAnnotation removes the specified annotation on the associated MachinePool.
func (s *ManagedMachinePoolScope) RemoveCAPIMachinePoolAnnotation(key string) {
delete(s.MachinePool.Annotations, key)
}
// GetCAPIMachinePoolAnnotation gets the specified annotation on the associated MachinePool.
func (s *ManagedMachinePoolScope) GetCAPIMachinePoolAnnotation(key string) (success bool, value string) {
val, ok := s.MachinePool.Annotations[key]
return ok, val
}