Skip to content

Commit

Permalink
add the test cases for updating validation (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho authored Oct 10, 2023
1 parent f05a469 commit 9ab5b47
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: autoscaling.mercari.com/v1beta1
kind: Tortoise
metadata:
name: tortoise-sample
namespace: default
spec:
updateMode: "Off"
deletionPolicy: "DeleteAll"
targetRefs:
horizontalPodAutoscalerName: "sample"
scaleTargetRef:
kind: Deployment
name: sample
resourcePolicy:
- containerName: istio-proxy
autoscalingPolicy:
cpu: Horizontal
memory: Vertical
- containerName: nginx
autoscalingPolicy:
cpu: Horizontal
memory: Vertical
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
27 changes: 27 additions & 0 deletions api/v1beta1/testdata/validating/no-horizontal-with-hpa/hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: sample
namespace: default
spec:
maxReplicas: 10
metrics:
- type: ContainerResource
containerResource:
name: cpu
container: nginx
target:
type: Utilization
averageUtilization: 60
- type: ContainerResource
containerResource:
name: cpu
container: istio-proxy
target:
type: Utilization
averageUtilization: 60
minReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: autoscaling.mercari.com/v1beta1
kind: Tortoise
metadata:
name: tortoise-sample
namespace: default
spec:
updateMode: "Off"
deletionPolicy: "DeleteAll"
targetRefs:
horizontalPodAutoscalerName: "sample"
scaleTargetRef:
kind: Deployment
name: sample
resourcePolicy:
- containerName: istio-proxy
autoscalingPolicy:
cpu: Vertical
memory: Vertical
- containerName: nginx
autoscalingPolicy:
cpu: Vertical
memory: Vertical
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: autoscaling.mercari.com/v1beta1
kind: Tortoise
metadata:
name: tortoise-sample
namespace: default
spec:
updateMode: "Off"
deletionPolicy: "NoDelete"
targetRefs:
scaleTargetRef:
kind: Deployment
name: sample
resourcePolicy:
- containerName: istio-proxy
autoscalingPolicy:
cpu: Horizontal
memory: Vertical
- containerName: nginx
autoscalingPolicy:
cpu: Horizontal
memory: Vertical
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,27 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: sample
namespace: default
spec:
maxReplicas: 10
metrics:
- type: ContainerResource
containerResource:
name: cpu
container: nginx
target:
type: Utilization
averageUtilization: 60
- type: ContainerResource
containerResource:
name: cpu
container: istio-proxy
target:
type: Utilization
averageUtilization: 60
minReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: autoscaling.mercari.com/v1beta1
kind: Tortoise
metadata:
name: tortoise-sample
namespace: default
spec:
updateMode: "Off"
deletionPolicy: "NoDelete"
targetRefs:
scaleTargetRef:
kind: Deployment
name: sample
resourcePolicy:
- containerName: istio-proxy
autoscalingPolicy:
cpu: Vertical
memory: Vertical
- containerName: nginx
autoscalingPolicy:
cpu: Vertical
memory: Vertical
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: autoscaling.mercari.com/v1beta1
kind: Tortoise
metadata:
name: tortoise-sample
namespace: default
spec:
updateMode: "Off"
deletionPolicy: "NoDelete"
targetRefs:
horizontalPodAutoscalerName: "sample"
scaleTargetRef:
kind: Deployment
name: sample
resourcePolicy:
- containerName: istio-proxy
autoscalingPolicy:
cpu: Horizontal
memory: Vertical
- containerName: nginx
autoscalingPolicy:
cpu: Horizontal
memory: Vertical
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,27 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: sample
namespace: default
spec:
maxReplicas: 10
metrics:
- type: ContainerResource
containerResource:
name: cpu
container: nginx
target:
type: Utilization
averageUtilization: 60
- type: ContainerResource
containerResource:
name: cpu
container: istio-proxy
target:
type: Utilization
averageUtilization: 60
minReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: autoscaling.mercari.com/v1beta1
kind: Tortoise
metadata:
name: tortoise-sample
namespace: default
spec:
updateMode: "Off"
deletionPolicy: "DeleteAll"
targetRefs:
scaleTargetRef:
kind: Deployment
name: sample
resourcePolicy:
- containerName: istio-proxy
autoscalingPolicy:
cpu: Vertical
memory: Vertical
- containerName: nginx
autoscalingPolicy:
cpu: Vertical
memory: Vertical
18 changes: 8 additions & 10 deletions api/v1beta1/tortoise_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,17 @@ func (r *Tortoise) ValidateUpdate(old runtime.Object) (admission.Warnings, error
// newly specified or updated.
return nil, fmt.Errorf("%s: immutable field get changed", fieldPath.Child("targetRefs", "horizontalPodAutoscalerName"))
}
} else {
if oldTortoise.Spec.TargetRefs.HorizontalPodAutoscalerName != nil {
// Old has HorizontalPodAutoscalerName, but the new one doesn't.
// removed.
return nil, fmt.Errorf("%s: immutable field get changed", fieldPath.Child("targetRefs", "horizontalPodAutoscalerName"))
}
}

if hasHorizontal(oldTortoise) && !hasHorizontal(r) && r.Spec.DeletionPolicy == DeletionPolicyNoDelete {
// TODO: add test for this.
if hasHorizontal(oldTortoise) && !hasHorizontal(r) {
if r.Spec.DeletionPolicy == DeletionPolicyNoDelete {
// The old one 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"))
}

// 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 r.Spec.TargetRefs.HorizontalPodAutoscalerName != nil {
return nil, fmt.Errorf("%s: no horizontal policy exists. It will cause the deletion of HPA and you need to remove horizontalPodAutoscalerName to allow the deletion.", fieldPath.Child("targetRefs", "horizontalPodAutoscalerName"))
}
}

if reflect.DeepEqual(oldTortoise.Spec.ResourcePolicy, r.Spec.ResourcePolicy) {
Expand Down
Loading

0 comments on commit 9ab5b47

Please sign in to comment.