/
apicurito_controller.go
322 lines (273 loc) · 11.1 KB
/
apicurito_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
/*
* Copyright (C) 2020 Red Hat, 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 apicurito
import (
"context"
"fmt"
"reflect"
"time"
"k8s.io/apimachinery/pkg/types"
"github.com/go-logr/logr"
"github.com/RHsyseng/operator-utils/pkg/resource/compare"
"github.com/RHsyseng/operator-utils/pkg/resource/read"
"github.com/RHsyseng/operator-utils/pkg/resource/write"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
routev1 "github.com/openshift/api/route/v1"
corev1 "k8s.io/api/core/v1"
"github.com/apicurio/apicurio-operators/apicurito/pkg/resources"
pkg "github.com/apicurio/apicurio-operators/apicurito/pkg"
api "github.com/apicurio/apicurio-operators/apicurito/pkg/apis/apicur/v1alpha1"
"github.com/apicurio/apicurio-operators/apicurito/pkg/configuration"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var log = logf.Log.WithName("controller_apicurito")
/**
* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller
* business logic. Delete these comments after modifying this file.*
*/
// Add creates a new Apicurito 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 {
v := &ReconcileApicurito{client: mgr.GetClient(), scheme: mgr.GetScheme()}
if err := ConsoleYAMLSampleExists(); err == nil {
createConsoleYAMLSamples(v.client)
}
return v
}
// 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("apicurito-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource Apicurito
err = c.Watch(&source.Kind{Type: &api.Apicurito{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
// TODO(user): Modify this to be the types you create that are owned by the primary resource
// Watch for changes to secondary resource Pods and requeue the owner Apicurito
err = c.Watch(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &api.Apicurito{},
})
if err != nil {
return err
}
return nil
}
var _ reconcile.Reconciler = &ReconcileApicurito{}
// ReconcileApicurito reconciles a Apicurito object
type ReconcileApicurito 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
}
// Reconcile reads that state of the cluster for a Apicurito object and makes changes based on the state read
// and what is in the Apicurito.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 *ReconcileApicurito) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling Apicurito.")
// Fetch the Apicurito instance
apicurito := &api.Apicurito{}
err := r.client.Get(ctx, request.NamespacedName, apicurito)
if err != nil {
if errors.IsNotFound(err) {
// Request object not fd, 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("Apicurito resource not found. Ignoring since object must be deleted.")
if err := consoleLinkExists(); err == nil {
apicurito.ObjectMeta = metav1.ObjectMeta{
Name: request.Name,
Namespace: request.Namespace,
}
removeConsoleLink(r.client, apicurito)
}
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
reqLogger.Error(err, "Failed to get Apicurito.")
return reconcile.Result{}, err
}
if apicurito.Status.Phase == api.ApicuritoPhaseMissing {
r.updateStatus(ctx, apicurito, api.ApicuritoPhaseStarting, reqLogger)
}
c := &configuration.Config{}
if err = c.Config(apicurito); err != nil {
reqLogger.Error(err, "failed to generate configuration")
r.updateStatus(ctx, apicurito, api.ApicuritoPhaseInstallError, reqLogger)
return reconcile.Result{
Requeue: true,
RequeueAfter: 10 * time.Second,
}, err
}
var rs resources.Generator = resources.Resource{
Client: r.client,
Apicurito: apicurito,
Cfg: c,
Logger: reqLogger,
}
if apicurito.Status.Phase == api.ApicuritoPhaseStarting || apicurito.Status.Phase == api.ApicuritoPhaseInstallError {
r.updateStatus(ctx, apicurito, api.ApicuritoPhaseInstalling, reqLogger)
}
// Fetch routes resources and apply them before the rest
// This is needed because ConfigMaps require the routes to be present and should run only once
// at startup
route := &routev1.Route{}
err = r.client.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("%s-%s", apicurito.Name, "generator"), Namespace: apicurito.Namespace}, route)
if err != nil && errors.IsNotFound(err) {
routes := rs.Routes()
err = r.applyResources(apicurito, routes, reqLogger)
if err != nil {
reqLogger.Info("Apicurito CR resource changed in the meantime, requeue and rerun in 10 seconds", "err", err)
r.updateStatus(ctx, apicurito, api.ApicuritoPhaseInstallError, reqLogger)
return reconcile.Result{
Requeue: true,
RequeueAfter: 10 * time.Second,
}, err
}
time.Sleep(5 * time.Second)
}
if err := consoleLinkExists(); err == nil {
createConsoleLink(r.client, apicurito)
}
// generate all resources and apply them
res, err := rs.Generate()
if err != nil {
reqLogger.Error(err, "failed to generate resources")
r.updateStatus(ctx, apicurito, api.ApicuritoPhaseInstallError, reqLogger)
return reconcile.Result{
Requeue: true,
RequeueAfter: 10 * time.Second,
}, err
}
err = r.applyResources(apicurito, res, reqLogger)
if err != nil {
reqLogger.Info("Apicurito CR changed in the meantime, requeue and rerun in 10 seconds", "err", err)
r.updateStatus(ctx, apicurito, api.ApicuritoPhaseInstallError, reqLogger)
return reconcile.Result{
Requeue: true,
RequeueAfter: 10 * time.Second,
}, err
}
r.updateStatus(ctx, apicurito, api.ApicuritoPhaseInstalled, reqLogger)
return reconcile.Result{
Requeue: true,
RequeueAfter: 20 * time.Second,
}, nil
}
func (r *ReconcileApicurito) updateStatus(ctx context.Context, apicurito *api.Apicurito, phase api.ApicuritoPhase, logger logr.Logger) {
target := apicurito.DeepCopy()
target.Status.Phase = phase
target.Status.Version = pkg.Version
err := r.client.Status().Update(ctx, target)
time.Sleep(3 * time.Second)
if err != nil {
logger.Info("Failed to update apicurito status", "err", err)
}
}
func (r *ReconcileApicurito) applyResources(apicurito *api.Apicurito, res []client.Object, logger logr.Logger) (err error) {
deployed, err := getDeployedResources(apicurito, r.client)
// Normalize deployed Deployments
deploymentType := reflect.TypeOf(appsv1.Deployment{})
if v, exists := deployed[deploymentType]; exists {
for i := range v {
for ii := range v[i].(*appsv1.Deployment).Spec.Template.Spec.Containers {
v[i].(*appsv1.Deployment).Spec.Template.Spec.Containers[ii].Resources.Limits = resources.Normalize(v[i].(*appsv1.Deployment).Spec.Template.Spec.Containers[ii].Resources.Limits)
v[i].(*appsv1.Deployment).Spec.Template.Spec.Containers[ii].Resources.Requests = resources.Normalize(v[i].(*appsv1.Deployment).Spec.Template.Spec.Containers[ii].Resources.Requests)
}
}
}
requested := compare.NewMapBuilder().Add(res...).ResourceMap()
comparator := getComparator()
deltas := comparator.Compare(deployed, requested)
writer := write.New(r.client).WithOwnerController(apicurito, r.scheme)
for resourceType, delta := range deltas {
if !delta.HasChanges() {
continue
}
logger.Info("", "instances of ", resourceType, "Will create ", len(delta.Added), "update ", len(delta.Updated), "and delete", len(delta.Removed))
_, err := writer.AddResources(delta.Added)
if err != nil {
return fmt.Errorf("Apicurito CR changed: AddResources: %s", err)
}
_, err = writer.UpdateResources(deployed[resourceType], delta.Updated)
if err != nil {
return fmt.Errorf("Apicurito CR changed: UpdateResources : %s", err)
}
_, err = writer.RemoveResources(delta.Removed)
if err != nil {
return fmt.Errorf("Apicurito CR changed: RemoveResources: %s", err)
}
}
return
}
func getDeployedResources(cr *api.Apicurito, client client.Client) (map[reflect.Type][]client.Object, error) {
var log = logf.Log.WithName("getDeployedResources")
reader := read.New(client).WithNamespace(cr.Namespace).WithOwnerObject(cr)
resourceMap, err := reader.ListAll(
&corev1.ConfigMapList{},
&corev1.ServiceList{},
&appsv1.DeploymentList{},
&routev1.RouteList{},
&corev1.ServiceAccountList{},
)
if err != nil {
log.Error(err, "Failed to list deployed objects. ", err)
return nil, err
}
return resourceMap, nil
}
func getComparator() compare.MapComparator {
resourceComparator := compare.DefaultComparator()
configMapType := reflect.TypeOf(corev1.ConfigMap{})
resourceComparator.SetComparator(configMapType, func(deployed client.Object, requested client.Object) bool {
configMap1 := deployed.(*corev1.ConfigMap)
configMap2 := requested.(*corev1.ConfigMap)
var pairs [][2]interface{}
pairs = append(pairs, [2]interface{}{configMap1.Name, configMap2.Name})
pairs = append(pairs, [2]interface{}{configMap1.Namespace, configMap2.Namespace})
pairs = append(pairs, [2]interface{}{configMap1.Labels, configMap2.Labels})
pairs = append(pairs, [2]interface{}{configMap1.Annotations, configMap2.Annotations})
pairs = append(pairs, [2]interface{}{configMap1.Data, configMap2.Data})
pairs = append(pairs, [2]interface{}{configMap1.BinaryData, configMap2.BinaryData})
equal := compare.EqualPairs(pairs)
if !equal {
log.Info("Resources are not equal", "deployed", deployed, "requested", requested)
}
return equal
})
return compare.MapComparator{Comparator: resourceComparator}
}