-
Notifications
You must be signed in to change notification settings - Fork 0
/
createorupdate.go
87 lines (70 loc) · 2.14 KB
/
createorupdate.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
80
81
82
83
84
85
86
87
package crutil
import (
"context"
"encoding/json"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/sergi/go-diff/diffmatchpatch"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
util "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
func spewDiff(a, b interface{}) string {
aStr := spew.Sdump(a)
bStr := spew.Sdump(b)
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(aStr, bStr, false)
return dmp.DiffPrettyText(diffs)
}
func jsonDiff(a, b interface{}) string {
aStr, _ := json.MarshalIndent(a, "", " ")
bStr, _ := json.MarshalIndent(b, "", " ")
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(string(aStr), string(bStr), false)
return dmp.DiffPrettyText(diffs)
}
type Object interface {
metav1.Object
runtime.Object
}
// also copied from controllerutil package
func mutate(f util.MutateFn, key client.ObjectKey, obj client.Object) error {
if err := f(); err != nil {
return err
}
if newKey := client.ObjectKeyFromObject(obj); key != newKey {
return fmt.Errorf("MutateFn cannot mutate object name and/or object namespace")
}
return nil
}
var _ = util.CreateOrUpdate
func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object, f util.MutateFn) (util.OperationResult, error) {
key := client.ObjectKeyFromObject(obj)
if err := c.Get(ctx, key, obj); err != nil {
if !apierrors.IsNotFound(err) {
return util.OperationResultNone, err
}
if err := mutate(f, key, obj); err != nil {
return util.OperationResultNone, err
}
if err := c.Create(ctx, obj); err != nil {
return util.OperationResultNone, err
}
return util.OperationResultCreated, nil
}
existing := obj.DeepCopyObject()
if err := mutate(f, key, obj); err != nil {
return util.OperationResultNone, err
}
if equality.Semantic.DeepEqual(existing, obj) {
return util.OperationResultNone, nil
}
if err := c.Update(ctx, obj); err != nil {
return util.OperationResultNone, err
}
fmt.Println(jsonDiff(existing, obj))
return util.OperationResultUpdated, nil
}