github.com/uhthomas/helm@v3.0.0-beta.3+incompatible/pkg/kube/resource.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package kube // import "helm.sh/helm/pkg/kube" 18 19 import "k8s.io/cli-runtime/pkg/resource" 20 21 // ResourceList provides convenience methods for comparing collections of Infos. 22 type ResourceList []*resource.Info 23 24 // Append adds an Info to the Result. 25 func (r *ResourceList) Append(val *resource.Info) { 26 *r = append(*r, val) 27 } 28 29 // Visit implements resource.Visitor. 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 // Filter returns a new Result with Infos that satisfy the predicate fn. 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 // Get returns the Info from the result that matches the name and kind. 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 // Contains checks to see if an object exists. 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 // Difference will return a new Result with objects not contained in rs. 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 // Intersect will return a new Result with objects contained in both Results. 78 func (r ResourceList) Intersect(rs ResourceList) ResourceList { 79 return r.Filter(rs.Contains) 80 } 81 82 // isMatchingInfo returns true if infos match on Name and GroupVersionKind. 83 func isMatchingInfo(a, b *resource.Info) bool { 84 return a.Name == b.Name && a.Mapping.GroupVersionKind.Kind == b.Mapping.GroupVersionKind.Kind 85 }