@@ -18,10 +18,9 @@ package v1alpha2
18
18
19
19
import (
20
20
"errors"
21
- "fmt"
22
21
23
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured "
24
- runtime "k8s.io/apimachinery/pkg/runtime "
22
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23
+ "k8s.io/utils/ptr "
25
24
26
25
v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
27
26
)
@@ -39,11 +38,17 @@ func (src *InferencePool) ConvertTo(dst *v1.InferencePool) error {
39
38
if err != nil {
40
39
return err
41
40
}
42
- dst .TypeMeta = src .TypeMeta
41
+
42
+ meta := metav1.TypeMeta {
43
+ Kind : src .Kind ,
44
+ APIVersion : v1 .GroupVersion .String (), // Ensure the API version is set correctly.
45
+ }
46
+ dst .TypeMeta = meta
43
47
dst .ObjectMeta = src .ObjectMeta
44
48
dst .Spec .TargetPorts = []v1.Port {{Number : v1 .PortNumber (src .Spec .TargetPortNumber )}}
45
49
dst .Spec .EndpointPickerRef = endpointPickRef
46
50
dst .Status = * v1Status
51
+
47
52
if src .Spec .Selector != nil {
48
53
dst .Spec .Selector .MatchLabels = make (map [v1.LabelKey ]v1.LabelValue , len (src .Spec .Selector ))
49
54
for k , v := range src .Spec .Selector {
@@ -66,11 +71,17 @@ func (dst *InferencePool) ConvertFrom(src *v1.InferencePool) error {
66
71
if err != nil {
67
72
return err
68
73
}
69
- dst .TypeMeta = src .TypeMeta
74
+
75
+ meta := metav1.TypeMeta {
76
+ Kind : src .Kind ,
77
+ APIVersion : GroupVersion .String (), // Ensure the API version is set correctly.
78
+ }
79
+ dst .TypeMeta = meta
70
80
dst .ObjectMeta = src .ObjectMeta
71
81
dst .Spec .TargetPortNumber = int32 (src .Spec .TargetPorts [0 ].Number )
72
82
dst .Spec .ExtensionRef = extensionRef
73
83
dst .Status = * status
84
+
74
85
if src .Spec .Selector .MatchLabels != nil {
75
86
dst .Spec .Selector = make (map [LabelKey ]LabelValue , len (src .Spec .Selector .MatchLabels ))
76
87
for k , v := range src .Spec .Selector .MatchLabels {
@@ -84,22 +95,96 @@ func convertStatusToV1(src *InferencePoolStatus) (*v1.InferencePoolStatus, error
84
95
if src == nil {
85
96
return nil , errors .New ("src cannot be nil" )
86
97
}
87
- u , err := toUnstructured (src )
88
- if err != nil {
89
- return nil , err
98
+ if src .Parents == nil {
99
+ return & v1.InferencePoolStatus {}, nil
100
+ }
101
+ out := & v1.InferencePoolStatus {
102
+ Parents : make ([]v1.ParentStatus , 0 , len (src .Parents )),
90
103
}
91
- return convert [v1.InferencePoolStatus ](u )
104
+ for _ , p := range src .Parents {
105
+ ps := v1.ParentStatus {
106
+ ParentRef : toV1ParentRef (p .GatewayRef ),
107
+ Conditions : make ([]metav1.Condition , 0 , len (p .Conditions )),
108
+ }
109
+ for _ , c := range p .Conditions {
110
+ cc := c
111
+ // v1alpha2: "Accepted" -> v1: "SupportedByParent"
112
+ if cc .Type == string (v1 .InferencePoolConditionAccepted ) &&
113
+ cc .Reason == string (InferencePoolReasonAccepted ) {
114
+ cc .Reason = string (v1 .InferencePoolReasonAccepted )
115
+ }
116
+ ps .Conditions = append (ps .Conditions , cc )
117
+ }
118
+ out .Parents = append (out .Parents , ps )
119
+ }
120
+ return out , nil
92
121
}
93
122
94
123
func convertStatusFromV1 (src * v1.InferencePoolStatus ) (* InferencePoolStatus , error ) {
95
124
if src == nil {
96
125
return nil , errors .New ("src cannot be nil" )
97
126
}
98
- u , err := toUnstructured (src )
99
- if err != nil {
100
- return nil , err
127
+ if src .Parents == nil {
128
+ return & InferencePoolStatus {}, nil
129
+ }
130
+ out := & InferencePoolStatus {
131
+ Parents : make ([]PoolStatus , 0 , len (src .Parents )),
101
132
}
102
- return convert [InferencePoolStatus ](u )
133
+ for _ , p := range src .Parents {
134
+ ps := PoolStatus {
135
+ GatewayRef : fromV1ParentRef (p .ParentRef ),
136
+ Conditions : make ([]metav1.Condition , 0 , len (p .Conditions )),
137
+ }
138
+ for _ , c := range p .Conditions {
139
+ cc := c
140
+ // v1: "SupportedByParent" -> v1alpha2: "Accepted"
141
+ if cc .Type == string (v1 .InferencePoolConditionAccepted ) &&
142
+ cc .Reason == string (v1 .InferencePoolReasonAccepted ) {
143
+ cc .Reason = string (InferencePoolReasonAccepted )
144
+ }
145
+ ps .Conditions = append (ps .Conditions , cc )
146
+ }
147
+ out .Parents = append (out .Parents , ps )
148
+ }
149
+ return out , nil
150
+ }
151
+
152
+ func toV1ParentRef (in ParentGatewayReference ) v1.ParentReference {
153
+ out := v1.ParentReference {
154
+ Name : v1 .ObjectName (in .Name ),
155
+ }
156
+ if in .Group != nil {
157
+ g := v1 .Group (* in .Group )
158
+ out .Group = & g
159
+ }
160
+ if in .Kind != nil {
161
+ k := v1 .Kind (* in .Kind )
162
+ out .Kind = & k
163
+ }
164
+ if in .Namespace != nil {
165
+ ns := v1 .Namespace (* in .Namespace )
166
+ out .Namespace = & ns
167
+ }
168
+ return out
169
+ }
170
+
171
+ func fromV1ParentRef (in v1.ParentReference ) ParentGatewayReference {
172
+ out := ParentGatewayReference {
173
+ Name : ObjectName (in .Name ),
174
+ }
175
+ if in .Group != nil {
176
+ g := Group (* in .Group )
177
+ out .Group = & g
178
+ }
179
+ if in .Kind != nil {
180
+ k := Kind (* in .Kind )
181
+ out .Kind = & k
182
+ }
183
+ if in .Namespace != nil {
184
+ ns := Namespace (* in .Namespace )
185
+ out .Namespace = & ns
186
+ }
187
+ return out
103
188
}
104
189
105
190
func convertExtensionRefToV1 (src * Extension ) (v1.EndpointPickerRef , error ) {
@@ -108,19 +193,17 @@ func convertExtensionRefToV1(src *Extension) (v1.EndpointPickerRef, error) {
108
193
return endpointPickerRef , errors .New ("src cannot be nil" )
109
194
}
110
195
if src .Group != nil {
111
- v1Group := v1 .Group (* src .Group )
112
- endpointPickerRef .Group = & v1Group
196
+ endpointPickerRef .Group = ptr .To (v1 .Group (* src .Group ))
113
197
}
114
198
if src .Kind != nil {
115
- endpointPickerRef .Kind = v1 .Kind (* src .Kind )
199
+ endpointPickerRef .Kind = ptr . To ( v1 .Kind (* src .Kind ) )
116
200
}
117
201
endpointPickerRef .Name = v1 .ObjectName (src .Name )
118
202
if src .PortNumber != nil {
119
- v1PortNumber := v1 .PortNumber (* src .PortNumber )
120
- endpointPickerRef .PortNumber = & v1PortNumber
203
+ endpointPickerRef .PortNumber = ptr .To (v1 .PortNumber (* src .PortNumber ))
121
204
}
122
205
if src .FailureMode != nil {
123
- endpointPickerRef .FailureMode = v1 .ExtensionFailureMode (* src .FailureMode )
206
+ endpointPickerRef .FailureMode = ptr . To ( v1 .EndpointPickerFailureMode (* src .FailureMode ) )
124
207
}
125
208
126
209
return endpointPickerRef , nil
@@ -132,37 +215,17 @@ func convertEndpointPickerRefFromV1(src *v1.EndpointPickerRef) (Extension, error
132
215
return extension , errors .New ("src cannot be nil" )
133
216
}
134
217
if src .Group != nil {
135
- group := Group (* src .Group )
136
- extension .Group = & group
218
+ extension .Group = ptr .To (Group (* src .Group ))
137
219
}
138
- if src .Kind != "" {
139
- kind := Kind (src .Kind )
140
- extension .Kind = & kind
220
+ if src .Kind != nil {
221
+ extension .Kind = ptr .To (Kind (* src .Kind ))
141
222
}
142
223
extension .Name = ObjectName (src .Name )
143
224
if src .PortNumber != nil {
144
- portNumber := PortNumber (* src .PortNumber )
145
- extension .PortNumber = & portNumber
225
+ extension .PortNumber = ptr .To (PortNumber (* src .PortNumber ))
146
226
}
147
- if src .FailureMode != "" {
148
- extensionFailureMode := ExtensionFailureMode (src .FailureMode )
149
- extension .FailureMode = & extensionFailureMode
227
+ if src .FailureMode != nil {
228
+ extension .FailureMode = ptr .To (ExtensionFailureMode (* src .FailureMode ))
150
229
}
151
230
return extension , nil
152
231
}
153
-
154
- func toUnstructured (obj any ) (* unstructured.Unstructured , error ) {
155
- u , err := runtime .DefaultUnstructuredConverter .ToUnstructured (obj )
156
- if err != nil {
157
- return nil , err
158
- }
159
- return & unstructured.Unstructured {Object : u }, nil
160
- }
161
-
162
- func convert [T any ](u * unstructured.Unstructured ) (* T , error ) {
163
- var res T
164
- if err := runtime .DefaultUnstructuredConverter .FromUnstructured (u .Object , & res ); err != nil {
165
- return nil , fmt .Errorf ("error converting unstructured to T: %v" , err )
166
- }
167
- return & res , nil
168
- }
0 commit comments