Skip to content

Commit 7663773

Browse files
authored
remove empty condition list when doing v1 and v1alpha2 conversion. (#1447)
* remove empty condition list when doing conversion. * empty to empty and nil to nil. * special conversion for default.
1 parent 9f2aa5a commit 7663773

File tree

2 files changed

+84
-33
lines changed

2 files changed

+84
-33
lines changed

apix/v1alpha2/inferencepool_conversion.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,35 @@ func convertStatusToV1(src *InferencePoolStatus) (*v1.InferencePoolStatus, error
104104
for _, p := range src.Parents {
105105
ps := v1.ParentStatus{
106106
ParentRef: toV1ParentRef(p.GatewayRef),
107-
Conditions: make([]metav1.Condition, 0, len(p.Conditions)),
107+
Conditions: make([]metav1.Condition, 0),
108108
}
109109
for _, c := range p.Conditions {
110110
cc := c
111+
if isV1Alpha2DefaultConditon(c) {
112+
continue
113+
}
111114
// v1alpha2: "Accepted" -> v1: "SupportedByParent"
112115
if cc.Type == string(v1.InferencePoolConditionAccepted) &&
113116
cc.Reason == string(InferencePoolReasonAccepted) {
114117
cc.Reason = string(v1.InferencePoolReasonAccepted)
115118
}
116119
ps.Conditions = append(ps.Conditions, cc)
117120
}
121+
if len(ps.Conditions) == 0 && len(src.Parents) == 1 {
122+
// Reset the conditions to nil since v1 version does not have default condition.
123+
// Default is only configured when length of src.Parents is 1.
124+
ps.Conditions = nil
125+
}
118126
out.Parents = append(out.Parents, ps)
119127
}
120128
return out, nil
121129
}
122130

131+
func isV1Alpha2DefaultConditon(c metav1.Condition) bool {
132+
return InferencePoolConditionType(c.Type) == InferencePoolConditionAccepted &&
133+
c.Status == metav1.ConditionUnknown && InferencePoolReason(c.Reason) == InferencePoolReasonPending
134+
}
135+
123136
func convertStatusFromV1(src *v1.InferencePoolStatus) (*InferencePoolStatus, error) {
124137
if src == nil {
125138
return nil, errors.New("src cannot be nil")
@@ -133,16 +146,18 @@ func convertStatusFromV1(src *v1.InferencePoolStatus) (*InferencePoolStatus, err
133146
for _, p := range src.Parents {
134147
ps := PoolStatus{
135148
GatewayRef: fromV1ParentRef(p.ParentRef),
136-
Conditions: make([]metav1.Condition, 0, len(p.Conditions)),
137149
}
138-
for _, c := range p.Conditions {
150+
if p.Conditions != nil {
151+
ps.Conditions = make([]metav1.Condition, len(p.Conditions))
152+
}
153+
for idx, c := range p.Conditions {
139154
cc := c
140155
// v1: "SupportedByParent" -> v1alpha2: "Accepted"
141156
if cc.Type == string(v1.InferencePoolConditionAccepted) &&
142157
cc.Reason == string(v1.InferencePoolReasonAccepted) {
143158
cc.Reason = string(InferencePoolReasonAccepted)
144159
}
145-
ps.Conditions = append(ps.Conditions, cc)
160+
ps.Conditions[idx] = cc
146161
}
147162
out.Parents = append(out.Parents, ps)
148163
}

apix/v1alpha2/inferencepool_conversion_test.go

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ func TestInferencePoolConvertTo(t *testing.T) {
7373
{
7474
GatewayRef: ParentGatewayReference{Name: "my-gateway"},
7575
Conditions: []metav1.Condition{
76+
{ // v1alpha2 default condition is skipped from conversion.
77+
Type: string(v1.InferencePoolConditionAccepted),
78+
Status: metav1.ConditionUnknown,
79+
Reason: string(InferencePoolReasonPending),
80+
LastTransitionTime: timestamp,
81+
},
7682
{
7783
Type: string(InferencePoolConditionAccepted),
7884
Status: metav1.ConditionTrue,
@@ -127,7 +133,7 @@ func TestInferencePoolConvertTo(t *testing.T) {
127133
wantErr: false,
128134
},
129135
{
130-
name: "conversion from v1alpha2 to v1 with empty extensionRef",
136+
name: "conversion from v1alpha2 to v1 with empty extensionRef and default v1alpha2 status condition",
131137
src: &InferencePool{
132138
TypeMeta: metav1.TypeMeta{
133139
Kind: "InferencePool",
@@ -149,9 +155,9 @@ func TestInferencePoolConvertTo(t *testing.T) {
149155
GatewayRef: ParentGatewayReference{Name: "my-gateway"},
150156
Conditions: []metav1.Condition{
151157
{
152-
Type: string(InferencePoolConditionAccepted),
153-
Status: metav1.ConditionTrue,
154-
Reason: string(InferencePoolReasonAccepted),
158+
Type: string(v1.InferencePoolConditionAccepted),
159+
Status: metav1.ConditionUnknown,
160+
Reason: string(InferencePoolReasonPending),
155161
LastTransitionTime: timestamp,
156162
},
157163
},
@@ -180,14 +186,6 @@ func TestInferencePoolConvertTo(t *testing.T) {
180186
Parents: []v1.ParentStatus{
181187
{
182188
ParentRef: v1.ParentReference{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-
},
191189
},
192190
},
193191
},
@@ -300,7 +298,7 @@ func TestInferencePoolConvertFrom(t *testing.T) {
300298
wantErr: false,
301299
},
302300
{
303-
name: "conversion from v1 to v1alpha2 with empty extensionRef",
301+
name: "conversion from v1 to v1alpha2 with empty extensionRef and nil status condition",
304302
src: &v1.InferencePool{
305303
TypeMeta: metav1.TypeMeta{
306304
Kind: "InferencePool",
@@ -322,14 +320,6 @@ func TestInferencePoolConvertFrom(t *testing.T) {
322320
Parents: []v1.ParentStatus{
323321
{
324322
ParentRef: v1.ParentReference{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-
},
333323
},
334324
},
335325
},
@@ -353,14 +343,60 @@ func TestInferencePoolConvertFrom(t *testing.T) {
353343
Parents: []PoolStatus{
354344
{
355345
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-
},
346+
},
347+
},
348+
},
349+
},
350+
wantErr: false,
351+
},
352+
{
353+
name: "conversion from v1 to v1alpha2 with empty extensionRef and empty status condition",
354+
src: &v1.InferencePool{
355+
TypeMeta: metav1.TypeMeta{
356+
Kind: "InferencePool",
357+
APIVersion: v1.GroupVersion.String(),
358+
},
359+
ObjectMeta: metav1.ObjectMeta{
360+
Name: "test-pool",
361+
Namespace: "test-ns",
362+
},
363+
Spec: v1.InferencePoolSpec{
364+
Selector: v1.LabelSelector{
365+
MatchLabels: map[v1.LabelKey]v1.LabelValue{
366+
"app": "my-model-server",
367+
},
368+
},
369+
TargetPorts: []v1.Port{{Number: v1.PortNumber(int32(8080))}},
370+
},
371+
Status: v1.InferencePoolStatus{
372+
Parents: []v1.ParentStatus{
373+
{
374+
ParentRef: v1.ParentReference{Name: "my-gateway"},
375+
Conditions: []metav1.Condition{},
376+
},
377+
},
378+
},
379+
},
380+
want: &InferencePool{
381+
TypeMeta: metav1.TypeMeta{
382+
Kind: "InferencePool",
383+
APIVersion: GroupVersion.String(),
384+
},
385+
ObjectMeta: metav1.ObjectMeta{
386+
Name: "test-pool",
387+
Namespace: "test-ns",
388+
},
389+
Spec: InferencePoolSpec{
390+
Selector: map[LabelKey]LabelValue{
391+
"app": "my-model-server",
392+
},
393+
TargetPortNumber: 8080,
394+
},
395+
Status: InferencePoolStatus{
396+
Parents: []PoolStatus{
397+
{
398+
GatewayRef: ParentGatewayReference{Name: "my-gateway"},
399+
Conditions: []metav1.Condition{},
364400
},
365401
},
366402
},

0 commit comments

Comments
 (0)