k8s.io/client-go@v0.31.1/tools/events/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 events 18 19 import ( 20 "fmt" 21 22 corev1 "k8s.io/api/core/v1" 23 eventsv1 "k8s.io/api/events/v1" 24 eventsv1beta1 "k8s.io/api/events/v1beta1" 25 "k8s.io/apimachinery/pkg/fields" 26 "k8s.io/apimachinery/pkg/runtime/schema" 27 "k8s.io/apimachinery/pkg/types" 28 ) 29 30 var mapping = map[schema.GroupVersion]string{ 31 eventsv1.SchemeGroupVersion: "regarding", 32 eventsv1beta1.SchemeGroupVersion: "regarding", 33 corev1.SchemeGroupVersion: "involvedObject", 34 } 35 36 // GetFieldSelector returns the appropriate field selector based on the API version being used to communicate with the server. 37 // The returned field selector can be used with List and Watch to filter desired events. 38 func GetFieldSelector(eventsGroupVersion schema.GroupVersion, regardingGroupVersionKind schema.GroupVersionKind, regardingName string, regardingUID types.UID) (fields.Selector, error) { 39 field := fields.Set{} 40 41 if _, ok := mapping[eventsGroupVersion]; !ok { 42 return nil, fmt.Errorf("unknown version %v", eventsGroupVersion) 43 } 44 prefix := mapping[eventsGroupVersion] 45 46 if len(regardingName) > 0 { 47 field[prefix+".name"] = regardingName 48 } 49 50 if len(regardingGroupVersionKind.Kind) > 0 { 51 field[prefix+".kind"] = regardingGroupVersionKind.Kind 52 } 53 54 regardingGroupVersion := regardingGroupVersionKind.GroupVersion() 55 if !regardingGroupVersion.Empty() { 56 field[prefix+".apiVersion"] = regardingGroupVersion.String() 57 } 58 59 if len(regardingUID) > 0 { 60 field[prefix+".uid"] = string(regardingUID) 61 } 62 63 return field.AsSelector(), nil 64 }