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

opendefensecloud / artifact-conduit / 21704304780

05 Feb 2026 08:28AM UTC coverage: 83.805% (-0.1%) from 83.918%
21704304780

push

github

web-flow
Update to go 1.25.7 (#194)

740 of 883 relevant lines covered (83.81%)

1183.72 hits per line

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

82.6
/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
        "time"
14

15
        "github.com/go-logr/logr"
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/runtime"
21
        "k8s.io/client-go/tools/record"
22
        ctrl "sigs.k8s.io/controller-runtime"
23
        "sigs.k8s.io/controller-runtime/pkg/client"
24
        "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
25
)
26

27
const (
28
        orderFinalizer = "arc.opendefense.cloud/order-finalizer"
29
)
30

31
// OrderReconciler reconciles a Order object
32
type OrderReconciler struct {
33
        client.Client
34
        Scheme   *runtime.Scheme
35
        Recorder record.EventRecorder
36
}
37

38
type desiredAW struct {
39
        index       int
40
        objectMeta  metav1.ObjectMeta
41
        artifact    *arcv1alpha1.OrderArtifact
42
        typeSpec    *arcv1alpha1.ArtifactTypeSpec
43
        srcEndpoint *arcv1alpha1.Endpoint
44
        dstEndpoint *arcv1alpha1.Endpoint
45
        srcSecret   *corev1.Secret
46
        dstSecret   *corev1.Secret
47
        sha         string
48
        cron        *arcv1alpha1.Cron
49
}
50

51
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=endpoints,verbs=get;list;watch
52
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=artifacttypes,verbs=get;list;watch
53
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=clusterartifacttypes,verbs=get;list;watch
54
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=artifactworkflows,verbs=get;list;watch;create;update;patch;delete
55
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=orders,verbs=get;list;watch;create;update;patch;delete
56
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=orders/status,verbs=get;update;patch
57
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=orders/finalizers,verbs=update
58
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
59
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
60

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

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

76
        // Update last reconcile time
77
        order.Status.LastReconcileAt = metav1.Now()
2,887✔
78

2,887✔
79
        // Handle deletion: cleanup artifact workflows, then remove finalizer
2,887✔
80
        if !order.DeletionTimestamp.IsZero() {
2,890✔
81
                log.V(1).Info("Order is being deleted")
3✔
82
                r.Recorder.Event(order, corev1.EventTypeWarning, "Deleting", "Order is being deleted, cleaning up artifact workflows")
3✔
83

3✔
84
                // Cleanup all artifact workflows
3✔
85
                if len(order.Status.ArtifactWorkflows) > 0 {
5✔
86
                        for sha := range order.Status.ArtifactWorkflows {
6✔
87
                                // Remove ArtifactWorkflow
4✔
88
                                aw := &arcv1alpha1.ArtifactWorkflow{
4✔
89
                                        ObjectMeta: awObjectMeta(order, sha),
4✔
90
                                }
4✔
91
                                _ = r.Delete(ctx, aw) // Ignore errors
4✔
92
                                delete(order.Status.ArtifactWorkflows, sha)
4✔
93
                        }
4✔
94
                        if err := r.Status().Update(ctx, order); err != nil {
3✔
95
                                return ctrlResult, errLogAndWrap(log, err, "failed to update order status")
1✔
96
                        }
1✔
97
                        log.V(1).Info("Order artifact workflows cleaned up")
1✔
98

1✔
99
                        // Requeue until all artifact workflows are gone
1✔
100
                        return ctrlResult, nil
1✔
101
                }
102
                // All artifact workflows are gone, remove finalizer
103
                if slices.Contains(order.Finalizers, orderFinalizer) {
2✔
104
                        log.V(1).Info("No artifact workflows, removing finalizer from Order")
1✔
105
                        order.Finalizers = slices.DeleteFunc(order.Finalizers, func(f string) bool {
2✔
106
                                return f == orderFinalizer
1✔
107
                        })
1✔
108
                        if err := r.Update(ctx, order); err != nil {
1✔
109
                                return ctrlResult, errLogAndWrap(log, err, "failed to remove finalizer")
×
110
                        }
×
111
                }
112
                return ctrlResult, nil
1✔
113
        }
114

115
        // Add finalizer if not present and not deleting
116
        if order.DeletionTimestamp.IsZero() {
5,768✔
117
                if !slices.Contains(order.Finalizers, orderFinalizer) {
2,902✔
118
                        log.V(1).Info("Adding finalizer to Order")
18✔
119
                        order.Finalizers = append(order.Finalizers, orderFinalizer)
18✔
120
                        if err := r.Update(ctx, order); err != nil {
18✔
121
                                return ctrlResult, errLogAndWrap(log, err, "failed to add finalizer")
×
122
                        }
×
123
                        // Return without requeue; the Update event will trigger reconciliation again
124
                        return ctrlResult, nil
18✔
125
                }
126
        }
127

128
        // Handle force reconcile annotation
129
        forceAt, err := GetForceAtAnnotationValue(order)
2,866✔
130
        if err != nil {
2,866✔
131
                log.V(1).Error(err, "Invalid force reconcile annotation, ignoring")
×
132
        }
×
133
        if !forceAt.IsZero() && (order.Status.LastForceAt.IsZero() || forceAt.After(order.Status.LastForceAt.Time)) {
2,867✔
134
                log.V(1).Info("Force reconcile requested")
1✔
135
                r.Recorder.Event(order, corev1.EventTypeNormal, "ForceReconcile", "Force reconcile requested via annotation")
1✔
136
                // Delete existing artifact workflows to force re-creation
1✔
137
                for sha := range order.Status.ArtifactWorkflows {
2✔
138
                        // Remove Secret and ArtifactWorkflow
1✔
139
                        aw := &arcv1alpha1.ArtifactWorkflow{
1✔
140
                                ObjectMeta: awObjectMeta(order, sha),
1✔
141
                        }
1✔
142
                        _ = r.Delete(ctx, aw) // Ignore errors
1✔
143
                        delete(order.Status.ArtifactWorkflows, sha)
1✔
144
                        r.Recorder.Eventf(order, corev1.EventTypeNormal, "ForceReconcile", "Deleted artifact workflow '%s' with sha %s", aw.Name, sha)
1✔
145
                }
1✔
146
                // Update last force time
147
                order.Status.LastForceAt = metav1.Now()
1✔
148
                if err := r.Status().Update(ctx, order); err != nil {
1✔
149
                        return ctrlResult, errLogAndWrap(log, err, "failed to update last force time")
×
150
                }
×
151
                // Return without requeue; the update event will trigger reconciliation again
152
                return ctrlResult, nil
1✔
153
        }
154

155
        // Make sure status is initialized
156
        if order.Status.ArtifactWorkflows == nil {
2,963✔
157
                order.Status.ArtifactWorkflows = map[string]arcv1alpha1.OrderArtifactWorkflowStatus{}
98✔
158
        }
98✔
159

160
        // Before we compare to our status, let's fetch all necessary information
161
        // to compute desired state:
162
        desiredAWs := map[string]desiredAW{}
2,865✔
163
        for i, artifact := range order.Spec.Artifacts {
6,886✔
164
                daw, err := r.computeDesiredAW(ctx, log, order, &artifact, i)
4,021✔
165
                if err != nil {
4,121✔
166
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "ComputationFailed", "Failed to compute desired artifact workflow for artifact index %d: %v", i, err)
100✔
167
                        order.Status.Message = fmt.Sprintf("Failed to compute desired artifact workflow for artifact index %d: %v", i, err)
100✔
168
                        if err := r.Status().Update(ctx, order); err != nil {
100✔
169
                                return ctrlResult, errLogAndWrap(log, err, "failed to update status")
×
170
                        }
×
171
                        return ctrlResult, errLogAndWrap(log, err, "failed to compute desired artifact workflow")
100✔
172
                }
173
                desiredAWs[daw.sha] = *daw
3,921✔
174
        }
175
        order.Status.Message = "" // Clear any previous error message
2,765✔
176

2,765✔
177
        // List missing artifact workflows
2,765✔
178
        var createAWs []string
2,765✔
179
        for sha := range desiredAWs {
6,683✔
180
                if _, exists := order.Status.ArtifactWorkflows[sha]; exists {
7,815✔
181
                        continue
3,897✔
182
                }
183

184
                createAWs = append(createAWs, sha)
21✔
185
        }
186

187
        // Find obsolete artifact workflows
188
        var deleteAWs []string
2,765✔
189
        for sha := range order.Status.ArtifactWorkflows {
6,663✔
190
                if _, exists := desiredAWs[sha]; exists {
7,795✔
191
                        continue
3,897✔
192
                }
193

194
                deleteAWs = append(deleteAWs, sha)
1✔
195
        }
196

197
        // Find finished artifact workflows to clean up
198
        var finishedAWs []string
2,765✔
199
        for sha := range order.Status.ArtifactWorkflows {
6,663✔
200
                awStatus := order.Status.ArtifactWorkflows[sha]
3,898✔
201

3,898✔
202
                // Only consider succeeded workflows for TTL cleanup
3,898✔
203
                if awStatus.Phase != arcv1alpha1.WorkflowSucceeded {
7,693✔
204
                        continue
3,795✔
205
                }
206

207
                // Do not clean up ArtifactWorkflows with cron specified
208
                if daw, ok := desiredAWs[sha]; ok && daw.cron != nil {
103✔
209
                        continue
×
210
                }
211

212
                // If TTL is set, check if it has expired
213
                if order.Spec.TTLSecondsAfterCompletion != nil && *order.Spec.TTLSecondsAfterCompletion > 0 {
103✔
214
                        if time.Since(awStatus.CompletionTime.Time) > time.Duration(*order.Spec.TTLSecondsAfterCompletion)*time.Second {
×
215
                                finishedAWs = append(finishedAWs, sha)
×
216
                        } else {
×
217
                                // Requeue when the next TTL expires
×
218
                                ctrlResult.RequeueAfter = time.Duration((*order.Spec.TTLSecondsAfterCompletion)+1)*time.Second - time.Since(awStatus.CompletionTime.Time)
×
219
                        }
×
220
                        continue
×
221
                }
222

223
                // No TTL set, cleanup immediately
224
                finishedAWs = append(finishedAWs, sha)
103✔
225
        }
226

227
        // Create missing artifact workflows
228
        for _, sha := range createAWs {
2,786✔
229
                daw := desiredAWs[sha]
21✔
230
                aw, err := r.hydrateArtifactWorkflow(&daw)
21✔
231
                if err != nil {
21✔
232
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "HydrationFailed", "Failed to hydrate artifact workflow for artifact index %d: %v", daw.index, err)
×
233
                        return ctrlResult, errLogAndWrap(log, err, "failed to hydrate artifact workflow")
×
234
                }
×
235

236
                // Set owner references
237
                if err := controllerutil.SetControllerReference(order, aw, r.Scheme); err != nil {
21✔
238
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "HydrationFailed", "Failed to set controller reference for artifact workflow: %v", err)
×
239
                        return ctrlResult, errLogAndWrap(log, err, "failed to set controller reference")
×
240
                }
×
241

242
                // Create artifact workflow
243
                if err := r.Create(ctx, aw); err != nil {
21✔
244
                        if apierrors.IsAlreadyExists(err) {
×
245
                                // Already created by a previous reconcile — that's fine
×
246
                                continue
×
247
                        }
248
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "CreationFailed", "Failed to create artifact workflow for artifact index %d: %v", daw.index, err)
×
249
                        return ctrlResult, errLogAndWrap(log, err, "failed to create artifact workflow")
×
250
                } else {
21✔
251
                        r.Recorder.Eventf(order, corev1.EventTypeNormal, "ArtifactWorkflowCreated", "Created artifact workflow '%s' for artifact index %d", aw.Name, daw.index)
21✔
252
                        log.V(1).Info("Created artifact workflow", "artifactWorkflow", aw.Name)
21✔
253
                }
21✔
254

255
                // Update status
256
                order.Status.ArtifactWorkflows[sha] = arcv1alpha1.OrderArtifactWorkflowStatus{
21✔
257
                        ArtifactIndex: daw.index,
21✔
258
                        WorkflowStatus: arcv1alpha1.WorkflowStatus{
21✔
259
                                Phase: arcv1alpha1.WorkflowUnknown,
21✔
260
                        },
21✔
261
                }
21✔
262
        }
263

264
        // Delete obsolete artifact workflows
265
        for _, sha := range deleteAWs {
2,766✔
266
                // Does not exist anymore, let's clean up!
1✔
267
                if err := r.Delete(ctx, &arcv1alpha1.ArtifactWorkflow{
1✔
268
                        ObjectMeta: awObjectMeta(order, sha),
1✔
269
                }); client.IgnoreNotFound(err) != nil {
1✔
270
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "DeletionFailed", "Failed to delete obsolete artifact workflow '%s': %v", sha, err)
×
271
                        return ctrlResult, errLogAndWrap(log, err, "failed to delete artifact workflow")
×
272
                }
×
273

274
                // Update status
275
                delete(order.Status.ArtifactWorkflows, sha)
1✔
276
                log.V(1).Info("Deleted obsolete artifact workflow", "artifactWorkflow", sha)
1✔
277
                r.Recorder.Eventf(order, corev1.EventTypeNormal, "ArtifactWorkflowDeleted", "Deleted obsolete artifact workflow '%s'", sha)
1✔
278
        }
279

280
        // Delete finished artifact workflows
281
        for _, sha := range finishedAWs {
2,868✔
282
                // Finished, let's clean up!
103✔
283
                if err := r.Delete(ctx, &arcv1alpha1.ArtifactWorkflow{
103✔
284
                        ObjectMeta: awObjectMeta(order, sha),
103✔
285
                }); client.IgnoreNotFound(err) != nil {
103✔
286
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "DeletionFailed", "Failed to delete finished artifact workflow '%s': %v", sha, err)
×
287
                        return ctrlResult, errLogAndWrap(log, err, "failed to delete artifact workflow")
×
288
                }
×
289

290
                log.V(1).Info("Deleted finished artifact workflow", "artifactWorkflow", sha)
103✔
291
                r.Recorder.Eventf(order, corev1.EventTypeNormal, "ArtifactWorkflowDeleted", "Deleted finished artifact workflow '%s'", sha)
103✔
292
        }
293

294
        anyPhaseChanged := false
2,765✔
295
        for sha, daw := range desiredAWs {
6,683✔
296
                if slices.Contains(createAWs, sha) {
3,939✔
297
                        // If it was just created we skip the update
21✔
298
                        continue
21✔
299
                }
300
                if daw.cron == nil && order.Status.ArtifactWorkflows[sha].Phase.Completed() {
4,000✔
301
                        // We do not need to check for updates if the workflow is completed and is NOT cron
103✔
302
                        continue
103✔
303
                }
304
                aw := arcv1alpha1.ArtifactWorkflow{}
3,794✔
305
                if err := r.Get(ctx, namespacedName(daw.objectMeta.Namespace, daw.objectMeta.Name), &aw); err != nil {
3,794✔
306
                        delete(order.Status.ArtifactWorkflows, sha)
×
307
                        log.V(1).Info("Artifact workflow not found, deleting from status.", "artifactWorkflow", sha)
×
308
                        if err := r.Status().Update(ctx, order); err != nil {
×
309
                                return ctrlResult, errLogAndWrap(log, err, "failed to update status")
×
310
                        }
×
311
                        return ctrlResult, errLogAndWrap(log, err, "failed to get artifact workflow")
×
312
                }
313
                orderAWStatus := order.Status.ArtifactWorkflows[sha]
3,794✔
314
                if orderAWStatus.Phase != aw.Status.Phase ||
3,794✔
315
                        orderAWStatus.Succeeded != aw.Status.Succeeded ||
3,794✔
316
                        orderAWStatus.Failed != aw.Status.Failed ||
3,794✔
317
                        !orderAWStatus.LastScheduled.Equal(aw.Status.LastScheduled) {
5,483✔
318
                        orderAWStatus.WorkflowStatus = aw.Status.WorkflowStatus
1,689✔
319
                        order.Status.ArtifactWorkflows[sha] = orderAWStatus
1,689✔
320
                        anyPhaseChanged = true
1,689✔
321
                }
1,689✔
322
        }
323

324
        // Update status
325
        if len(createAWs) > 0 || len(deleteAWs) > 0 || anyPhaseChanged {
4,427✔
326
                log.V(1).Info("Updating order status")
1,662✔
327
                // Make sure ArtifactIndex is up to date
1,662✔
328
                for sha, daw := range desiredAWs {
4,049✔
329
                        aws := order.Status.ArtifactWorkflows[sha]
2,387✔
330
                        aws.ArtifactIndex = daw.index
2,387✔
331
                        order.Status.ArtifactWorkflows[sha] = aws
2,387✔
332
                }
2,387✔
333
                if err := r.Status().Update(ctx, order); err != nil {
1,727✔
334
                        return ctrlResult, errLogAndWrap(log, err, "failed to update status")
65✔
335
                }
65✔
336
        }
337

338
        return ctrlResult, nil
2,700✔
339
}
340

341
func (r *OrderReconciler) hydrateArtifactWorkflow(daw *desiredAW) (*arcv1alpha1.ArtifactWorkflow, error) {
21✔
342
        params, err := dawToParameters(daw)
21✔
343
        if err != nil {
21✔
344
                return nil, err
×
345
        }
×
346

347
        // Next we create the ArtifactWorkflow instance
348
        aw := &arcv1alpha1.ArtifactWorkflow{
21✔
349
                ObjectMeta: daw.objectMeta,
21✔
350
                Spec: arcv1alpha1.ArtifactWorkflowSpec{
21✔
351
                        WorkflowTemplateRef: daw.typeSpec.WorkflowTemplateRef,
21✔
352
                        Parameters:          params,
21✔
353
                        SrcSecretRef:        daw.srcEndpoint.Spec.SecretRef,
21✔
354
                        DstSecretRef:        daw.dstEndpoint.Spec.SecretRef,
21✔
355
                        Cron:                daw.cron,
21✔
356
                },
21✔
357
        }
21✔
358

21✔
359
        return aw, nil
21✔
360
}
361

362
func (r *OrderReconciler) computeDesiredAW(ctx context.Context, log logr.Logger, order *arcv1alpha1.Order, artifact *arcv1alpha1.OrderArtifact, i int) (*desiredAW, error) {
4,021✔
363
        log = log.WithValues("artifactIndex", i)
4,021✔
364

4,021✔
365
        // We need the referenced src- and dst-endpoints for the artifact
4,021✔
366
        srcRefName := artifact.SrcRef.Name
4,021✔
367
        if srcRefName == "" {
4,393✔
368
                srcRefName = order.Spec.Defaults.SrcRef.Name
372✔
369
        }
372✔
370
        dstRefName := artifact.DstRef.Name
4,021✔
371
        if dstRefName == "" {
4,516✔
372
                dstRefName = order.Spec.Defaults.DstRef.Name
495✔
373
        }
495✔
374

375
        srcEndpoint := &arcv1alpha1.Endpoint{}
4,021✔
376
        if err := r.Get(ctx, namespacedName(order.Namespace, srcRefName), srcEndpoint); err != nil {
4,035✔
377
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidEndpoint", "Failed to fetch source endpoint '%s': %v", srcRefName, err)
14✔
378
                return nil, errLogAndWrap(log, err, "failed to fetch endpoint for source")
14✔
379
        }
14✔
380
        dstEndpoint := &arcv1alpha1.Endpoint{}
4,007✔
381
        if err := r.Get(ctx, namespacedName(order.Namespace, dstRefName), dstEndpoint); err != nil {
4,021✔
382
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidEndpoint", "Failed to fetch destination endpoint '%s': %v", dstRefName, err)
14✔
383
                return nil, errLogAndWrap(log, err, "failed to fetch endpoint for destination")
14✔
384
        }
14✔
385

386
        // Validate that the endpoint usage is correct
387
        if srcEndpoint.Spec.Usage != arcv1alpha1.EndpointUsagePullOnly && srcEndpoint.Spec.Usage != arcv1alpha1.EndpointUsageAll {
4,008✔
388
                err := fmt.Errorf("endpoint '%s' usage '%s' is not compatible with source usage", srcEndpoint.Name, srcEndpoint.Spec.Usage)
15✔
389
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidEndpoint", "Source endpoint '%s' has incompatible usage '%s'", srcEndpoint.Name, srcEndpoint.Spec.Usage)
15✔
390
                return nil, errLogAndWrap(log, err, "artifact validation failed")
15✔
391
        }
15✔
392
        if dstEndpoint.Spec.Usage != arcv1alpha1.EndpointUsagePushOnly && dstEndpoint.Spec.Usage != arcv1alpha1.EndpointUsageAll {
3,992✔
393
                err := fmt.Errorf("endpoint '%s' usage '%s' is not compatible with destination usage", dstEndpoint.Name, dstEndpoint.Spec.Usage)
14✔
394
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidEndpoint", "Destination endpoint '%s' has incompatible usage '%s'", dstEndpoint.Name, dstEndpoint.Spec.Usage)
14✔
395
                return nil, errLogAndWrap(log, err, "artifact validation failed")
14✔
396
        }
14✔
397

398
        // Validate against ArtifactType rules
399
        artifactType := &arcv1alpha1.ArtifactType{}
3,964✔
400
        if err := r.Get(ctx, namespacedName(order.Namespace, artifact.Type), artifactType); client.IgnoreNotFound(err) != nil {
3,964✔
401
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidArtifactType", "Failed to fetch ArtifactType '%s': %v", artifact.Type, err)
×
402
                return nil, errLogAndWrap(log, err, "failed to fetch referenced ArtifactType")
×
403
        }
×
404
        var (
3,964✔
405
                artifactTypeGen  int64
3,964✔
406
                artifactTypeSpec *arcv1alpha1.ArtifactTypeSpec
3,964✔
407
        )
3,964✔
408
        if artifactType.Name == "" { // was not found, let's check ClusterArtifactType
7,822✔
409
                clusterArtifactType := &arcv1alpha1.ClusterArtifactType{}
3,858✔
410
                if err := r.Get(ctx, namespacedName("", artifact.Type), clusterArtifactType); err != nil {
3,891✔
411
                        return nil, errLogAndWrap(log, err, "failed to fetch ArtifactType or ClusterArtifactType")
33✔
412
                }
33✔
413
                artifactTypeSpec = &clusterArtifactType.Spec
3,825✔
414
                artifactTypeGen = clusterArtifactType.Generation
3,825✔
415
                // NOTE: ClusterArtifactTypes can only reference ClusterWorkflowTemplates, so we enforce this here:
3,825✔
416
                artifactTypeSpec.WorkflowTemplateRef.ClusterScope = true
3,825✔
417
        } else {
106✔
418
                artifactTypeSpec = &artifactType.Spec
106✔
419
                artifactTypeGen = artifactType.Generation
106✔
420
        }
106✔
421

422
        if len(artifactTypeSpec.Rules.SrcTypes) > 0 && !slices.Contains(artifactTypeSpec.Rules.SrcTypes, srcEndpoint.Spec.Type) {
3,941✔
423
                err := fmt.Errorf("source endpoint type '%s' is not allowed by ArtifactType rules", srcEndpoint.Spec.Type)
10✔
424
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidArtifactType", "Source endpoint type '%s' is not allowed by ArtifactType '%s' rules", srcEndpoint.Spec.Type, artifact.Type)
10✔
425
                return nil, errLogAndWrap(log, err, "artifact validation failed")
10✔
426
        }
10✔
427
        if len(artifactTypeSpec.Rules.DstTypes) > 0 && !slices.Contains(artifactTypeSpec.Rules.DstTypes, dstEndpoint.Spec.Type) {
3,921✔
428
                err := fmt.Errorf("destination endpoint type '%s' is not allowed by ArtifactType rules", dstEndpoint.Spec.Type)
×
429
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidArtifactType", "Destination endpoint type '%s' is not allowed by ArtifactType '%s' rules", dstEndpoint.Spec.Type, artifact.Type)
×
430
                return nil, errLogAndWrap(log, err, "artifact validation failed")
×
431
        }
×
432

433
        // Next, we need the secret contents
434
        srcSecret := &corev1.Secret{}
3,921✔
435
        if srcEndpoint.Spec.SecretRef.Name != "" {
7,840✔
436
                if err := r.Get(ctx, namespacedName(order.Namespace, srcEndpoint.Spec.SecretRef.Name), srcSecret); err != nil {
3,919✔
437
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidSecret", "Failed to fetch source secret '%s': %v", srcEndpoint.Spec.SecretRef.Name, err)
×
438
                        return nil, errLogAndWrap(log, err, "failed to fetch secret for source")
×
439
                }
×
440
        }
441

442
        dstSecret := &corev1.Secret{}
3,921✔
443
        if dstEndpoint.Spec.SecretRef.Name != "" {
7,840✔
444
                if err := r.Get(ctx, namespacedName(order.Namespace, dstEndpoint.Spec.SecretRef.Name), dstSecret); err != nil {
3,919✔
445
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidSecret", "Failed to fetch destination secret '%s': %v", dstEndpoint.Spec.SecretRef.Name, err)
×
446
                        return nil, errLogAndWrap(log, err, "failed to fetch secret for destination")
×
447
                }
×
448
        }
449

450
        // Cron schedule if any
451
        cron := artifact.Cron
3,921✔
452
        if cron == nil {
7,839✔
453
                cron = order.Spec.Defaults.Cron
3,918✔
454
        }
3,918✔
455

456
        // Create a hash based on all related data for idempotency and compute the workflow name
457
        h := sha256.New()
3,921✔
458
        data := []any{
3,921✔
459
                order.Namespace,
3,921✔
460
                artifact.Type,
3,921✔
461
                artifact.Spec.Raw,
3,921✔
462
                artifactTypeGen,
3,921✔
463
                srcEndpoint.Name,
3,921✔
464
                dstEndpoint.Name,
3,921✔
465
                order.Status.LastForceAt,
3,921✔
466
                cron,
3,921✔
467
        }
3,921✔
468

3,921✔
469
        if err := json.NewEncoder(h).Encode(data); err != nil {
3,921✔
470
                return nil, errLogAndWrap(log, err, "failed to marshal artifact workflow data")
×
471
        }
×
472

473
        sha := hex.EncodeToString(h.Sum(nil))[:16]
3,921✔
474

3,921✔
475
        // We gave all the information to further process this artifact workflow.
3,921✔
476
        // Let's store it to compare it to the current status!
3,921✔
477
        return &desiredAW{
3,921✔
478
                index:       i,
3,921✔
479
                objectMeta:  awObjectMeta(order, sha),
3,921✔
480
                artifact:    artifact,
3,921✔
481
                typeSpec:    artifactTypeSpec,
3,921✔
482
                srcEndpoint: srcEndpoint,
3,921✔
483
                dstEndpoint: dstEndpoint,
3,921✔
484
                srcSecret:   srcSecret,
3,921✔
485
                dstSecret:   dstSecret,
3,921✔
486
                sha:         sha,
3,921✔
487
                cron:        cron,
3,921✔
488
        }, nil
3,921✔
489
}
490

491
// SetupWithManager sets up the controller with the Manager.
492
func (r *OrderReconciler) SetupWithManager(mgr ctrl.Manager) error {
1✔
493
        return ctrl.NewControllerManagedBy(mgr).
1✔
494
                For(&arcv1alpha1.Order{}).
1✔
495
                Owns(&arcv1alpha1.ArtifactWorkflow{}).
1✔
496
                Complete(r)
1✔
497
}
1✔
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