-
Notifications
You must be signed in to change notification settings - Fork 141
/
stage_helpers.go
79 lines (74 loc) · 2.09 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
package v1alpha1
import (
"context"
"time"
"github.com/pkg/errors"
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, errors.Wrapf(
err,
"error getting Stage %q in namespace %q",
namespacedName.Name,
namespacedName.Namespace,
)
}
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 := refreshObject(ctx, c, stage, time.Now); err != nil {
return nil, errors.Wrap(err, "refresh")
}
return stage, nil
}
// ClearStageRefresh is called by the Stage controller to clear the refresh
// annotation on the Stage (if present). A client (e.g. UI) who requested a
// Stage refresh, can wait until the annotation is cleared, to understand that
// the controller successfully reconciled the Stage after the refresh request.
func ClearStageRefresh(
ctx context.Context,
c client.Client,
stage *Stage,
) error {
if stage.Annotations == nil {
return nil
}
if _, ok := stage.Annotations[AnnotationKeyRefresh]; !ok {
return nil
}
newStage := Stage{
ObjectMeta: metav1.ObjectMeta{
Name: stage.Name,
Namespace: stage.Namespace,
},
}
return clearRefreshObject(ctx, c, &newStage)
}