This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
cluster_autoscaler.go
81 lines (60 loc) · 2.6 KB
/
cluster_autoscaler.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
package nodepool
import (
"context"
"fmt"
"strconv"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/Azure/go-autorest/autorest/to"
"github.com/giantswarm/microerror"
capzexp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"github.com/giantswarm/azure-operator/v5/service/controller/key"
)
const (
clusterAutoscalerEnabledTagName = "cluster-autoscaler-enabled"
)
func (r *Resource) disableClusterAutoscaler(ctx context.Context, azureMachinePool capzexp.AzureMachinePool) error {
resourceGroup := key.ClusterID(&azureMachinePool)
vmssName := key.NodePoolVMSSName(&azureMachinePool)
r.Logger.LogCtx(ctx, "level", "info", "message", fmt.Sprintf("Disabling cluster autoscaler for nodepool %s", vmssName))
virtualMachineScaleSetsClient, err := r.ClientFactory.GetVirtualMachineScaleSetsClient(ctx, azureMachinePool.ObjectMeta)
if err != nil {
return microerror.Mask(err)
}
err = setClusterAutoscalerEnabled(ctx, virtualMachineScaleSetsClient, resourceGroup, vmssName, false)
if err != nil {
return microerror.Mask(err)
}
r.Logger.LogCtx(ctx, "level", "info", "message", fmt.Sprintf("Disabled cluster autoscaler for nodepool %s", vmssName))
return nil
}
func (r *Resource) enableClusterAutoscaler(ctx context.Context, azureMachinePool capzexp.AzureMachinePool) error {
resourceGroup := key.ClusterID(&azureMachinePool)
vmssName := key.NodePoolVMSSName(&azureMachinePool)
r.Logger.LogCtx(ctx, "level", "info", "message", fmt.Sprintf("Enabling cluster autoscaler for nodepool %s", vmssName))
virtualMachineScaleSetsClient, err := r.ClientFactory.GetVirtualMachineScaleSetsClient(ctx, azureMachinePool.ObjectMeta)
if err != nil {
return microerror.Mask(err)
}
err = setClusterAutoscalerEnabled(ctx, virtualMachineScaleSetsClient, resourceGroup, vmssName, true)
if err != nil {
return microerror.Mask(err)
}
r.Logger.LogCtx(ctx, "level", "info", "message", fmt.Sprintf("Enabled cluster autoscaler for nodepool %s", vmssName))
return nil
}
func setClusterAutoscalerEnabled(ctx context.Context, virtualMachineScaleSetsClient *compute.VirtualMachineScaleSetsClient, resourceGroup, vmssName string, enabled bool) error {
vmss, err := virtualMachineScaleSetsClient.Get(ctx, resourceGroup, vmssName)
if err != nil {
return microerror.Mask(err)
}
tags := vmss.Tags
tags[clusterAutoscalerEnabledTagName] = to.StringPtr(strconv.FormatBool(enabled))
params := compute.VirtualMachineScaleSetUpdate{
Tags: tags,
}
_, err = virtualMachineScaleSetsClient.Update(ctx, resourceGroup, vmssName, params)
if err != nil {
return microerror.Mask(err)
}
return nil
}