-
Notifications
You must be signed in to change notification settings - Fork 74
/
etcdlockserver_controller.go
199 lines (166 loc) · 6.41 KB
/
etcdlockserver_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
/*
Copyright 2019 PlanetScale Inc.
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 etcdlockserver
import (
"context"
"flag"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"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/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
planetscalev2 "planetscale.dev/vitess-operator/pkg/apis/planetscale/v2"
"planetscale.dev/vitess-operator/pkg/operator/environment"
"planetscale.dev/vitess-operator/pkg/operator/metrics"
"planetscale.dev/vitess-operator/pkg/operator/reconciler"
"planetscale.dev/vitess-operator/pkg/operator/results"
)
const (
controllerName = "etcdlockserver-controller"
)
var (
maxConcurrentReconciles = flag.Int("etcdlockserver_concurrent_reconciles", 10, "the maximum number of different etcdlockservers to reconcile concurrently")
)
var log = logrus.WithField("controller", "EtcdLockserver")
// watchResources should contain all the resource types that this controller creates.
var watchResources = []client.Object{
&corev1.Pod{},
&corev1.Service{},
&corev1.PersistentVolumeClaim{},
&policyv1.PodDisruptionBudget{},
}
// Add creates a new EtcdLockserver 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) error {
return add(mgr, newReconciler(mgr))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
c := mgr.GetClient()
scheme := mgr.GetScheme()
recorder := mgr.GetEventRecorderFor(controllerName)
return &ReconcileEtcdLockserver{
client: c,
scheme: scheme,
recorder: recorder,
reconciler: reconciler.New(c, scheme, recorder),
}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New(controllerName, mgr, controller.Options{
Reconciler: r,
MaxConcurrentReconciles: *maxConcurrentReconciles,
})
if err != nil {
return err
}
// Watch for changes to primary resource EtcdLockserver
if err := c.Watch(source.Kind(mgr.GetCache(), &planetscalev2.EtcdLockserver{}), &handler.EnqueueRequestForObject{}); err != nil {
return err
}
// Watch for changes to secondary resources and requeue the owner EtcdLockserver.
for _, resource := range watchResources {
err := c.Watch(source.Kind(mgr.GetCache(), resource), handler.EnqueueRequestForOwner(
mgr.GetScheme(),
mgr.GetRESTMapper(),
&planetscalev2.EtcdLockserver{},
handler.OnlyControllerOwner(),
))
if err != nil {
return err
}
}
return nil
}
var _ reconcile.Reconciler = &ReconcileEtcdLockserver{}
// ReconcileEtcdLockserver reconciles a EtcdLockserver object
type ReconcileEtcdLockserver 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
recorder record.EventRecorder
reconciler *reconciler.Reconciler
}
// Reconcile reads that state of the cluster for a EtcdLockserver object and makes changes based on the state read
// and what is in the EtcdLockserver.Spec
// Note:
// 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 *ReconcileEtcdLockserver) Reconcile(cctx context.Context, request reconcile.Request) (reconcile.Result, error) {
ctx, cancel := context.WithTimeout(cctx, environment.ReconcileTimeout())
defer cancel()
resultBuilder := &results.Builder{}
log := log.WithFields(logrus.Fields{
"namespace": request.Namespace,
"etcdlockserver": request.Name,
})
log.Info("Reconciling EtcdLockserver")
// Fetch the EtcdLockserver instance.
ls := &planetscalev2.EtcdLockserver{}
err := r.client.Get(ctx, request.NamespacedName, ls)
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
return resultBuilder.Result()
}
// Error reading the object - requeue the request.
return resultBuilder.Error(err)
}
// Materialize defaults.
planetscalev2.DefaultEtcdLockserver(ls)
// Reset status, since that's all out of date info that we will recompute now.
oldStatus := ls.Status
ls.Status = *planetscalev2.NewEtcdLockserverStatus()
// Create/update Services.
svcResult, err := r.reconcileServices(ctx, ls)
resultBuilder.Merge(svcResult, err)
// Create/update desired etcd members.
memberResult, err := r.reconcileMembers(ctx, ls)
resultBuilder.Merge(memberResult, err)
// Create/update PDB.
pdbResult, err := r.reconcilePodDisruptionBudget(ctx, ls)
resultBuilder.Merge(pdbResult, err)
// Update status if needed.
ls.Status.ObservedGeneration = ls.Generation
if !apiequality.Semantic.DeepEqual(&ls.Status, &oldStatus) {
if err := r.client.Status().Update(ctx, ls); err != nil {
if !apierrors.IsConflict(err) {
r.recorder.Eventf(ls, corev1.EventTypeWarning, "StatusUpdateFailed", "failed to update status: %v", err)
}
resultBuilder.Error(err)
}
}
// Update metrics.
available := float64(0)
if ls.Status.Available == corev1.ConditionTrue {
available = 1
}
clusterAvailable.WithLabelValues(ls.Name).Set(available)
result, err := resultBuilder.Result()
reconcileCount.WithLabelValues(ls.Name, metrics.Result(err)).Inc()
return result, err
}