...
1
16
17 package kube
18
19 import "k8s.io/cli-runtime/pkg/resource"
20
21
22 type ResourceList []*resource.Info
23
24
25 func (r *ResourceList) Append(val *resource.Info) {
26 *r = append(*r, val)
27 }
28
29
30 func (r ResourceList) Visit(fn resource.VisitorFunc) error {
31 for _, i := range r {
32 if err := fn(i, nil); err != nil {
33 return err
34 }
35 }
36 return nil
37 }
38
39
40 func (r ResourceList) Filter(fn func(*resource.Info) bool) ResourceList {
41 var result ResourceList
42 for _, i := range r {
43 if fn(i) {
44 result.Append(i)
45 }
46 }
47 return result
48 }
49
50
51 func (r ResourceList) Get(info *resource.Info) *resource.Info {
52 for _, i := range r {
53 if isMatchingInfo(i, info) {
54 return i
55 }
56 }
57 return nil
58 }
59
60
61 func (r ResourceList) Contains(info *resource.Info) bool {
62 for _, i := range r {
63 if isMatchingInfo(i, info) {
64 return true
65 }
66 }
67 return false
68 }
69
70
71 func (r ResourceList) Difference(rs ResourceList) ResourceList {
72 return r.Filter(func(info *resource.Info) bool {
73 return !rs.Contains(info)
74 })
75 }
76
77
78 func (r ResourceList) Intersect(rs ResourceList) ResourceList {
79 return r.Filter(rs.Contains)
80 }
81
82
83 func isMatchingInfo(a, b *resource.Info) bool {
84 return a.Name == b.Name && a.Namespace == b.Namespace && a.Mapping.GroupVersionKind.Kind == b.Mapping.GroupVersionKind.Kind
85 }
86
View as plain text