Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the test cases for updating validation #161

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading