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

feat: add HorizontalPodAutoscaler support for EnvoyProxy API #2257

Merged
merged 5 commits into from
Dec 6, 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
24 changes: 24 additions & 0 deletions api/v1alpha1/envoyproxy_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"fmt"
"sort"
"strings"

autoscalingv2 "k8s.io/api/autoscaling/v2"
v1 "k8s.io/api/core/v1"

"github.com/envoyproxy/gateway/internal/utils/ptr"
)

// DefaultEnvoyProxyProvider returns a new EnvoyProxyProvider with default settings.
Expand Down Expand Up @@ -37,6 +42,21 @@ func DefaultEnvoyProxyKubeProvider() *EnvoyProxyKubernetesProvider {
}
}

func DefaultEnvoyProxyHpaMetrics() []autoscalingv2.MetricSpec {
return []autoscalingv2.MetricSpec{
{
Resource: &autoscalingv2.ResourceMetricSource{
Name: v1.ResourceCPU,
Target: autoscalingv2.MetricTarget{
Type: autoscalingv2.UtilizationMetricType,
AverageUtilization: ptr.To[int32](80),
},
},
Type: autoscalingv2.ResourceMetricSourceType,
},
}
}

// GetEnvoyProxyKubeProvider returns the EnvoyProxyKubernetesProvider of EnvoyProxyProvider or
// a default EnvoyProxyKubernetesProvider if unspecified. If EnvoyProxyProvider is not of
// type "Kubernetes", a nil EnvoyProxyKubernetesProvider is returned.
Expand Down Expand Up @@ -64,6 +84,10 @@ func (r *EnvoyProxyProvider) GetEnvoyProxyKubeProvider() *EnvoyProxyKubernetesPr
r.Kubernetes.EnvoyService.Type = GetKubernetesServiceType(ServiceTypeLoadBalancer)
}

if r.Kubernetes.EnvoyHpa != nil {
r.Kubernetes.EnvoyHpa.setDefault()
}

return r.Kubernetes
}

Expand Down
9 changes: 9 additions & 0 deletions api/v1alpha1/envoyproxy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ type EnvoyProxyKubernetesProvider struct {
// +kubebuilder:validation:XValidation:message="loadBalancerIP can only be set for LoadBalancer type",rule="!has(self.loadBalancerIP) || self.type == 'LoadBalancer'"
// +kubebuilder:validation:XValidation:message="loadBalancerIP must be a valid IPv4 address",rule="!has(self.loadBalancerIP) || self.loadBalancerIP.matches(r\"^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4})\")"
EnvoyService *KubernetesServiceSpec `json:"envoyService,omitempty"`

// EnvoyHpa defines the Horizontal Pod Autoscaler settings for Envoy Proxy Deployment.
// Once the HPA is being set, Replicas field from EnvoyDeployment will be ignored.
//
// +optional
// +kubebuilder:validation:XValidation:message="minReplicas must be greater than 0",rule="!has(self.minReplicas) || self.minReplicas > 0"
ardikabs marked this conversation as resolved.
Show resolved Hide resolved
// +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 or equal to minReplicas",rule="!has(self.minReplicas) || self.maxReplicas > self.minReplicas"
EnvoyHpa *KubernetesHorizontalPodAutoscalerSpec `json:"envoyHpa,omitempty"`
}

// ProxyLogging defines logging parameters for managed proxies.
Expand Down
6 changes: 6 additions & 0 deletions api/v1alpha1/kubernetes_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ func (deployment *KubernetesDeploymentSpec) defaultKubernetesDeploymentSpec(imag
deployment.Container.Image = DefaultKubernetesContainerImage(image)
}
}

func (hpa *KubernetesHorizontalPodAutoscalerSpec) setDefault() {
if len(hpa.Metrics) == 0 {
hpa.Metrics = DefaultEnvoyProxyHpaMetrics()
}
}
32 changes: 32 additions & 0 deletions api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package v1alpha1

import (
appv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -275,3 +276,34 @@ const (
// https://github.com/google/re2/wiki/Syntax.
StringMatchRegularExpression StringMatchType = "RegularExpression"
)

// KubernetesHorizontalPodAutoscalerSpec defines Kubernetes Horizontal Pod Autoscaler settings of Envoy Proxy Deployment
// See k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec
type KubernetesHorizontalPodAutoscalerSpec struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this's the right direction, at last, you need borrow everything from k8s.io.autoscaling.v2.HorizontalPodAutoScalerSpec.

Copy link
Contributor Author

@ardikabs ardikabs Dec 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i borrow everything from HPASpec https://github.com/kubernetes/api/blob/6946e304d7b686796dc3f3bfaad56928484abf40/autoscaling/v2/types.go#L51, only exclude scaleTargetRef, because it will be automatically refers to the EnvoyProxy Deployment resource, it means i am trying to not let user specify it, because the deployment name is generated by EG. thought @zirain ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we still provide scaleTargetRef, then technically user could specify unrelated resource than Envoy Proxy Deployment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's something I didn't wanted/expected, so I'm -1 on this.
I'd like to let other maintainers make the dicession.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current approach looks well, any better ideas, IMO HPA fields are hard to abstract.

// minReplicas is the lower limit for the number of replicas to which the autoscaler
// can scale down. It defaults to 1 replica.
//
// +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.
//
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
Metrics []autoscalingv2.MetricSpec `json:"metrics,omitempty"`

// 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.
//
// +optional
Behavior *autoscalingv2.HorizontalPodAutoscalerBehavior `json:"behavior,omitempty"`
}
43 changes: 43 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading