Skip to content

Commit 637763f

Browse files
committed
handle nil
1 parent f7dcedb commit 637763f

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

apix/v1alpha2/inferencepool_conversion.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ func (dst *InferencePool) ConvertFrom(src *v1.InferencePool) error {
8181
}
8282

8383
func convertStatusToV1(src *InferencePoolStatus) (*v1.InferencePoolStatus, error) {
84+
if src == nil {
85+
return nil, nil
86+
}
8487
u, err := toUnstructured(src)
8588
if err != nil {
8689
return nil, err
@@ -89,6 +92,9 @@ func convertStatusToV1(src *InferencePoolStatus) (*v1.InferencePoolStatus, error
8992
}
9093

9194
func convertStatusFromV1(src *v1.InferencePoolStatus) (*InferencePoolStatus, error) {
95+
if src == nil {
96+
return nil, nil
97+
}
9298
u, err := toUnstructured(src)
9399
if err != nil {
94100
return nil, err
@@ -97,6 +103,9 @@ func convertStatusFromV1(src *v1.InferencePoolStatus) (*InferencePoolStatus, err
97103
}
98104

99105
func convertExtensionRefToV1(src *Extension) (*v1.Extension, error) {
106+
if src == nil {
107+
return nil, nil
108+
}
100109
u, err := toUnstructured(src)
101110
if err != nil {
102111
return nil, err
@@ -105,6 +114,9 @@ func convertExtensionRefToV1(src *Extension) (*v1.Extension, error) {
105114
}
106115

107116
func convertExtensionRefFromV1(src *v1.Extension) (*Extension, error) {
117+
if src == nil {
118+
return nil, nil
119+
}
108120
u, err := toUnstructured(src)
109121
if err != nil {
110122
return nil, err

apix/v1alpha2/inferencepool_conversion_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,74 @@ func TestInferencePoolConvertTo(t *testing.T) {
126126
},
127127
wantErr: false,
128128
},
129+
{
130+
name: "conversion from v1alpha2 to v1 with nil extensionRef",
131+
src: &InferencePool{
132+
TypeMeta: metav1.TypeMeta{
133+
Kind: "InferencePool",
134+
APIVersion: "inference.networking.x-k8s.io/v1alpha2",
135+
},
136+
ObjectMeta: metav1.ObjectMeta{
137+
Name: "test-pool",
138+
Namespace: "test-ns",
139+
},
140+
Spec: InferencePoolSpec{
141+
Selector: map[LabelKey]LabelValue{
142+
"app": "my-model-server",
143+
},
144+
TargetPortNumber: 8080,
145+
},
146+
Status: InferencePoolStatus{
147+
Parents: []PoolStatus{
148+
{
149+
GatewayRef: ParentGatewayReference{Name: "my-gateway"},
150+
Conditions: []metav1.Condition{
151+
{
152+
Type: string(InferencePoolConditionAccepted),
153+
Status: metav1.ConditionTrue,
154+
Reason: string(InferencePoolReasonAccepted),
155+
LastTransitionTime: timestamp,
156+
},
157+
},
158+
},
159+
},
160+
},
161+
},
162+
want: &v1.InferencePool{
163+
TypeMeta: metav1.TypeMeta{
164+
Kind: "InferencePool",
165+
APIVersion: "inference.networking.x-k8s.io/v1alpha2",
166+
},
167+
ObjectMeta: metav1.ObjectMeta{
168+
Name: "test-pool",
169+
Namespace: "test-ns",
170+
},
171+
Spec: v1.InferencePoolSpec{
172+
Selector: v1.LabelSelector{
173+
MatchLabels: map[v1.LabelKey]v1.LabelValue{
174+
"app": "my-model-server",
175+
},
176+
},
177+
TargetPortNumber: 8080,
178+
},
179+
Status: v1.InferencePoolStatus{
180+
Parents: []v1.PoolStatus{
181+
{
182+
GatewayRef: v1.ParentGatewayReference{Name: "my-gateway"},
183+
Conditions: []metav1.Condition{
184+
{
185+
Type: string(v1.InferencePoolConditionAccepted),
186+
Status: metav1.ConditionTrue,
187+
Reason: string(v1.InferencePoolReasonAccepted),
188+
LastTransitionTime: timestamp,
189+
},
190+
},
191+
},
192+
},
193+
},
194+
},
195+
wantErr: false,
196+
},
129197
}
130198

131199
for _, tt := range tests {
@@ -231,6 +299,74 @@ func TestInferencePoolConvertFrom(t *testing.T) {
231299
},
232300
wantErr: false,
233301
},
302+
{
303+
name: "conversion from v1 to v1alpha2 with nil extensionRef",
304+
src: &v1.InferencePool{
305+
TypeMeta: metav1.TypeMeta{
306+
Kind: "InferencePool",
307+
APIVersion: "inference.networking.k8s.io/v1",
308+
},
309+
ObjectMeta: metav1.ObjectMeta{
310+
Name: "test-pool",
311+
Namespace: "test-ns",
312+
},
313+
Spec: v1.InferencePoolSpec{
314+
Selector: v1.LabelSelector{
315+
MatchLabels: map[v1.LabelKey]v1.LabelValue{
316+
"app": "my-model-server",
317+
},
318+
},
319+
TargetPortNumber: 8080,
320+
},
321+
Status: v1.InferencePoolStatus{
322+
Parents: []v1.PoolStatus{
323+
{
324+
GatewayRef: v1.ParentGatewayReference{Name: "my-gateway"},
325+
Conditions: []metav1.Condition{
326+
{
327+
Type: string(v1.InferencePoolConditionAccepted),
328+
Status: metav1.ConditionTrue,
329+
Reason: string(v1.InferencePoolReasonAccepted),
330+
LastTransitionTime: timestamp,
331+
},
332+
},
333+
},
334+
},
335+
},
336+
},
337+
want: &InferencePool{
338+
TypeMeta: metav1.TypeMeta{
339+
Kind: "InferencePool",
340+
APIVersion: "inference.networking.k8s.io/v1",
341+
},
342+
ObjectMeta: metav1.ObjectMeta{
343+
Name: "test-pool",
344+
Namespace: "test-ns",
345+
},
346+
Spec: InferencePoolSpec{
347+
Selector: map[LabelKey]LabelValue{
348+
"app": "my-model-server",
349+
},
350+
TargetPortNumber: 8080,
351+
},
352+
Status: InferencePoolStatus{
353+
Parents: []PoolStatus{
354+
{
355+
GatewayRef: ParentGatewayReference{Name: "my-gateway"},
356+
Conditions: []metav1.Condition{
357+
{
358+
Type: string(InferencePoolConditionAccepted),
359+
Status: metav1.ConditionTrue,
360+
Reason: string(InferencePoolReasonAccepted),
361+
LastTransitionTime: timestamp,
362+
},
363+
},
364+
},
365+
},
366+
},
367+
},
368+
wantErr: false,
369+
},
234370
{
235371
name: "nil source",
236372
src: nil,

0 commit comments

Comments
 (0)