• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opendefensecloud / artifact-conduit / 19498774385

19 Nov 2025 10:52AM UTC coverage: 55.268% (+5.3%) from 49.926%
19498774385

Pull #38

github

web-flow
Merge 3ef2c6afb into ec3ffa8e1
Pull Request #38: Refactor Fragment into ArtifactWorkflow

204 of 244 new or added lines in 4 files covered. (83.61%)

3 existing lines in 1 file now uncovered.

299 of 541 relevant lines covered (55.27%)

12.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.61
/pkg/controller/order_controller.go
1
// Copyright 2025 BWI GmbH and Artifact Conduit contributors
2
// SPDX-License-Identifier: Apache-2.0
3

4
package controller
5

6
import (
7
        "context"
8
        "crypto/sha256"
9
        "encoding/hex"
10
        "encoding/json"
11
        "fmt"
12
        "slices"
13
        "strconv"
14
        "strings"
15

16
        arcv1alpha1 "go.opendefense.cloud/arc/api/arc/v1alpha1"
17
        corev1 "k8s.io/api/core/v1"
18
        apierrors "k8s.io/apimachinery/pkg/api/errors"
19
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20
        "k8s.io/apimachinery/pkg/fields"
21
        "k8s.io/apimachinery/pkg/runtime"
22
        "k8s.io/apimachinery/pkg/types"
23
        ctrl "sigs.k8s.io/controller-runtime"
24
        "sigs.k8s.io/controller-runtime/pkg/builder"
25
        "sigs.k8s.io/controller-runtime/pkg/client"
26
        "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
27
        "sigs.k8s.io/controller-runtime/pkg/handler"
28
        "sigs.k8s.io/controller-runtime/pkg/predicate"
29
        "sigs.k8s.io/controller-runtime/pkg/reconcile"
30
)
31

32
const (
33
        orderFinalizer          = "arc.bwi.de/order-finalizer"
34
        workflowConfigSecretKey = "config.json"
35
)
36

37
// OrderReconciler reconciles a Order object
38
type OrderReconciler struct {
39
        client.Client
40
        Scheme *runtime.Scheme
41
}
42

43
type desiredAW struct {
44
        index       int
45
        objectMeta  metav1.ObjectMeta
46
        artifact    *arcv1alpha1.OrderArtifact
47
        srcEndpoint *arcv1alpha1.Endpoint
48
        dstEndpoint *arcv1alpha1.Endpoint
49
        srcSecret   *corev1.Secret
50
        dstSecret   *corev1.Secret
51
}
52

53
//+kubebuilder:rbac:groups=arc.bwi.de,resources=endpoints,verbs=get;list;watch
54
//+kubebuilder:rbac:groups=arc.bwi.de,resources=artifactworkflows,verbs=get;list;watch;create;update;patch;delete
55
//+kubebuilder:rbac:groups=arc.bwi.de,resources=orders,verbs=get;list;watch;create;update;patch;delete
56
//+kubebuilder:rbac:groups=arc.bwi.de,resources=orders/status,verbs=get;update;patch
57
//+kubebuilder:rbac:groups=arc.bwi.de,resources=orders/finalizers,verbs=update
58
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
59

60
// Reconcile moves the current state of the cluster closer to the desired state
61
func (r *OrderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
35✔
62
        log := ctrl.LoggerFrom(ctx)
35✔
63

35✔
64
        // Fetch the Order instance
35✔
65
        order := &arcv1alpha1.Order{}
35✔
66
        if err := r.Get(ctx, req.NamespacedName, order); err != nil {
37✔
67
                if apierrors.IsNotFound(err) {
4✔
68
                        // Object not found, return. Created objects are automatically garbage collected.
2✔
69
                        return ctrl.Result{}, nil
2✔
70
                }
2✔
NEW
71
                return ctrl.Result{}, errLogAndWrap(log, err, "failed to get object")
×
72
        }
73

74
        // Handle deletion: cleanup fragments, then remove finalizer
75
        if !order.DeletionTimestamp.IsZero() {
35✔
76
                log.V(1).Info("Order is being deleted")
2✔
77
                if len(order.Status.ArtifactWorkflows) > 0 {
3✔
78
                        for sha := range order.Status.ArtifactWorkflows {
3✔
79
                                // Remove Secret and ArtifactWorkflow
2✔
80
                                aw := &arcv1alpha1.ArtifactWorkflow{
2✔
81
                                        ObjectMeta: awObjectMeta(order, sha),
2✔
82
                                }
2✔
83
                                _ = r.Delete(ctx, aw) // Ignore errors
2✔
84
                                delete(order.Status.ArtifactWorkflows, sha)
2✔
85
                        }
2✔
86
                        if err := r.Status().Update(ctx, order); err != nil {
1✔
NEW
87
                                return ctrl.Result{}, errLogAndWrap(log, err, "failed to update order status")
×
88
                        }
×
89
                        log.V(1).Info("Order artifact workflows cleaned up")
1✔
90
                        // Requeue until all fragments are gone
1✔
91
                        return ctrl.Result{Requeue: true}, nil
1✔
92
                }
93
                // All fragments are gone, remove finalizer
94
                if slices.Contains(order.Finalizers, orderFinalizer) {
2✔
95
                        log.V(1).Info("No artifact workflows, removing finalizer from Order")
1✔
96
                        order.Finalizers = slices.DeleteFunc(order.Finalizers, func(f string) bool {
2✔
97
                                return f == orderFinalizer
1✔
98
                        })
1✔
99
                        if err := r.Update(ctx, order); err != nil {
1✔
NEW
100
                                return ctrl.Result{}, errLogAndWrap(log, err, "failed to remove finalizer")
×
101
                        }
×
102
                }
103
                return ctrl.Result{}, nil
1✔
104
        }
105

106
        // Add finalizer if not present and not deleting
107
        if order.DeletionTimestamp.IsZero() {
62✔
108
                if !slices.Contains(order.Finalizers, orderFinalizer) {
38✔
109
                        log.V(1).Info("Adding finalizer to Order")
7✔
110
                        order.Finalizers = append(order.Finalizers, orderFinalizer)
7✔
111
                        if err := r.Update(ctx, order); err != nil {
7✔
NEW
112
                                return ctrl.Result{}, errLogAndWrap(log, err, "failed to add finalizer")
×
113
                        }
×
114
                        // Return without requeue; the Update event will trigger reconciliation again
115
                        return ctrl.Result{}, nil
7✔
116
                }
117
        }
118

119
        // Before we compare to our status, let's fetch all necessary information
120
        // to compute desired state:
121
        desiredAWs := map[string]desiredAW{}
24✔
122
        for i, artifact := range order.Spec.Artifacts {
68✔
123
                log := log.WithValues("artifactIndex", i)
44✔
124

44✔
125
                // We need the referenced src- and dst-endpoints for the artifact
44✔
126
                srcRefName := artifact.SrcRef.Name
44✔
127
                if srcRefName == "" {
52✔
128
                        srcRefName = order.Spec.Defaults.SrcRef.Name
8✔
129
                }
8✔
130
                dstRefName := artifact.DstRef.Name
44✔
131
                if dstRefName == "" {
56✔
132
                        dstRefName = order.Spec.Defaults.DstRef.Name
12✔
133
                }
12✔
134
                srcEndpoint := &arcv1alpha1.Endpoint{}
44✔
135
                if err := r.Get(ctx, namespacedName(order.Namespace, srcRefName), srcEndpoint); err != nil {
44✔
NEW
136
                        // TODO: should we set status to something and not error here?
×
NEW
137
                        return ctrl.Result{}, errLogAndWrap(log, err, "failed to fetch endpoint for source")
×
UNCOV
138
                }
×
139
                dstEndpoint := &arcv1alpha1.Endpoint{}
44✔
140
                if err := r.Get(ctx, namespacedName(order.Namespace, dstRefName), dstEndpoint); err != nil {
44✔
NEW
141
                        // TODO: should we set status to something and not error here?
×
NEW
142
                        return ctrl.Result{}, errLogAndWrap(log, err, "failed to fetch endpoint for destination")
×
NEW
143
                }
×
144

145
                // Next, we need the secret contents
146
                // TODO: When a secret fetch fails, we stop the reconciliation of the whole order.
147
                //       Should we instead not fail but skip invalid artifacts?
148
                srcSecret := &corev1.Secret{}
44✔
149
                if srcEndpoint.Spec.SecretRef.Name != "" {
85✔
150
                        if err := r.Get(ctx, namespacedName(order.Namespace, srcEndpoint.Spec.SecretRef.Name), srcSecret); err != nil {
41✔
NEW
151
                                return ctrl.Result{}, errLogAndWrap(log, err, "failed to fetch secret for source")
×
NEW
152
                        }
×
153
                }
154

155
                dstSecret := &corev1.Secret{}
44✔
156
                if srcEndpoint.Spec.SecretRef.Name != "" {
85✔
157
                        if err := r.Get(ctx, namespacedName(order.Namespace, dstEndpoint.Spec.SecretRef.Name), dstSecret); err != nil {
41✔
NEW
158
                                return ctrl.Result{}, errLogAndWrap(log, err, "failed to fetch secret for destination")
×
NEW
159
                        }
×
160
                }
161

162
                // Create a hash based on all related data for idempotency and compute the workflow name
163
                h := sha256.New()
44✔
164
                data := []any{
44✔
165
                        order.Namespace,
44✔
166
                        artifact.Type, artifact.Spec.Raw,
44✔
167
                        srcEndpoint.Name, srcEndpoint.Generation,
44✔
168
                        dstEndpoint.Name, dstEndpoint.Generation,
44✔
169
                        srcSecret.Name, srcSecret.Generation,
44✔
170
                        dstSecret.Name, dstSecret.Generation,
44✔
171
                }
44✔
172
                jsonData, err := json.Marshal(data)
44✔
173
                if err != nil {
44✔
NEW
174
                        return ctrl.Result{}, errLogAndWrap(log, err, "failed to marshal fragment data")
×
175
                }
×
176
                h.Write(jsonData)
44✔
177
                sha := hex.EncodeToString(h.Sum(nil))[:16]
44✔
178

44✔
179
                // We gave all the information to furhter process this artifact workflow.
44✔
180
                // Let's store it to compare it to the current status!
44✔
181
                desiredAWs[sha] = desiredAW{
44✔
182
                        index:       i,
44✔
183
                        objectMeta:  awObjectMeta(order, sha),
44✔
184
                        artifact:    &artifact,
44✔
185
                        srcEndpoint: srcEndpoint,
44✔
186
                        dstEndpoint: dstEndpoint,
44✔
187
                        srcSecret:   srcSecret,
44✔
188
                        dstSecret:   dstSecret,
44✔
189
                }
44✔
190
        }
191

192
        // List missing fragments
193
        createAWs := []string{}
24✔
194
        for sha := range desiredAWs {
68✔
195
                _, exists := order.Status.ArtifactWorkflows[sha]
44✔
196
                if exists {
73✔
197
                        continue
29✔
198
                }
199
                createAWs = append(createAWs, sha)
15✔
200
        }
201

202
        // Make sure status is initialized
203
        if order.Status.ArtifactWorkflows == nil {
31✔
204
                order.Status.ArtifactWorkflows = map[string]arcv1alpha1.OrderArtifactWorkflowStatus{}
7✔
205
        }
7✔
206

207
        // Find obsolete fragments
208
        deleteAWs := []string{}
24✔
209
        for sha := range order.Status.ArtifactWorkflows {
54✔
210
                _, exists := desiredAWs[sha]
30✔
211
                if exists {
59✔
212
                        continue
29✔
213
                }
214
                deleteAWs = append(deleteAWs, sha)
1✔
215
        }
216

217
        // Create missing fragments
218
        for _, sha := range createAWs {
39✔
219
                daw := desiredAWs[sha]
15✔
220
                aw, err := r.hydrateArtifactWorkflow(&daw)
15✔
221
                if err != nil {
15✔
NEW
222
                        return ctrl.Result{}, errLogAndWrap(log, err, "failed to hydrate artifact workflow")
×
NEW
223
                }
×
224

225
                // Set owner references
226
                if err := controllerutil.SetControllerReference(order, aw, r.Scheme); err != nil {
15✔
NEW
227
                        return ctrl.Result{}, errLogAndWrap(log, err, "failed to set controller reference")
×
UNCOV
228
                }
×
229

230
                // Create artifact workflow
231
                if err := r.Create(ctx, aw); err != nil {
16✔
232
                        if apierrors.IsAlreadyExists(err) {
2✔
233
                                // Already created by a previous reconcile — that's fine
1✔
234
                                continue
1✔
235
                        }
NEW
236
                        return ctrl.Result{}, errLogAndWrap(log, err, "failed to create artifact workflow")
×
237
                }
238

239
                // Update status
240
                order.Status.ArtifactWorkflows[sha] = arcv1alpha1.OrderArtifactWorkflowStatus{
14✔
241
                        ArtifactIndex: daw.index,
14✔
242
                        Phase:         arcv1alpha1.WorkflowPhaseInit,
14✔
243
                }
14✔
244
        }
245

246
        // Delete obsolete fragments
247
        for _, sha := range deleteAWs {
25✔
248
                // Does not exist anymore, let's clean up!
1✔
249
                if err := r.Delete(ctx, &arcv1alpha1.ArtifactWorkflow{
1✔
250
                        ObjectMeta: awObjectMeta(order, sha),
1✔
251
                }); client.IgnoreNotFound(err) != nil {
1✔
NEW
252
                        return ctrl.Result{}, errLogAndWrap(log, err, "failed to delete artifact workflow")
×
UNCOV
253
                }
×
254

255
                // Update status
256
                delete(order.Status.ArtifactWorkflows, sha)
1✔
257
        }
258

259
        // Update status
260
        if len(createAWs) > 0 || len(deleteAWs) > 0 {
34✔
261
                log.V(1).Info("Updating Order.Status")
10✔
262
                if err := r.Status().Update(ctx, order); err != nil {
11✔
263
                        return ctrl.Result{}, errLogAndWrap(log, err, "failed to update status")
1✔
264
                }
1✔
265
        }
266

267
        return ctrl.Result{}, nil
23✔
268
}
269

270
func (r *OrderReconciler) hydrateArtifactWorkflow(daw *desiredAW) (*arcv1alpha1.ArtifactWorkflow, error) {
15✔
271
        params, err := dawToParameters(daw)
15✔
272
        if err != nil {
15✔
NEW
273
                return nil, err
×
NEW
274
        }
×
275

276
        // Next we create the ArtifactWorkflow instance
277
        aw := &arcv1alpha1.ArtifactWorkflow{
15✔
278
                ObjectMeta: daw.objectMeta,
15✔
279
                Spec: arcv1alpha1.ArtifactWorkflowSpec{
15✔
280
                        Type:         daw.artifact.Type,
15✔
281
                        Parameters:   params,
15✔
282
                        SrcSecretRef: daw.srcEndpoint.Spec.SecretRef,
15✔
283
                        DstSecretRef: daw.dstEndpoint.Spec.SecretRef,
15✔
284
                },
15✔
285
        }
15✔
286

15✔
287
        return aw, nil
15✔
288
}
289

290
// generateReconcileRequestsForEndpoint generates reconcile requests for all Endpoints referenced by an Order
291
func (r *OrderReconciler) generateReconcileRequestsForEndpoint(ctx context.Context, endpoint client.Object) []reconcile.Request {
23✔
292
        resourcesReferencingEndpoint := &arcv1alpha1.OrderList{}
23✔
293
        listOps := &client.ListOptions{
23✔
294
                FieldSelector: fields.SelectorFromSet(fields.Set{".spec.srcRef.name": endpoint.GetName(), ".spec.dstRef.name": endpoint.GetName()}),
23✔
295
                Namespace:     endpoint.GetNamespace(),
23✔
296
        }
23✔
297
        err := r.List(ctx, resourcesReferencingEndpoint, listOps)
23✔
298
        if err != nil {
46✔
299
                return []reconcile.Request{}
23✔
300
        }
23✔
301

NEW
302
        requests := make([]reconcile.Request, len(resourcesReferencingEndpoint.Items))
×
NEW
303
        for i, item := range resourcesReferencingEndpoint.Items {
×
304
                log := ctrl.LoggerFrom(ctx)
×
NEW
305
                log.V(1).Info("Generating reconcile request for resource because referenced endpoint has changed...")
×
306
                requests[i] = reconcile.Request{
×
307
                        NamespacedName: types.NamespacedName{
×
308
                                Name:      item.GetName(),
×
309
                                Namespace: item.GetNamespace(),
×
310
                        },
×
311
                }
×
312
        }
×
313
        return requests
×
314
}
315

316
// SetupWithManager sets up the controller with the Manager.
317
func (r *OrderReconciler) SetupWithManager(mgr ctrl.Manager) error {
1✔
318
        return ctrl.NewControllerManagedBy(mgr).
1✔
319
                For(&arcv1alpha1.Order{}).
1✔
320
                Watches(
1✔
321
                        &arcv1alpha1.Endpoint{},
1✔
322
                        handler.EnqueueRequestsFromMapFunc(r.generateReconcileRequestsForEndpoint),
1✔
323
                        builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
1✔
324
                ).
1✔
325
                Owns(&arcv1alpha1.ArtifactWorkflow{}).
1✔
326
                Complete(r)
1✔
327
}
1✔
328

329
func namespacedName(namespace, name string) types.NamespacedName {
170✔
330
        return types.NamespacedName{
170✔
331
                Namespace: namespace,
170✔
332
                Name:      name,
170✔
333
        }
170✔
334
}
170✔
335

336
func awName(order *arcv1alpha1.Order, sha string) string {
47✔
337
        return fmt.Sprintf("%s-%s", order.Name, sha)
47✔
338
}
47✔
339

340
func awObjectMeta(order *arcv1alpha1.Order, sha string) metav1.ObjectMeta {
47✔
341
        return metav1.ObjectMeta{
47✔
342
                Namespace: order.Namespace,
47✔
343
                Name:      awName(order, sha),
47✔
344
        }
47✔
345
}
47✔
346

347
// TODO: add unit tests
348
func dawToParameters(daw *desiredAW) ([]arcv1alpha1.ArtifactWorkflowParameter, error) {
15✔
349
        params := []arcv1alpha1.ArtifactWorkflowParameter{
15✔
350
                {
15✔
351
                        Name:  paramName("src", "type"),
15✔
352
                        Value: string(daw.srcEndpoint.Spec.Type),
15✔
353
                },
15✔
354
                {
15✔
355
                        Name:  paramName("src", "remoteURL"),
15✔
356
                        Value: daw.srcEndpoint.Spec.RemoteURL,
15✔
357
                },
15✔
358
                {
15✔
359
                        Name:  paramName("dst", "type"),
15✔
360
                        Value: string(daw.dstEndpoint.Spec.Type),
15✔
361
                },
15✔
362
                {
15✔
363
                        Name:  paramName("dst", "remoteURL"),
15✔
364
                        Value: daw.dstEndpoint.Spec.RemoteURL,
15✔
365
                },
15✔
366
                {
15✔
367
                        Name:  "srcSecret",
15✔
368
                        Value: fmt.Sprintf("%v", daw.srcSecret.Name != ""),
15✔
369
                },
15✔
370
                {
15✔
371
                        Name:  "dstSecret",
15✔
372
                        Value: fmt.Sprintf("%v", daw.dstSecret.Name != ""),
15✔
373
                },
15✔
374
        }
15✔
375

15✔
376
        spec := map[string]any{}
15✔
377
        raw := daw.artifact.Spec.Raw
15✔
378
        if len(raw) == 0 {
22✔
379
                raw = []byte("{}")
7✔
380
        }
7✔
381
        if err := json.Unmarshal(raw, &spec); err != nil {
15✔
NEW
382
                return nil, err
×
NEW
383
        }
×
384
        flattened := map[string]any{}
15✔
385
        flattenMap("spec", spec, flattened)
15✔
386
        for name, value := range flattened {
23✔
387
                params = append(params, arcv1alpha1.ArtifactWorkflowParameter{
8✔
388
                        Name:  name,
8✔
389
                        Value: fmt.Sprintf("%v", value),
8✔
390
                })
8✔
391
        }
8✔
392

393
        return params, nil
15✔
394
}
395

396
// TODO: add unit tests
397
func paramName(prefix, suffix string) string {
60✔
398
        return prefix + strings.ToUpper(suffix[:1]) + suffix[1:]
60✔
399
}
60✔
400

401
// TODO: add unit tests
402
func flattenMap(prefix string, src map[string]any, dst map[string]any) {
15✔
403
        for k, v := range src {
23✔
404
                kt := strings.ToUpper(k[:1]) + k[1:]
8✔
405
                switch child := v.(type) {
8✔
NEW
406
                case map[string]any:
×
NEW
407
                        flattenMap(prefix+k, child, dst)
×
NEW
408
                case []any:
×
NEW
409
                        for i, av := range child {
×
NEW
410
                                dst[prefix+kt+strconv.Itoa(i)] = av
×
NEW
411
                        }
×
412
                default:
8✔
413
                        dst[prefix+kt] = v
8✔
414
                }
415
        }
416
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc