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

opendefensecloud / artifact-conduit / 20374201173

19 Dec 2025 03:15PM UTC coverage: 85.983% (+0.3%) from 85.694%
20374201173

push

github

web-flow
Add ConvertToList() for Endpoint (#158)

Implementation using `Usage` and `Secret`.

Closes #129

595 of 692 relevant lines covered (85.98%)

1025.03 hits per line

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

82.57
/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
}
49

50
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=endpoints,verbs=get;list;watch
51
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=artifacttypes,verbs=get;list;watch
52
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=clusterartifacttypes,verbs=get;list;watch
53
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=artifactworkflows,verbs=get;list;watch;create;update;patch;delete
54
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=orders,verbs=get;list;watch;create;update;patch;delete
55
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=orders/status,verbs=get;update;patch
56
//+kubebuilder:rbac:groups=arc.opendefense.cloud,resources=orders/finalizers,verbs=update
57
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
58
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
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) {
1,928✔
62
        log := ctrl.LoggerFrom(ctx)
1,928✔
63
        ctrlResult := ctrl.Result{}
1,928✔
64

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

75
        // Update last reconcile time
76
        order.Status.LastReconcileAt = metav1.Now()
1,925✔
77

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

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

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

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

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

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

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

1,775✔
176
        // List missing artifact workflows
1,775✔
177
        createAWs := []string{}
1,775✔
178
        for sha := range desiredAWs {
4,568✔
179
                _, exists := order.Status.ArtifactWorkflows[sha]
2,793✔
180
                if exists {
5,562✔
181
                        continue
2,769✔
182
                }
183
                createAWs = append(createAWs, sha)
24✔
184
        }
185

186
        // Find obsolete artifact workflows
187
        deleteAWs := []string{}
1,775✔
188
        for sha := range order.Status.ArtifactWorkflows {
4,545✔
189
                _, exists := desiredAWs[sha]
2,770✔
190
                if exists {
5,539✔
191
                        continue
2,769✔
192
                }
193
                deleteAWs = append(deleteAWs, sha)
1✔
194
        }
195

196
        // Find finished artifact workflows to clean up
197
        finishedAWs := []string{}
1,775✔
198
        for sha := range order.Status.ArtifactWorkflows {
4,545✔
199
                awStatus := order.Status.ArtifactWorkflows[sha]
2,770✔
200

2,770✔
201
                // Only consider succeeded workflows for TTL cleanup
2,770✔
202
                if awStatus.Phase != arcv1alpha1.WorkflowSucceeded {
5,491✔
203
                        continue
2,721✔
204
                }
205

206
                // If TTL is set, check if it has expired
207
                if order.Spec.TTLSecondsAfterCompletion != nil && *order.Spec.TTLSecondsAfterCompletion > 0 {
49✔
208
                        if time.Since(awStatus.CompletionTime.Time) > time.Duration(*order.Spec.TTLSecondsAfterCompletion)*time.Second {
×
209
                                finishedAWs = append(finishedAWs, sha)
×
210
                        } else {
×
211
                                // Requeue when the next TTL expires
×
212
                                ctrlResult.RequeueAfter = time.Duration((*order.Spec.TTLSecondsAfterCompletion)+1)*time.Second - time.Since(awStatus.CompletionTime.Time)
×
213
                        }
×
214
                        continue
×
215
                }
216

217
                // No TTL set, cleanup immediately
218
                finishedAWs = append(finishedAWs, sha)
49✔
219
        }
220

221
        // Create missing artifact workflows
222
        for _, sha := range createAWs {
1,799✔
223
                daw := desiredAWs[sha]
24✔
224
                aw, err := r.hydrateArtifactWorkflow(&daw)
24✔
225
                if err != nil {
24✔
226
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "HydrationFailed", "Failed to hydrate artifact workflow for artifact index %d: %v", daw.index, err)
×
227
                        return ctrlResult, errLogAndWrap(log, err, "failed to hydrate artifact workflow")
×
228
                }
×
229

230
                // Set owner references
231
                if err := controllerutil.SetControllerReference(order, aw, r.Scheme); err != nil {
24✔
232
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "HydrationFailed", "Failed to set controller reference for artifact workflow: %v", err)
×
233
                        return ctrlResult, errLogAndWrap(log, err, "failed to set controller reference")
×
234
                }
×
235

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

249
                // Update status
250
                order.Status.ArtifactWorkflows[sha] = arcv1alpha1.OrderArtifactWorkflowStatus{
20✔
251
                        ArtifactIndex: daw.index,
20✔
252
                        Phase:         arcv1alpha1.WorkflowUnknown,
20✔
253
                }
20✔
254
        }
255

256
        // Delete obsolete artifact workflows
257
        for _, sha := range deleteAWs {
1,776✔
258
                // Does not exist anymore, let's clean up!
1✔
259
                if err := r.Delete(ctx, &arcv1alpha1.ArtifactWorkflow{
1✔
260
                        ObjectMeta: awObjectMeta(order, sha),
1✔
261
                }); client.IgnoreNotFound(err) != nil {
1✔
262
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "DeletionFailed", "Failed to delete obsolete artifact workflow '%s': %v", sha, err)
×
263
                        return ctrlResult, errLogAndWrap(log, err, "failed to delete artifact workflow")
×
264
                }
×
265

266
                // Update status
267
                delete(order.Status.ArtifactWorkflows, sha)
1✔
268
                log.V(1).Info("Deleted obsolete artifact workflow", "artifactWorkflow", sha)
1✔
269
                r.Recorder.Eventf(order, corev1.EventTypeNormal, "ArtifactWorkflowDeleted", "Deleted obsolete artifact workflow '%s'", sha)
1✔
270
        }
271

272
        // Delete finished artifact workflows
273
        for _, sha := range finishedAWs {
1,824✔
274
                // Finished, let's clean up!
49✔
275
                if err := r.Delete(ctx, &arcv1alpha1.ArtifactWorkflow{
49✔
276
                        ObjectMeta: awObjectMeta(order, sha),
49✔
277
                }); client.IgnoreNotFound(err) != nil {
49✔
278
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "DeletionFailed", "Failed to delete finished artifact workflow '%s': %v", sha, err)
×
279
                        return ctrlResult, errLogAndWrap(log, err, "failed to delete artifact workflow")
×
280
                }
×
281

282
                log.V(1).Info("Deleted finished artifact workflow", "artifactWorkflow", sha)
49✔
283
                r.Recorder.Eventf(order, corev1.EventTypeNormal, "ArtifactWorkflowDeleted", "Deleted finished artifact workflow '%s'", sha)
49✔
284
        }
285

286
        anyPhaseChanged := false
1,775✔
287
        for sha, daw := range desiredAWs {
4,568✔
288
                if slices.Contains(createAWs, sha) {
2,817✔
289
                        // If it was just created we skip the update
24✔
290
                        continue
24✔
291
                }
292
                if order.Status.ArtifactWorkflows[sha].Phase.Completed() {
2,818✔
293
                        // We do not need to check for updates if the workflow is completed
49✔
294
                        continue
49✔
295
                }
296
                aw := arcv1alpha1.ArtifactWorkflow{}
2,720✔
297
                if err := r.Get(ctx, namespacedName(daw.objectMeta.Namespace, daw.objectMeta.Name), &aw); err != nil {
2,720✔
298
                        delete(order.Status.ArtifactWorkflows, sha)
×
299
                        log.V(1).Info("Artifact workflow not found, deleting from status.", "artifactWorkflow", sha)
×
300
                        if err := r.Status().Update(ctx, order); err != nil {
×
301
                                return ctrlResult, errLogAndWrap(log, err, "failed to update status")
×
302
                        }
×
303
                        return ctrlResult, errLogAndWrap(log, err, "failed to get artifact workflow")
×
304
                }
305
                if order.Status.ArtifactWorkflows[sha].Phase != aw.Status.Phase {
3,830✔
306
                        awStatus := order.Status.ArtifactWorkflows[sha]
1,110✔
307
                        awStatus.Phase = aw.Status.Phase
1,110✔
308
                        awStatus.CompletionTime = aw.Status.CompletionTime
1,110✔
309
                        order.Status.ArtifactWorkflows[sha] = awStatus
1,110✔
310
                        anyPhaseChanged = true
1,110✔
311
                }
1,110✔
312
        }
313

314
        // Update status
315
        if len(createAWs) > 0 || len(deleteAWs) > 0 || anyPhaseChanged {
2,822✔
316
                log.V(1).Info("Updating order status")
1,047✔
317
                // Make sure ArtifactIndex is up to date
1,047✔
318
                for sha, daw := range desiredAWs {
2,728✔
319
                        aws := order.Status.ArtifactWorkflows[sha]
1,681✔
320
                        aws.ArtifactIndex = daw.index
1,681✔
321
                        order.Status.ArtifactWorkflows[sha] = aws
1,681✔
322
                }
1,681✔
323
                if err := r.Status().Update(ctx, order); err != nil {
1,113✔
324
                        return ctrlResult, errLogAndWrap(log, err, "failed to update status")
66✔
325
                }
66✔
326
        }
327

328
        return ctrlResult, nil
1,709✔
329
}
330

331
func (r *OrderReconciler) hydrateArtifactWorkflow(daw *desiredAW) (*arcv1alpha1.ArtifactWorkflow, error) {
24✔
332
        params, err := dawToParameters(daw)
24✔
333
        if err != nil {
24✔
334
                return nil, err
×
335
        }
×
336

337
        // Next we create the ArtifactWorkflow instance
338
        aw := &arcv1alpha1.ArtifactWorkflow{
24✔
339
                ObjectMeta: daw.objectMeta,
24✔
340
                Spec: arcv1alpha1.ArtifactWorkflowSpec{
24✔
341
                        WorkflowTemplateRef: daw.typeSpec.WorkflowTemplateRef,
24✔
342
                        Parameters:          params,
24✔
343
                        SrcSecretRef:        daw.srcEndpoint.Spec.SecretRef,
24✔
344
                        DstSecretRef:        daw.dstEndpoint.Spec.SecretRef,
24✔
345
                },
24✔
346
        }
24✔
347

24✔
348
        return aw, nil
24✔
349
}
350

351
func (r *OrderReconciler) computeDesiredAW(ctx context.Context, log logr.Logger, order *arcv1alpha1.Order, artifact *arcv1alpha1.OrderArtifact, i int) (*desiredAW, error) {
2,926✔
352
        log = log.WithValues("artifactIndex", i)
2,926✔
353

2,926✔
354
        // We need the referenced src- and dst-endpoints for the artifact
2,926✔
355
        srcRefName := artifact.SrcRef.Name
2,926✔
356
        if srcRefName == "" {
3,241✔
357
                srcRefName = order.Spec.Defaults.SrcRef.Name
315✔
358
        }
315✔
359
        dstRefName := artifact.DstRef.Name
2,926✔
360
        if dstRefName == "" {
3,340✔
361
                dstRefName = order.Spec.Defaults.DstRef.Name
414✔
362
        }
414✔
363
        srcEndpoint := &arcv1alpha1.Endpoint{}
2,926✔
364
        if err := r.Get(ctx, namespacedName(order.Namespace, srcRefName), srcEndpoint); err != nil {
2,940✔
365
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidEndpoint", "Failed to fetch source endpoint '%s': %v", srcRefName, err)
14✔
366
                return nil, errLogAndWrap(log, err, "failed to fetch endpoint for source")
14✔
367
        }
14✔
368
        dstEndpoint := &arcv1alpha1.Endpoint{}
2,912✔
369
        if err := r.Get(ctx, namespacedName(order.Namespace, dstRefName), dstEndpoint); err != nil {
2,926✔
370
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidEndpoint", "Failed to fetch destination endpoint '%s': %v", dstRefName, err)
14✔
371
                return nil, errLogAndWrap(log, err, "failed to fetch endpoint for destination")
14✔
372
        }
14✔
373

374
        // Validate that the endpoint usage is correct
375
        if srcEndpoint.Spec.Usage != arcv1alpha1.EndpointUsagePullOnly && srcEndpoint.Spec.Usage != arcv1alpha1.EndpointUsageAll {
2,912✔
376
                err := fmt.Errorf("endpoint '%s' usage '%s' is not compatible with source usage", srcEndpoint.Name, srcEndpoint.Spec.Usage)
14✔
377
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidEndpoint", "Source endpoint '%s' has incompatible usage '%s'", srcEndpoint.Name, srcEndpoint.Spec.Usage)
14✔
378
                return nil, errLogAndWrap(log, err, "artifact validation failed")
14✔
379
        }
14✔
380
        if dstEndpoint.Spec.Usage != arcv1alpha1.EndpointUsagePushOnly && dstEndpoint.Spec.Usage != arcv1alpha1.EndpointUsageAll {
2,898✔
381
                err := fmt.Errorf("endpoint '%s' usage '%s' is not compatible with destination usage", dstEndpoint.Name, dstEndpoint.Spec.Usage)
14✔
382
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidEndpoint", "Destination endpoint '%s' has incompatible usage '%s'", dstEndpoint.Name, dstEndpoint.Spec.Usage)
14✔
383
                return nil, errLogAndWrap(log, err, "artifact validation failed")
14✔
384
        }
14✔
385

386
        // Validate against ArtifactType rules
387
        artifactType := &arcv1alpha1.ArtifactType{}
2,870✔
388
        if err := r.Get(ctx, namespacedName(order.Namespace, artifact.Type), artifactType); client.IgnoreNotFound(err) != nil {
2,870✔
389
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidArtifactType", "Failed to fetch ArtifactType '%s': %v", artifact.Type, err)
×
390
                return nil, errLogAndWrap(log, err, "failed to fetch referenced ArtifactType")
×
391
        }
×
392
        var (
2,870✔
393
                artifactTypeGen  int64
2,870✔
394
                artifactTypeSpec *arcv1alpha1.ArtifactTypeSpec
2,870✔
395
        )
2,870✔
396
        if artifactType.Name == "" { // was not found, let's check ClusterArtifactType
5,667✔
397
                clusterArtifactType := &arcv1alpha1.ClusterArtifactType{}
2,797✔
398
                if err := r.Get(ctx, namespacedName("", artifact.Type), clusterArtifactType); err != nil {
2,861✔
399
                        return nil, errLogAndWrap(log, err, "failed to fetch ArtifactType or ClusterArtifactType")
64✔
400
                }
64✔
401
                artifactTypeSpec = &clusterArtifactType.Spec
2,733✔
402
                artifactTypeGen = clusterArtifactType.Generation
2,733✔
403
                // NOTE: ClusterArtifactTypes can only referes ClusterWorkflowTemplates, so we enforce this here:
2,733✔
404
                artifactTypeSpec.WorkflowTemplateRef.ClusterScope = true
2,733✔
405
        } else {
73✔
406
                artifactTypeSpec = &artifactType.Spec
73✔
407
                artifactTypeGen = artifactType.Generation
73✔
408
        }
73✔
409

410
        if len(artifactTypeSpec.Rules.SrcTypes) > 0 && !slices.Contains(artifactTypeSpec.Rules.SrcTypes, srcEndpoint.Spec.Type) {
2,816✔
411
                err := fmt.Errorf("source endpoint type '%s' is not allowed by ArtifactType rules", srcEndpoint.Spec.Type)
10✔
412
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidArtifactType", "Source endpoint type '%s' is not allowed by ArtifactType '%s' rules", srcEndpoint.Spec.Type, artifact.Type)
10✔
413
                return nil, errLogAndWrap(log, err, "artifact validation failed")
10✔
414
        }
10✔
415
        if len(artifactTypeSpec.Rules.DstTypes) > 0 && !slices.Contains(artifactTypeSpec.Rules.DstTypes, dstEndpoint.Spec.Type) {
2,796✔
416
                err := fmt.Errorf("destination endpoint type '%s' is not allowed by ArtifactType rules", dstEndpoint.Spec.Type)
×
417
                r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidArtifactType", "Destination endpoint type '%s' is not allowed by ArtifactType '%s' rules", dstEndpoint.Spec.Type, artifact.Type)
×
418
                return nil, errLogAndWrap(log, err, "artifact validation failed")
×
419
        }
×
420

421
        // Next, we need the secret contents
422
        srcSecret := &corev1.Secret{}
2,796✔
423
        if srcEndpoint.Spec.SecretRef.Name != "" {
5,523✔
424
                if err := r.Get(ctx, namespacedName(order.Namespace, srcEndpoint.Spec.SecretRef.Name), srcSecret); err != nil {
2,727✔
425
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidSecret", "Failed to fetch source secret '%s': %v", srcEndpoint.Spec.SecretRef.Name, err)
×
426
                        return nil, errLogAndWrap(log, err, "failed to fetch secret for source")
×
427
                }
×
428
        }
429

430
        dstSecret := &corev1.Secret{}
2,796✔
431
        if dstEndpoint.Spec.SecretRef.Name != "" {
5,523✔
432
                if err := r.Get(ctx, namespacedName(order.Namespace, dstEndpoint.Spec.SecretRef.Name), dstSecret); err != nil {
2,727✔
433
                        r.Recorder.Eventf(order, corev1.EventTypeWarning, "InvalidSecret", "Failed to fetch destination secret '%s': %v", dstEndpoint.Spec.SecretRef.Name, err)
×
434
                        return nil, errLogAndWrap(log, err, "failed to fetch secret for destination")
×
435
                }
×
436
        }
437

438
        // Create a hash based on all related data for idempotency and compute the workflow name
439
        h := sha256.New()
2,796✔
440
        data := []any{
2,796✔
441
                order.Namespace,
2,796✔
442
                artifact.Type, artifact.Spec.Raw, artifactTypeGen,
2,796✔
443
                srcEndpoint.Name,
2,796✔
444
                dstEndpoint.Name,
2,796✔
445
                order.Status.LastForceAt,
2,796✔
446
        }
2,796✔
447
        jsonData, err := json.Marshal(data)
2,796✔
448
        if err != nil {
2,796✔
449
                return nil, errLogAndWrap(log, err, "failed to marshal artifact workflow data")
×
450
        }
×
451
        h.Write(jsonData)
2,796✔
452
        sha := hex.EncodeToString(h.Sum(nil))[:16]
2,796✔
453

2,796✔
454
        // We gave all the information to further process this artifact workflow.
2,796✔
455
        // Let's store it to compare it to the current status!
2,796✔
456
        return &desiredAW{
2,796✔
457
                index:       i,
2,796✔
458
                objectMeta:  awObjectMeta(order, sha),
2,796✔
459
                artifact:    artifact,
2,796✔
460
                typeSpec:    artifactTypeSpec,
2,796✔
461
                srcEndpoint: srcEndpoint,
2,796✔
462
                dstEndpoint: dstEndpoint,
2,796✔
463
                srcSecret:   srcSecret,
2,796✔
464
                dstSecret:   dstSecret,
2,796✔
465
                sha:         sha,
2,796✔
466
        }, nil
2,796✔
467
}
468

469
// SetupWithManager sets up the controller with the Manager.
470
func (r *OrderReconciler) SetupWithManager(mgr ctrl.Manager) error {
1✔
471
        return ctrl.NewControllerManagedBy(mgr).
1✔
472
                For(&arcv1alpha1.Order{}).
1✔
473
                Owns(&arcv1alpha1.ArtifactWorkflow{}).
1✔
474
                Complete(r)
1✔
475
}
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