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

Adds EnvoyProxy to Translator and Kube Infra Manager #924

Merged
merged 1 commit into from
Jan 17, 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
59 changes: 58 additions & 1 deletion api/config/v1alpha1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,66 @@ func DefaultProvider() *Provider {
}
}

// GetProvider returns the Provider of EnvoyGateway or a default Provider if unspecified.
func (e *EnvoyGateway) GetProvider() *Provider {
if e.Provider != nil {
return e.Provider
}
return DefaultProvider()
e.Provider = DefaultProvider()

return e.Provider
}

// DefaultResourceProvider returns a new ResourceProvider with default settings.
func DefaultResourceProvider() *ResourceProvider {
return &ResourceProvider{
Type: ProviderTypeKubernetes,
}
}

// GetProvider returns the ResourceProvider of EnvoyProxy or a default ResourceProvider
// if unspecified.
func (e *EnvoyProxy) GetProvider() *ResourceProvider {
if e.Spec.Provider != nil {
return e.Spec.Provider
}
e.Spec.Provider = DefaultResourceProvider()

return e.Spec.Provider
}

// DefaultKubeResourceProvider returns a new KubernetesResourceProvider with default settings.
func DefaultKubeResourceProvider() *KubernetesResourceProvider {
return &KubernetesResourceProvider{
EnvoyDeployment: DefaultKubernetesDeployment(),
}
}

// DefaultKubernetesDeployment returns a new KubernetesDeploymentSpec with default settings.
func DefaultKubernetesDeployment() *KubernetesDeploymentSpec {
repl := int32(DefaultEnvoyReplicas)
return &KubernetesDeploymentSpec{
Replicas: &repl,
}
}

// GetKubeResourceProvider returns the KubernetesResourceProvider of ResourceProvider or
// a default KubernetesResourceProvider if unspecified. If ResourceProvider is not of
// type "Kubernetes", a nil KubernetesResourceProvider is returned.
func (r *ResourceProvider) GetKubeResourceProvider() *KubernetesResourceProvider {
if r.Type != ProviderTypeKubernetes {
return nil
}

if r.Kubernetes == nil {
r.Kubernetes = DefaultKubeResourceProvider()
return r.Kubernetes
}

if r.Kubernetes.EnvoyDeployment == nil {
r.Kubernetes.EnvoyDeployment = DefaultKubernetesDeployment()
return r.Kubernetes
}

return r.Kubernetes
}
5 changes: 5 additions & 0 deletions api/config/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

package v1alpha1

const (
// DefaultEnvoyReplicas is the default number of Envoy replicas.
DefaultEnvoyReplicas = 1
)

// ProviderType defines the types of providers supported by Envoy Gateway.
//
// +kubebuilder:validation:Enum=Kubernetes
Expand Down
3 changes: 3 additions & 0 deletions internal/gatewayapi/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR XdsIRMap
if len(t.ProxyImage) > 0 {
gwInfraIR.Proxy.Image = t.ProxyImage
}
if resources.EnvoyProxy != nil {
gwInfraIR.Proxy.Config = resources.EnvoyProxy
}

// save the IR references in the map before the translation starts
xdsIR[irKey] = gwXdsIR
Expand Down
27 changes: 27 additions & 0 deletions internal/gatewayapi/testdata/envoyproxy-valid.in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
envoyproxy:
apiVersion: config.gateway.envoyproxy.io/v1alpha1
kind: EnvoyProxy
metadata:
namespace: envoy-gateway-system
name: test
spec:
provider:
type: Kubernetes
kubernetes:
envoyDeployment:
replicas: 2
gateways:
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
namespace: envoy-gateway
name: gateway-1
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Same
63 changes: 63 additions & 0 deletions internal/gatewayapi/testdata/envoyproxy-valid.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
gateways:
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
namespace: envoy-gateway
name: gateway-1
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Same
status:
listeners:
- name: http
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
attachedRoutes: 0
conditions:
- type: Programmed
status: "True"
reason: Programmed
message: Listener is ready
xdsIR:
envoy-gateway-gateway-1:
http:
- name: envoy-gateway-gateway-1-http
address: 0.0.0.0
port: 10080
hostnames:
- "*"
infraIR:
envoy-gateway-gateway-1:
proxy:
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
gateway.envoyproxy.io/owning-gateway-name: gateway-1
name: envoy-gateway-gateway-1
config:
apiVersion: config.gateway.envoyproxy.io/v1alpha1
kind: EnvoyProxy
metadata:
namespace: envoy-gateway-system
name: test
spec:
provider:
type: Kubernetes
kubernetes:
envoyDeployment:
replicas: 2
image: envoyproxy/envoy:translator-tests
listeners:
- address: ""
ports:
- name: http
protocol: "HTTP"
servicePort: 80
containerPort: 10080
14 changes: 11 additions & 3 deletions internal/infrastructure/kubernetes/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"

egcfgv1a1 "github.com/envoyproxy/gateway/api/config/v1alpha1"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/gatewayapi"
"github.com/envoyproxy/gateway/internal/ir"
Expand Down Expand Up @@ -117,7 +118,14 @@ func (i *Infra) expectedDeployment(infra *ir.Infra) (*appsv1.Deployment, error)
return nil, fmt.Errorf("missing owning gateway labels")
}

deployment := &appsv1.Deployment{
// Get the EnvoyProxy config to configure the ret.
provider := infra.GetProxyInfra().GetProxyConfig().GetProvider()
if provider.Type != egcfgv1a1.ProviderTypeKubernetes {
return nil, fmt.Errorf("invalid provider type %v for Kubernetes infra manager", provider.Type)
}
deployCfg := provider.GetKubeResourceProvider().EnvoyDeployment

ret := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
Expand All @@ -128,7 +136,7 @@ func (i *Infra) expectedDeployment(infra *ir.Infra) (*appsv1.Deployment, error)
Labels: labels,
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32(1),
Replicas: deployCfg.Replicas,
Selector: envoySelector(infra.GetProxyInfra().GetProxyMetadata().Labels),
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -179,7 +187,7 @@ func (i *Infra) expectedDeployment(infra *ir.Infra) (*appsv1.Deployment, error)
},
}

return deployment, nil
return ret, nil
}

func expectedContainers(infra *ir.Infra) ([]corev1.Container, error) {
Expand Down
10 changes: 10 additions & 0 deletions internal/infrastructure/kubernetes/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ func TestExpectedDeployment(t *testing.T) {
for _, port := range ports {
checkContainerHasPort(t, deploy, port)
}

// Set the deployment replicas.
repl := int32(2)
infra.Proxy.GetProxyConfig().GetProvider().GetKubeResourceProvider().EnvoyDeployment.Replicas = &repl

deploy, err = kube.expectedDeployment(infra)
require.NoError(t, err)

// Check the number of replicas is as expected.
assert.Equal(t, repl, *deploy.Spec.Replicas)
}

func deploymentWithImage(deploy *appsv1.Deployment, image string) *appsv1.Deployment {
Expand Down
9 changes: 9 additions & 0 deletions internal/ir/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ func (p *ProxyInfra) GetProxyMetadata() *InfraMetadata {
return p.Metadata
}

// GetProxyConfig returns the ProxyInfra config.
func (p *ProxyInfra) GetProxyConfig() *v1alpha1.EnvoyProxy {
if p.Config == nil {
p.Config = new(v1alpha1.EnvoyProxy)
}

return p.Config
}

// Validate validates the provided Infra.
func (i *Infra) Validate() error {
if i == nil {
Expand Down