This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
/
controller.go
202 lines (175 loc) · 7.41 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
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
/*
Copyright 2020 Critical Stack, LLC
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 controllers
import (
"context"
"fmt"
"time"
configutil "github.com/criticalstack/crit/pkg/config/util"
critv1 "github.com/criticalstack/crit/pkg/config/v1alpha2"
"github.com/go-logr/logr"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/patch"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
controlplanev1 "github.com/criticalstack/cluster-api-bootstrap-provider-crit/apis/controlplane/v1alpha2"
"github.com/criticalstack/cluster-api-bootstrap-provider-crit/internal"
"github.com/criticalstack/cluster-api-bootstrap-provider-crit/internal/remote"
)
// CritControlPlaneReconciler reconciles a CritControlPlane object
type CritControlPlaneReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
controller controller.Controller
recorder record.EventRecorder
remoteClientGetter remote.ClusterClientGetter
managementCluster internal.ManagementCluster
}
func (r *CritControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
c, err := ctrl.NewControllerManagedBy(mgr).
For(&controlplanev1.CritControlPlane{}).
Owns(&clusterv1.Machine{}).
Watches(
&source.Kind{Type: &clusterv1.Cluster{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(r.ClusterToCritControlPlane),
},
).
WithOptions(options).
Build(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
}
r.Scheme = mgr.GetScheme()
r.controller = c
r.recorder = mgr.GetEventRecorderFor("crit-control-plane-controller")
if r.remoteClientGetter == nil {
r.remoteClientGetter = remote.NewClusterClient
}
if r.managementCluster == nil {
r.managementCluster = &internal.Management{Client: r.Client}
}
return nil
}
// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;patch
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;patch
// +kubebuilder:rbac:groups=core,resources=configmaps,namespace=kube-system,verbs=get;list;watch;create
// +kubebuilder:rbac:groups=rbac,resources=roles,namespace=kube-system,verbs=get;list;watch;create
// +kubebuilder:rbac:groups=rbac,resources=rolebindings,namespace=kube-system,verbs=get;list;watch;create
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io;bootstrap.cluster.x-k8s.io;controlplane.cluster.x-k8s.io,resources=*,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines;machines/status,verbs=get;list;watch;create;update;patch;delete
func (r *CritControlPlaneReconciler) Reconcile(req ctrl.Request) (res ctrl.Result, reterr error) {
ctx := context.Background()
logger := r.Log.WithValues("namespace", req.Namespace, "critcontrolplane", req.NamespacedName)
// Fetch the CritControlPlane instance.
ccp := &controlplanev1.CritControlPlane{}
if err := r.Client.Get(ctx, req.NamespacedName, ccp); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
logger.Error(err, "Failed to retrieve requested CritControlPlane resource from the API Server")
return ctrl.Result{Requeue: true}, nil
}
// Fetch the Cluster.
cluster, err := util.GetOwnerCluster(ctx, r.Client, ccp.ObjectMeta)
if err != nil {
logger.Error(err, "Failed to retrieve owner Cluster from the API Server")
return ctrl.Result{}, err
}
if cluster == nil {
logger.Info("Cluster Controller has not yet set OwnerRef")
return ctrl.Result{}, nil
}
logger = logger.WithValues("cluster", cluster.Name)
if util.IsPaused(cluster, ccp) {
logger.Info("Reconciliation is paused")
return ctrl.Result{}, nil
}
// Wait for the cluster infrastructure to be ready before creating machines
if !cluster.Status.InfrastructureReady {
return ctrl.Result{}, nil
}
// Initialize the patch helper.
patchHelper, err := patch.NewHelper(ccp, r.Client)
if err != nil {
logger.Error(err, "Failed to configure the patch helper")
return ctrl.Result{Requeue: true}, nil
}
defer func() {
if requeueErr, ok := errors.Cause(reterr).(capierrors.HasRequeueAfterError); ok {
if res.RequeueAfter == 0 {
res.RequeueAfter = requeueErr.GetRequeueAfter()
reterr = nil
}
}
// Always attempt to update status.
if err := r.updateStatus(ctx, ccp, cluster); err != nil {
logger.Error(err, "Failed to update CritControlPlane Status")
reterr = kerrors.NewAggregate([]error{reterr, err})
}
// Always attempt to Patch the CritControlPlane object and status after each reconciliation.
if err := patchHelper.Patch(ctx, ccp); err != nil {
logger.Error(err, "Failed to patch CritControlPlane")
reterr = kerrors.NewAggregate([]error{reterr, err})
}
// TODO: remove this as soon as we have a proper remote cluster cache in place.
// Make CCP to requeue in case status is not ready, so we can check for node status without waiting for a full resync (by default 10 minutes).
// Only requeue if we are not going in exponential backoff due to error, or if we are not already re-queueing, or if the object has a deletion timestamp.
if reterr == nil && !res.Requeue && !(res.RequeueAfter > 0) && ccp.ObjectMeta.DeletionTimestamp.IsZero() {
if !ccp.Status.Ready {
res = ctrl.Result{RequeueAfter: 20 * time.Second}
}
}
}()
if !ccp.ObjectMeta.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, cluster, ccp)
}
obj, err := configutil.Unmarshal([]byte(ccp.Spec.CritConfigSpec.Config))
if err != nil {
return ctrl.Result{}, err
}
cfg, ok := obj.(*critv1.ControlPlaneConfiguration)
if !ok {
return ctrl.Result{}, errors.Errorf("expected ControlPlaneConfiguration, received %T", obj)
}
return r.reconcileControlPlane(ctx, cluster, ccp, cfg)
}
// ClusterToCritControlPlane is a handler.ToRequestsFunc to be used to enqeue
// requests for reconciliation for CritControlPlane based on updates to a
// Cluster.
func (r *CritControlPlaneReconciler) ClusterToCritControlPlane(o handler.MapObject) []ctrl.Request {
c, ok := o.Object.(*clusterv1.Cluster)
if !ok {
r.Log.Error(nil, fmt.Sprintf("Expected a Cluster but got a %T", o.Object))
return nil
}
controlPlaneRef := c.Spec.ControlPlaneRef
if controlPlaneRef != nil && controlPlaneRef.Kind == "CritControlPlane" {
name := client.ObjectKey{Namespace: controlPlaneRef.Namespace, Name: controlPlaneRef.Name}
return []ctrl.Request{{NamespacedName: name}}
}
return nil
}