Skip to content

Commit

Permalink
support vertical only tortoise (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho authored Oct 6, 2023
1 parent 84e6410 commit 2f3edc9
Show file tree
Hide file tree
Showing 95 changed files with 520 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample
namespace: default
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: istio-proxy
image: istio-proxy:1.0.0
ports:
- containerPort: 81
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: sample
namespace: default
spec:
maxReplicas: 10
minReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: autoscaling.mercari.com/v1beta1
kind: Tortoise
metadata:
name: tortoise-sample
namespace: default
spec:
updateMode: "Off"
deletionPolicy: "DeleteAll"
targetRefs:
# All vertical, but HPA is specified.
horizontalPodAutoscalerName: sample
scaleTargetRef:
kind: Deployment
name: sample
resourcePolicy:
- containerName: istio-proxy
autoscalingPolicy:
cpu: Vertical
memory: Vertical
- containerName: nginx
autoscalingPolicy:
cpu: Vertical
memory: Vertical
3 changes: 2 additions & 1 deletion api/v1beta1/tortoise_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ type TortoiseStatus struct {
}

type TargetsStatus struct {
HorizontalPodAutoscaler string `json:"horizontalPodAutoscaler" protobuf:"bytes,1,name=horizontalPodAutoscaler"`
// +optional
HorizontalPodAutoscaler string `json:"horizontalPodAutoscaler" protobuf:"bytes,1,opt,name=horizontalPodAutoscaler"`
Deployment string `json:"deployment" protobuf:"bytes,2,name=deployment"`
VerticalPodAutoscalers []TargetStatusVerticalPodAutoscaler `json:"verticalPodAutoscalers" protobuf:"bytes,3,name=verticalPodAutoscalers"`
}
Expand Down
25 changes: 16 additions & 9 deletions api/v1beta1/tortoise_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ func (r *Tortoise) Default() {

var _ webhook.Validator = &Tortoise{}

func hasHorizontal(tortoise *Tortoise) bool {
for _, r := range tortoise.Spec.ResourcePolicy {
for _, p := range r.AutoscalingPolicy {
if p == AutoscalingTypeHorizontal {
return true
}
}
}
return false
}

func validateTortoise(t *Tortoise) error {
fieldPath := field.NewPath("spec")

Expand All @@ -129,20 +140,16 @@ func validateTortoise(t *Tortoise) error {
return fmt.Errorf("%s: shouldn't be empty", fieldPath.Child("targetRefs", "scaleTargetRef", "kind"))
}

for _, p := range t.Spec.ResourcePolicy {
for _, ap := range p.AutoscalingPolicy {
if ap == AutoscalingTypeHorizontal {
return nil
}
}
}

if t.Spec.UpdateMode == UpdateModeEmergency &&
t.Status.TortoisePhase != TortoisePhaseWorking && t.Status.TortoisePhase != TortoisePhaseEmergency && t.Status.TortoisePhase != TortoisePhaseBackToNormal {
return fmt.Errorf("%s: emergency mode is only available for tortoises with Running phase", fieldPath.Child("updateMode"))
}

return fmt.Errorf("%s: at least one policy should be Horizontal", fieldPath.Child("resourcePolicy", "autoscalingPolicy"))
if !hasHorizontal(t) && t.Spec.TargetRefs.HorizontalPodAutoscalerName != nil {
return fmt.Errorf("%s: at least one policy should be Horizontal when HorizontalPodAutoscalerName isn't nil", fieldPath.Child("resourcePolicy", "autoscalingPolicy"))
}

return nil
}

type resourceNameAndContainerName struct {
Expand Down
3 changes: 3 additions & 0 deletions api/v1beta1/tortoise_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,8 @@ var _ = Describe("MarkdownTortoise Webhook", func() {
It("invalid: Tortoise has resource policy for non-existing container", func() {
validateTest(filepath.Join("testdata", "validating", "useless-policy", "tortoise.yaml"), filepath.Join("testdata", "validating", "useless-policy", "hpa.yaml"), filepath.Join("testdata", "validating", "useless-policy", "deployment.yaml"), false)
})
It("invalid: Tortoise has HPA specified, but no Horizoltal in autoscalingPolicy", func() {
validateTest(filepath.Join("testdata", "validating", "hpa-specified-but-no-horizontal", "tortoise.yaml"), filepath.Join("testdata", "validating", "hpa-specified-but-no-horizontal", "hpa.yaml"), filepath.Join("testdata", "validating", "hpa-specified-but-no-horizontal", "deployment.yaml"), false)
})
})
})
1 change: 0 additions & 1 deletion config/crd/bases/autoscaling.mercari.com_tortoises.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,6 @@ spec:
type: array
required:
- deployment
- horizontalPodAutoscaler
- verticalPodAutoscalers
type: object
tortoisePhase:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
metadata:
creationTimestamp: null
name: mercari-app
namespace: default
spec:
Expand Down
1 change: 0 additions & 1 deletion controllers/testdata/deletion-no-delete/before/hpa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-hpa-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
metadata:
creationTimestamp: null
name: mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-monitor-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-updater-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
metadata:
creationTimestamp: null
name: mercari-app
namespace: default
spec:
Expand Down
1 change: 0 additions & 1 deletion controllers/testdata/deletion-policy-all/before/hpa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-hpa-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
metadata:
creationTimestamp: null
name: mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-monitor-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-updater-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-hpa-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
metadata:
creationTimestamp: null
name: mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-monitor-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-updater-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
metadata:
creationTimestamp: null
name: mercari-app
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-hpa-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
metadata:
creationTimestamp: null
name: mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-monitor-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
creationTimestamp: null
name: tortoise-updater-mercari
namespace: default
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
metadata:
name: mercari
namespace: default
spec:
resourcePolicy:
- autoscalingPolicy:
cpu: "Vertical"
memory: "Vertical"
containerName: app
- autoscalingPolicy:
cpu: "Vertical"
memory: "Vertical"
containerName: istio-proxy
targetRefs:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: mercari-app
status:
conditions:
containerRecommendationFromVPA:
- containerName: app
maxRecommendation:
cpu:
quantity: "3"
updatedAt: null
memory:
quantity: 3Gi
updatedAt: null
recommendation:
cpu:
quantity: "3"
updatedAt: null
memory:
quantity: 3Gi
updatedAt: null
- containerName: istio-proxy
maxRecommendation:
cpu:
quantity: "3"
updatedAt: null
memory:
quantity: 3Gi
updatedAt: null
recommendation:
cpu:
quantity: "3"
updatedAt: null
memory:
quantity: 3Gi
updatedAt: null
tortoiseConditions: null
recommendations:
horizontal:
maxReplicas:
- from: 0
timezone: Local
to: 24
updatedAt: "2023-10-06T01:15:47Z"
value: 20
minReplicas:
- from: 0
timezone: Local
to: 24
updatedAt: "2023-10-06T01:15:47Z"
value: 5
targetUtilizations:
- containerName: app
targetUtilization: {}
- containerName: istio-proxy
targetUtilization: {}
vertical:
containerResourceRecommendation:
- RecommendedResource:
cpu: "3"
memory: 3Gi
containerName: app
- RecommendedResource:
cpu: "3"
memory: 3Gi
containerName: istio-proxy
targets:
deployment: ""
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Loading

0 comments on commit 2f3edc9

Please sign in to comment.