-
Notifications
You must be signed in to change notification settings - Fork 117
/
helpers.go
44 lines (40 loc) · 979 Bytes
/
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
package v1alpha1
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func refreshObject(
ctx context.Context,
c client.Client,
obj client.Object,
nowFunc func() time.Time,
) error {
patchBytes := []byte(
fmt.Sprintf(
`{"metadata":{"annotations":{"%s":"%s"}}}`,
AnnotationKeyRefresh,
nowFunc().UTC().Format(time.RFC3339),
),
)
patch := client.RawPatch(types.MergePatchType, patchBytes)
if err := c.Patch(ctx, obj, patch); err != nil {
return errors.Wrap(err, "patch annotation")
}
return nil
}
func clearRefreshObject(
ctx context.Context,
c client.Client,
obj client.Object,
) error {
patchBytes := []byte(fmt.Sprintf(`{"metadata":{"annotations":{"%s":null}}}`, AnnotationKeyRefresh))
patch := client.RawPatch(types.MergePatchType, patchBytes)
if err := c.Patch(ctx, obj, patch); err != nil {
return errors.Wrap(err, "patch annotation")
}
return nil
}