github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/native/informer_transformed_pod.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 native 18 19 import ( 20 "fmt" 21 "sync" 22 23 corev1 "k8s.io/api/core/v1" 24 "k8s.io/client-go/tools/cache" 25 ) 26 27 type PodTransformerFunc func(src, dest *corev1.Pod) 28 29 var ( 30 podTransformers []PodTransformerFunc 31 podTransformerMtx sync.RWMutex 32 ) 33 34 func SetPodTransformer(f PodTransformerFunc) { 35 podTransformerMtx.Lock() 36 defer podTransformerMtx.Unlock() 37 podTransformers = append(podTransformers, f) 38 } 39 40 func GetPodTransformer() (cache.TransformFunc, bool) { 41 podTransformerMtx.RLock() 42 defer podTransformerMtx.RUnlock() 43 44 if len(podTransformers) == 0 { 45 return nil, false 46 } 47 48 return func(obj interface{}) (interface{}, error) { 49 pod, ok := obj.(*corev1.Pod) 50 if !ok { 51 return nil, fmt.Errorf("unexpected object type: %T", obj) 52 } 53 54 returnedPod := &corev1.Pod{ 55 TypeMeta: pod.TypeMeta, 56 ObjectMeta: pod.ObjectMeta, 57 } 58 for _, f := range podTransformers { 59 f(pod, returnedPod) 60 } 61 return returnedPod, nil 62 }, true 63 }