Skip to content

Commit

Permalink
WIP: add conversion webhook logic
Browse files Browse the repository at this point in the history
  • Loading branch information
voelzmo committed Nov 7, 2022
1 parent e3a1149 commit 8b62580
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
98 changes: 98 additions & 0 deletions apis/autoscaling/v1alpha2/hvpa_conversion.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

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

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

0 comments on commit 8b62580

Please sign in to comment.