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 5, 2023
1 parent a4b309b commit 03c193d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 28 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/envoyproxy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type EnvoyProxyKubernetesProvider struct {
// +optional
// +kubebuilder:validation:XValidation:message="minReplicas must be greater than 0",rule="!has(self.minReplicas) || self.minReplicas > 0"
// +kubebuilder:validation:XValidation:message="maxReplicas must be greater than 0",rule="!has(self.maxReplicas) || self.maxReplicas > 0"
// +kubebuilder:validation:XValidation:message="maxReplicas cannot be less than minReplicas",rule="!has(self.minReplicas) || self.maxReplicas >= self.minReplicas"
// +kubebuilder:validation:XValidation:message="maxReplicas cannot be less than or equal to minReplicas",rule="!has(self.minReplicas) || self.maxReplicas > self.minReplicas"
EnvoyHpa *KubernetesHorizontalPodAutoscalerSpec `json:"envoyHpa,omitempty"`
}

Expand Down
10 changes: 3 additions & 7 deletions api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,29 +278,25 @@ 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
// It cannot be less that minReplicas.
//
// +optional
MaxReplicas *int32 `json:"maxReplicas,omitempty"`
MaxReplicas *int32 `json:"maxReplicas"`

// 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.
//
// +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 @@ -5366,16 +5366,15 @@ spec:
maxReplicas:
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
cannot be less that minReplicas.
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,17 +5901,19 @@ 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
required:
- maxReplicas
type: object
x-kubernetes-validations:
- message: minReplicas must be greater than 0
rule: '!has(self.minReplicas) || self.minReplicas > 0'
- message: maxReplicas must be greater than 0
rule: '!has(self.maxReplicas) || self.maxReplicas > 0'
- message: maxReplicas cannot be less than minReplicas
rule: '!has(self.minReplicas) || self.maxReplicas >= self.minReplicas'
- message: maxReplicas cannot be less than or equal to minReplicas
rule: '!has(self.minReplicas) || self.maxReplicas > self.minReplicas'
envoyService:
description: EnvoyService defines the desired state of the
Envoy service resource. If unspecified, default settings
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 Expand Up @@ -523,7 +522,9 @@ func TestHorizontalPodAutoscaler(t *testing.T) {
{
caseName: "default",
infra: newTestInfra(),
hpa: &egv1a1.KubernetesHorizontalPodAutoscalerSpec{},
hpa: &egv1a1.KubernetesHorizontalPodAutoscalerSpec{
MaxReplicas: ptr.To[int32](1),
},
},
{
caseName: "custom",
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. |
| `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
80 changes: 80 additions & 0 deletions test/cel-validation/envoyproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,86 @@ func TestEnvoyProxyProvider(t *testing.T) {
},
wantErrors: []string{},
},
{
desc: "ProxyHpa-maxReplicas-is-required",
mutate: func(envoy *egv1a1.EnvoyProxy) {
envoy.Spec = egv1a1.EnvoyProxySpec{
Provider: &egv1a1.EnvoyProxyProvider{
Type: egv1a1.ProviderTypeKubernetes,
Kubernetes: &egv1a1.EnvoyProxyKubernetesProvider{
EnvoyHpa: &egv1a1.KubernetesHorizontalPodAutoscalerSpec{},
},
},
}
},
wantErrors: []string{"spec.provider.kubernetes.envoyHpa.maxReplicas: Required value"},
},
{
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),
MaxReplicas: ptr.To[int32](2),
},
},
},
}
},
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 or equal to 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 03c193d

Please sign in to comment.