/
patch.go
39 lines (31 loc) · 1.01 KB
/
patch.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
package statushandler
import (
"context"
"encoding/json"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1"
)
type patchValue struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
// patchUpdateStatus performs the JSONPatch patch update to the Atlas Custom Resource.
// The "jsonPatch" merge allows to update only status field so is more
func patchUpdateStatus(kubeClient client.Client, resource mdbv1.AtlasCustomResource) error {
return doPatch(kubeClient, resource, resource.GetStatus())
}
func doPatch(kubeClient client.Client, resource client.Object, statusValue interface{}) error {
payload := []patchValue{{
Op: "replace",
Path: "/status",
Value: statusValue,
}}
data, err := json.Marshal(payload)
if err != nil {
return err
}
patch := client.RawPatch(types.JSONPatchType, data)
return kubeClient.Status().Patch(context.Background(), resource, patch)
}