Skip to content

Commit

Permalink
chore: add celvalidation for envoyHpa
Browse files Browse the repository at this point in the history
Signed-off-by: Ardika Bagus <[email protected]>
  • Loading branch information
ardikabs committed Dec 2, 2023
1 parent 2ec05a7 commit d92295a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
5 changes: 1 addition & 4 deletions api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,16 @@ const (
)

// KubernetesHorizontalPodAutoscalerSpec defines Kubernetes Horizontal Pod Autoscaler settings of Envoy Proxy Deployment
// See k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec
type KubernetesHorizontalPodAutoscalerSpec struct {
// minReplicas is the lower limit for the number of replicas to which the autoscaler
// can scale down. It defaults to 1 replica.
// See k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec
//
// +optional
MinReplicas *int32 `json:"minReplicas,omitempty"`

// maxReplicas is the upper limit for the number of replicas to which the autoscaler can scale up.
// It cannot be less that minReplicas. It defaults to 1 replica.
// See k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec
//
// +optional
MaxReplicas *int32 `json:"maxReplicas,omitempty"`
Expand All @@ -299,8 +298,6 @@ type KubernetesHorizontalPodAutoscalerSpec struct {
// If left empty, it defaults to being based on CPU utilization with average on 80% usage.
//
// +optional
//
// See k8s.io.autoscaling.v2.HorizontalPodAutoScalerBehavior.
Metrics []autoscalingv2.MetricSpec `json:"metrics,omitempty"`

// behavior configures the scaling behavior of the target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5367,15 +5367,14 @@ spec:
description: maxReplicas is the upper limit for the number
of replicas to which the autoscaler can scale up. It
cannot be less that minReplicas. It defaults to 1 replica.
See k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec
format: int32
type: integer
metrics:
description: "metrics contains the specifications for
which to use to calculate the desired replica count
(the maximum replica count across all metrics will be
used). If left empty, it defaults to being based on
CPU utilization with average on 80% usage. \n See k8s.io.autoscaling.v2.HorizontalPodAutoScalerBehavior."
description: metrics contains the specifications for which
to use to calculate the desired replica count (the maximum
replica count across all metrics will be used). If left
empty, it defaults to being based on CPU utilization
with average on 80% usage.
items:
description: MetricSpec specifies how to scale based
on a single metric (only `type` and one other matching
Expand Down Expand Up @@ -5902,7 +5901,7 @@ spec:
minReplicas:
description: minReplicas is the lower limit for the number
of replicas to which the autoscaler can scale down.
It defaults to 1 replica. See k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec
It defaults to 1 replica.
format: int32
type: integer
type: object
Expand Down
5 changes: 2 additions & 3 deletions internal/infrastructure/kubernetes/infra_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
"context"
"reflect"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/resource"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"

"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/pointer"
"sigs.k8s.io/yaml"
Expand Down
9 changes: 4 additions & 5 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -981,17 +981,16 @@ _Appears in:_



KubernetesHorizontalPodAutoscalerSpec defines Kubernetes Horizontal Pod Autoscaler settings of Envoy Proxy Deployment
KubernetesHorizontalPodAutoscalerSpec defines Kubernetes Horizontal Pod Autoscaler settings of Envoy Proxy Deployment See k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec

_Appears in:_
- [EnvoyProxyKubernetesProvider](#envoyproxykubernetesprovider)

| Field | Description |
| --- | --- |
| `minReplicas` _integer_ | minReplicas is the lower limit for the number of replicas to which the autoscaler can scale down. It defaults to 1 replica. See k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec |
| `maxReplicas` _integer_ | maxReplicas is the upper limit for the number of replicas to which the autoscaler can scale up. It cannot be less that minReplicas. It defaults to 1 replica. See k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec |
| `metrics` _[MetricSpec](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#metricspec-v2-autoscaling) array_ | metrics contains the specifications for which to use to calculate the desired replica count (the maximum replica count across all metrics will be used). If left empty, it defaults to being based on CPU utilization with average on 80% usage.
See k8s.io.autoscaling.v2.HorizontalPodAutoScalerBehavior. |
| `minReplicas` _integer_ | minReplicas is the lower limit for the number of replicas to which the autoscaler can scale down. It defaults to 1 replica. |
| `maxReplicas` _integer_ | maxReplicas is the upper limit for the number of replicas to which the autoscaler can scale up. It cannot be less that minReplicas. It defaults to 1 replica. |
| `metrics` _[MetricSpec](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#metricspec-v2-autoscaling) array_ | metrics contains the specifications for which to use to calculate the desired replica count (the maximum replica count across all metrics will be used). If left empty, it defaults to being based on CPU utilization with average on 80% usage. |
| `behavior` _[HorizontalPodAutoscalerBehavior](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#horizontalpodautoscalerbehavior-v2-autoscaling)_ | behavior configures the scaling behavior of the target in both Up and Down directions (scaleUp and scaleDown fields respectively). If not set, the default HPAScalingRules for scale up and scale down are used. See k8s.io.autoscaling.v2.HorizontalPodAutoScalerBehavior. |


Expand Down
65 changes: 65 additions & 0 deletions test/cel-validation/envoyproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,71 @@ func TestEnvoyProxyProvider(t *testing.T) {
},
wantErrors: []string{},
},
{
desc: "ProxyHpa-minReplicas-less-than-0",
mutate: func(envoy *egv1a1.EnvoyProxy) {
envoy.Spec = egv1a1.EnvoyProxySpec{
Provider: &egv1a1.EnvoyProxyProvider{
Type: egv1a1.ProviderTypeKubernetes,
Kubernetes: &egv1a1.EnvoyProxyKubernetesProvider{
EnvoyHpa: &egv1a1.KubernetesHorizontalPodAutoscalerSpec{
MinReplicas: ptr.To[int32](-1),
},
},
},
}
},
wantErrors: []string{"minReplicas must be greater than 0"},
},
{
desc: "ProxyHpa-maxReplicas-less-than-0",
mutate: func(envoy *egv1a1.EnvoyProxy) {
envoy.Spec = egv1a1.EnvoyProxySpec{
Provider: &egv1a1.EnvoyProxyProvider{
Type: egv1a1.ProviderTypeKubernetes,
Kubernetes: &egv1a1.EnvoyProxyKubernetesProvider{
EnvoyHpa: &egv1a1.KubernetesHorizontalPodAutoscalerSpec{
MaxReplicas: ptr.To[int32](-1),
},
},
},
}
},
wantErrors: []string{"maxReplicas must be greater than 0"},
},
{
desc: "ProxyHpa-maxReplicas-less-than-minReplicas",
mutate: func(envoy *egv1a1.EnvoyProxy) {
envoy.Spec = egv1a1.EnvoyProxySpec{
Provider: &egv1a1.EnvoyProxyProvider{
Type: egv1a1.ProviderTypeKubernetes,
Kubernetes: &egv1a1.EnvoyProxyKubernetesProvider{
EnvoyHpa: &egv1a1.KubernetesHorizontalPodAutoscalerSpec{
MinReplicas: ptr.To[int32](5),
MaxReplicas: ptr.To[int32](2),
},
},
},
}
},
wantErrors: []string{"maxReplicas cannot be less than minReplicas"},
},
{
desc: "ProxyHpa-valid",
mutate: func(envoy *egv1a1.EnvoyProxy) {
envoy.Spec = egv1a1.EnvoyProxySpec{
Provider: &egv1a1.EnvoyProxyProvider{
Type: egv1a1.ProviderTypeKubernetes,
Kubernetes: &egv1a1.EnvoyProxyKubernetesProvider{
EnvoyHpa: &egv1a1.KubernetesHorizontalPodAutoscalerSpec{
MinReplicas: ptr.To[int32](5),
MaxReplicas: ptr.To[int32](10),
},
},
},
}
},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit d92295a

Please sign in to comment.