github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/resourcemanager/reporter/converter.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 reporter 18 19 import ( 20 "context" 21 "sync" 22 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 25 "github.com/kubewharf/katalyst-api/pkg/protocol/reporterplugin/v1alpha1" 26 "github.com/kubewharf/katalyst-core/pkg/client" 27 "github.com/kubewharf/katalyst-core/pkg/config" 28 "github.com/kubewharf/katalyst-core/pkg/metaserver" 29 "github.com/kubewharf/katalyst-core/pkg/metrics" 30 ) 31 32 // Converter converts the report fields to the format the reporter expects, for example, 33 // if the target crd version needs to be changed from v1alpha1 to v1, the converter can convert 34 // the v1alpha1's plugin report fields to v1 fields and report to the v1 reporter 35 type Converter interface { 36 Convert(ctx context.Context, reportFields []*v1alpha1.ReportField) (*v1alpha1.ReportContent, error) 37 } 38 39 var converterInitializers sync.Map 40 41 // ConverterInitFunc is the function to initialize a converter 42 type ConverterInitFunc func(*client.GenericClientSet, *metaserver.MetaServer, 43 metrics.MetricEmitter, *config.Configuration) (Converter, error) 44 45 // RegisterConverterInitializer registers a converter initializer function 46 // for a specific gvk, the function will be called when the reporter manager 47 // is created. The function should return a converter for the gvk. If the 48 // function is called multiple times for the same gvk, the last one will be 49 // used. 50 func RegisterConverterInitializer(gvk metav1.GroupVersionKind, initFunc ConverterInitFunc) { 51 converterInitializers.Store(gvk, initFunc) 52 } 53 54 func getRegisteredConverterInitializers() map[metav1.GroupVersionKind]ConverterInitFunc { 55 converters := make(map[metav1.GroupVersionKind]ConverterInitFunc) 56 converterInitializers.Range(func(key, value interface{}) bool { 57 converters[key.(metav1.GroupVersionKind)] = value.(ConverterInitFunc) 58 return true 59 }) 60 return converters 61 }