/
autosizednodes_controller.go
137 lines (116 loc) · 3.75 KB
/
autosizednodes_controller.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
package autosizednodes
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"strings"
"github.com/Azure/go-autorest/autorest/to"
mcv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
"github.com/sirupsen/logrus"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"github.com/Azure/ARO-RP/pkg/operator"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
)
type Reconciler struct {
client client.Client
log *logrus.Entry
}
const (
ControllerName = "AutoSizedNodes"
configName = "dynamic-node"
)
func NewReconciler(log *logrus.Entry, client client.Client) *Reconciler {
return &Reconciler{
client: client,
log: log,
}
}
func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
var aro arov1alpha1.Cluster
var err error
err = r.client.Get(ctx, request.NamespacedName, &aro)
if err != nil {
err = fmt.Errorf("unable to fetch aro cluster: %w", err)
return ctrl.Result{}, client.IgnoreNotFound(err)
}
r.log.Infof("Config changed, autoSize: %t\n", aro.Spec.OperatorFlags.GetSimpleBoolean(operator.AutosizedNodesEnabled))
// key is used to locate the object in the etcd
key := types.NamespacedName{
Name: configName,
}
if !aro.Spec.OperatorFlags.GetSimpleBoolean(operator.AutosizedNodesEnabled) {
// defaults to deleting the config
config := mcv1.KubeletConfig{
ObjectMeta: metav1.ObjectMeta{
Name: configName,
},
}
err = r.client.Delete(ctx, &config)
if err != nil {
err = fmt.Errorf("could not delete KubeletConfig: %w", err)
}
return ctrl.Result{}, client.IgnoreNotFound(err)
}
defaultConfig := makeConfig()
var config mcv1.KubeletConfig
err = r.client.Get(ctx, key, &config)
if kerrors.IsNotFound(err) {
// If config doesn't exist, create a new one
err := r.client.Create(ctx, &defaultConfig, &client.CreateOptions{})
if err != nil {
err = fmt.Errorf("could not create KubeletConfig: %w", err)
}
return ctrl.Result{}, err
}
if err != nil {
// If error, return it (controller-runtime will requeue for a retry)
return ctrl.Result{}, fmt.Errorf("could not fetch KubeletConfig: %w", err)
}
// If already exists, update the spec
config.Spec = defaultConfig.Spec
err = r.client.Update(ctx, &config)
if err != nil {
err = fmt.Errorf("could not update KubeletConfig: %w", err)
}
return ctrl.Result{}, err
}
// SetupWithManager prepares the controller with info who to watch
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
clusterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool {
name := o.GetName()
return strings.EqualFold(arov1alpha1.SingletonClusterName, name)
})
b := ctrl.NewControllerManagedBy(mgr).
For(&arov1alpha1.Cluster{}, builder.WithPredicates(clusterPredicate))
// Controller adds ControllerManagedBy to KubeletConfit created by this controller.
// Any changes will trigger reconcile, but only for that config.
return b.
Named(ControllerName).
Owns(&mcv1.KubeletConfig{}).
Complete(r)
}
func makeConfig() mcv1.KubeletConfig {
return mcv1.KubeletConfig{
ObjectMeta: metav1.ObjectMeta{
Name: configName,
},
Spec: mcv1.KubeletConfigSpec{
AutoSizingReserved: to.BoolPtr(true),
MachineConfigPoolSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "machineconfiguration.openshift.io/mco-built-in",
Operator: metav1.LabelSelectorOpExists,
},
},
},
},
}
}