-
Notifications
You must be signed in to change notification settings - Fork 114
/
stage_helpers.go
125 lines (115 loc) · 3.76 KB
/
stage_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package v1alpha1
import (
"context"
"errors"
"fmt"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// GetStage returns a pointer to the Stage resource specified by the
// namespacedName argument. If no such resource is found, nil is returned
// instead.
func GetStage(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
) (*Stage, error) {
stage := Stage{}
if err := c.Get(ctx, namespacedName, &stage); err != nil {
if err = client.IgnoreNotFound(err); err == nil {
return nil, nil
}
return nil, fmt.Errorf(
"error getting Stage %q in namespace %q: %w",
namespacedName.Name,
namespacedName.Namespace,
err,
)
}
return &stage, nil
}
// RefreshStage forces reconciliation of a Stage by setting an annotation
// on the Stage, causing the controller to reconcile it. Currently, the
// annotation value is the timestamp of the request, but might in the
// future include additional metadata/context necessary for the request.
func RefreshStage(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
) (*Stage, error) {
stage := &Stage{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespacedName.Namespace,
Name: namespacedName.Name,
},
}
if err := patchAnnotation(ctx, c, stage, AnnotationKeyRefresh, time.Now().Format(time.RFC3339)); err != nil {
return nil, fmt.Errorf("refresh: %w", err)
}
return stage, nil
}
// ReverifyStageFreight forces reconfirmation of the verification of the
// Freight associated with a Stage by setting an AnnotationKeyReverify
// annotation on the Stage, causing the controller to rerun the verification.
// The annotation value is the identifier of the existing VerificationInfo for
// the Stage.
func ReverifyStageFreight(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
) error {
stage, err := GetStage(ctx, c, namespacedName)
if err != nil || stage == nil {
if stage == nil {
err = fmt.Errorf("Stage %q in namespace %q not found", namespacedName.Name, namespacedName.Namespace)
}
return err
}
curFreight := stage.Status.CurrentFreight
if curFreight == nil {
return errors.New("stage has no current freight")
}
if curFreight.VerificationInfo == nil {
return errors.New("stage has no existing verification info")
}
if curFreight.VerificationInfo.ID == "" {
return fmt.Errorf("stage verification info has no ID")
}
return patchAnnotation(ctx, c, stage, AnnotationKeyReverify, curFreight.VerificationInfo.ID)
}
// AbortStageFreightVerification forces aborting the verification of the
// Freight associated with a Stage by setting an AnnotationKeyAbort
// annotation on the Stage, causing the controller to abort the verification.
// The annotation value is the identifier of the existing VerificationInfo for
// the Stage.
func AbortStageFreightVerification(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
) error {
stage, err := GetStage(ctx, c, namespacedName)
if err != nil || stage == nil {
if stage == nil {
err = fmt.Errorf("Stage %q in namespace %q not found", namespacedName.Name, namespacedName.Namespace)
}
return err
}
curFreight := stage.Status.CurrentFreight
if curFreight == nil {
return errors.New("stage has no current freight")
}
if curFreight.VerificationInfo == nil {
return errors.New("stage has no existing verification info")
}
if stage.Status.CurrentFreight.VerificationInfo.Phase.IsTerminal() {
// The verification is already in a terminal phase, so we can skip the
// abort request.
return nil
}
if curFreight.VerificationInfo.ID == "" {
return fmt.Errorf("stage verification info has no ID")
}
return patchAnnotation(ctx, c, stage, AnnotationKeyAbort, curFreight.VerificationInfo.ID)
}