forked from tigera/operator
/
core_controller.go
333 lines (294 loc) · 12.4 KB
/
core_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
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
326
327
328
329
330
331
332
333
// Copyright (c) 2019 Tigera, Inc. All rights reserved.
// 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 installation
import (
"context"
"fmt"
"time"
"k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1"
operator "github.com/tigera/operator/pkg/apis/operator/v1"
"github.com/tigera/operator/pkg/controller/status"
"github.com/tigera/operator/pkg/controller/utils"
"github.com/tigera/operator/pkg/render"
configv1 "github.com/openshift/api/config/v1"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var log = logf.Log.WithName("controller_installation")
var openshiftNetworkConfig = "cluster"
// Add creates a new Installation Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager, openshift bool) error {
return add(mgr, newReconciler(mgr, openshift))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, openshift bool) *ReconcileInstallation {
log.WithValues("openshift", openshift).Info("Checking type of cluster")
r := &ReconcileInstallation{
client: mgr.GetClient(),
scheme: mgr.GetScheme(),
watches: make(map[runtime.Object]struct{}),
openshift: openshift,
status: status.New(mgr.GetClient(), "network"),
}
r.status.Run()
return r
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r *ReconcileInstallation) error {
// Create a new controller
c, err := controller.New("tigera-installation-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return fmt.Errorf("Failed to create tigera-installation-controller: %v", err)
}
r.controller = c
// Watch for changes to primary resource Installation
err = c.Watch(&source.Kind{Type: &operator.Installation{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return fmt.Errorf("tigera-installation-controller failed to watch primary resource: %v", err)
}
if r.openshift {
// Watch for openshift network configuration as well. If we're running in OpenShift, we need to
// merge this configuration with our own and the write back the status object.
err = c.Watch(&source.Kind{Type: &configv1.Network{}}, &handler.EnqueueRequestForObject{})
if err != nil {
if !apierrors.IsNotFound(err) {
return fmt.Errorf("tigera-installation-controller failed to watch Tigera network resource: %v", err)
}
}
}
// Add watches on component dependencies.
componentDeps := []runtime.Object{
&v1.Service{
TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "elasticsearch-tigera-elasticsearch",
Namespace: "calico-monitoring",
},
},
&v1.Secret{
TypeMeta: metav1.TypeMeta{Kind: "Secret", APIVersion: "V1"},
ObjectMeta: metav1.ObjectMeta{
Name: "manager-tls",
//TODO: Read this in from env or something
Namespace: "tigera-operator",
},
},
}
for _, dep := range componentDeps {
err = r.AddWatch(dep)
if err != nil {
objMeta := dep.(metav1.ObjectMetaAccessor).GetObjectMeta()
log.Info("Adding watch on dependency failed", "name", objMeta.GetName(), "namespace", objMeta.GetNamespace(), "error", err.Error())
}
}
for _, t := range secondaryResources() {
pred := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
// Create occurs because we've created it, so we can safely ignore it.
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
if utils.IgnoreObject(e.ObjectOld) && !utils.IgnoreObject(e.ObjectNew) {
// Don't skip the removal of the "ignore" annotation. We want to
// reconcile when that happens.
return true
}
// Otherwise, ignore updates to objects when metadata.Generation does not change.
return e.MetaOld.GetGeneration() != e.MetaNew.GetGeneration()
},
}
err = c.Watch(&source.Kind{Type: t}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &operator.Installation{},
}, pred)
if err != nil {
return fmt.Errorf("tigera-installation-controller failed to watch %s: %v", t, err)
}
}
return nil
}
// secondaryResources returns a list of the secondary resources that this controller
// monitors for changes. Add resources here which correspond to the resources created by
// this controller.
func secondaryResources() []runtime.Object {
return []runtime.Object{
&apps.DaemonSet{},
&rbacv1.ClusterRole{},
&rbacv1.ClusterRoleBinding{},
&v1.ServiceAccount{},
&v1beta1.APIService{},
&v1.Service{},
}
}
var _ reconcile.Reconciler = &ReconcileInstallation{}
// ReconcileInstallation reconciles a Installation object
type ReconcileInstallation struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
controller controller.Controller
watches map[runtime.Object]struct{}
openshift bool
status *status.StatusManager
}
// AddWatch creates a watch on the given object. Only Create events are processed.
func (r *ReconcileInstallation) AddWatch(obj runtime.Object) error {
logger := log.WithName("add_watch")
objMeta := obj.(metav1.ObjectMetaAccessor).GetObjectMeta()
if _, exists := r.watches[obj]; !exists {
logger.Info("Watch doesn't exist, creating", "name", objMeta.GetName(), "namespace", objMeta.GetNamespace())
pred := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return e.Meta.GetName() == objMeta.GetName() && e.Meta.GetNamespace() == objMeta.GetNamespace()
},
UpdateFunc: func(e event.UpdateEvent) bool {
return e.MetaNew.GetName() == objMeta.GetName() && e.MetaNew.GetNamespace() == objMeta.GetNamespace()
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
}
err := r.controller.Watch(&source.Kind{Type: obj}, &handler.EnqueueRequestForObject{}, pred)
if err == nil {
r.watches[obj] = struct{}{}
}
return err
}
logger.Info("Watch exists, skipping", "name", objMeta.GetName(), "namespace", objMeta.GetNamespace())
return nil
}
// GetInstallation returns the default installation instance with defaults populated.
func GetInstallation(ctx context.Context, client client.Client, openshift bool) (*operator.Installation, error) {
// Fetch the Installation instance. We only support a single instance named "default".
instance := &operator.Installation{}
err := client.Get(ctx, utils.DefaultInstanceKey, instance)
if err != nil {
return nil, err
}
// Populate the instance with defaults for any fields not provided by the user.
fillDefaults(instance, openshift)
return instance, nil
}
// Reconcile reads that state of the cluster for a Installation object and makes changes based on the state read
// and what is in the Installation.Spec. The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileInstallation) Reconcile(request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.V(1).Info("Reconciling Installation.operator.tigera.io")
ctx := context.Background()
// Query for the installation object.
instance, err := GetInstallation(ctx, r.client, r.openshift)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
reqLogger.Info("Installation config not found")
r.status.SetDegraded("Installation not found", err.Error())
r.status.ClearAvailable()
return reconcile.Result{}, nil
}
r.status.SetDegraded("Error querying installation", err.Error())
return reconcile.Result{}, err
}
r.status.Enable()
reqLogger.V(2).Info("Loaded config", "config", instance)
openshiftConfig := &configv1.Network{}
if r.openshift {
// If configured to run in openshift, then also fetch the openshift configuration API.
reqLogger.V(1).Info("Querying for openshift network config")
err = r.client.Get(ctx, types.NamespacedName{Name: openshiftNetworkConfig}, openshiftConfig)
if err != nil {
// Error reading the object - requeue the request.
r.status.SetDegraded("Unable to read openshift network configuration", err.Error())
return reconcile.Result{}, err
}
// Use the openshift provided CIDRs.
instance.Spec.IPPools = []operator.IPPool{}
for _, net := range openshiftConfig.Spec.ClusterNetwork {
instance.Spec.IPPools = append(instance.Spec.IPPools, operator.IPPool{CIDR: net.CIDR})
}
}
// Validate the configuration.
if err = validateCustomResource(instance); err != nil {
r.status.SetDegraded("Error validating CRD", err.Error())
return reconcile.Result{}, err
}
// Query for pull secrets in operator namespace
pullSecrets, err := utils.GetNetworkingPullSecrets(instance, r.client)
if err != nil {
log.Error(err, "Error retrieving Pull secrets")
r.status.SetDegraded("Error retrieving pull secrets", err.Error())
return reconcile.Result{}, err
}
// Create a component handler to manage the rendered components.
handler := utils.NewComponentHandler(log, r.client, r.scheme, instance)
// Render the desired Calico components based on our configuration and then
// create or update them.
calico := render.Calico(instance, pullSecrets, r.openshift)
for _, component := range calico.Render() {
if err := handler.CreateOrUpdate(ctx, component, nil); err != nil {
r.status.SetDegraded("Error creating / updating resource", err.Error())
return reconcile.Result{}, err
}
}
// TODO: We handle too many components in this controller at the moment. Once we are done consolidating,
// we can have the CreateOrUpdate logic handle this for us.
r.status.SetDaemonsets([]types.NamespacedName{{Name: "calico-node", Namespace: "calico-system"}})
r.status.SetDeployments([]types.NamespacedName{{Name: "calico-kube-controllers", Namespace: "calico-system"}})
// We have successfully reconciled the Calico installation.
if r.openshift {
// If configured to run in openshift, update the config status with the current state.
reqLogger.V(1).Info("Updating openshift cluster network status")
openshiftConfig.Status.ClusterNetwork = openshiftConfig.Spec.ClusterNetwork
openshiftConfig.Status.ServiceNetwork = openshiftConfig.Spec.ServiceNetwork
openshiftConfig.Status.ClusterNetworkMTU = 1440
openshiftConfig.Status.NetworkType = "Calico"
if err = r.client.Update(ctx, openshiftConfig); err != nil {
r.status.SetDegraded("Error updating openshift network status", err.Error())
return reconcile.Result{}, err
}
}
// Clear the degraded bit if we've reached this far.
r.status.ClearDegraded()
if !r.status.IsAvailable() {
// Schedule a kick to check again in the near future. Hopefully by then
// things will be available.
return reconcile.Result{RequeueAfter: 30 * time.Second}, nil
}
// Everything is available - update the CRD status.
instance.Status.Variant = instance.Spec.Variant
if err = r.client.Status().Update(ctx, instance); err != nil {
return reconcile.Result{}, err
}
// Created successfully - don't requeue
reqLogger.V(1).Info("Finished reconciling network installation")
return reconcile.Result{}, nil
}