diff --git a/apis/serving/common/common.go b/apis/serving/common/common.go index d4414eb..02438ea 100644 --- a/apis/serving/common/common.go +++ b/apis/serving/common/common.go @@ -12,3 +12,17 @@ type Resources struct { Requests *ResourceItem `json:"requests,omitempty"` Limits *ResourceItem `json:"limits,omitempty"` } + +type DeploymentTargetHPAConf struct { + CPU *int32 `json:"cpu,omitempty"` + GPU *int32 `json:"gpu,omitempty"` + Memory *string `json:"memory,omitempty"` + QPS *int64 `json:"qps,omitempty"` + MinReplicas *int32 `json:"min_replicas,omitempty"` + MaxReplicas *int32 `json:"max_replicas,omitempty"` +} + +type LabelItemSchema struct { + Key string `json:"key"` + Value string `json:"value"` +} diff --git a/apis/serving/common/zz_generated.deepcopy.go b/apis/serving/common/zz_generated.deepcopy.go index 14654ef..a226b74 100644 --- a/apis/serving/common/zz_generated.deepcopy.go +++ b/apis/serving/common/zz_generated.deepcopy.go @@ -21,7 +21,65 @@ limitations under the License. package common -import () +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *DeploymentTargetHPAConf) DeepCopyInto(out *DeploymentTargetHPAConf) { + *out = *in + if in.CPU != nil { + in, out := &in.CPU, &out.CPU + *out = new(int32) + **out = **in + } + if in.GPU != nil { + in, out := &in.GPU, &out.GPU + *out = new(int32) + **out = **in + } + if in.Memory != nil { + in, out := &in.Memory, &out.Memory + *out = new(string) + **out = **in + } + if in.QPS != nil { + in, out := &in.QPS, &out.QPS + *out = new(int64) + **out = **in + } + if in.MinReplicas != nil { + in, out := &in.MinReplicas, &out.MinReplicas + *out = new(int32) + **out = **in + } + if in.MaxReplicas != nil { + in, out := &in.MaxReplicas, &out.MaxReplicas + *out = new(int32) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DeploymentTargetHPAConf. +func (in *DeploymentTargetHPAConf) DeepCopy() *DeploymentTargetHPAConf { + if in == nil { + return nil + } + out := new(DeploymentTargetHPAConf) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *LabelItemSchema) DeepCopyInto(out *LabelItemSchema) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new LabelItemSchema. +func (in *LabelItemSchema) DeepCopy() *LabelItemSchema { + if in == nil { + return nil + } + out := new(LabelItemSchema) + in.DeepCopyInto(out) + return out +} // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ResourceItem) DeepCopyInto(out *ResourceItem) { diff --git a/apis/serving/v1alpha1/bentodeployment_types.go b/apis/serving/v1alpha1/bentodeployment_types.go index aa5103a..9ac9e70 100644 --- a/apis/serving/v1alpha1/bentodeployment_types.go +++ b/apis/serving/v1alpha1/bentodeployment_types.go @@ -22,7 +22,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/bentoml/yatai-deployment/apis/serving/common" - "github.com/bentoml/yatai-schemas/modelschemas" ) // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! @@ -47,9 +46,9 @@ type BentoDeploymentSpec struct { BentoTag string `json:"bento_tag"` - Resources *common.Resources `json:"resources,omitempty"` - Autoscaling *modelschemas.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` - Envs *[]modelschemas.LabelItemSchema `json:"envs,omitempty"` + Resources *common.Resources `json:"resources,omitempty"` + Autoscaling *common.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` + Envs *[]common.LabelItemSchema `json:"envs,omitempty"` Runners []BentoDeploymentRunnerSpec `json:"runners,omitempty"` } diff --git a/apis/serving/v1alpha1/zz_generated.deepcopy.go b/apis/serving/v1alpha1/zz_generated.deepcopy.go index fa4a31c..aeea302 100644 --- a/apis/serving/v1alpha1/zz_generated.deepcopy.go +++ b/apis/serving/v1alpha1/zz_generated.deepcopy.go @@ -22,10 +22,10 @@ limitations under the License. package v1alpha1 import ( - "github.com/bentoml/yatai-deployment/apis/serving/common" - "github.com/bentoml/yatai-schemas/modelschemas" "k8s.io/api/autoscaling/v2beta2" runtime "k8s.io/apimachinery/pkg/runtime" + + "github.com/bentoml/yatai-deployment/apis/serving/common" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. @@ -141,17 +141,16 @@ func (in *BentoDeploymentSpec) DeepCopyInto(out *BentoDeploymentSpec) { } if in.Autoscaling != nil { in, out := &in.Autoscaling, &out.Autoscaling - *out = (*in).DeepCopy() + *out = new(common.DeploymentTargetHPAConf) + (*in).DeepCopyInto(*out) } if in.Envs != nil { in, out := &in.Envs, &out.Envs - *out = new([]modelschemas.LabelItemSchema) + *out = new([]common.LabelItemSchema) if **in != nil { in, out := *in, *out - *out = make([]modelschemas.LabelItemSchema, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } + *out = make([]common.LabelItemSchema, len(*in)) + copy(*out, *in) } } if in.Runners != nil { diff --git a/apis/serving/v1alpha2/bentodeployment_types.go b/apis/serving/v1alpha2/bentodeployment_types.go index cb9c367..eb9a218 100644 --- a/apis/serving/v1alpha2/bentodeployment_types.go +++ b/apis/serving/v1alpha2/bentodeployment_types.go @@ -20,17 +20,16 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/bentoml/yatai-deployment/apis/serving/common" - "github.com/bentoml/yatai-schemas/modelschemas" ) // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! // NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. type BentoDeploymentRunnerSpec struct { - Name string `json:"name,omitempty"` - Resources *common.Resources `json:"resources,omitempty"` - Autoscaling *modelschemas.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` - Envs *[]modelschemas.LabelItemSchema `json:"envs,omitempty"` + Name string `json:"name,omitempty"` + Resources *common.Resources `json:"resources,omitempty"` + Autoscaling *common.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` + Envs *[]common.LabelItemSchema `json:"envs,omitempty"` } type BentoDeploymentIngressSpec struct { @@ -44,9 +43,9 @@ type BentoDeploymentSpec struct { BentoTag string `json:"bento_tag"` - Resources *common.Resources `json:"resources,omitempty"` - Autoscaling *modelschemas.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` - Envs *[]modelschemas.LabelItemSchema `json:"envs,omitempty"` + Resources *common.Resources `json:"resources,omitempty"` + Autoscaling *common.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` + Envs *[]common.LabelItemSchema `json:"envs,omitempty"` Runners []BentoDeploymentRunnerSpec `json:"runners,omitempty"` diff --git a/apis/serving/v1alpha2/zz_generated.deepcopy.go b/apis/serving/v1alpha2/zz_generated.deepcopy.go index f116b01..a538a31 100644 --- a/apis/serving/v1alpha2/zz_generated.deepcopy.go +++ b/apis/serving/v1alpha2/zz_generated.deepcopy.go @@ -22,9 +22,9 @@ limitations under the License. package v1alpha2 import ( - "github.com/bentoml/yatai-deployment/apis/serving/common" - "github.com/bentoml/yatai-schemas/modelschemas" runtime "k8s.io/apimachinery/pkg/runtime" + + "github.com/bentoml/yatai-deployment/apis/serving/common" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. @@ -111,17 +111,16 @@ func (in *BentoDeploymentRunnerSpec) DeepCopyInto(out *BentoDeploymentRunnerSpec } if in.Autoscaling != nil { in, out := &in.Autoscaling, &out.Autoscaling - *out = (*in).DeepCopy() + *out = new(common.DeploymentTargetHPAConf) + (*in).DeepCopyInto(*out) } if in.Envs != nil { in, out := &in.Envs, &out.Envs - *out = new([]modelschemas.LabelItemSchema) + *out = new([]common.LabelItemSchema) if **in != nil { in, out := *in, *out - *out = make([]modelschemas.LabelItemSchema, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } + *out = make([]common.LabelItemSchema, len(*in)) + copy(*out, *in) } } } @@ -146,17 +145,16 @@ func (in *BentoDeploymentSpec) DeepCopyInto(out *BentoDeploymentSpec) { } if in.Autoscaling != nil { in, out := &in.Autoscaling, &out.Autoscaling - *out = (*in).DeepCopy() + *out = new(common.DeploymentTargetHPAConf) + (*in).DeepCopyInto(*out) } if in.Envs != nil { in, out := &in.Envs, &out.Envs - *out = new([]modelschemas.LabelItemSchema) + *out = new([]common.LabelItemSchema) if **in != nil { in, out := *in, *out - *out = make([]modelschemas.LabelItemSchema, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } + *out = make([]common.LabelItemSchema, len(*in)) + copy(*out, *in) } } if in.Runners != nil { diff --git a/apis/serving/v1alpha3/bentodeployment_conversion.go b/apis/serving/v1alpha3/bentodeployment_conversion.go index 8b30617..577389e 100644 --- a/apis/serving/v1alpha3/bentodeployment_conversion.go +++ b/apis/serving/v1alpha3/bentodeployment_conversion.go @@ -24,17 +24,17 @@ import ( "github.com/bentoml/yatai-common/utils" resourcesv1alpha1 "github.com/bentoml/yatai-image-builder/apis/resources/v1alpha1" resourcesclient "github.com/bentoml/yatai-image-builder/generated/resources/clientset/versioned/typed/resources/v1alpha1" - "github.com/bentoml/yatai-schemas/modelschemas" + "github.com/bentoml/yatai-deployment/apis/serving/common" "github.com/bentoml/yatai-deployment/apis/serving/v2alpha1" ) -func TransformToOldHPA(hpa *v2alpha1.Autoscaling) (oldHpa *modelschemas.DeploymentTargetHPAConf, err error) { +func TransformToOldHPA(hpa *v2alpha1.Autoscaling) (oldHpa *common.DeploymentTargetHPAConf, err error) { if hpa == nil { return } - oldHpa = &modelschemas.DeploymentTargetHPAConf{ + oldHpa = &common.DeploymentTargetHPAConf{ MinReplicas: utils.Int32Ptr(hpa.MinReplicas), MaxReplicas: utils.Int32Ptr(hpa.MaxReplicas), } @@ -82,7 +82,7 @@ func TransformToOldHPA(hpa *v2alpha1.Autoscaling) (oldHpa *modelschemas.Deployme return } -func TransformToNewHPA(oldHpa *modelschemas.DeploymentTargetHPAConf) (hpa *v2alpha1.Autoscaling, err error) { +func TransformToNewHPA(oldHpa *common.DeploymentTargetHPAConf) (hpa *v2alpha1.Autoscaling, err error) { if oldHpa == nil { return } @@ -219,7 +219,7 @@ func TransformToOldExtraPodSpec(src *v2alpha1.ExtraPodSpec) (dst *ExtraPodSpec) return } -func TransformToNewEnvs(oldEnvs *[]modelschemas.LabelItemSchema) (envs []corev1.EnvVar) { +func TransformToNewEnvs(oldEnvs *[]common.LabelItemSchema) (envs []corev1.EnvVar) { if oldEnvs == nil { return } @@ -233,18 +233,18 @@ func TransformToNewEnvs(oldEnvs *[]modelschemas.LabelItemSchema) (envs []corev1. return } -func TransformToOldEnvs(envs []corev1.EnvVar) (oldEnvs *[]modelschemas.LabelItemSchema) { +func TransformToOldEnvs(envs []corev1.EnvVar) (oldEnvs *[]common.LabelItemSchema) { if envs == nil { return } - oldEnvs_ := []modelschemas.LabelItemSchema{} + oldEnvs_ := []common.LabelItemSchema{} for _, item := range envs { if item.Value == "" { continue } - oldEnvs_ = append(oldEnvs_, modelschemas.LabelItemSchema{ + oldEnvs_ = append(oldEnvs_, common.LabelItemSchema{ Key: item.Name, Value: item.Value, }) diff --git a/apis/serving/v1alpha3/bentodeployment_types.go b/apis/serving/v1alpha3/bentodeployment_types.go index 7ec9b02..dc79e14 100644 --- a/apis/serving/v1alpha3/bentodeployment_types.go +++ b/apis/serving/v1alpha3/bentodeployment_types.go @@ -21,8 +21,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/bentoml/yatai-deployment/apis/serving/common" - - "github.com/bentoml/yatai-schemas/modelschemas" ) // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! @@ -42,12 +40,12 @@ type ExtraPodSpec struct { } type BentoDeploymentRunnerSpec struct { - Name string `json:"name,omitempty"` - Annotations map[string]string `json:"annotations,omitempty"` - Labels map[string]string `json:"labels,omitempty"` - Resources *common.Resources `json:"resources,omitempty"` - Autoscaling *modelschemas.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` - Envs *[]modelschemas.LabelItemSchema `json:"envs,omitempty"` + Name string `json:"name,omitempty"` + Annotations map[string]string `json:"annotations,omitempty"` + Labels map[string]string `json:"labels,omitempty"` + Resources *common.Resources `json:"resources,omitempty"` + Autoscaling *common.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` + Envs *[]common.LabelItemSchema `json:"envs,omitempty"` // +optional ExtraPodMetadata *ExtraPodMetadata `json:"extra_pod_metadata,omitempty"` @@ -76,9 +74,9 @@ type BentoDeploymentSpec struct { BentoTag string `json:"bento_tag"` - Resources *common.Resources `json:"resources,omitempty"` - Autoscaling *modelschemas.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` - Envs *[]modelschemas.LabelItemSchema `json:"envs,omitempty"` + Resources *common.Resources `json:"resources,omitempty"` + Autoscaling *common.DeploymentTargetHPAConf `json:"autoscaling,omitempty"` + Envs *[]common.LabelItemSchema `json:"envs,omitempty"` Runners []BentoDeploymentRunnerSpec `json:"runners,omitempty"` diff --git a/apis/serving/v1alpha3/zz_generated.deepcopy.go b/apis/serving/v1alpha3/zz_generated.deepcopy.go index ce6c5c5..8e53ae4 100644 --- a/apis/serving/v1alpha3/zz_generated.deepcopy.go +++ b/apis/serving/v1alpha3/zz_generated.deepcopy.go @@ -22,10 +22,10 @@ limitations under the License. package v1alpha3 import ( - "github.com/bentoml/yatai-deployment/apis/serving/common" - "github.com/bentoml/yatai-schemas/modelschemas" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" runtime "k8s.io/apimachinery/pkg/runtime" + + "github.com/bentoml/yatai-deployment/apis/serving/common" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. @@ -160,17 +160,16 @@ func (in *BentoDeploymentRunnerSpec) DeepCopyInto(out *BentoDeploymentRunnerSpec } if in.Autoscaling != nil { in, out := &in.Autoscaling, &out.Autoscaling - *out = (*in).DeepCopy() + *out = new(common.DeploymentTargetHPAConf) + (*in).DeepCopyInto(*out) } if in.Envs != nil { in, out := &in.Envs, &out.Envs - *out = new([]modelschemas.LabelItemSchema) + *out = new([]common.LabelItemSchema) if **in != nil { in, out := *in, *out - *out = make([]modelschemas.LabelItemSchema, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } + *out = make([]common.LabelItemSchema, len(*in)) + copy(*out, *in) } } if in.ExtraPodMetadata != nil { @@ -219,17 +218,16 @@ func (in *BentoDeploymentSpec) DeepCopyInto(out *BentoDeploymentSpec) { } if in.Autoscaling != nil { in, out := &in.Autoscaling, &out.Autoscaling - *out = (*in).DeepCopy() + *out = new(common.DeploymentTargetHPAConf) + (*in).DeepCopyInto(*out) } if in.Envs != nil { in, out := &in.Envs, &out.Envs - *out = new([]modelschemas.LabelItemSchema) + *out = new([]common.LabelItemSchema) if **in != nil { in, out := *in, *out - *out = make([]modelschemas.LabelItemSchema, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } + *out = make([]common.LabelItemSchema, len(*in)) + copy(*out, *in) } } if in.Runners != nil { diff --git a/controllers/bentodeployment_controller.go b/controllers/bentodeployment_controller.go index d479b9d..4ab3c02 100644 --- a/controllers/bentodeployment_controller.go +++ b/controllers/bentodeployment_controller.go @@ -62,12 +62,12 @@ import ( commonconfig "github.com/bentoml/yatai-common/config" commonconsts "github.com/bentoml/yatai-common/consts" "github.com/bentoml/yatai-common/system" + commonutils "github.com/bentoml/yatai-common/utils" resourcesv1alpha1 "github.com/bentoml/yatai-image-builder/apis/resources/v1alpha1" servingcommon "github.com/bentoml/yatai-deployment/apis/serving/common" servingconversion "github.com/bentoml/yatai-deployment/apis/serving/conversion" - servingv1alpha3 "github.com/bentoml/yatai-deployment/apis/serving/v1alpha3" servingv2alpha1 "github.com/bentoml/yatai-deployment/apis/serving/v2alpha1" "github.com/bentoml/yatai-deployment/utils" "github.com/bentoml/yatai-deployment/version" @@ -468,7 +468,7 @@ func (r *BentoDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Requ } } var hpaConf *modelschemas.DeploymentTargetHPAConf - hpaConf, err = servingv1alpha3.TransformToOldHPA(runner.Autoscaling) + hpaConf, err = TransformToOldHPA(runner.Autoscaling) if err != nil { return } @@ -487,7 +487,7 @@ func (r *BentoDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Requ } var hpaConf *modelschemas.DeploymentTargetHPAConf - hpaConf, err = servingv1alpha3.TransformToOldHPA(bentoDeployment.Spec.Autoscaling) + hpaConf, err = TransformToOldHPA(bentoDeployment.Spec.Autoscaling) if err != nil { return } @@ -3091,3 +3091,56 @@ func (r *BentoDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error { } return m.WithEventFilter(pred).Complete(r) } + +func TransformToOldHPA(hpa *servingv2alpha1.Autoscaling) (oldHpa *modelschemas.DeploymentTargetHPAConf, err error) { + if hpa == nil { + return + } + + oldHpa = &modelschemas.DeploymentTargetHPAConf{ + MinReplicas: commonutils.Int32Ptr(hpa.MinReplicas), + MaxReplicas: commonutils.Int32Ptr(hpa.MaxReplicas), + } + + for _, metric := range hpa.Metrics { + if metric.Type == autoscalingv2beta2.PodsMetricSourceType { + if metric.Pods == nil { + continue + } + if metric.Pods.Metric.Name == commonconsts.KubeHPAQPSMetric { + if metric.Pods.Target.Type != autoscalingv2beta2.UtilizationMetricType { + continue + } + if metric.Pods.Target.AverageValue == nil { + continue + } + qps := metric.Pods.Target.AverageValue.Value() + oldHpa.QPS = &qps + } + } else if metric.Type == autoscalingv2beta2.ResourceMetricSourceType { + if metric.Resource == nil { + continue + } + if metric.Resource.Name == corev1.ResourceCPU { + if metric.Resource.Target.Type != autoscalingv2beta2.UtilizationMetricType { + continue + } + if metric.Resource.Target.AverageUtilization == nil { + continue + } + cpu := *metric.Resource.Target.AverageUtilization + oldHpa.CPU = &cpu + } else if metric.Resource.Name == corev1.ResourceMemory { + if metric.Resource.Target.Type != autoscalingv2beta2.UtilizationMetricType { + continue + } + if metric.Resource.Target.AverageUtilization == nil { + continue + } + memory := metric.Resource.Target.AverageValue.String() + oldHpa.Memory = &memory + } + } + } + return +}