Repository: vllm-project/aibrix
File: pkg/webhook/podautoscaler_webhook.go (Go)

Code before:
		strategyPath := specPath.Child("scalingStrategy")
		allErrs = append(allErrs, field.NotSupported(strategyPath, pa.Spec.ScalingStrategy, []string{
			string(autoscalingv1alpha1.HPA),
			string(autoscalingv1alpha1.KPA),
			string(autoscalingv1alpha1.APA),
		}))
	}

	// 4. Validate MetricsSources
	metricsPath := specPath.Child("metricsSources")
	if len(pa.Spec.MetricsSources) != 1 {
		allErrs = append(allErrs, field.Invalid(metricsPath, pa.Spec.MetricsSources, "exactly one metricsSource is required"))
	} else {
		ms := &pa.Spec.MetricsSources[0]
		msPath := metricsPath.Index(0)

		if ms.TargetMetric == "" {
			allErrs = append(allErrs, field.Required(msPath.Child("targetMetric"), "must be set"))
		}
		if ms.TargetValue == "" {
			allErrs = append(allErrs, field.Required(msPath.Child("targetValue"), "must be set"))
		} else {
			val, err := strconv.ParseFloat(ms.TargetValue, 64)
			if err != nil {
				allErrs = append(allErrs, field.Invalid(msPath.Child("targetValue"), ms.TargetValue, "must be a valid number"))
			} else if val <= 0 {
				allErrs = append(allErrs, field.Invalid(msPath.Child("targetValue"), ms.TargetValue, "must be greater than 0"))
			}
		}

		switch ms.MetricSourceType {
		case autoscalingv1alpha1.POD:
			if ms.ProtocolType == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("protocolType"), "required for metricSourceType=pod"))
			}
			if ms.Port == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("port"), "required for metricSourceType=pod"))
			}
			if ms.Path == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("path"), "required for metricSourceType=pod"))
			}

		case autoscalingv1alpha1.EXTERNAL, autoscalingv1alpha1.DOMAIN:
			if ms.ProtocolType == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("protocolType"), "required for metricSourceType=external/domain"))
			}
			if ms.Endpoint == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("endpoint"), "required for metricSourceType=external/domain"))
			}
			if ms.Path == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("path"), "required for metricSourceType=external/domain"))

Review comment (suggestion):
```suggestion
			} else if val.Sign() <= 0 {
```

Code after:
		strategyPath := specPath.Child("scalingStrategy")
		allErrs = append(allErrs, field.NotSupported(strategyPath, pa.Spec.ScalingStrategy, []string{
			string(autoscalingv1alpha1.HPA),
			string(autoscalingv1alpha1.KPA),
			string(autoscalingv1alpha1.APA),
		}))
	}

	// 4. Validate MetricsSources
	metricsPath := specPath.Child("metricsSources")
	if len(pa.Spec.MetricsSources) != 1 {
		allErrs = append(allErrs, field.Invalid(metricsPath, pa.Spec.MetricsSources, "exactly one metricsSource is required"))
	} else {
		ms := &pa.Spec.MetricsSources[0]
		msPath := metricsPath.Index(0)

		if ms.TargetMetric == "" {
			allErrs = append(allErrs, field.Required(msPath.Child("targetMetric"), "must be set"))
		}
		if ms.TargetValue == "" {
			allErrs = append(allErrs, field.Required(msPath.Child("targetValue"), "must be set"))
		} else {
			qty, err := resource.ParseQuantity(ms.TargetValue)
			if err != nil {
				allErrs = append(allErrs, field.Invalid(msPath.Child("targetValue"), ms.TargetValue, "must be a valid number"))
			} else {
				if qty.Sign() <= 0 {
					allErrs = append(allErrs, field.Invalid(msPath.Child("targetValue"), ms.TargetValue, "must be greater than 0"))
				}
			}
		}

		switch ms.MetricSourceType {
		case autoscalingv1alpha1.POD:
			if ms.ProtocolType == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("protocolType"), "required for metricSourceType=pod"))
			}
			if ms.Port == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("port"), "required for metricSourceType=pod"))
			}
			if ms.Path == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("path"), "required for metricSourceType=pod"))
			}

		case autoscalingv1alpha1.EXTERNAL, autoscalingv1alpha1.DOMAIN:
			if ms.ProtocolType == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("protocolType"), "required for metricSourceType=external/domain"))
			}
			if ms.Endpoint == "" {
				allErrs = append(allErrs, field.Required(msPath.Child("endpoint"), "required for metricSourceType=external/domain"))
			}