-
Notifications
You must be signed in to change notification settings - Fork 117
/
promotion_helpers.go
55 lines (51 loc) · 1.5 KB
/
promotion_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
package v1alpha1
import (
"context"
"fmt"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// GetPromotion returns a pointer to the Promotion resource specified by the
// namespacedName argument. If no such resource is found, nil is returned
// instead.
func GetPromotion(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
) (*Promotion, error) {
promo := Promotion{}
if err := c.Get(ctx, namespacedName, &promo); err != nil {
if err = client.IgnoreNotFound(err); err == nil {
return nil, nil
}
return nil, fmt.Errorf(
"error getting Promotion %q in namespace %q: %w",
namespacedName.Name,
namespacedName.Namespace,
err,
)
}
return &promo, nil
}
// RefreshPromotion forces reconciliation of a Promotion by setting an annotation
// on the Promotion, 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 RefreshPromotion(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
) (*Promotion, error) {
promo := &Promotion{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespacedName.Namespace,
Name: namespacedName.Name,
},
}
if err := patchAnnotation(ctx, c, promo, AnnotationKeyRefresh, time.Now().Format(time.RFC3339)); err != nil {
return nil, fmt.Errorf("refresh: %w", err)
}
return promo, nil
}