|
| 1 | +/* |
| 2 | +Copyright 2025 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 conversion |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 26 | + "k8s.io/apimachinery/pkg/util/sets" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client/apiutil" |
| 29 | +) |
| 30 | + |
| 31 | +func NewHubSpokeConverter[hubObject runtime.Object](hub runtime.Object, spokeConverter ...SpokeConverter[hubObject]) func(scheme *runtime.Scheme) (Converter, error) { |
| 32 | + return func(scheme *runtime.Scheme) (Converter, error) { |
| 33 | + hubGVK, err := apiutil.GVKForObject(hub, scheme) |
| 34 | + if err != nil { |
| 35 | + return nil, fmt.Errorf("failed to create hub spoke converter: %w", err) |
| 36 | + } |
| 37 | + allGVKs, err := objectGVKs(scheme, hub) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("failed to create hub spoke converter for %s: %w", hubGVK.Kind, err) |
| 40 | + } |
| 41 | + spokeVersions := sets.New[string]() |
| 42 | + for _, gvk := range allGVKs { |
| 43 | + if gvk != hubGVK { |
| 44 | + spokeVersions.Insert(gvk.Version) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + c := &hubSpokeConverter[hubObject]{ |
| 49 | + scheme: scheme, |
| 50 | + hubGVK: hubGVK, |
| 51 | + spokeConverterByGVK: map[schema.GroupVersionKind]SpokeConverter[hubObject]{}, |
| 52 | + } |
| 53 | + |
| 54 | + spokeConverterVersions := sets.New[string]() |
| 55 | + for _, sc := range spokeConverter { |
| 56 | + spokeGVK, err := apiutil.GVKForObject(sc.GetSpoke(), scheme) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + if hubGVK.GroupKind() != spokeGVK.GroupKind() { |
| 61 | + return nil, fmt.Errorf("failed to create hub spoke converter for %s: "+ |
| 62 | + "spoke converter GroupKind %s does not match hub GroupKind %s", |
| 63 | + hubGVK.Kind, spokeGVK.GroupKind(), hubGVK.GroupKind()) |
| 64 | + } |
| 65 | + |
| 66 | + if _, ok := c.spokeConverterByGVK[spokeGVK]; ok { |
| 67 | + return nil, fmt.Errorf("failed to create hub spoke converter for %s: "+ |
| 68 | + "duplicate spoke converter for version %s", |
| 69 | + hubGVK.Kind, spokeGVK.Version) |
| 70 | + } |
| 71 | + c.spokeConverterByGVK[spokeGVK] = sc |
| 72 | + spokeConverterVersions.Insert(spokeGVK.Version) |
| 73 | + } |
| 74 | + |
| 75 | + if !spokeConverterVersions.Equal(spokeVersions) { |
| 76 | + return nil, fmt.Errorf("failed to create hub spoke converter for %s: "+ |
| 77 | + "expected spoke converter for %s got spoke converter for %s", |
| 78 | + hubGVK.Kind, strings.Join(spokeVersions.UnsortedList(), ","), strings.Join(spokeConverterVersions.UnsortedList(), ",")) |
| 79 | + } |
| 80 | + |
| 81 | + return c, nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +type hubSpokeConverter[hubObject runtime.Object] struct { |
| 86 | + scheme *runtime.Scheme |
| 87 | + hubGVK schema.GroupVersionKind |
| 88 | + spokeConverterByGVK map[schema.GroupVersionKind]SpokeConverter[hubObject] |
| 89 | +} |
| 90 | + |
| 91 | +func (c hubSpokeConverter[hubObject]) ConvertObject(ctx context.Context, src, dst runtime.Object) error { |
| 92 | + srcGVK := src.GetObjectKind().GroupVersionKind() |
| 93 | + dstGVK := dst.GetObjectKind().GroupVersionKind() |
| 94 | + |
| 95 | + srcIsHub := c.hubGVK == srcGVK |
| 96 | + dstIsHub := c.hubGVK == dstGVK |
| 97 | + _, srcIsConvertible := c.spokeConverterByGVK[srcGVK] |
| 98 | + _, dstIsConvertible := c.spokeConverterByGVK[dstGVK] |
| 99 | + |
| 100 | + switch { |
| 101 | + case srcIsHub && dstIsConvertible: |
| 102 | + return c.spokeConverterByGVK[dstGVK].ConvertHubToSpoke(ctx, src.(hubObject), dst) |
| 103 | + case dstIsHub && srcIsConvertible: |
| 104 | + return c.spokeConverterByGVK[srcGVK].ConvertSpokeToHub(ctx, src, dst.(hubObject)) |
| 105 | + case srcIsConvertible && dstIsConvertible: |
| 106 | + hubGVK := c.hubGVK |
| 107 | + hub, err := c.scheme.New(hubGVK) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("failed to allocate an instance for gvk %v: %w", hubGVK, err) |
| 110 | + } |
| 111 | + if err := c.spokeConverterByGVK[srcGVK].ConvertSpokeToHub(ctx, src, hub.(hubObject)); err != nil { |
| 112 | + return fmt.Errorf("%T failed to convert to hub version %T : %w", src, hub, err) |
| 113 | + } |
| 114 | + if err := c.spokeConverterByGVK[dstGVK].ConvertHubToSpoke(ctx, hub.(hubObject), dst); err != nil { |
| 115 | + return fmt.Errorf("%T failed to convert from hub version %T : %w", dst, hub, err) |
| 116 | + } |
| 117 | + } |
| 118 | + return fmt.Errorf("%T is not convertible to %T", src, dst) |
| 119 | +} |
| 120 | + |
| 121 | +type SpokeConverter[hubObject runtime.Object] interface { |
| 122 | + GetSpoke() runtime.Object |
| 123 | + ConvertHubToSpoke(ctx context.Context, hub hubObject, spoke runtime.Object) error |
| 124 | + ConvertSpokeToHub(ctx context.Context, spoke runtime.Object, hub hubObject) error |
| 125 | +} |
| 126 | + |
| 127 | +func NewSpokeConverter[hubObject, spokeObject client.Object]( |
| 128 | + spoke spokeObject, |
| 129 | + convertHubToSpokeFunc func(ctx context.Context, src hubObject, dst spokeObject) error, |
| 130 | + convertSpokeToHubFunc func(ctx context.Context, src spokeObject, dst hubObject) error, |
| 131 | +) SpokeConverter[hubObject] { |
| 132 | + return &spokeConverter[hubObject, spokeObject]{ |
| 133 | + spoke: spoke, |
| 134 | + convertSpokeToHubFunc: convertSpokeToHubFunc, |
| 135 | + convertHubToSpokeFunc: convertHubToSpokeFunc, |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +type spokeConverter[hubObject, spokeObject runtime.Object] struct { |
| 140 | + spoke spokeObject |
| 141 | + convertHubToSpokeFunc func(ctx context.Context, src hubObject, dst spokeObject) error |
| 142 | + convertSpokeToHubFunc func(ctx context.Context, src spokeObject, dst hubObject) error |
| 143 | +} |
| 144 | + |
| 145 | +func (c spokeConverter[hubObject, spokeObject]) GetSpoke() runtime.Object { |
| 146 | + return c.spoke |
| 147 | +} |
| 148 | + |
| 149 | +func (c spokeConverter[hubObject, spokeObject]) ConvertHubToSpoke(ctx context.Context, hub hubObject, spoke runtime.Object) error { |
| 150 | + return c.convertHubToSpokeFunc(ctx, hub, spoke.(spokeObject)) |
| 151 | +} |
| 152 | + |
| 153 | +func (c spokeConverter[hubObject, spokeObject]) ConvertSpokeToHub(ctx context.Context, spoke runtime.Object, hub hubObject) error { |
| 154 | + return c.convertSpokeToHubFunc(ctx, spoke.(spokeObject), hub) |
| 155 | +} |
0 commit comments