-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
60 lines (50 loc) · 1.37 KB
/
utils.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
package utils
import (
"context"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type VerbType string
type (
TransformStatusFunc func(obj client.Object) client.Object
)
const (
VerbPatched VerbType = "Patched"
VerbUnchanged VerbType = "Unchanged"
)
func PatchStatus(ctx context.Context, c client.Client, obj client.Object, transform TransformStatusFunc, opts ...client.SubResourcePatchOption) (client.Object, VerbType, error) {
key := types.NamespacedName{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}
err := c.Get(ctx, key, obj)
if err != nil {
return nil, VerbUnchanged, err
}
// The body of the request was in an unknown format -
// accepted media types include:
// - application/json-patch+json,
// - application/merge-patch+json,
// - application/apply-patch+yaml
patch := client.MergeFrom(obj)
obj = transform(obj.DeepCopyObject().(client.Object))
err = c.Status().Patch(ctx, obj, patch, opts...)
if err != nil {
return nil, VerbUnchanged, err
}
return obj, VerbPatched, nil
}
func MakeKubeClientSet(restConfig rest.Config) (*kubernetes.Clientset, error) {
clientset, err := kubernetes.NewForConfig(&restConfig)
if err != nil {
return nil, err
}
return clientset, nil
}
type EnvVars struct {
}
func SetEnvVars() *EnvVars {
return &EnvVars{}
}