-
Notifications
You must be signed in to change notification settings - Fork 254
/
callbacks.go
360 lines (307 loc) · 11.7 KB
/
callbacks.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Copyright 2018 The CDI Authors.
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 controller
import (
"context"
"fmt"
"reflect"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
sdk "kubevirt.io/controller-lifecycle-operator-sdk/pkg/sdk"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
"kubevirt.io/containerized-data-importer/pkg/common"
cdicontroller "kubevirt.io/containerized-data-importer/pkg/controller"
cc "kubevirt.io/containerized-data-importer/pkg/controller/common"
"kubevirt.io/controller-lifecycle-operator-sdk/pkg/sdk/callbacks"
)
func addReconcileCallbacks(r *ReconcileCDI) {
r.reconciler.AddCallback(&appsv1.Deployment{}, reconcileDeleteControllerDeployment)
r.reconciler.AddCallback(&corev1.ServiceAccount{}, reconcileServiceAccountRead)
r.reconciler.AddCallback(&corev1.ServiceAccount{}, reconcileServiceAccounts)
r.reconciler.AddCallback(&corev1.ServiceAccount{}, reconcileSCC)
r.reconciler.AddCallback(&appsv1.Deployment{}, reconcileCreateRoute)
r.reconciler.AddCallback(&appsv1.Deployment{}, reconcileCreatePrometheusInfra)
r.reconciler.AddCallback(&appsv1.Deployment{}, reconcileRemainingRelationshipLabels)
r.reconciler.AddCallback(&appsv1.Deployment{}, reconcileDeleteSecrets)
r.reconciler.AddCallback(&appsv1.Deployment{}, reconcileCDICRD)
r.reconciler.AddCallback(&extv1.CustomResourceDefinition{}, reconcileInitializeCRD)
r.reconciler.AddCallback(&extv1.CustomResourceDefinition{}, reconcileSetConfigAuthority)
r.reconciler.AddCallback(&extv1.CustomResourceDefinition{}, reconcileHandleOldVersion)
}
func isControllerDeployment(d *appsv1.Deployment) bool {
return d.Name == "cdi-deployment"
}
func reconcileDeleteControllerDeployment(args *callbacks.ReconcileCallbackArgs) error {
switch args.State {
case callbacks.ReconcileStatePostDelete, callbacks.ReconcileStateOperatorDelete:
default:
return nil
}
var deployment *appsv1.Deployment
if args.DesiredObject != nil {
deployment = args.DesiredObject.(*appsv1.Deployment)
} else if args.CurrentObject != nil {
deployment = args.CurrentObject.(*appsv1.Deployment)
} else {
args.Logger.Info("Received callback with no desired/current object")
return nil
}
if !isControllerDeployment(deployment) {
return nil
}
args.Logger.Info("Deleting CDI deployment and all import/upload/clone pods/services")
err := args.Client.Delete(context.TODO(), deployment, &client.DeleteOptions{
PropagationPolicy: &[]metav1.DeletionPropagation{metav1.DeletePropagationForeground}[0],
})
cr := args.Resource.(runtime.Object)
if err != nil && !errors.IsNotFound(err) {
args.Logger.Error(err, "Error deleting cdi controller deployment")
args.Recorder.Event(cr, corev1.EventTypeWarning, deleteResourceFailed, fmt.Sprintf("Failed to delete deployment %s, %v", deployment.Name, err))
return err
}
args.Recorder.Event(cr, corev1.EventTypeNormal, deleteResourceSuccess, fmt.Sprintf("Deleted deployment %s successfully", deployment.Name))
if err = deleteWorkerResources(args.Logger, args.Client); err != nil {
args.Logger.Error(err, "Error deleting worker resources")
args.Recorder.Event(cr, corev1.EventTypeWarning, deleteResourceFailed, fmt.Sprintf("Failed to deleted worker resources %v", err))
return err
}
args.Recorder.Event(cr, corev1.EventTypeNormal, deleteResourceSuccess, "Deleted worker resources successfully")
return nil
}
func reconcileCreateRoute(args *callbacks.ReconcileCallbackArgs) error {
if args.State != callbacks.ReconcileStatePostRead {
return nil
}
deployment := args.CurrentObject.(*appsv1.Deployment)
if !isControllerDeployment(deployment) || !sdk.CheckDeploymentReady(deployment) {
return nil
}
cr := args.Resource.(runtime.Object)
if err := ensureUploadProxyRouteExists(context.TODO(), args.Logger, args.Client, args.Scheme, deployment); err != nil {
args.Recorder.Event(cr, corev1.EventTypeWarning, createResourceFailed, fmt.Sprintf("Failed to ensure upload proxy route exists, %v", err))
return err
}
args.Recorder.Event(cr, corev1.EventTypeNormal, createResourceSuccess, "Successfully ensured upload proxy route exists")
return nil
}
func reconcileSCC(args *callbacks.ReconcileCallbackArgs) error {
switch args.State {
case callbacks.ReconcileStatePreCreate, callbacks.ReconcileStatePostRead:
default:
return nil
}
sa := args.DesiredObject.(*corev1.ServiceAccount)
if sa.Name != common.ControllerServiceAccountName {
return nil
}
cr := args.Resource.(runtime.Object)
if err := ensureSCCExists(context.TODO(), args.Logger, args.Client, args.Namespace, common.ControllerServiceAccountName, common.CronJobServiceAccountName); err != nil {
args.Recorder.Event(cr, corev1.EventTypeWarning, createResourceFailed, fmt.Sprintf("Failed to ensure SecurityContextConstraint exists, %v", err))
return err
}
args.Recorder.Event(cr, corev1.EventTypeNormal, createResourceSuccess, "Successfully ensured SecurityContextConstraint exists")
return nil
}
func reconcileCreatePrometheusInfra(args *callbacks.ReconcileCallbackArgs) error {
if args.State != callbacks.ReconcileStatePostRead {
return nil
}
deployment := args.CurrentObject.(*appsv1.Deployment)
// we don't check sdk.CheckDeploymentReady(deployment) since we want Prometheus to cover NotReady state as well
if !isControllerDeployment(deployment) {
return nil
}
cr := args.Resource.(runtime.Object)
namespace := deployment.GetNamespace()
if namespace == "" {
return fmt.Errorf("cluster scoped owner not supported")
}
if deployed, err := isPrometheusDeployed(args.Logger, args.Client, namespace); err != nil {
return err
} else if !deployed {
return nil
}
if err := ensurePrometheusResourcesExist(context.TODO(), args.Client, args.Scheme, deployment); err != nil {
args.Recorder.Event(cr, corev1.EventTypeWarning, createResourceFailed, fmt.Sprintf("Failed to ensure prometheus resources exists, %v", err))
return err
}
return nil
}
func deleteWorkerResources(l logr.Logger, c client.Client) error {
listTypes := []client.ObjectList{&corev1.PodList{}, &corev1.ServiceList{}}
ls, err := labels.Parse(fmt.Sprintf("cdi.kubevirt.io in (%s, %s, %s)",
common.ImporterPodName, common.UploadServerCDILabel, common.ClonerSourcePodName))
if err != nil {
return err
}
for _, lt := range listTypes {
lo := &client.ListOptions{
LabelSelector: ls,
}
if err := cc.BulkDeleteResources(context.TODO(), c, lt, lo); err != nil {
return err
}
}
return nil
}
func reconcileSetConfigAuthority(args *callbacks.ReconcileCallbackArgs) error {
if args.State != callbacks.ReconcileStatePostRead {
return nil
}
crd := args.CurrentObject.(*extv1.CustomResourceDefinition)
if crd.Name != "cdiconfigs.cdi.kubevirt.io" {
return nil
}
cdi, ok := args.Resource.(*cdiv1.CDI)
if !ok {
return nil
}
if _, ok = cdi.Annotations[cdicontroller.AnnConfigAuthority]; ok {
return nil
}
if cdi.Spec.Config == nil {
cl := &cdiv1.CDIConfigList{}
err := args.Client.List(context.TODO(), cl)
if err != nil {
if meta.IsNoMatchError(err) {
return nil
}
return err
}
if len(cl.Items) != 1 {
return nil
}
cs := cl.Items[0].Spec.DeepCopy()
if !reflect.DeepEqual(cs, &cdiv1.CDIConfigSpec{}) {
cdi.Spec.Config = cs
}
}
if cdi.Annotations == nil {
cdi.Annotations = map[string]string{}
}
cdi.Annotations[cdicontroller.AnnConfigAuthority] = ""
return args.Client.Update(context.TODO(), cdi)
}
func getSpecVersion(version string, crd *extv1.CustomResourceDefinition) *extv1.CustomResourceDefinitionVersion {
for _, v := range crd.Spec.Versions {
if v.Name == version {
return &v
}
}
return nil
}
func rewriteOldObjects(args *callbacks.ReconcileCallbackArgs, version string, crd *extv1.CustomResourceDefinition) error {
args.Logger.Info("Rewriting old objects")
kind := crd.Spec.Names.Kind
gvk := schema.GroupVersionKind{
Group: crd.Spec.Group,
Version: version,
Kind: kind,
}
ul := &unstructured.UnstructuredList{}
ul.SetGroupVersionKind(gvk)
err := args.Client.List(context.TODO(), ul, &client.ListOptions{})
if err != nil {
return err
}
for _, item := range ul.Items {
nn := client.ObjectKey{Namespace: item.GetNamespace(), Name: item.GetName()}
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(item.GetObjectKind().GroupVersionKind())
err = args.Client.Get(context.TODO(), nn, u)
if err != nil {
return err
}
err = args.Client.Update(context.TODO(), u)
if err != nil {
return err
}
}
return nil
}
func removeStoredVersion(args *callbacks.ReconcileCallbackArgs, desiredVersion string, crd *extv1.CustomResourceDefinition) error {
args.Logger.Info("Removing stored version")
crd.Status.StoredVersions = []string{desiredVersion}
return args.Client.Status().Update(context.TODO(), crd)
}
// Handle upgrade from clusters that had v1alpha1 as a storage version
// and remove it from all CRDs managed by us
func reconcileHandleOldVersion(args *callbacks.ReconcileCallbackArgs) error {
if args.State != callbacks.ReconcileStatePostRead {
return nil
}
currentCrd := args.CurrentObject.(*extv1.CustomResourceDefinition)
desiredCrd := args.DesiredObject.(*extv1.CustomResourceDefinition)
desiredVersion := newestVersion(desiredCrd)
if olderVersionsExist(desiredVersion, currentCrd) {
restoreOlderVersions(currentCrd, desiredCrd)
if !desiredIsStorage(desiredVersion, currentCrd) {
// Let kubernetes add it
return nil
}
if err := rewriteOldObjects(args, desiredVersion, currentCrd); err != nil {
return err
}
if err := removeStoredVersion(args, desiredVersion, currentCrd); err != nil {
return err
}
}
return nil
}
func olderVersionsExist(desiredVersion string, crd *extv1.CustomResourceDefinition) bool {
for _, version := range crd.Status.StoredVersions {
if version != desiredVersion {
return true
}
}
return false
}
func desiredIsStorage(desiredVersion string, crd *extv1.CustomResourceDefinition) bool {
specVersion := getSpecVersion(desiredVersion, crd)
return specVersion != nil && specVersion.Storage
}
func newestVersion(crd *extv1.CustomResourceDefinition) string {
orderedVersions := []string{"v1", "v1beta1", "v1alpha1"}
for _, version := range orderedVersions {
specVersion := getSpecVersion(version, crd)
if specVersion != nil {
return version
}
}
return ""
}
// Merge both old and new versions into new CRD, so we have both in the desiredCrd object
func restoreOlderVersions(currentCrd, desiredCrd *extv1.CustomResourceDefinition) *extv1.CustomResourceDefinition {
for _, version := range currentCrd.Status.StoredVersions {
specVersion := getSpecVersion(version, desiredCrd)
if specVersion == nil {
// Not available in desired CRD, restore from current CRD
specVersion := getSpecVersion(version, currentCrd)
// We are only allowed one storage version
// The desired CRD already has one
specVersion.Storage = false
desiredCrd.Spec.Versions = append(desiredCrd.Spec.Versions, *specVersion)
}
}
return desiredCrd
}