-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
75 lines (69 loc) · 1.56 KB
/
update.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
package workutils
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
workapiv1 "open-cluster-management.io/api/work/v1"
)
// Update updates ManifestWork
func (client *WorkUtilsClient) Update(work workapiv1.ManifestWork, obj runtime.Object) (workapiv1.ManifestWork, error) {
manifests := work.Spec.Workload.Manifests
group, version, kind, err := getGVKFromObject(obj)
if err != nil {
return work, err
}
objName, err := getNameFromObject(obj)
if err != nil {
return work, err
}
objNamespace, err := getNamespaceFromObject(obj)
if err != nil {
return work, err
}
for i, manifest := range manifests {
o, gvk, err := decode(manifest.Raw, scheme.Scheme)
if err != nil {
return work, err
}
if gvk.Group != "" {
if gvk.Group != group {
continue
}
} else {
if gvk.Version != version {
continue
}
}
if gvk.Kind != kind {
continue
}
name, err := getNameFromObject(o)
if err != nil {
return work, err
}
rawExtension, err := objToRawExtension(obj, client.Scheme)
if err != nil {
return work, err
}
if name == objName {
if objNamespace == "" {
manifests[i] = workapiv1.Manifest{
RawExtension: rawExtension,
}
work.Spec.Workload.Manifests = manifests
return work, nil
}
namespace, err := getNamespaceFromObject(o)
if err != nil {
return work, nil
}
if namespace == objNamespace {
manifests[i] = workapiv1.Manifest{
RawExtension: rawExtension,
}
work.Spec.Workload.Manifests = manifests
return work, nil
}
}
}
return work, nil
}