/
init.go
67 lines (55 loc) · 1.56 KB
/
init.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
package handler
import (
"encoding/json"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/caicloud/cyclone/pkg/apis/cyclone/v1alpha1"
"github.com/caicloud/cyclone/pkg/util/k8s"
)
var (
// K8sClient is used to operate k8s resources
K8sClient k8s.Interface
)
// Init initializes the server resources handlers.
func Init(c k8s.Interface) {
K8sClient = c
}
// BuildPatch builds string patch from input p map,
// map key used as patch path, map value used as patch value.
func BuildPatch(p map[string]string) ([]byte, error) {
patches := []JSONPatch{}
for p, v := range p {
patch := JSONPatch{
Op: "replace",
Path: p,
Value: v,
}
patches = append(patches, patch)
}
patchesBytes, err := json.Marshal(patches)
if err != nil {
return nil, err
}
return patchesBytes, nil
}
// JSONPatch contains information about application/json-patch+json type patch
type JSONPatch struct {
// Op represents operation type
Op string `json:"op"`
// Path represents elements will be operate
Path string `json:"path"`
// Value represents the new value of the operation elements
Value string `json:"value"`
}
// BuildWfrStatusPatch builds patch for updating status of workflowrun
func BuildWfrStatusPatch(statusPhase v1alpha1.StatusPhase, reason string) ([]byte, error) {
now := metav1.Time{Time: time.Now()}
p := map[string]string{
"/status/overall/phase": string(statusPhase),
"/status/overall/lastTransitionTime": now.UTC().Format(time.RFC3339),
}
if len(reason) > 0 {
p["/status/overall/reason"] = reason
}
return BuildPatch(p)
}