github.com/ironcore-dev/gardener-extension-provider-ironcore@v0.3.2-0.20240314231816-8336447fb9a0/pkg/apis/config/loader/loader.go (about) 1 // SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and IronCore contributors 2 // SPDX-License-Identifier: Apache-2.0 3 4 package loader 5 6 import ( 7 "os" 8 9 "k8s.io/apimachinery/pkg/runtime" 10 "k8s.io/apimachinery/pkg/runtime/schema" 11 "k8s.io/apimachinery/pkg/runtime/serializer/json" 12 "k8s.io/apimachinery/pkg/runtime/serializer/versioning" 13 14 "github.com/ironcore-dev/gardener-extension-provider-ironcore/pkg/apis/config" 15 "github.com/ironcore-dev/gardener-extension-provider-ironcore/pkg/apis/config/install" 16 ) 17 18 var ( 19 codec runtime.Codec 20 scheme *runtime.Scheme 21 ) 22 23 func init() { 24 scheme = runtime.NewScheme() 25 install.Install(scheme) 26 yamlSerializer := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme, scheme) 27 codec = versioning.NewDefaultingCodecForScheme( 28 scheme, 29 yamlSerializer, 30 yamlSerializer, 31 schema.GroupVersion{Version: "v1alpha1"}, 32 runtime.InternalGroupVersioner, 33 ) 34 } 35 36 // LoadFromFile takes a filename and de-serializes the contents into ControllerConfiguration object. 37 func LoadFromFile(filename string) (*config.ControllerConfiguration, error) { 38 bytes, err := os.ReadFile(filename) 39 if err != nil { 40 return nil, err 41 } 42 43 return Load(bytes) 44 } 45 46 // Load takes a byte slice and de-serializes the contents into ControllerConfiguration object. 47 // Encapsulates de-serialization without assuming the source is a file. 48 func Load(data []byte) (*config.ControllerConfiguration, error) { 49 cfg := &config.ControllerConfiguration{} 50 51 if len(data) == 0 { 52 return cfg, nil 53 } 54 55 decoded, _, err := codec.Decode(data, &schema.GroupVersionKind{Version: "v1alpha1", Kind: "Config"}, cfg) 56 if err != nil { 57 return nil, err 58 } 59 60 return decoded.(*config.ControllerConfiguration), nil 61 }