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