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

opendefensecloud / artifact-conduit / 19921133521

04 Dec 2025 07:28AM UTC coverage: 65.622% (-0.2%) from 65.824%
19921133521

push

github

jastBytes
Potential fix for code scanning alert no. 7: Workflow does not contain permissions

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

649 of 989 relevant lines covered (65.62%)

411.9 hits per line

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

85.31
/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) {
1,158✔
62
        log := ctrl.LoggerFrom(ctx)
1,158✔
63
        ctrlResult := ctrl.Result{}
1,158✔
64

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

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

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

3✔
83
                // Cleanup all artifact workflows
3✔
84
                if len(order.Status.ArtifactWorkflows) > 0 {
5✔
85
                        for sha := range order.Status.ArtifactWorkflows {
6✔
86
                                // Remove ArtifactWorkflow
4✔
87
                                aw := &arcv1alpha1.ArtifactWorkflow{
4✔
88
                                        ObjectMeta: awObjectMeta(order, sha),
4✔
89
                                }
4✔
90
                                _ = r.Delete(ctx, aw) // Ignore errors
4✔
91
                                delete(order.Status.ArtifactWorkflows, sha)
4✔
92
                        }
4✔
93
                        if err := r.Status().Update(ctx, order); err != nil {
3✔
94
                                return ctrlResult, errLogAndWrap(log, err, "failed to update order status")
1✔
95
                        }
1✔
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() {
2,308✔
116
                if !slices.Contains(order.Finalizers, orderFinalizer) {
1,171✔
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,137✔
129
        if err != nil {
1,137✔
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,139✔
133
                log.V(1).Info("Force reconcile requested")
2✔
134
                r.Recorder.Event(order, corev1.EventTypeNormal, "ForceReconcile", "Force reconcile requested via annotation")
2✔
135
                // Delete existing artifact workflows to force re-creation
2✔
136
                for sha := range order.Status.ArtifactWorkflows {
4✔
137
                        // Remove Secret and ArtifactWorkflow
2✔
138
                        aw := &arcv1alpha1.ArtifactWorkflow{
2✔
139
                                ObjectMeta: awObjectMeta(order, sha),
2✔
140
                        }
2✔
141
                        _ = r.Delete(ctx, aw) // Ignore errors
2✔
142
                        delete(order.Status.ArtifactWorkflows, sha)
2✔
143
                }
2✔
144
                // Update last force time
145
                order.Status.LastForceAt = metav1.Now()
2✔
146
                if err := r.Status().Update(ctx, order); err != nil {
3✔
147
                        return ctrlResult, errLogAndWrap(log, err, "failed to update last force time")
1✔
148
                }
1✔
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 {
1,231✔
155
                order.Status.ArtifactWorkflows = map[string]arcv1alpha1.OrderArtifactWorkflowStatus{}
96✔
156
        }
96✔
157

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

1,027✔
175
        // List missing artifact workflows
1,027✔
176
        createAWs := []string{}
1,027✔
177
        for sha := range desiredAWs {
2,512✔
178
                _, exists := order.Status.ArtifactWorkflows[sha]
1,485✔
179
                if exists {
2,945✔
180
                        continue
1,460✔
181
                }
182
                createAWs = append(createAWs, sha)
25✔
183
        }
184

185
        // Find obsolete artifact workflows
186
        deleteAWs := []string{}
1,027✔
187
        for sha := range order.Status.ArtifactWorkflows {
2,488✔
188
                _, exists := desiredAWs[sha]
1,461✔
189
                if exists {
2,921✔
190
                        continue
1,460✔
191
                }
192
                deleteAWs = append(deleteAWs, sha)
1✔
193
        }
194

195
        // Find finished artifact workflows to clean up
196
        finishedAWs := []string{}
1,027✔
197
        for sha := range order.Status.ArtifactWorkflows {
2,488✔
198
                awStatus := order.Status.ArtifactWorkflows[sha]
1,461✔
199

1,461✔
200
                // Only consider succeeded workflows for TTL cleanup
1,461✔
201
                if awStatus.Phase != arcv1alpha1.WorkflowSucceeded {
2,875✔
202
                        continue
1,414✔
203
                }
204

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

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

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

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

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

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

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

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

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

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

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

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

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

322
        return ctrlResult, nil
998✔
323
}
324

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

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

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

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

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

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

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

404
        if len(artifactTypeSpec.Rules.SrcTypes) > 0 && !slices.Contains(artifactTypeSpec.Rules.SrcTypes, srcEndpoint.Spec.Type) {
1,507✔
405
                err := fmt.Errorf("source endpoint type '%s' is not allowed by ArtifactType rules", srcEndpoint.Spec.Type)
10✔
406
                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))
10✔
407
                return nil, errLogAndWrap(log, err, "artifact validation failed")
10✔
408
        }
10✔
409
        if len(artifactTypeSpec.Rules.DstTypes) > 0 && !slices.Contains(artifactTypeSpec.Rules.DstTypes, dstEndpoint.Spec.Type) {
1,487✔
410
                err := fmt.Errorf("destination endpoint type '%s' is not allowed by ArtifactType rules", dstEndpoint.Spec.Type)
×
411
                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))
×
412
                return nil, errLogAndWrap(log, err, "artifact validation failed")
×
413
        }
×
414

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

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

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

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

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