sigs.k8s.io/controller-runtime@v0.18.2/pkg/envtest/helper.go (about) 1 /* 2 Copyright 2021 The Kubernetes 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 envtest 18 19 import ( 20 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 21 "k8s.io/client-go/kubernetes/scheme" 22 ) 23 24 var ( 25 crdScheme = scheme.Scheme 26 ) 27 28 // init is required to correctly initialize the crdScheme package variable. 29 func init() { 30 _ = apiextensionsv1.AddToScheme(crdScheme) 31 } 32 33 // mergePaths merges two string slices containing paths. 34 // This function makes no guarantees about order of the merged slice. 35 func mergePaths(s1, s2 []string) []string { 36 m := make(map[string]struct{}) 37 for _, s := range s1 { 38 m[s] = struct{}{} 39 } 40 for _, s := range s2 { 41 m[s] = struct{}{} 42 } 43 merged := make([]string, len(m)) 44 i := 0 45 for key := range m { 46 merged[i] = key 47 i++ 48 } 49 return merged 50 } 51 52 // mergeCRDs merges two CRD slices using their names. 53 // This function makes no guarantees about order of the merged slice. 54 func mergeCRDs(s1, s2 []*apiextensionsv1.CustomResourceDefinition) []*apiextensionsv1.CustomResourceDefinition { 55 m := make(map[string]*apiextensionsv1.CustomResourceDefinition) 56 for _, obj := range s1 { 57 m[obj.GetName()] = obj 58 } 59 for _, obj := range s2 { 60 m[obj.GetName()] = obj 61 } 62 merged := make([]*apiextensionsv1.CustomResourceDefinition, len(m)) 63 i := 0 64 for _, obj := range m { 65 merged[i] = obj.DeepCopy() 66 i++ 67 } 68 return merged 69 }