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