Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci-kal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ linters:
policy: SuggestFix # SuggestFix | Warn # The policy for pointers in optional fields. Defaults to `SuggestFix`.
omitempty:
policy: SuggestFix # SuggestFix | Warn | Ignore # The policy for omitempty in optional fields. Defaults to `SuggestFix`.
omitzero:
policy: Forbid # Enforce the `omitempty` route with a pointer for all optional structs.
exclusions:
generated: strict
paths:
Expand Down
189 changes: 104 additions & 85 deletions api/v1/inferencepool_types.go

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions api/v1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ type LabelValue string
// LabelSelector defines a query for resources based on their labels.
// This simplified version uses only the matchLabels field.
type LabelSelector struct {
// matchLabels contains a set of required {key,value} pairs.
// MatchLabels contains a set of required {key,value} pairs.
// An object must match every label in this map to be selected.
// The matching logic is an AND operation on all entries.
//
// +required
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=64
MatchLabels map[LabelKey]LabelValue `json:"matchLabels,omitempty" protobuf:"bytes,1,rep,name=matchLabels"`
MatchLabels map[LabelKey]LabelValue `json:"matchLabels,omitempty"`
}
40 changes: 30 additions & 10 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 108 additions & 45 deletions apix/v1alpha2/inferencepool_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package v1alpha2

import (
"errors"
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
runtime "k8s.io/apimachinery/pkg/runtime"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"

v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
)
Expand All @@ -39,11 +38,17 @@ func (src *InferencePool) ConvertTo(dst *v1.InferencePool) error {
if err != nil {
return err
}
dst.TypeMeta = src.TypeMeta

meta := metav1.TypeMeta{
Kind: src.Kind,
APIVersion: v1.GroupVersion.String(), // Ensure the API version is set correctly.
}
dst.TypeMeta = meta
dst.ObjectMeta = src.ObjectMeta
dst.Spec.TargetPorts = []v1.Port{{Number: v1.PortNumber(src.Spec.TargetPortNumber)}}
dst.Spec.EndpointPickerRef = endpointPickRef
dst.Status = *v1Status

if src.Spec.Selector != nil {
dst.Spec.Selector.MatchLabels = make(map[v1.LabelKey]v1.LabelValue, len(src.Spec.Selector))
for k, v := range src.Spec.Selector {
Expand All @@ -66,11 +71,17 @@ func (dst *InferencePool) ConvertFrom(src *v1.InferencePool) error {
if err != nil {
return err
}
dst.TypeMeta = src.TypeMeta

meta := metav1.TypeMeta{
Kind: src.Kind,
APIVersion: GroupVersion.String(), // Ensure the API version is set correctly.
}
dst.TypeMeta = meta
dst.ObjectMeta = src.ObjectMeta
dst.Spec.TargetPortNumber = int32(src.Spec.TargetPorts[0].Number)
dst.Spec.ExtensionRef = extensionRef
dst.Status = *status

if src.Spec.Selector.MatchLabels != nil {
dst.Spec.Selector = make(map[LabelKey]LabelValue, len(src.Spec.Selector.MatchLabels))
for k, v := range src.Spec.Selector.MatchLabels {
Expand All @@ -84,22 +95,96 @@ func convertStatusToV1(src *InferencePoolStatus) (*v1.InferencePoolStatus, error
if src == nil {
return nil, errors.New("src cannot be nil")
}
u, err := toUnstructured(src)
if err != nil {
return nil, err
if src.Parents == nil {
return &v1.InferencePoolStatus{}, nil
}
out := &v1.InferencePoolStatus{
Parents: make([]v1.ParentStatus, 0, len(src.Parents)),
}
return convert[v1.InferencePoolStatus](u)
for _, p := range src.Parents {
ps := v1.ParentStatus{
ParentRef: toV1ParentRef(p.GatewayRef),
Conditions: make([]metav1.Condition, 0, len(p.Conditions)),
}
for _, c := range p.Conditions {
cc := c
// v1alpha2: "Accepted" -> v1: "SupportedByParent"
if cc.Type == string(v1.InferencePoolConditionAccepted) &&
cc.Reason == string(InferencePoolReasonAccepted) {
cc.Reason = string(v1.InferencePoolReasonAccepted)
}
ps.Conditions = append(ps.Conditions, cc)
}
out.Parents = append(out.Parents, ps)
}
return out, nil
}

func convertStatusFromV1(src *v1.InferencePoolStatus) (*InferencePoolStatus, error) {
if src == nil {
return nil, errors.New("src cannot be nil")
}
u, err := toUnstructured(src)
if err != nil {
return nil, err
if src.Parents == nil {
return &InferencePoolStatus{}, nil
}
out := &InferencePoolStatus{
Parents: make([]PoolStatus, 0, len(src.Parents)),
}
return convert[InferencePoolStatus](u)
for _, p := range src.Parents {
ps := PoolStatus{
GatewayRef: fromV1ParentRef(p.ParentRef),
Conditions: make([]metav1.Condition, 0, len(p.Conditions)),
}
for _, c := range p.Conditions {
cc := c
// v1: "SupportedByParent" -> v1alpha2: "Accepted"
if cc.Type == string(v1.InferencePoolConditionAccepted) &&
cc.Reason == string(v1.InferencePoolReasonAccepted) {
cc.Reason = string(InferencePoolReasonAccepted)
}
ps.Conditions = append(ps.Conditions, cc)
}
out.Parents = append(out.Parents, ps)
}
return out, nil
}

func toV1ParentRef(in ParentGatewayReference) v1.ParentReference {
out := v1.ParentReference{
Name: v1.ObjectName(in.Name),
}
if in.Group != nil {
g := v1.Group(*in.Group)
out.Group = &g
}
if in.Kind != nil {
k := v1.Kind(*in.Kind)
out.Kind = &k
}
if in.Namespace != nil {
ns := v1.Namespace(*in.Namespace)
out.Namespace = &ns
}
return out
}

func fromV1ParentRef(in v1.ParentReference) ParentGatewayReference {
out := ParentGatewayReference{
Name: ObjectName(in.Name),
}
if in.Group != nil {
g := Group(*in.Group)
out.Group = &g
}
if in.Kind != nil {
k := Kind(*in.Kind)
out.Kind = &k
}
if in.Namespace != nil {
ns := Namespace(*in.Namespace)
out.Namespace = &ns
}
return out
}

func convertExtensionRefToV1(src *Extension) (v1.EndpointPickerRef, error) {
Expand All @@ -108,19 +193,17 @@ func convertExtensionRefToV1(src *Extension) (v1.EndpointPickerRef, error) {
return endpointPickerRef, errors.New("src cannot be nil")
}
if src.Group != nil {
v1Group := v1.Group(*src.Group)
endpointPickerRef.Group = &v1Group
endpointPickerRef.Group = ptr.To(v1.Group(*src.Group))
}
if src.Kind != nil {
endpointPickerRef.Kind = v1.Kind(*src.Kind)
endpointPickerRef.Kind = ptr.To(v1.Kind(*src.Kind))
}
endpointPickerRef.Name = v1.ObjectName(src.Name)
if src.PortNumber != nil {
v1PortNumber := v1.PortNumber(*src.PortNumber)
endpointPickerRef.PortNumber = &v1PortNumber
endpointPickerRef.PortNumber = ptr.To(v1.PortNumber(*src.PortNumber))
}
if src.FailureMode != nil {
endpointPickerRef.FailureMode = v1.ExtensionFailureMode(*src.FailureMode)
endpointPickerRef.FailureMode = ptr.To(v1.EndpointPickerFailureMode(*src.FailureMode))
}

return endpointPickerRef, nil
Expand All @@ -132,37 +215,17 @@ func convertEndpointPickerRefFromV1(src *v1.EndpointPickerRef) (Extension, error
return extension, errors.New("src cannot be nil")
}
if src.Group != nil {
group := Group(*src.Group)
extension.Group = &group
extension.Group = ptr.To(Group(*src.Group))
}
if src.Kind != "" {
kind := Kind(src.Kind)
extension.Kind = &kind
if src.Kind != nil {
extension.Kind = ptr.To(Kind(*src.Kind))
}
extension.Name = ObjectName(src.Name)
if src.PortNumber != nil {
portNumber := PortNumber(*src.PortNumber)
extension.PortNumber = &portNumber
extension.PortNumber = ptr.To(PortNumber(*src.PortNumber))
}
if src.FailureMode != "" {
extensionFailureMode := ExtensionFailureMode(src.FailureMode)
extension.FailureMode = &extensionFailureMode
if src.FailureMode != nil {
extension.FailureMode = ptr.To(ExtensionFailureMode(*src.FailureMode))
}
return extension, nil
}

func toUnstructured(obj any) (*unstructured.Unstructured, error) {
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{Object: u}, nil
}

func convert[T any](u *unstructured.Unstructured) (*T, error) {
var res T
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &res); err != nil {
return nil, fmt.Errorf("error converting unstructured to T: %v", err)
}
return &res, nil
}
Loading