github.com/ironcore-dev/gardener-extension-provider-ironcore@v0.3.2-0.20240314231816-8336447fb9a0/pkg/apis/ironcore/helper/scheme.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 helper 5 6 import ( 7 "fmt" 8 9 "github.com/gardener/gardener/extensions/pkg/controller" 10 extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1" 11 kutil "github.com/gardener/gardener/pkg/utils/kubernetes" 12 "k8s.io/apimachinery/pkg/runtime" 13 "k8s.io/apimachinery/pkg/runtime/serializer" 14 utilruntime "k8s.io/apimachinery/pkg/util/runtime" 15 16 api "github.com/ironcore-dev/gardener-extension-provider-ironcore/pkg/apis/ironcore" 17 "github.com/ironcore-dev/gardener-extension-provider-ironcore/pkg/apis/ironcore/install" 18 ) 19 20 var ( 21 // Scheme is a scheme with the types relevant for ironcore actuators. 22 Scheme *runtime.Scheme 23 24 decoder runtime.Decoder 25 26 // lenientDecoder is a decoder that does not use strict mode. 27 lenientDecoder runtime.Decoder 28 ) 29 30 func init() { 31 Scheme = runtime.NewScheme() 32 utilruntime.Must(install.AddToScheme(Scheme)) 33 34 decoder = serializer.NewCodecFactory(Scheme, serializer.EnableStrict).UniversalDecoder() 35 lenientDecoder = serializer.NewCodecFactory(Scheme).UniversalDecoder() 36 } 37 38 // InfrastructureConfigFromInfrastructure extracts the InfrastructureConfig from the 39 // ProviderConfig section of the given Infrastructure. 40 func InfrastructureConfigFromInfrastructure(infra *extensionsv1alpha1.Infrastructure) (*api.InfrastructureConfig, error) { 41 config := &api.InfrastructureConfig{} 42 if infra.Spec.ProviderConfig != nil && infra.Spec.ProviderConfig.Raw != nil { 43 if _, _, err := decoder.Decode(infra.Spec.ProviderConfig.Raw, nil, config); err != nil { 44 return nil, err 45 } 46 return config, nil 47 } 48 return &api.InfrastructureConfig{}, nil 49 } 50 51 // InfrastructureStatusFromRaw extracts the InfrastructureStatus from the 52 // ProviderStatus section of the given Infrastructure. 53 func InfrastructureStatusFromRaw(raw *runtime.RawExtension) (*api.InfrastructureStatus, error) { 54 config := &api.InfrastructureStatus{} 55 if raw != nil && raw.Raw != nil { 56 if _, _, err := lenientDecoder.Decode(raw.Raw, nil, config); err != nil { 57 return nil, err 58 } 59 return config, nil 60 } 61 return &api.InfrastructureStatus{}, nil 62 } 63 64 // CloudProfileConfigFromCluster decodes the provider specific cloud profile configuration for a cluster 65 func CloudProfileConfigFromCluster(cluster *controller.Cluster) (*api.CloudProfileConfig, error) { 66 var cloudProfileConfig *api.CloudProfileConfig 67 if cluster != nil && cluster.CloudProfile != nil && cluster.CloudProfile.Spec.ProviderConfig != nil && cluster.CloudProfile.Spec.ProviderConfig.Raw != nil { 68 cloudProfileConfig = &api.CloudProfileConfig{} 69 if _, _, err := decoder.Decode(cluster.CloudProfile.Spec.ProviderConfig.Raw, nil, cloudProfileConfig); err != nil { 70 return nil, fmt.Errorf("could not decode providerConfig of cloudProfile for '%s': %w", kutil.ObjectName(cluster.CloudProfile), err) 71 } 72 } 73 return cloudProfileConfig, nil 74 }