/
controller.go
176 lines (143 loc) · 5.23 KB
/
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
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
package clusterconfig
import (
"context"
"fmt"
"sync"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
metalk8sscalitycomv1alpha1 "github.com/scality/metalk8s/operator/api/v1alpha1"
"github.com/scality/metalk8s/operator/pkg/controller/clusterconfig/controlplane"
"github.com/scality/metalk8s/operator/pkg/controller/clusterconfig/workloadplane"
"github.com/scality/metalk8s/operator/pkg/controller/utils"
)
type ClusterConfigReconciler struct {
client client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
}
const (
// ClusterConfig name to manage, since we only support one ClusterConfig
// the object is created by the operator and it's the only one that will be managed
instanceName = "main"
// Name of the controller
controllerName = "clusterconfig-controller"
// Name of the component
componentName = "metalk8s-clusterconfig"
// Name of the application
appName = "clusterconfig"
)
var log = logf.Log.WithName(controllerName)
// Create a new ClusterConfigReconciler
func newClusterConfigReconciler(mgr ctrl.Manager) *ClusterConfigReconciler {
return &ClusterConfigReconciler{
client: mgr.GetClient(),
scheme: mgr.GetScheme(),
recorder: mgr.GetEventRecorderFor(controllerName),
}
}
// Add create the new Reconciler
func Add(mgr ctrl.Manager) error {
reconciler := newClusterConfigReconciler(mgr)
instance := &metalk8sscalitycomv1alpha1.ClusterConfig{
ObjectMeta: metav1.ObjectMeta{Name: instanceName},
Spec: metalk8sscalitycomv1alpha1.ClusterConfigSpec{},
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
err := reconciler.client.Create(ctx, instance)
if err != nil && !errors.IsAlreadyExists(err) {
return err
}
return ctrl.NewControllerManagedBy(mgr).
For(&metalk8sscalitycomv1alpha1.ClusterConfig{}).
Owns(&metalk8sscalitycomv1alpha1.VirtualIPPool{}).
Owns(&corev1.Namespace{}).
Complete(reconciler)
}
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *ClusterConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
reqLogger := log.WithValues("Request.Name", req.Name)
reqLogger.Info("reconciling ClusterConfig: START")
defer reqLogger.Info("reconciling ClusterConfig: STOP")
instance := &metalk8sscalitycomv1alpha1.ClusterConfig{}
err := r.client.Get(ctx, req.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
if req.Name == instanceName {
// NOTE: The main ClusterConfig object get created at operator startup,
// so if, for whatever reason, this one get deleted we just "panic" so that
// the operator restart and re-create the ClusterConfig
panic(fmt.Errorf(
"%s ClusterConfig object should not be deleted", req.Name,
))
}
reqLogger.Info("ClusterConfig already deleted: nothing to do")
return utils.EndReconciliation()
}
reqLogger.Error(err, "cannot read ClusterConfig: requeue")
return utils.Requeue(err)
}
if instance.Name != instanceName {
if err := r.client.Delete(ctx, instance); err != nil {
reqLogger.Error(
err, "cannot delete extra ClusterConfig: requeue",
)
return utils.Requeue(err)
}
reqLogger.Info("deleting extra ClusterConfig, consider updating the main one", "Main.Name", instanceName)
return utils.EndReconciliation()
}
// Starting here some change might be done on the cluster, so make sure to publish status update
defer r.client.Status().Update(ctx, instance)
subReconcilers := map[string]utils.SubReconciler{
"ControlPlaneIngress": &controlplane.IngressReconciler{},
"WorkloadPlaneVirtualIPPool": &workloadplane.VirtualIPPoolReconciler{},
}
// Load all sub reconcilers
for key, subrec := range subReconcilers {
logger := reqLogger.WithValues("SubReconciler", key)
handler := utils.NewObjectHandler(instance, r.client, r.scheme, r.recorder, logger, componentName, appName)
subrec.Load(handler, logger, instance)
}
result := reconcileAllSubReconcilers(ctx, subReconcilers)
if result.ShouldReturn() {
instance.SetReadyCondition(metav1.ConditionFalse, "Reconciling", "Reconciliation in progress")
return result.GetResult()
}
instance.SetReadyCondition(metav1.ConditionTrue, "Ready", "Everything good")
return utils.EndReconciliation()
}
func reconcileAllSubReconcilers(ctx context.Context, subReconcilers map[string]utils.SubReconciler) utils.SubReconcilerResult {
var wg sync.WaitGroup
var mutex sync.Mutex
result := utils.NothingToDo()
// Run all sub reconcilers
for key := range subReconcilers {
wg.Add(1)
go func(key string) {
defer wg.Done()
subrec := subReconcilers[key]
logger := subrec.GetLogger()
logger.Info("reconciling: START")
defer logger.Info("reconciling: STOP")
if res := subrec.Reconcile(ctx); res.ShouldReturn() {
mutex.Lock()
if res.IsSuperior(result) {
result = res
}
mutex.Unlock()
}
}(key)
}
// Wait for all sub reconcilers to complete
wg.Wait()
return result
}