Skip to content

Commit 63e9468

Browse files
robscottdanehanscapri-xiyue
authored
Final bits of v1.0 API cleanup (#1441)
* Refactors InferencePool v1 Status Signed-off-by: Daneyon Hansen <[email protected]> * Renames Pool v1 FailMode Types Signed-off-by: Daneyon Hansen <[email protected]> * Runs API docs generators Signed-off-by: Daneyon Hansen <[email protected]> * Resolves latest kubeapilinter failures Signed-off-by: Daneyon Hansen <[email protected]> * Resolve robscott feedback Signed-off-by: Daneyon Hansen <[email protected]> * fixed linter * Final bits of API cleanup --------- Signed-off-by: Daneyon Hansen <[email protected]> Co-authored-by: Daneyon Hansen <[email protected]> Co-authored-by: Xiyue Yu <[email protected]>
1 parent aac2fee commit 63e9468

File tree

15 files changed

+410
-306
lines changed

15 files changed

+410
-306
lines changed

.golangci-kal.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ linters:
3232
policy: SuggestFix # SuggestFix | Warn # The policy for pointers in optional fields. Defaults to `SuggestFix`.
3333
omitempty:
3434
policy: SuggestFix # SuggestFix | Warn | Ignore # The policy for omitempty in optional fields. Defaults to `SuggestFix`.
35+
omitzero:
36+
policy: Forbid # Enforce the `omitempty` route with a pointer for all optional structs.
3537
exclusions:
3638
generated: strict
3739
paths:

api/v1/inferencepool_types.go

Lines changed: 104 additions & 85 deletions
Large diffs are not rendered by default.

api/v1/shared_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,12 @@ type LabelValue string
132132
// LabelSelector defines a query for resources based on their labels.
133133
// This simplified version uses only the matchLabels field.
134134
type LabelSelector struct {
135-
// matchLabels contains a set of required {key,value} pairs.
135+
// MatchLabels contains a set of required {key,value} pairs.
136136
// An object must match every label in this map to be selected.
137137
// The matching logic is an AND operation on all entries.
138138
//
139139
// +required
140+
// +kubebuilder:validation:MinItems=1
140141
// +kubebuilder:validation:MaxItems=64
141-
MatchLabels map[LabelKey]LabelValue `json:"matchLabels,omitempty" protobuf:"bytes,1,rep,name=matchLabels"`
142+
MatchLabels map[LabelKey]LabelValue `json:"matchLabels,omitempty"`
142143
}

api/v1/zz_generated.deepcopy.go

Lines changed: 30 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apix/v1alpha2/inferencepool_conversion.go

Lines changed: 108 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ package v1alpha2
1818

1919
import (
2020
"errors"
21-
"fmt"
2221

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"
2524

2625
v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
2726
)
@@ -39,11 +38,17 @@ func (src *InferencePool) ConvertTo(dst *v1.InferencePool) error {
3938
if err != nil {
4039
return err
4140
}
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
4347
dst.ObjectMeta = src.ObjectMeta
4448
dst.Spec.TargetPorts = []v1.Port{{Number: v1.PortNumber(src.Spec.TargetPortNumber)}}
4549
dst.Spec.EndpointPickerRef = endpointPickRef
4650
dst.Status = *v1Status
51+
4752
if src.Spec.Selector != nil {
4853
dst.Spec.Selector.MatchLabels = make(map[v1.LabelKey]v1.LabelValue, len(src.Spec.Selector))
4954
for k, v := range src.Spec.Selector {
@@ -66,11 +71,17 @@ func (dst *InferencePool) ConvertFrom(src *v1.InferencePool) error {
6671
if err != nil {
6772
return err
6873
}
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
7080
dst.ObjectMeta = src.ObjectMeta
7181
dst.Spec.TargetPortNumber = int32(src.Spec.TargetPorts[0].Number)
7282
dst.Spec.ExtensionRef = extensionRef
7383
dst.Status = *status
84+
7485
if src.Spec.Selector.MatchLabels != nil {
7586
dst.Spec.Selector = make(map[LabelKey]LabelValue, len(src.Spec.Selector.MatchLabels))
7687
for k, v := range src.Spec.Selector.MatchLabels {
@@ -84,22 +95,96 @@ func convertStatusToV1(src *InferencePoolStatus) (*v1.InferencePoolStatus, error
8495
if src == nil {
8596
return nil, errors.New("src cannot be nil")
8697
}
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)),
90103
}
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
92121
}
93122

94123
func convertStatusFromV1(src *v1.InferencePoolStatus) (*InferencePoolStatus, error) {
95124
if src == nil {
96125
return nil, errors.New("src cannot be nil")
97126
}
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)),
101132
}
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
103188
}
104189

105190
func convertExtensionRefToV1(src *Extension) (v1.EndpointPickerRef, error) {
@@ -108,19 +193,17 @@ func convertExtensionRefToV1(src *Extension) (v1.EndpointPickerRef, error) {
108193
return endpointPickerRef, errors.New("src cannot be nil")
109194
}
110195
if src.Group != nil {
111-
v1Group := v1.Group(*src.Group)
112-
endpointPickerRef.Group = &v1Group
196+
endpointPickerRef.Group = ptr.To(v1.Group(*src.Group))
113197
}
114198
if src.Kind != nil {
115-
endpointPickerRef.Kind = v1.Kind(*src.Kind)
199+
endpointPickerRef.Kind = ptr.To(v1.Kind(*src.Kind))
116200
}
117201
endpointPickerRef.Name = v1.ObjectName(src.Name)
118202
if src.PortNumber != nil {
119-
v1PortNumber := v1.PortNumber(*src.PortNumber)
120-
endpointPickerRef.PortNumber = &v1PortNumber
203+
endpointPickerRef.PortNumber = ptr.To(v1.PortNumber(*src.PortNumber))
121204
}
122205
if src.FailureMode != nil {
123-
endpointPickerRef.FailureMode = v1.ExtensionFailureMode(*src.FailureMode)
206+
endpointPickerRef.FailureMode = ptr.To(v1.EndpointPickerFailureMode(*src.FailureMode))
124207
}
125208

126209
return endpointPickerRef, nil
@@ -132,37 +215,17 @@ func convertEndpointPickerRefFromV1(src *v1.EndpointPickerRef) (Extension, error
132215
return extension, errors.New("src cannot be nil")
133216
}
134217
if src.Group != nil {
135-
group := Group(*src.Group)
136-
extension.Group = &group
218+
extension.Group = ptr.To(Group(*src.Group))
137219
}
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))
141222
}
142223
extension.Name = ObjectName(src.Name)
143224
if src.PortNumber != nil {
144-
portNumber := PortNumber(*src.PortNumber)
145-
extension.PortNumber = &portNumber
225+
extension.PortNumber = ptr.To(PortNumber(*src.PortNumber))
146226
}
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))
150229
}
151230
return extension, nil
152231
}
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

Comments
 (0)