|
| 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 v1alpha2 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 24 | + runtime "k8s.io/apimachinery/pkg/runtime" |
| 25 | + |
| 26 | + v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1" |
| 27 | +) |
| 28 | + |
| 29 | +// ConvertTo converts this InferencePool (v1alpha2) to the v1 version. |
| 30 | +func (src *InferencePool) ConvertTo() (*v1.InferencePool, error) { |
| 31 | + if src == nil { |
| 32 | + return nil, nil |
| 33 | + } |
| 34 | + |
| 35 | + v1EndPointPickerConfig, err := convertEndpointPickerConfigToV1(&src.Spec.EndpointPickerConfig) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + v1Status, err := converStatusToV1(src.Status) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + dst := &v1.InferencePool{ |
| 44 | + TypeMeta: src.TypeMeta, |
| 45 | + ObjectMeta: src.ObjectMeta, |
| 46 | + Spec: v1.InferencePoolSpec{ |
| 47 | + TargetPortNumber: src.Spec.TargetPortNumber, |
| 48 | + EndpointPickerConfig: *v1EndPointPickerConfig, |
| 49 | + }, |
| 50 | + Status: *v1Status, |
| 51 | + } |
| 52 | + if src.Spec.Selector != nil { |
| 53 | + dst.Spec.Selector.MatchLabels = make(map[v1.LabelKey]v1.LabelValue, len(src.Spec.Selector)) |
| 54 | + for k, v := range src.Spec.Selector { |
| 55 | + dst.Spec.Selector.MatchLabels[v1.LabelKey(k)] = v1.LabelValue(v) |
| 56 | + } |
| 57 | + } |
| 58 | + return dst, nil |
| 59 | +} |
| 60 | + |
| 61 | +// ConvertFrom converts from the v1 version to this version (v1alpha2). |
| 62 | +func ConvertFrom(src *v1.InferencePool) (*InferencePool, error) { |
| 63 | + if src == nil { |
| 64 | + return nil, nil |
| 65 | + } |
| 66 | + |
| 67 | + endPointPickerConfig, err := convertEndpointPickerConfigFromV1(&src.Spec.EndpointPickerConfig) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + status, err := converStatusFromV1(src.Status) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + dst := &InferencePool{ |
| 76 | + TypeMeta: metav1.TypeMeta{ |
| 77 | + Kind: "InferencePool", |
| 78 | + APIVersion: "inference.networking.x-k8s.io/v1alpha2", |
| 79 | + }, |
| 80 | + ObjectMeta: src.ObjectMeta, |
| 81 | + Spec: InferencePoolSpec{ |
| 82 | + TargetPortNumber: src.Spec.TargetPortNumber, |
| 83 | + EndpointPickerConfig: *endPointPickerConfig, |
| 84 | + }, |
| 85 | + Status: *status, |
| 86 | + } |
| 87 | + |
| 88 | + if src.Spec.Selector.MatchLabels != nil { |
| 89 | + dst.Spec.Selector = make(map[LabelKey]LabelValue, len(src.Spec.Selector.MatchLabels)) |
| 90 | + for k, v := range src.Spec.Selector.MatchLabels { |
| 91 | + dst.Spec.Selector[LabelKey(k)] = LabelValue(v) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return dst, nil |
| 96 | +} |
| 97 | + |
| 98 | +func converStatusToV1(src InferencePoolStatus) (*v1.InferencePoolStatus, error) { |
| 99 | + u, err := toUnstructured(&src) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + return convert[v1.InferencePoolStatus](u) |
| 104 | +} |
| 105 | + |
| 106 | +func converStatusFromV1(src v1.InferencePoolStatus) (*InferencePoolStatus, error) { |
| 107 | + u, err := toUnstructured(&src) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + return convert[InferencePoolStatus](u) |
| 112 | +} |
| 113 | + |
| 114 | +func convertEndpointPickerConfigToV1(src *EndpointPickerConfig) (*v1.EndpointPickerConfig, error) { |
| 115 | + u, err := toUnstructured(&src) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + return convert[v1.EndpointPickerConfig](u) |
| 120 | +} |
| 121 | + |
| 122 | +func convertEndpointPickerConfigFromV1(src *v1.EndpointPickerConfig) (*EndpointPickerConfig, error) { |
| 123 | + u, err := toUnstructured(&src) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + return convert[EndpointPickerConfig](u) |
| 128 | +} |
| 129 | + |
| 130 | +func toUnstructured(obj any) (*unstructured.Unstructured, error) { |
| 131 | + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + return &unstructured.Unstructured{Object: u}, nil |
| 136 | +} |
| 137 | + |
| 138 | +func convert[T any](u *unstructured.Unstructured) (*T, error) { |
| 139 | + var res T |
| 140 | + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &res); err != nil { |
| 141 | + return nil, fmt.Errorf("error converting unstructured to T: %v", err) |
| 142 | + } |
| 143 | + return &res, nil |
| 144 | +} |
0 commit comments