Skip to content

Commit

Permalink
implement v1beta2
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho committed Oct 11, 2023
1 parent f0f81be commit 93bdea8
Show file tree
Hide file tree
Showing 104 changed files with 3,470 additions and 1,194 deletions.
8 changes: 8 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ resources:
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: mercari.com
group: autoscaling
kind: Tortoise
path: github.com/mercari/tortoise/api/v1beta2
version: v1beta2
version: "3"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Tortoise, you don't need a rearing cage, but need VPA in your Kubernetes cluster
Tortoise, they only need the deployment name basically.

```yaml
apiVersion: autoscaling.mercari.com/v1beta1
apiVersion: autoscaling.mercari.com/v1beta2
kind: Tortoise
metadata:
name: lovely-tortoise
Expand All @@ -55,7 +55,7 @@ Tortoise, then they'll prepare/keep adjusting HPA and VPA to achieve efficient a
## API definition
- [Tortoise](./api/v1beta1/tortoise_types.go)
- [Tortoise](./api/v1beta2/tortoise_types.go)
## Contribution
Expand Down
4 changes: 2 additions & 2 deletions api/autoscaling/v2/horizontalpodautoscaler_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"

"github.com/mercari/tortoise/api/v1beta1"
"github.com/mercari/tortoise/api/v1beta2"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -46,7 +46,7 @@ func mutateTest(before, after, torotise string) {

y, err := os.ReadFile(torotise)
Expect(err).NotTo(HaveOccurred())
tor := &v1beta1.Tortoise{}
tor := &v1beta2.Tortoise{}
err = yaml.NewYAMLOrJSONDecoder(bytes.NewReader(y), 4096).Decode(tor)
status := tor.Status
Expect(err).NotTo(HaveOccurred())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: autoscaling.mercari.com/v1beta1
apiVersion: autoscaling.mercari.com/v1beta2
kind: Tortoise
metadata:
name: tortoise-sample
Expand Down Expand Up @@ -36,7 +36,9 @@ status:
memory:
phase: Working
targets:
deployment: sample
scaleTargetRef:
kind: Deployment
name: sample
horizontalPodAutoscaler: sample
verticalPodAutoscalers:
- name: tortoise-monitor-sample
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: autoscaling.mercari.com/v1beta1
apiVersion: autoscaling.mercari.com/v1beta2
kind: Tortoise
metadata:
name: tortoise-sample
Expand Down Expand Up @@ -36,7 +36,9 @@ status:
memory:
phase: Working
targets:
deployment: sample
scaleTargetRef:
kind: Deployment
name: sample
horizontalPodAutoscaler: sample2
verticalPodAutoscalers:
- name: tortoise-monitor-sample
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: autoscaling.mercari.com/v1beta1
apiVersion: autoscaling.mercari.com/v1beta2
kind: Tortoise
metadata:
name: tortoise-sample
Expand Down Expand Up @@ -36,7 +36,9 @@ status:
memory:
phase: Working
targets:
deployment: sample
scaleTargetRef:
kind: Deployment
name: sample
horizontalPodAutoscaler: sample
verticalPodAutoscalers:
- name: tortoise-monitor-sample
Expand Down
4 changes: 2 additions & 2 deletions api/autoscaling/v2/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/mercari/tortoise/api/v1beta1"
"github.com/mercari/tortoise/api/v1beta2"
"github.com/mercari/tortoise/pkg/config"
"github.com/mercari/tortoise/pkg/hpa"
"github.com/mercari/tortoise/pkg/tortoise"
Expand Down Expand Up @@ -115,7 +115,7 @@ var _ = BeforeSuite(func() {
Expect(cfg).NotTo(BeNil())

scheme := runtime.NewScheme()
err = v1beta1.AddToScheme(scheme)
err = v1beta2.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
err = clientgoscheme.AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
Expand Down
116 changes: 60 additions & 56 deletions api/v1alpha1/tortoise_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,47 @@ import (
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/conversion"

"github.com/mercari/tortoise/api/v1beta1"
"github.com/mercari/tortoise/api/v1beta2"
)

// ConvertTo converts this CronJob to the Hub version (v1beta1).
// ConvertTo converts this CronJob to the Hub version (v1beta2).
func (src *Tortoise) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1beta1.Tortoise)
dst := dstRaw.(*v1beta2.Tortoise)
dst.ObjectMeta = src.ObjectMeta

dst.Spec = v1beta1.TortoiseSpec{
TargetRefs: v1beta1.TargetRefs{
dst.Spec = v1beta2.TortoiseSpec{
TargetRefs: v1beta2.TargetRefs{
HorizontalPodAutoscalerName: src.Spec.TargetRefs.HorizontalPodAutoscalerName,
ScaleTargetRef: v1beta1.CrossVersionObjectReference{
ScaleTargetRef: v1beta2.CrossVersionObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: src.Spec.TargetRefs.DeploymentName,
},
},
UpdateMode: v1beta1.UpdateMode(src.Spec.UpdateMode),
UpdateMode: v1beta2.UpdateMode(src.Spec.UpdateMode),
ResourcePolicy: containerResourcePolicyConversionToV1Beta1(src.Spec.ResourcePolicy),
DeletionPolicy: v1beta1.DeletionPolicy(src.Spec.DeletionPolicy),
DeletionPolicy: v1beta2.DeletionPolicy(src.Spec.DeletionPolicy),
}

dst.Status = v1beta1.TortoiseStatus{
TortoisePhase: v1beta1.TortoisePhase(src.Status.TortoisePhase),
Conditions: v1beta1.Conditions{
dst.Status = v1beta2.TortoiseStatus{
TortoisePhase: v1beta2.TortoisePhase(src.Status.TortoisePhase),
Conditions: v1beta2.Conditions{
ContainerRecommendationFromVPA: containerRecommendationFromVPAConversionToV1Beta1(src.Status.Conditions.ContainerRecommendationFromVPA),
},
Targets: v1beta1.TargetsStatus{
Targets: v1beta2.TargetsStatus{
HorizontalPodAutoscaler: src.Status.Targets.HorizontalPodAutoscaler,
Deployment: src.Status.Targets.Deployment,
VerticalPodAutoscalers: verticalPodAutoscalersConversionToV1Beta1(src.Status.Targets.VerticalPodAutoscalers),
ScaleTargetRef: v1beta2.CrossVersionObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: src.Status.Targets.Deployment,
},
VerticalPodAutoscalers: verticalPodAutoscalersConversionToV1Beta1(src.Status.Targets.VerticalPodAutoscalers),
},
}
if src.Status.Recommendations.Horizontal != nil {
dst.Status = v1beta1.TortoiseStatus{
Recommendations: v1beta1.Recommendations{
Horizontal: v1beta1.HorizontalRecommendations{
dst.Status = v1beta2.TortoiseStatus{
Recommendations: v1beta2.Recommendations{
Horizontal: v1beta2.HorizontalRecommendations{
TargetUtilizations: hPATargetUtilizationRecommendationPerContainerConversionToV1Beta1(src.Status.Recommendations.Horizontal.TargetUtilizations),
MaxReplicas: replicasRecommendationConversionToV1Beta1(src.Status.Recommendations.Horizontal.MaxReplicas),
MinReplicas: replicasRecommendationConversionToV1Beta1(src.Status.Recommendations.Horizontal.MinReplicas),
Expand All @@ -76,9 +80,9 @@ func (src *Tortoise) ConvertTo(dstRaw conversion.Hub) error {
}
}
if src.Status.Recommendations.Vertical != nil {
dst.Status = v1beta1.TortoiseStatus{
Recommendations: v1beta1.Recommendations{
Vertical: v1beta1.VerticalRecommendations{
dst.Status = v1beta2.TortoiseStatus{
Recommendations: v1beta2.Recommendations{
Vertical: v1beta2.VerticalRecommendations{
ContainerResourceRecommendation: containerResourceRecommendationConversionToV1Beta1(src.Status.Recommendations.Vertical.ContainerResourceRecommendation),
},
},
Expand All @@ -88,9 +92,9 @@ func (src *Tortoise) ConvertTo(dstRaw conversion.Hub) error {
return nil
}

// ConvertFrom converts from the Hub version (v1beta1) to this version.
// ConvertFrom converts from the Hub version (v1beta2) to this version.
func (dst *Tortoise) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1beta1.Tortoise)
src := srcRaw.(*v1beta2.Tortoise)
if src.Spec.TargetRefs.ScaleTargetRef.Kind != "Deployment" {
return fmt.Errorf("scaleTargetRef is not Deployment, but %s which isn't supported in v1alpha1", src.Spec.TargetRefs.ScaleTargetRef.Kind)
}
Expand All @@ -113,7 +117,7 @@ func (dst *Tortoise) ConvertFrom(srcRaw conversion.Hub) error {
},
Targets: TargetsStatus{
HorizontalPodAutoscaler: src.Status.Targets.HorizontalPodAutoscaler,
Deployment: src.Status.Targets.Deployment,
Deployment: src.Status.Targets.ScaleTargetRef.Name,
VerticalPodAutoscalers: verticalPodAutoscalersConversionFromV1Beta1(src.Status.Targets.VerticalPodAutoscalers),
},
Recommendations: Recommendations{
Expand All @@ -130,7 +134,7 @@ func (dst *Tortoise) ConvertFrom(srcRaw conversion.Hub) error {
return nil
}

func verticalPodAutoscalersConversionFromV1Beta1(vpas []v1beta1.TargetStatusVerticalPodAutoscaler) []TargetStatusVerticalPodAutoscaler {
func verticalPodAutoscalersConversionFromV1Beta1(vpas []v1beta2.TargetStatusVerticalPodAutoscaler) []TargetStatusVerticalPodAutoscaler {
converted := make([]TargetStatusVerticalPodAutoscaler, 0, len(vpas))
for _, vpa := range vpas {
converted = append(converted, TargetStatusVerticalPodAutoscaler{
Expand All @@ -141,18 +145,18 @@ func verticalPodAutoscalersConversionFromV1Beta1(vpas []v1beta1.TargetStatusVert
return converted
}

func verticalPodAutoscalersConversionToV1Beta1(vpas []TargetStatusVerticalPodAutoscaler) []v1beta1.TargetStatusVerticalPodAutoscaler {
converted := make([]v1beta1.TargetStatusVerticalPodAutoscaler, 0, len(vpas))
func verticalPodAutoscalersConversionToV1Beta1(vpas []TargetStatusVerticalPodAutoscaler) []v1beta2.TargetStatusVerticalPodAutoscaler {
converted := make([]v1beta2.TargetStatusVerticalPodAutoscaler, 0, len(vpas))
for _, vpa := range vpas {
converted = append(converted, v1beta1.TargetStatusVerticalPodAutoscaler{
converted = append(converted, v1beta2.TargetStatusVerticalPodAutoscaler{
Name: vpa.Name,
Role: v1beta1.VerticalPodAutoscalerRole(vpa.Role),
Role: v1beta2.VerticalPodAutoscalerRole(vpa.Role),
})
}
return converted
}

func containerResourceRecommendationConversionFromV1Beta1(resources []v1beta1.RecommendedContainerResources) []RecommendedContainerResources {
func containerResourceRecommendationConversionFromV1Beta1(resources []v1beta2.RecommendedContainerResources) []RecommendedContainerResources {
converted := make([]RecommendedContainerResources, 0, len(resources))
for _, resource := range resources {
converted = append(converted, RecommendedContainerResources{
Expand All @@ -163,18 +167,18 @@ func containerResourceRecommendationConversionFromV1Beta1(resources []v1beta1.Re
return converted
}

func containerResourceRecommendationConversionToV1Beta1(resources []RecommendedContainerResources) []v1beta1.RecommendedContainerResources {
converted := make([]v1beta1.RecommendedContainerResources, 0, len(resources))
func containerResourceRecommendationConversionToV1Beta1(resources []RecommendedContainerResources) []v1beta2.RecommendedContainerResources {
converted := make([]v1beta2.RecommendedContainerResources, 0, len(resources))
for _, resource := range resources {
converted = append(converted, v1beta1.RecommendedContainerResources{
converted = append(converted, v1beta2.RecommendedContainerResources{
ContainerName: resource.ContainerName,
RecommendedResource: resource.RecommendedResource,
})
}
return converted
}

func replicasRecommendationConversionFromV1Beta1(recommendations []v1beta1.ReplicasRecommendation) []ReplicasRecommendation {
func replicasRecommendationConversionFromV1Beta1(recommendations []v1beta2.ReplicasRecommendation) []ReplicasRecommendation {
converted := make([]ReplicasRecommendation, 0, len(recommendations))
for _, recommendation := range recommendations {
converted = append(converted, ReplicasRecommendation{
Expand All @@ -189,10 +193,10 @@ func replicasRecommendationConversionFromV1Beta1(recommendations []v1beta1.Repli
return converted
}

func replicasRecommendationConversionToV1Beta1(recommendations []ReplicasRecommendation) []v1beta1.ReplicasRecommendation {
converted := make([]v1beta1.ReplicasRecommendation, 0, len(recommendations))
func replicasRecommendationConversionToV1Beta1(recommendations []ReplicasRecommendation) []v1beta2.ReplicasRecommendation {
converted := make([]v1beta2.ReplicasRecommendation, 0, len(recommendations))
for _, recommendation := range recommendations {
converted = append(converted, v1beta1.ReplicasRecommendation{
converted = append(converted, v1beta2.ReplicasRecommendation{
From: recommendation.From,
To: recommendation.To,
WeekDay: recommendation.WeekDay,
Expand All @@ -204,7 +208,7 @@ func replicasRecommendationConversionToV1Beta1(recommendations []ReplicasRecomme
return converted
}

func hPATargetUtilizationRecommendationPerContainerConversionFromV1Beta1(recommendations []v1beta1.HPATargetUtilizationRecommendationPerContainer) []HPATargetUtilizationRecommendationPerContainer {
func hPATargetUtilizationRecommendationPerContainerConversionFromV1Beta1(recommendations []v1beta2.HPATargetUtilizationRecommendationPerContainer) []HPATargetUtilizationRecommendationPerContainer {
converted := make([]HPATargetUtilizationRecommendationPerContainer, 0, len(recommendations))
for _, recommendation := range recommendations {
converted = append(converted, HPATargetUtilizationRecommendationPerContainer{
Expand All @@ -215,18 +219,18 @@ func hPATargetUtilizationRecommendationPerContainerConversionFromV1Beta1(recomme
return converted
}

func hPATargetUtilizationRecommendationPerContainerConversionToV1Beta1(recommendations []HPATargetUtilizationRecommendationPerContainer) []v1beta1.HPATargetUtilizationRecommendationPerContainer {
converted := make([]v1beta1.HPATargetUtilizationRecommendationPerContainer, 0, len(recommendations))
func hPATargetUtilizationRecommendationPerContainerConversionToV1Beta1(recommendations []HPATargetUtilizationRecommendationPerContainer) []v1beta2.HPATargetUtilizationRecommendationPerContainer {
converted := make([]v1beta2.HPATargetUtilizationRecommendationPerContainer, 0, len(recommendations))
for _, recommendation := range recommendations {
converted = append(converted, v1beta1.HPATargetUtilizationRecommendationPerContainer{
converted = append(converted, v1beta2.HPATargetUtilizationRecommendationPerContainer{
ContainerName: recommendation.ContainerName,
TargetUtilization: recommendation.TargetUtilization,
})
}
return converted
}

func containerRecommendationFromVPAConversionFromV1Beta1(conditions []v1beta1.ContainerRecommendationFromVPA) []ContainerRecommendationFromVPA {
func containerRecommendationFromVPAConversionFromV1Beta1(conditions []v1beta2.ContainerRecommendationFromVPA) []ContainerRecommendationFromVPA {
converted := make([]ContainerRecommendationFromVPA, 0, len(conditions))
for _, condition := range conditions {
converted = append(converted, ContainerRecommendationFromVPA{
Expand All @@ -238,10 +242,10 @@ func containerRecommendationFromVPAConversionFromV1Beta1(conditions []v1beta1.Co
return converted
}

func containerRecommendationFromVPAConversionToV1Beta1(conditions []ContainerRecommendationFromVPA) []v1beta1.ContainerRecommendationFromVPA {
converted := make([]v1beta1.ContainerRecommendationFromVPA, 0, len(conditions))
func containerRecommendationFromVPAConversionToV1Beta1(conditions []ContainerRecommendationFromVPA) []v1beta2.ContainerRecommendationFromVPA {
converted := make([]v1beta2.ContainerRecommendationFromVPA, 0, len(conditions))
for _, condition := range conditions {
converted = append(converted, v1beta1.ContainerRecommendationFromVPA{
converted = append(converted, v1beta2.ContainerRecommendationFromVPA{
ContainerName: condition.ContainerName,
MaxRecommendation: resourceQuantityMapConversionToV1Beta1(condition.MaxRecommendation),
Recommendation: resourceQuantityMapConversionToV1Beta1(condition.Recommendation),
Expand All @@ -250,23 +254,23 @@ func containerRecommendationFromVPAConversionToV1Beta1(conditions []ContainerRec
return converted
}

func resourceQuantityMapConversionFromV1Beta1(resources map[v1.ResourceName]v1beta1.ResourceQuantity) map[v1.ResourceName]ResourceQuantity {
func resourceQuantityMapConversionFromV1Beta1(resources map[v1.ResourceName]v1beta2.ResourceQuantity) map[v1.ResourceName]ResourceQuantity {
converted := make(map[v1.ResourceName]ResourceQuantity, len(resources))
for k, v := range resources {
converted[k] = ResourceQuantity(v)
}
return converted
}

func resourceQuantityMapConversionToV1Beta1(resources map[v1.ResourceName]ResourceQuantity) map[v1.ResourceName]v1beta1.ResourceQuantity {
converted := make(map[v1.ResourceName]v1beta1.ResourceQuantity, len(resources))
func resourceQuantityMapConversionToV1Beta1(resources map[v1.ResourceName]ResourceQuantity) map[v1.ResourceName]v1beta2.ResourceQuantity {
converted := make(map[v1.ResourceName]v1beta2.ResourceQuantity, len(resources))
for k, v := range resources {
converted[k] = v1beta1.ResourceQuantity(v)
converted[k] = v1beta2.ResourceQuantity(v)
}
return converted
}

func containerResourcePolicyConversionFromV1Beta1(policies []v1beta1.ContainerResourcePolicy) []ContainerResourcePolicy {
func containerResourcePolicyConversionFromV1Beta1(policies []v1beta2.ContainerResourcePolicy) []ContainerResourcePolicy {
converted := make([]ContainerResourcePolicy, 0, len(policies))
for _, policy := range policies {
converted = append(converted, ContainerResourcePolicy{
Expand All @@ -278,10 +282,10 @@ func containerResourcePolicyConversionFromV1Beta1(policies []v1beta1.ContainerRe
return converted
}

func containerResourcePolicyConversionToV1Beta1(policies []ContainerResourcePolicy) []v1beta1.ContainerResourcePolicy {
converted := make([]v1beta1.ContainerResourcePolicy, 0, len(policies))
func containerResourcePolicyConversionToV1Beta1(policies []ContainerResourcePolicy) []v1beta2.ContainerResourcePolicy {
converted := make([]v1beta2.ContainerResourcePolicy, 0, len(policies))
for _, policy := range policies {
converted = append(converted, v1beta1.ContainerResourcePolicy{
converted = append(converted, v1beta2.ContainerResourcePolicy{
ContainerName: policy.ContainerName,
MinAllocatedResources: policy.MinAllocatedResources,
AutoscalingPolicy: autoscalingPolicyConversionToV1Beta1(policy.AutoscalingPolicy),
Expand All @@ -290,18 +294,18 @@ func containerResourcePolicyConversionToV1Beta1(policies []ContainerResourcePoli
return converted
}

func autoscalingPolicyConversionFromV1Beta1(policies map[v1.ResourceName]v1beta1.AutoscalingType) map[v1.ResourceName]AutoscalingType {
func autoscalingPolicyConversionFromV1Beta1(policies map[v1.ResourceName]v1beta2.AutoscalingType) map[v1.ResourceName]AutoscalingType {
converted := make(map[v1.ResourceName]AutoscalingType, len(policies))
for k, v := range policies {
converted[k] = AutoscalingType(v)
}
return converted
}

func autoscalingPolicyConversionToV1Beta1(policies map[v1.ResourceName]AutoscalingType) map[v1.ResourceName]v1beta1.AutoscalingType {
converted := make(map[v1.ResourceName]v1beta1.AutoscalingType, len(policies))
func autoscalingPolicyConversionToV1Beta1(policies map[v1.ResourceName]AutoscalingType) map[v1.ResourceName]v1beta2.AutoscalingType {
converted := make(map[v1.ResourceName]v1beta2.AutoscalingType, len(policies))
for k, v := range policies {
converted[k] = v1beta1.AutoscalingType(v)
converted[k] = v1beta2.AutoscalingType(v)
}
return converted
}
Loading

0 comments on commit 93bdea8

Please sign in to comment.