diff --git a/apis/autoscaling/v1alpha2/hvpa_conversion.go b/apis/autoscaling/v1alpha2/hvpa_conversion.go new file mode 100644 index 00000000..e6050907 --- /dev/null +++ b/apis/autoscaling/v1alpha2/hvpa_conversion.go @@ -0,0 +1,98 @@ +package v1alpha2 + +import ( + "github.com/gardener/hvpa-controller/apis/autoscaling/v1alpha1" + autoscalingv2 "k8s.io/api/autoscaling/v2" + autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1" + "sigs.k8s.io/controller-runtime/pkg/conversion" +) + +func (src *Hvpa) ConvertTo(dstRaw conversion.Hub) error { + return nil +} + +func Convert_v2beta1_ResourceMetricSource_To_autoscaling_ResourceMetricSource(in *autoscalingv2beta1.ResourceMetricSource) *autoscalingv2.ResourceMetricSource { + out := &autoscalingv2.ResourceMetricSource{} + out.Name = in.Name + utilization := in.TargetAverageUtilization + averageValue := in.TargetAverageValue + + var metricType autoscalingv2.MetricTargetType + if utilization == nil { + metricType = autoscalingv2.AverageValueMetricType + } else { + metricType = autoscalingv2.UtilizationMetricType + } + out.Target = autoscalingv2.MetricTarget{ + Type: metricType, + AverageValue: averageValue, + AverageUtilization: utilization, + } + return out +} + +func (dst *Hvpa) ConvertFrom(srcRaw conversion.Hub) error { + src := srcRaw.(*v1alpha1.Hvpa) + + dst.Spec.Replicas = src.Spec.Replicas + dst.Spec.Vpa = VpaSpec{ + Selector: src.Spec.Vpa.Selector, + Deploy: src.Spec.Vpa.Deploy, + ScaleUp: Convert_v1alpha1_ScaleType_To_ScaleType(src.Spec.Vpa.ScaleUp), + ScaleDown: Convert_v1alpha1_ScaleType_To_ScaleType(src.Spec.Vpa.ScaleDown), + Template: VpaTemplate{ + ObjectMeta: src.Spec.Vpa.Template.ObjectMeta, + Spec: VpaTemplateSpec(src.Spec.Vpa.Template.Spec), + }, + LimitsRequestsGapScaleParams: Convert_v1alpha1_ScaleParams_To_ScaleParams(src.Spec.Vpa.LimitsRequestsGapScaleParams), + } + dst.Spec.WeightBasedScalingIntervals = []WeightBasedScalingInterval{} + for _, weightBasedScalingInterval := range src.Spec.WeightBasedScalingIntervals { + dst.Spec.WeightBasedScalingIntervals = append(dst.Spec.WeightBasedScalingIntervals, WeightBasedScalingInterval(weightBasedScalingInterval)) + } + dst.Spec.TargetRef = &autoscalingv2.CrossVersionObjectReference{ + Kind: src.Spec.TargetRef.Kind, + Name: src.Spec.TargetRef.Name, + APIVersion: src.Spec.TargetRef.APIVersion, + } + mw := MaintenanceTimeWindow(*src.Spec.MaintenanceTimeWindow) + dst.Spec.MaintenanceTimeWindow = &mw + + dst.Spec.Hpa.Selector = src.Spec.Hpa.Selector + dst.Spec.Hpa.Deploy = src.Spec.Hpa.Deploy + dst.Spec.Hpa.ScaleUp = Convert_v1alpha1_ScaleType_To_ScaleType(src.Spec.Hpa.ScaleUp) + dst.Spec.Hpa.ScaleDown = Convert_v1alpha1_ScaleType_To_ScaleType(src.Spec.Hpa.ScaleDown) + dst.Spec.Hpa.Template.ObjectMeta = src.Spec.Hpa.Template.ObjectMeta + dst.Spec.Hpa.Template.Spec.MinReplicas = src.Spec.Hpa.Template.Spec.MinReplicas + dst.Spec.Hpa.Template.Spec.MaxReplicas = src.Spec.Hpa.Template.Spec.MaxReplicas + for _, metric := range src.Spec.Hpa.Template.Spec.Metrics { + ms := autoscalingv2.MetricSpec{ + Type: autoscalingv2.MetricSourceType(metric.Type), + Object: nil, + Pods: nil, + Resource: Convert_v2beta1_ResourceMetricSource_To_autoscaling_ResourceMetricSource(metric.Resource), + ContainerResource: nil, + External: nil, + } + dst.Spec.Hpa.Template.Spec.Metrics = append(dst.Spec.Hpa.Template.Spec.Metrics, ms) + + } + + return nil +} + +func Convert_v1alpha1_ScaleParams_To_ScaleParams(in v1alpha1.ScaleParams) ScaleParams { + return ScaleParams{ + CPU: ChangeParams(in.CPU), + Memory: ChangeParams(in.Memory), + Replicas: ChangeParams(in.Replicas), + } +} + +func Convert_v1alpha1_ScaleType_To_ScaleType(in v1alpha1.ScaleType) ScaleType { + return ScaleType{ + UpdatePolicy: UpdatePolicy(in.UpdatePolicy), + MinChange: Convert_v1alpha1_ScaleParams_To_ScaleParams(in.MinChange), + StabilizationDuration: in.StabilizationDuration, + } +} diff --git a/vendor/github.com/gardener/hvpa-controller/apis/autoscaling/v1alpha1/hvpa_types.go b/vendor/github.com/gardener/hvpa-controller/apis/autoscaling/v1alpha1/hvpa_types.go index 6297f9a0..282bff9a 100644 --- a/vendor/github.com/gardener/hvpa-controller/apis/autoscaling/v1alpha1/hvpa_types.go +++ b/vendor/github.com/gardener/hvpa-controller/apis/autoscaling/v1alpha1/hvpa_types.go @@ -350,3 +350,6 @@ type HvpaList struct { /*func init() { SchemeBuilder.Register(&Hvpa{}, &HvpaList{}) }*/ + +// Hub marks this version as the API conversion Hub +func (*Hvpa) Hub() {} diff --git a/vendor/github.com/gardener/hvpa-controller/apis/autoscaling/v1alpha2/hvpa_conversion.go b/vendor/github.com/gardener/hvpa-controller/apis/autoscaling/v1alpha2/hvpa_conversion.go new file mode 100644 index 00000000..6771149a --- /dev/null +++ b/vendor/github.com/gardener/hvpa-controller/apis/autoscaling/v1alpha2/hvpa_conversion.go @@ -0,0 +1,44 @@ +package v1alpha2 + +import ( + "github.com/gardener/hvpa-controller/apis/autoscaling/v1alpha1" + autoscalingv2 "k8s.io/api/autoscaling/v2" + autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1" + core "k8s.io/api/core/v1" + "sigs.k8s.io/controller-runtime/pkg/conversion" +) + +func (src *Hvpa) ConvertTo(dstRaw conversion.Hub) error { + dst := dstRaw.(*v1alpha1.Hvpa) + + for i, metric := range src.Spec.Hpa.Template.Spec.Metrics { + Convert_v2beta1_ResourceMetricSource_To_autoscaling_ResourceMetricSource(metric.Resource, dst.Spec.Hpa.Template.Spec.Metrics[i]) + + } + + return nil +} + +func Convert_v2beta1_ResourceMetricSource_To_autoscaling_ResourceMetricSource(in *autoscalingv2beta1.ResourceMetricSource, out *autoscalingv2.ResourceMetricSource) error { + out.Name = core.ResourceName(in.Name) + utilization := in.TargetAverageUtilization + averageValue := in.TargetAverageValue + + var metricType autoscalingv2.MetricTargetType + if utilization == nil { + metricType = autoscalingv2.AverageValueMetricType + } else { + metricType = autoscalingv2.UtilizationMetricType + } + out.Target = autoscalingv2.MetricTarget{ + Type: metricType, + AverageValue: averageValue, + AverageUtilization: utilization, + } + return nil +} + +func (dst *Hvpa) ConvertFrom(srcRaw conversion.Hub) error { + //TODO implement me + panic("implement me") +}