-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_controller.go
337 lines (275 loc) · 11.4 KB
/
resource_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
334
335
336
337
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package controllers
import (
"context"
"errors"
"fmt"
"strings"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions"
"github.com/fluxcd/pkg/runtime/patch"
rreconcile "github.com/fluxcd/pkg/runtime/reconcile"
"github.com/open-component-model/ocm-controller/api/v1alpha1"
"github.com/open-component-model/ocm-controller/pkg/cache"
"github.com/open-component-model/ocm-controller/pkg/component"
"github.com/open-component-model/ocm-controller/pkg/metrics"
"github.com/open-component-model/ocm-controller/pkg/ocm"
"github.com/open-component-model/ocm-controller/pkg/snapshot"
"github.com/open-component-model/ocm-controller/pkg/status"
ocmmetav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
mh "github.com/open-component-model/pkg/metrics"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kuberecorder "k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// ResourceReconciler reconciles a Resource object.
type ResourceReconciler struct {
client.Client
Scheme *runtime.Scheme
kuberecorder.EventRecorder
OCMClient ocm.Contract
Cache cache.Cache
}
// +kubebuilder:rbac:groups=delivery.ocm.software,resources=resources,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=delivery.ocm.software,resources=resources/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=delivery.ocm.software,resources=resources/finalizers,verbs=update
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
// SetupWithManager sets up the controller with the Manager.
func (r *ResourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
const (
resourceKey = ".metadata.resource"
)
if err := mgr.GetFieldIndexer().IndexField(context.TODO(), &v1alpha1.Resource{}, resourceKey, func(rawObj client.Object) []string {
res, ok := rawObj.(*v1alpha1.Resource)
if !ok {
return nil
}
ns := res.Spec.SourceRef.Namespace
if ns == "" {
ns = res.GetNamespace()
}
return []string{fmt.Sprintf("%s/%s", ns, res.Spec.SourceRef.Name)}
}); err != nil {
return fmt.Errorf("failed setting index fields: %w", err)
}
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.Resource{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Watches(
&v1alpha1.ComponentVersion{},
handler.EnqueueRequestsFromMapFunc(r.findObjects(resourceKey)),
builder.WithPredicates(ComponentVersionChangedPredicate{}),
).
Complete(r)
}
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *ResourceReconciler) Reconcile(
ctx context.Context,
req ctrl.Request,
) (result ctrl.Result, err error) {
obj := &v1alpha1.Resource{}
if err = r.Client.Get(ctx, req.NamespacedName, obj); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, fmt.Errorf("failed to get resource object: %w", err)
}
if obj.Spec.Suspend {
return result, nil
}
if obj.Spec.SourceRef.ResourceRef == nil {
return ctrl.Result{}, fmt.Errorf("resource requires resource ref")
}
patchHelper := patch.NewSerialPatcher(obj, r.Client)
// Always attempt to patch the object and status after each reconciliation.
defer func() {
if derr := status.UpdateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter(), err); derr != nil {
err = errors.Join(err, derr)
}
if err != nil {
metrics.ResourceReconcileFailed.WithLabelValues(obj.Name).Inc()
}
}()
// Starts the progression by setting ReconcilingCondition.
// This will be checked in defer.
// Should only be deleted on a success.
rreconcile.ProgressiveStatus(false, obj, meta.ProgressingReason, "reconciliation in progress for resource: %s", obj.Name)
// if the snapshot name has not been generated then
// generate, patch the status and requeue
if obj.GetSnapshotName() == "" {
name, err := snapshot.GenerateSnapshotName(obj.GetName())
if err != nil {
err = fmt.Errorf("failed to generate snapshot name for: %s: %w", obj.GetName(), err)
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error())
return ctrl.Result{}, err
}
obj.Status.SnapshotName = name
return ctrl.Result{Requeue: true}, nil
}
return r.reconcile(ctx, obj)
}
func (r *ResourceReconciler) reconcile(
ctx context.Context,
obj *v1alpha1.Resource,
) (ctrl.Result, error) {
if obj.Generation != obj.Status.ObservedGeneration {
rreconcile.ProgressiveStatus(
false,
obj,
meta.ProgressingReason,
"processing object: new generation %d -> %d",
obj.Status.ObservedGeneration,
obj.Generation,
)
}
if obj.Spec.SourceRef.Namespace == "" {
obj.Spec.SourceRef.Namespace = obj.GetNamespace()
}
if obj.GetSnapshotName() == "" {
err := errors.New("snapshot name should not be empty")
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.SnapshotNameEmptyReason, err.Error())
return ctrl.Result{}, err
}
componentVersion := &v1alpha1.ComponentVersion{}
if err := r.Get(ctx, obj.Spec.SourceRef.GetObjectKey(), componentVersion); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
}
err = fmt.Errorf("failed to get component version: %w", err)
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ComponentVersionNotFoundReason, err.Error())
return ctrl.Result{}, err
}
if !conditions.IsReady(componentVersion) || componentVersion.GetRepositoryURL() == "" {
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ComponentVersionNotReadyReason, "component version not ready yet")
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
}
rreconcile.ProgressiveStatus(false, obj, meta.ProgressingReason, "component version %s ready, processing ocm resource", componentVersion.Name)
octx, err := r.OCMClient.CreateAuthenticatedOCMContext(ctx, componentVersion)
if err != nil {
err = fmt.Errorf("failed to create authenticated client: %w", err)
status.MarkAsStalled(r.EventRecorder, obj, v1alpha1.AuthenticatedContextCreationFailedReason, err.Error())
return ctrl.Result{}, nil
}
reader, digest, size, err := r.OCMClient.GetResource(ctx, octx, componentVersion, obj.Spec.SourceRef.ResourceRef)
if err != nil {
err = fmt.Errorf("failed to get resource: %w", err)
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.GetResourceFailedReason, err.Error())
return ctrl.Result{}, err
}
// The reader is unused here, but we should still close it, so it's not left over.
defer reader.Close()
version := "latest"
// GetVersion returns resourceRef.Version
if obj.Spec.SourceRef.GetVersion() != "" {
version = obj.Spec.SourceRef.GetVersion()
}
// This is important because THIS is the actual component for our resource. If we used ComponentVersion in the
// below identity, that would be the top-level component instead of the component that this resource belongs to.
componentDescriptor, err := component.GetComponentDescriptor(ctx, r.Client, obj.GetReferencePath(), componentVersion.Status.ComponentDescriptor)
if err != nil {
err = fmt.Errorf("failed to get component descriptor for resource: %w", err)
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.GetComponentDescriptorFailedReason, err.Error())
return ctrl.Result{}, err
}
if componentDescriptor == nil {
err := fmt.Errorf(
"couldn't find component descriptor for reference '%s' or any root components",
obj.GetReferencePath(),
)
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ComponentDescriptorNotFoundReason, err.Error())
return ctrl.Result{}, err
}
rreconcile.ProgressiveStatus(false, obj, meta.ProgressingReason, "resource retrieve, constructing snapshot with name %s", obj.GetSnapshotName())
identity := r.constructIdentity(componentDescriptor, obj, version)
snapshotCR := &v1alpha1.Snapshot{
ObjectMeta: metav1.ObjectMeta{
Namespace: obj.GetNamespace(),
Name: obj.GetSnapshotName(),
},
}
_, err = controllerutil.CreateOrUpdate(ctx, r.Client, snapshotCR, func() error {
if snapshotCR.ObjectMeta.CreationTimestamp.IsZero() {
if err := controllerutil.SetOwnerReference(obj, snapshotCR, r.Scheme); err != nil {
return fmt.Errorf("failed to set owner to snapshot object: %w", err)
}
}
// At this point the version is the ResourceRef.Version or `latest`
snapshotCR.Spec = v1alpha1.SnapshotSpec{
Identity: identity,
Digest: digest,
Tag: version,
}
return nil
})
if err != nil {
err = fmt.Errorf("failed to create or update snapshot: %w", err)
status.MarkNotReady(r.EventRecorder, obj, v1alpha1.CreateOrUpdateSnapshotFailedReason, err.Error())
return ctrl.Result{}, err
}
obj.Status.LastAppliedResourceVersion = obj.Spec.SourceRef.GetVersion()
obj.Status.LastAppliedComponentVersion = componentVersion.Status.ReconciledVersion
metrics.SnapshotNumberOfBytesReconciled.WithLabelValues(obj.GetSnapshotName(), digest, componentVersion.Name).Set(float64(size))
metrics.ResourceReconcileSuccess.WithLabelValues(obj.Name).Inc()
if product := IsProductOwned(obj); product != "" {
metrics.MPASResourceReconciledStatus.WithLabelValues(product, mh.MPASStatusSuccess).Inc()
}
status.MarkReady(r.EventRecorder, obj, "Applied version: %s", obj.Status.LastAppliedComponentVersion)
return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil
}
func (r *ResourceReconciler) constructIdentity(
componentDescriptor *v1alpha1.ComponentDescriptor,
obj *v1alpha1.Resource,
version string,
) ocmmetav1.Identity {
identity := ocmmetav1.Identity{
v1alpha1.ComponentNameKey: componentDescriptor.Name,
v1alpha1.ComponentVersionKey: componentDescriptor.Spec.Version,
v1alpha1.ResourceNameKey: obj.Spec.SourceRef.ResourceRef.Name,
v1alpha1.ResourceVersionKey: version,
}
for k, v := range obj.Spec.SourceRef.ResourceRef.ExtraIdentity {
identity[k] = v
}
if len(obj.Spec.SourceRef.ResourceRef.ReferencePath) > 0 {
var builder strings.Builder
for _, path := range obj.Spec.SourceRef.ResourceRef.ReferencePath {
builder.WriteString(path.String() + ":")
}
identity[v1alpha1.ResourceRefPath] = builder.String()
}
return identity
}
// this function will enqueue a reconciliation for any snapshot which is referenced
// in the .spec.sourceRef or spec.configRef field of a Localization.
func (r *ResourceReconciler) findObjects(key string) handler.MapFunc {
return func(ctx context.Context, obj client.Object) []reconcile.Request {
resources := &v1alpha1.ResourceList{}
if err := r.List(ctx, resources, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(key, client.ObjectKeyFromObject(obj).String()),
}); err != nil {
return []reconcile.Request{}
}
requests := make([]reconcile.Request, len(resources.Items))
for i, item := range resources.Items {
requests[i] = reconcile.Request{
NamespacedName: types.NamespacedName{
Name: item.GetName(),
Namespace: item.GetNamespace(),
},
}
}
return requests
}
}