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

opendefensecloud / artifact-conduit / 19737743006

27 Nov 2025 01:23PM UTC coverage: 61.531% (-0.2%) from 61.735%
19737743006

push

github

jastBytes
adjust docs

603 of 980 relevant lines covered (61.53%)

859.25 hits per line

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

79.5
/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.bwi.de/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.bwi.de,resources=endpoints,verbs=get;list;watch
51
//+kubebuilder:rbac:groups=arc.bwi.de,resources=artifacttypes,verbs=get;list;watch
52
//+kubebuilder:rbac:groups=arc.bwi.de,resources=clusterartifacttypes,verbs=get;list;watch
53
//+kubebuilder:rbac:groups=arc.bwi.de,resources=artifactworkflows,verbs=get;list;watch;create;update;patch;delete
54
//+kubebuilder:rbac:groups=arc.bwi.de,resources=orders,verbs=get;list;watch;create;update;patch;delete
55
//+kubebuilder:rbac:groups=arc.bwi.de,resources=orders/status,verbs=get;update;patch
56
//+kubebuilder:rbac:groups=arc.bwi.de,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) {
2,270✔
62
        log := ctrl.LoggerFrom(ctx)
2,270✔
63
        ctrlResult := ctrl.Result{}
2,270✔
64

2,270✔
65
        // Fetch the Order instance
2,270✔
66
        order := &arcv1alpha1.Order{}
2,270✔
67
        if err := r.Get(ctx, req.NamespacedName, order); err != nil {
2,273✔
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()
2,267✔
77

2,267✔
78
        // Handle deletion: cleanup artifact workflows, then remove finalizer
2,267✔
79
        if !order.DeletionTimestamp.IsZero() {
2,269✔
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() {
4,530✔
116
                if !slices.Contains(order.Finalizers, orderFinalizer) {
2,277✔
117
                        log.V(1).Info("Adding finalizer to Order")
12✔
118
                        order.Finalizers = append(order.Finalizers, orderFinalizer)
12✔
119
                        if err := r.Update(ctx, order); err != nil {
12✔
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
12✔
124
                }
125
        }
126

127
        // Handle force reconcile annotation
128
        forceAt, err := GetForceAtAnnotationValue(order)
2,253✔
129
        if err != nil {
2,253✔
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)) {
2,254✔
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
                }
1✔
144
                // Update last force time
145
                order.Status.LastForceAt = metav1.Now()
1✔
146
                if err := r.Status().Update(ctx, order); err != nil {
1✔
147
                        return ctrlResult, errLogAndWrap(log, err, "failed to update last force time")
×
148
                }
×
149
                // Return without requeue; the update event will trigger reconciliation again
150
                return ctrlResult, nil
1✔
151
        }
152

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

158
        // Before we compare to our status, let's fetch all necessary information
159
        // to compute desired state:
160
        desiredAWs := map[string]desiredAW{}
2,252✔
161
        for i, artifact := range order.Spec.Artifacts {
5,963✔
162
                daw, err := r.computeDesiredAW(ctx, log, order, &artifact, i)
3,711✔
163
                if err != nil {
3,771✔
164
                        r.Recorder.Event(order, corev1.EventTypeWarning, "ComputationFailed", fmt.Sprintf("Failed to compute desired artifact workflow for artifact index %d: %v", i, err))
60✔
165
                        order.Status.Message = fmt.Sprintf("Failed to compute desired artifact workflow for artifact index %d: %v", i, err)
60✔
166
                        if err := r.Status().Update(ctx, order); err != nil {
60✔
167
                                return ctrlResult, errLogAndWrap(log, err, "failed to update status")
×
168
                        }
×
169
                        return ctrlResult, errLogAndWrap(log, err, "failed to compute desired artifact workflow")
60✔
170
                }
171
                desiredAWs[daw.sha] = *daw
3,651✔
172
        }
173

174
        // List missing artifact workflows
175
        createAWs := []string{}
2,192✔
176
        for sha := range desiredAWs {
5,839✔
177
                _, exists := order.Status.ArtifactWorkflows[sha]
3,647✔
178
                if exists {
7,269✔
179
                        continue
3,622✔
180
                }
181
                createAWs = append(createAWs, sha)
25✔
182
        }
183

184
        // Find obsolete artifact workflows
185
        deleteAWs := []string{}
2,192✔
186
        for sha := range order.Status.ArtifactWorkflows {
5,815✔
187
                _, exists := desiredAWs[sha]
3,623✔
188
                if exists {
7,245✔
189
                        continue
3,622✔
190
                }
191
                deleteAWs = append(deleteAWs, sha)
1✔
192
        }
193

194
        // Find finished artifact workflows to clean up
195
        finishedAWs := []string{}
2,192✔
196
        for sha := range order.Status.ArtifactWorkflows {
5,815✔
197
                awStatus := order.Status.ArtifactWorkflows[sha]
3,623✔
198

3,623✔
199
                // Only consider succeeded workflows for TTL cleanup
3,623✔
200
                if awStatus.Phase != arcv1alpha1.WorkflowSucceeded {
7,135✔
201
                        continue
3,512✔
202
                }
203

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

215
                // No TTL set, cleanup immediately
216
                finishedAWs = append(finishedAWs, sha)
111✔
217
        }
218

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

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

234
                // Create artifact workflow
235
                if err := r.Create(ctx, aw); err != nil {
31✔
236
                        if apierrors.IsAlreadyExists(err) {
12✔
237
                                // Already created by a previous reconcile — that's fine
6✔
238
                                continue
6✔
239
                        }
240
                        r.Recorder.Event(order, corev1.EventTypeWarning, "CreationFailed", fmt.Sprintf("Failed to create artifact workflow for artifact index %d: %v", daw.index, err))
×
241
                        return ctrlResult, errLogAndWrap(log, err, "failed to create artifact workflow")
×
242
                }
243

244
                // Update status
245
                order.Status.ArtifactWorkflows[sha] = arcv1alpha1.OrderArtifactWorkflowStatus{
19✔
246
                        ArtifactIndex: daw.index,
19✔
247
                        Phase:         arcv1alpha1.WorkflowUnknown,
19✔
248
                }
19✔
249

19✔
250
                r.Recorder.Event(order, corev1.EventTypeNormal, "ArtifactWorkflowCreated", fmt.Sprintf("Created artifact workflow '%s' for artifact index %d", aw.Name, daw.index))
19✔
251
                log.V(1).Info("Created artifact workflow", "artifactWorkflow", aw.Name)
19✔
252
        }
253

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

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

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

280
                log.V(1).Info("Deleted finished artifact workflow", "artifactWorkflow", sha)
111✔
281
                r.Recorder.Event(order, corev1.EventTypeNormal, "ArtifactWorkflowDeleted", fmt.Sprintf("Deleted finished artifact workflow '%s'", sha))
111✔
282
        }
283

284
        anyPhaseChanged := false
2,192✔
285
        for sha, daw := range desiredAWs {
5,839✔
286
                if slices.Contains(createAWs, sha) {
3,672✔
287
                        // If it was just created we skip the update
25✔
288
                        continue
25✔
289
                }
290
                if order.Status.ArtifactWorkflows[sha].Phase.Completed() {
3,733✔
291
                        // We do not need to check for updates if the workflow is completed
111✔
292
                        continue
111✔
293
                }
294
                aw := arcv1alpha1.ArtifactWorkflow{}
3,511✔
295
                if err := r.Get(ctx, namespacedName(daw.objectMeta.Namespace, daw.objectMeta.Name), &aw); err != nil {
3,518✔
296
                        return ctrlResult, errLogAndWrap(log, err, "failed to get artifact workflow")
7✔
297
                }
7✔
298
                if order.Status.ArtifactWorkflows[sha].Phase != aw.Status.Phase {
4,928✔
299
                        awStatus := order.Status.ArtifactWorkflows[sha]
1,424✔
300
                        awStatus.Phase = aw.Status.Phase
1,424✔
301
                        awStatus.CompletionTime = aw.Status.CompletionTime
1,424✔
302
                        order.Status.ArtifactWorkflows[sha] = awStatus
1,424✔
303
                        anyPhaseChanged = true
1,424✔
304
                }
1,424✔
305
        }
306

307
        // Update status
308
        if len(createAWs) > 0 || len(deleteAWs) > 0 || anyPhaseChanged {
3,571✔
309
                log.V(1).Info("Updating order status")
1,386✔
310
                // Make sure ArtifactIndex is up to date
1,386✔
311
                for sha, daw := range desiredAWs {
3,719✔
312
                        aws := order.Status.ArtifactWorkflows[sha]
2,333✔
313
                        aws.ArtifactIndex = daw.index
2,333✔
314
                        order.Status.ArtifactWorkflows[sha] = aws
2,333✔
315
                }
2,333✔
316
                if err := r.Status().Update(ctx, order); err != nil {
1,453✔
317
                        return ctrlResult, errLogAndWrap(log, err, "failed to update status")
67✔
318
                }
67✔
319
        }
320

321
        return ctrlResult, nil
2,118✔
322
}
323

324
func (r *OrderReconciler) hydrateArtifactWorkflow(daw *desiredAW) (*arcv1alpha1.ArtifactWorkflow, error) {
25✔
325
        params, err := dawToParameters(daw)
25✔
326
        if err != nil {
25✔
327
                return nil, err
×
328
        }
×
329

330
        // Next we create the ArtifactWorkflow instance
331
        aw := &arcv1alpha1.ArtifactWorkflow{
25✔
332
                ObjectMeta: daw.objectMeta,
25✔
333
                Spec: arcv1alpha1.ArtifactWorkflowSpec{
25✔
334
                        WorkflowTemplateRef: daw.typeSpec.WorkflowTemplateRef,
25✔
335
                        Parameters:          params,
25✔
336
                        SrcSecretRef:        daw.srcEndpoint.Spec.SecretRef,
25✔
337
                        DstSecretRef:        daw.dstEndpoint.Spec.SecretRef,
25✔
338
                },
25✔
339
        }
25✔
340

25✔
341
        return aw, nil
25✔
342
}
343

344
func (r *OrderReconciler) computeDesiredAW(ctx context.Context, log logr.Logger, order *arcv1alpha1.Order, artifact *arcv1alpha1.OrderArtifact, i int) (*desiredAW, error) {
3,711✔
345
        log = log.WithValues("artifactIndex", i)
3,711✔
346

3,711✔
347
        // We need the referenced src- and dst-endpoints for the artifact
3,711✔
348
        srcRefName := artifact.SrcRef.Name
3,711✔
349
        if srcRefName == "" {
4,226✔
350
                srcRefName = order.Spec.Defaults.SrcRef.Name
515✔
351
        }
515✔
352
        dstRefName := artifact.DstRef.Name
3,711✔
353
        if dstRefName == "" {
4,405✔
354
                dstRefName = order.Spec.Defaults.DstRef.Name
694✔
355
        }
694✔
356
        srcEndpoint := &arcv1alpha1.Endpoint{}
3,711✔
357
        if err := r.Get(ctx, namespacedName(order.Namespace, srcRefName), srcEndpoint); err != nil {
3,711✔
358
                r.Recorder.Event(order, corev1.EventTypeWarning, "InvalidEndpoint", fmt.Sprintf("Failed to fetch source endpoint '%s': %v", srcRefName, err))
×
359
                return nil, errLogAndWrap(log, err, "failed to fetch endpoint for source")
×
360
        }
×
361
        dstEndpoint := &arcv1alpha1.Endpoint{}
3,711✔
362
        if err := r.Get(ctx, namespacedName(order.Namespace, dstRefName), dstEndpoint); err != nil {
3,711✔
363
                r.Recorder.Event(order, corev1.EventTypeWarning, "InvalidEndpoint", fmt.Sprintf("Failed to fetch destination endpoint '%s': %v", dstRefName, err))
×
364
                return nil, errLogAndWrap(log, err, "failed to fetch endpoint for destination")
×
365
        }
×
366

367
        // Validate that the endpoint usage is correct
368
        if srcEndpoint.Spec.Usage != arcv1alpha1.EndpointUsagePullOnly && srcEndpoint.Spec.Usage != arcv1alpha1.EndpointUsageAll {
3,711✔
369
                err := fmt.Errorf("endpoint '%s' usage '%s' is not compatible with source usage", srcEndpoint.Name, srcEndpoint.Spec.Usage)
×
370
                r.Recorder.Event(order, corev1.EventTypeWarning, "InvalidEndpoint", fmt.Sprintf("Source endpoint '%s' has incompatible usage '%s'", srcEndpoint.Name, srcEndpoint.Spec.Usage))
×
371
                return nil, errLogAndWrap(log, err, "artifact validation failed")
×
372
        }
×
373
        if dstEndpoint.Spec.Usage != arcv1alpha1.EndpointUsagePushOnly && dstEndpoint.Spec.Usage != arcv1alpha1.EndpointUsageAll {
3,711✔
374
                err := fmt.Errorf("endpoint '%s' usage '%s' is not compatible with destination usage", dstEndpoint.Name, dstEndpoint.Spec.Usage)
×
375
                r.Recorder.Event(order, corev1.EventTypeWarning, "InvalidEndpoint", fmt.Sprintf("Destination endpoint '%s' has incompatible usage '%s'", dstEndpoint.Name, dstEndpoint.Spec.Usage))
×
376
                return nil, errLogAndWrap(log, err, "artifact validation failed")
×
377
        }
×
378

379
        // Validate against ArtifactType rules
380
        artifactType := &arcv1alpha1.ArtifactType{}
3,711✔
381
        if err := r.Get(ctx, namespacedName(order.Namespace, artifact.Type), artifactType); client.IgnoreNotFound(err) != nil {
3,711✔
382
                r.Recorder.Event(order, corev1.EventTypeWarning, "InvalidArtifactType", fmt.Sprintf("Failed to fetch ArtifactType '%s': %v", artifact.Type, err))
×
383
                return nil, errLogAndWrap(log, err, "failed to fetch referenced ArtifactType")
×
384
        }
×
385
        var (
3,711✔
386
                artifactTypeGen  int64
3,711✔
387
                artifactTypeSpec *arcv1alpha1.ArtifactTypeSpec
3,711✔
388
        )
3,711✔
389
        if artifactType.Name == "" { // was not found, let's check ClusterArtifactType
7,266✔
390
                clusterArtifactType := &arcv1alpha1.ClusterArtifactType{}
3,555✔
391
                if err := r.Get(ctx, namespacedName("", artifact.Type), clusterArtifactType); err != nil {
3,604✔
392
                        return nil, errLogAndWrap(log, err, "failed to fetch ArtifactType or ClusterArtifactType")
49✔
393
                }
49✔
394
                artifactTypeSpec = &clusterArtifactType.Spec
3,506✔
395
                artifactTypeGen = clusterArtifactType.Generation
3,506✔
396
                // NOTE: ClusterArtifactTypes can only referes ClusterWorkflowTemplates, so we enforce this here:
3,506✔
397
                artifactTypeSpec.WorkflowTemplateRef.ClusterScope = true
3,506✔
398
        } else {
156✔
399
                artifactTypeSpec = &artifactType.Spec
156✔
400
                artifactTypeGen = artifactType.Generation
156✔
401
        }
156✔
402

403
        if len(artifactTypeSpec.Rules.SrcTypes) > 0 && !slices.Contains(artifactTypeSpec.Rules.SrcTypes, srcEndpoint.Spec.Type) {
3,673✔
404
                err := fmt.Errorf("source endpoint type '%s' is not allowed by ArtifactType rules", srcEndpoint.Spec.Type)
11✔
405
                r.Recorder.Event(order, corev1.EventTypeWarning, "InvalidArtifactType", fmt.Sprintf("Source endpoint type '%s' is not allowed by ArtifactType '%s' rules", srcEndpoint.Spec.Type, artifact.Type))
11✔
406
                return nil, errLogAndWrap(log, err, "artifact validation failed")
11✔
407
        }
11✔
408
        if len(artifactTypeSpec.Rules.DstTypes) > 0 && !slices.Contains(artifactTypeSpec.Rules.DstTypes, dstEndpoint.Spec.Type) {
3,651✔
409
                err := fmt.Errorf("destination endpoint type '%s' is not allowed by ArtifactType rules", dstEndpoint.Spec.Type)
×
410
                r.Recorder.Event(order, corev1.EventTypeWarning, "InvalidArtifactType", fmt.Sprintf("Destination endpoint type '%s' is not allowed by ArtifactType '%s' rules", dstEndpoint.Spec.Type, artifact.Type))
×
411
                return nil, errLogAndWrap(log, err, "artifact validation failed")
×
412
        }
×
413

414
        // Next, we need the secret contents
415
        srcSecret := &corev1.Secret{}
3,651✔
416
        if srcEndpoint.Spec.SecretRef.Name != "" {
7,110✔
417
                if err := r.Get(ctx, namespacedName(order.Namespace, srcEndpoint.Spec.SecretRef.Name), srcSecret); err != nil {
3,459✔
418
                        r.Recorder.Event(order, corev1.EventTypeWarning, "InvalidSecret", fmt.Sprintf("Failed to fetch source secret '%s': %v", srcEndpoint.Spec.SecretRef.Name, err))
×
419
                        return nil, errLogAndWrap(log, err, "failed to fetch secret for source")
×
420
                }
×
421
        }
422

423
        dstSecret := &corev1.Secret{}
3,651✔
424
        if dstEndpoint.Spec.SecretRef.Name != "" {
7,110✔
425
                if err := r.Get(ctx, namespacedName(order.Namespace, dstEndpoint.Spec.SecretRef.Name), dstSecret); err != nil {
3,459✔
426
                        r.Recorder.Event(order, corev1.EventTypeWarning, "InvalidSecret", fmt.Sprintf("Failed to fetch destination secret '%s': %v", dstEndpoint.Spec.SecretRef.Name, err))
×
427
                        return nil, errLogAndWrap(log, err, "failed to fetch secret for destination")
×
428
                }
×
429
        }
430

431
        // Create a hash based on all related data for idempotency and compute the workflow name
432
        h := sha256.New()
3,651✔
433
        data := []any{
3,651✔
434
                order.Namespace,
3,651✔
435
                artifact.Type, artifact.Spec.Raw, artifactTypeGen,
3,651✔
436
                srcEndpoint.Name,
3,651✔
437
                dstEndpoint.Name,
3,651✔
438
        }
3,651✔
439
        jsonData, err := json.Marshal(data)
3,651✔
440
        if err != nil {
3,651✔
441
                return nil, errLogAndWrap(log, err, "failed to marshal artifact workflow data")
×
442
        }
×
443
        h.Write(jsonData)
3,651✔
444
        sha := hex.EncodeToString(h.Sum(nil))[:16]
3,651✔
445

3,651✔
446
        // We gave all the information to further process this artifact workflow.
3,651✔
447
        // Let's store it to compare it to the current status!
3,651✔
448
        return &desiredAW{
3,651✔
449
                index:       i,
3,651✔
450
                objectMeta:  awObjectMeta(order, sha),
3,651✔
451
                artifact:    artifact,
3,651✔
452
                typeSpec:    artifactTypeSpec,
3,651✔
453
                srcEndpoint: srcEndpoint,
3,651✔
454
                dstEndpoint: dstEndpoint,
3,651✔
455
                srcSecret:   srcSecret,
3,651✔
456
                dstSecret:   dstSecret,
3,651✔
457
                sha:         sha,
3,651✔
458
        }, nil
3,651✔
459
}
460

461
// SetupWithManager sets up the controller with the Manager.
462
func (r *OrderReconciler) SetupWithManager(mgr ctrl.Manager) error {
1✔
463
        return ctrl.NewControllerManagedBy(mgr).
1✔
464
                For(&arcv1alpha1.Order{}).
1✔
465
                Owns(&arcv1alpha1.ArtifactWorkflow{}).
1✔
466
                Complete(r)
1✔
467
}
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