github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/internal/util/porch/function.go (about) 1 // Copyright 2022 The kpt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package porch 16 17 import ( 18 "context" 19 "time" 20 21 "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/cli-runtime/pkg/genericclioptions" 25 26 // Enable the GCP Authentication plugin 27 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" 28 ) 29 30 const Expiration time.Duration = 10 * time.Second 31 32 // FunctionListGetter gets the list of v1alpha1.Functions from the cluster. 33 type FunctionListGetter struct{} 34 35 func (f FunctionListGetter) Get(ctx context.Context) []v1alpha1.Function { 36 kubeflags := genericclioptions.NewConfigFlags(true) 37 client, err := CreateRESTClient(kubeflags) 38 if err != nil { 39 return nil 40 } 41 scheme := runtime.NewScheme() 42 if err := v1alpha1.SchemeBuilder.AddToScheme(scheme); err != nil { 43 return nil 44 } 45 codec := runtime.NewParameterCodec(scheme) 46 var functions v1alpha1.FunctionList 47 if err := client.Get(). 48 Timeout(Expiration). 49 Resource("functions"). 50 VersionedParams(&metav1.GetOptions{}, codec). 51 Do(ctx). 52 Into(&functions); err != nil { 53 return nil 54 } 55 return functions.Items 56 } 57 58 // FunctionGetter gets a specific v1alpha1.Functions by name. 59 type FunctionGetter struct{} 60 61 func (f FunctionGetter) Get(ctx context.Context, name, namespace string) (v1alpha1.Function, error) { 62 kubeflags := genericclioptions.NewConfigFlags(true) 63 var function v1alpha1.Function 64 client, err := CreateRESTClient(kubeflags) 65 if err != nil { 66 return function, err 67 } 68 scheme := runtime.NewScheme() 69 if err := v1alpha1.SchemeBuilder.AddToScheme(scheme); err != nil { 70 return function, err 71 } 72 codec := runtime.NewParameterCodec(scheme) 73 err = client.Get(). 74 Timeout(Expiration). 75 Resource("functions"). 76 Name(name). 77 Namespace(namespace). 78 VersionedParams(&metav1.GetOptions{}, codec). 79 Do(ctx). 80 Into(&function) 81 return function, err 82 } 83 84 func ToShortNames(functions []v1alpha1.Function) []string { 85 var shortNameFunctions []string 86 for _, function := range functions { 87 shortNameFunctions = append(shortNameFunctions, function.Name) 88 } 89 return shortNameFunctions 90 } 91 92 func UnifyKeywords(functions []v1alpha1.Function) []string { 93 var keywords []string 94 keywordsMap := map[string]bool{} 95 for _, function := range functions { 96 for _, kw := range function.Spec.Keywords { 97 if _, ok := keywordsMap[kw]; !ok { 98 keywordsMap[kw] = true 99 keywords = append(keywords, kw) 100 } 101 } 102 } 103 return keywords 104 }