Skip to content

Commit

Permalink
add test cases for mutating autoscaling policies (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho authored Oct 10, 2023
1 parent 49574ad commit f05a469
Show file tree
Hide file tree
Showing 35 changed files with 1,415 additions and 50 deletions.
7 changes: 7 additions & 0 deletions api/v1beta1/tortoise_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ func (r *Tortoise) ValidateUpdate(old runtime.Object) (admission.Warnings, error
}
}

if hasHorizontal(oldTortoise) && !hasHorizontal(r) && r.Spec.DeletionPolicy == DeletionPolicyNoDelete {
// TODO: add test for this.

// Old has horizontal, but the new one doesn't have any.
return nil, fmt.Errorf("%s: no horizontal policy exists. It will cause the deletion of HPA and you need to specify DeleteAll to allow the deletion.", fieldPath.Child("targetRefs", "resourcePolicy", "autoscalingPolicy"))
}

if reflect.DeepEqual(oldTortoise.Spec.ResourcePolicy, r.Spec.ResourcePolicy) {
return nil, fmt.Errorf("%s: immutable field get changed", fieldPath.Child("resourcePolicy"))
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/tortoise_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func validateTest(tortoise, hpa, deployment string, valid bool) {
}
}

var _ = Describe("MarkdownTortoise Webhook", func() {
var _ = Describe("Tortoise Webhook", func() {
Context("mutating", func() {
It("should mutate a Tortoise", func() {
mutateTest(filepath.Join("testdata", "mutating", "before.yaml"), filepath.Join("testdata", "mutating", "after.yaml"), filepath.Join("testdata", "mutating", "deployment.yaml"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
name: tortoise-hpa-mercari
namespace: default
spec:
behavior:
scaleDown:
policies:
- periodSeconds: 90
type: Percent
value: 2
selectPolicy: Max
scaleUp:
policies:
- periodSeconds: 60
type: Percent
value: 100
selectPolicy: Max
stabilizationWindowSeconds: 0
maxReplicas: 20
metrics:
- containerResource:
container: app
name: cpu
target:
averageUtilization: 50
type: Utilization
type: ContainerResource
- containerResource:
container: istio-proxy
name: cpu
target:
averageUtilization: 75
type: Utilization
type: ContainerResource
- containerResource:
container: istio-proxy
name: memory
target:
averageUtilization: 75
type: Utilization
type: ContainerResource
minReplicas: 5
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: mercari-app
status:
currentMetrics: null
desiredReplicas: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
metadata:
name: mercari
namespace: default
spec:
resourcePolicy:
- autoscalingPolicy:
cpu: Horizontal
memory: Vertical
containerName: app
- autoscalingPolicy:
cpu: Horizontal
memory: Horizontal
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:46Z"
value: 20
minReplicas:
- from: 0
timezone: Local
to: 24
updatedAt: "2023-10-06T01:15:46Z"
value: 5
targetUtilizations:
- containerName: app
targetUtilization:
cpu: 50
- containerName: istio-proxy
targetUtilization:
cpu: 75
memory: 75
vertical:
containerResourceRecommendation:
- RecommendedResource:
cpu: "6"
memory: 3Gi
containerName: app
- RecommendedResource:
cpu: "4"
memory: 4Gi
containerName: istio-proxy
targets:
deployment: ""
horizontalPodAutoscaler: tortoise-hpa-mercari
verticalPodAutoscalers:
- name: tortoise-updater-mercari
role: Updater
- name: tortoise-monitor-mercari
role: Monitor
tortoisePhase: Working
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
name: tortoise-monitor-mercari
namespace: default
spec:
resourcePolicy:
containerPolicies:
- containerName: app
- containerName: istio-proxy
targetRef:
apiVersion: apps/v1
kind: Deployment
name: mercari-app
updatePolicy:
updateMode: "Off"
status:
conditions:
- lastTransitionTime: null
status: "True"
type: RecommendationProvided
recommendation:
containerRecommendations:
- containerName: app
lowerBound:
cpu: "3"
memory: 3Gi
target:
cpu: "3"
memory: 3Gi
upperBound:
cpu: "5"
memory: 5Gi
- containerName: istio-proxy
lowerBound:
cpu: "3"
memory: 3Gi
target:
cpu: "3"
memory: 3Gi
upperBound:
cpu: "5"
memory: 5Gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
metadata:
annotations:
tortoise.autoscaling.mercari.com/managed-by-tortoise: "true"
tortoises.autoscaling.mercari.com/tortoise-name: mercari
name: tortoise-updater-mercari
namespace: default
spec:
recommenders:
- name: tortoise-controller
resourcePolicy:
containerPolicies:
- containerName: app
- containerName: istio-proxy
targetRef:
apiVersion: apps/v1
kind: Deployment
name: mercari-app
updatePolicy:
updateMode: Auto
status:
recommendation:
containerRecommendations:
- containerName: app
lowerBound:
cpu: "6"
memory: 3Gi
target:
cpu: "6"
memory: 3Gi
uncappedTarget:
cpu: "6"
memory: 3Gi
upperBound:
cpu: "6"
memory: 3Gi
- containerName: istio-proxy
lowerBound:
cpu: "4"
memory: 4Gi
target:
cpu: "4"
memory: 4Gi
uncappedTarget:
cpu: "4"
memory: 4Gi
upperBound:
cpu: "4"
memory: 4Gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
metadata:
name: mercari-app
namespace: default
spec:
selector:
matchLabels:
app: mercari
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mercari
spec:
containers:
- image: awesome-mercari-app-image
name: app
resources:
requests:
cpu: "10"
memory: 10Gi
- image: awesome-istio-proxy-image
name: istio-proxy
resources:
requests:
cpu: "4"
memory: 4Gi
status:
replicas: 10
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ spec:
stabilizationWindowSeconds: 0
maxReplicas: 100
metrics:
- resource:
- containerResource:
container: app
name: cpu
target:
averageUtilization: 80
averageUtilization: 50
type: Utilization
type: Resource
type: ContainerResource
- containerResource:
container: istio-proxy
name: cpu
target:
averageUtilization: 50
type: Utilization
type: ContainerResource
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
Expand Down
Loading

0 comments on commit f05a469

Please sign in to comment.