-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove.go
58 lines (51 loc) · 1.24 KB
/
remove.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
package workutils
import (
"slices"
"k8s.io/client-go/kubernetes/scheme"
workapiv1 "open-cluster-management.io/api/work/v1"
)
// Remove removes the resource with the given APIVersion, Kind, Name, and Namespace
func (client *WorkUtilsClient) Remove(work workapiv1.ManifestWork, resource Resource) (workapiv1.ManifestWork, error) {
err := validate(resource)
if err != nil {
return work, err
}
manifests := work.Spec.Workload.Manifests
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 != resource.Group {
continue
}
} else {
if gvk.Version != resource.Version {
continue
}
}
if gvk.Kind != resource.Kind {
continue
}
name, err := getNameFromObject(o)
if err != nil {
return work, err
}
if name == resource.Name {
if resource.Namespace == "" {
work.Spec.Workload.Manifests = slices.Delete(manifests, i, i+1)
return work, nil
}
namespace, err := getNamespaceFromObject(o)
if err != nil {
return work, nil
}
if namespace == resource.Namespace {
work.Spec.Workload.Manifests = slices.Delete(manifests, i, i+1)
return work, nil
}
}
}
return work, nil
}