-
Notifications
You must be signed in to change notification settings - Fork 117
/
warehouse_helpers.go
61 lines (57 loc) · 1.54 KB
/
warehouse_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
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"
)
// GetWarehouse returns a pointer to the Warehouse resource specified by the
// namespacedName argument. If no such resource is found, nil is returned
// instead.
func GetWarehouse(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
) (*Warehouse, error) {
warehouse := Warehouse{}
if err := c.Get(ctx, namespacedName, &warehouse); err != nil {
if err = client.IgnoreNotFound(err); err == nil {
return nil, nil
}
return nil, fmt.Errorf(
"error getting Warehouse %q in namespace %q: %w",
namespacedName.Name,
namespacedName.Namespace,
err,
)
}
return &warehouse, nil
}
// RefreshWarehouse forces reconciliation of a Warehouse by setting an annotation
// on the Warehouse, 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 RefreshWarehouse(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
) (*Warehouse, error) {
warehouse := &Warehouse{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespacedName.Namespace,
Name: namespacedName.Name,
},
}
if err := patchAnnotation(
ctx,
c,
warehouse,
AnnotationKeyRefresh,
time.Now().Format(time.RFC3339),
); err != nil {
return nil, fmt.Errorf("refresh: %w", err)
}
return warehouse, nil
}