Skip to content

Commit

Permalink
merge gateways support in translator
Browse files Browse the repository at this point in the history
Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com>
  • Loading branch information
cnvergence committed Oct 9, 2023
1 parent a785be8 commit 152eae3
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 23 deletions.
7 changes: 6 additions & 1 deletion internal/gatewayapi/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ type AddressesTranslator interface {
func (t *Translator) ProcessAddresses(gateways []*GatewayContext, xdsIR XdsIRMap, infraIR InfraIRMap, resources *Resources) {
for _, gateway := range gateways {
// Infra IR already exist
irKey := irStringKey(gateway.Gateway.Namespace, gateway.Gateway.Name)
var irKey string
if resources.EnvoyProxy != nil && resources.EnvoyProxy.Spec.MergeGateways != nil && *resources.EnvoyProxy.Spec.MergeGateways {
irKey = string(t.GatewayClassName)
} else {
irKey = irStringKey(gateway.Gateway.Namespace, gateway.Gateway.Name)
}
gwInfraIR := infraIR[irKey]

var ipAddr []string
Expand Down
6 changes: 6 additions & 0 deletions internal/gatewayapi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ func GatewayOwnerLabels(namespace, name string) map[string]string {
}
}

// GatewayClassOwnerLabel returns the GatewayCLass Owner label using
// the provided name as the value.
func GatewayClassOwnerLabel(name string) map[string]string {
return map[string]string{OwningGatewayClassLabel: name}
}

// servicePortToContainerPort translates a service port into an ephemeral
// container port.
func servicePortToContainerPort(servicePort int32) int32 {
Expand Down
35 changes: 16 additions & 19 deletions internal/gatewayapi/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,23 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR XdsIRMap
// and compute status for each, and add valid ones
// to the Xds IR.
for _, gateway := range gateways {
// init IR per gateway
irKey := irStringKey(gateway.Gateway.Namespace, gateway.Gateway.Name)
gwXdsIR := &ir.Xds{}
gwInfraIR := ir.NewInfra()
gwInfraIR.Proxy.Name = irKey
gwInfraIR.Proxy.GetProxyMetadata().Labels = GatewayOwnerLabels(gateway.Namespace, gateway.Name)
if resources.EnvoyProxy != nil {
gwInfraIR.Proxy.Config = resources.EnvoyProxy
}

// save the IR references in the map before the translation starts
xdsIR[irKey] = gwXdsIR
infraIR[irKey] = gwInfraIR

// Infra IR proxy ports must be unique.
var foundPorts []*protocolPort
var irKey string

if resources.EnvoyProxy != nil && resources.EnvoyProxy.Spec.MergeGateways != nil && *resources.EnvoyProxy.Spec.MergeGateways {
irKey = string(t.GatewayClassName)
} else {
irKey = irStringKey(gateway.Gateway.Namespace, gateway.Gateway.Name)
}

if resources.EnvoyProxy != nil {
infraIR[irKey].Proxy.Config = resources.EnvoyProxy
}

gwXdsIR.AccessLog = processAccessLog(gwInfraIR.Proxy.Config)
gwXdsIR.Tracing = processTracing(gateway.Gateway, gwInfraIR.Proxy.Config)
gwXdsIR.Metrics = processMetrics(gwInfraIR.Proxy.Config)
xdsIR[irKey].AccessLog = processAccessLog(infraIR[irKey].Proxy.Config)
xdsIR[irKey].Tracing = processTracing(gateway.Gateway, infraIR[irKey].Proxy.Config)
xdsIR[irKey].Metrics = processMetrics(infraIR[irKey].Proxy.Config)

for _, listener := range gateway.listeners {
// Process protocol & supported kinds
Expand Down Expand Up @@ -119,7 +116,7 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR XdsIRMap
// see more https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io/v1beta1.Listener.
irListener.Hostnames = append(irListener.Hostnames, "*")
}
gwXdsIR.HTTP = append(gwXdsIR.HTTP, irListener)
xdsIR[irKey].HTTP = append(xdsIR[irKey].HTTP, irListener)
}

// Add the listener to the Infra IR. Infra IR ports must have a unique port number per layer-4 protocol
Expand All @@ -146,7 +143,7 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR XdsIRMap
ContainerPort: containerPort,
}
// Only 1 listener is supported.
gwInfraIR.Proxy.Listeners[0].Ports = append(gwInfraIR.Proxy.Listeners[0].Ports, infraPort)
infraIR[irKey].Proxy.Listeners[0].Ports = append(infraIR[irKey].Proxy.Listeners[0].Ports, infraPort)
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions internal/gatewayapi/testdata/merge-multiple-gateways.in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
envoyproxy:
apiVersion: config.gateway.envoyproxy.io/v1alpha1
kind: EnvoyProxy
metadata:
namespace: envoy-gateway-system
name: test
spec:
mergeGateways: true
gateways:
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
name: gateway-1
namespace: envoy-gateway
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http

Check failure on line 18 in internal/gatewayapi/testdata/merge-multiple-gateways.in.yaml

View workflow job for this annotation

GitHub Actions / lint

18:7 [indentation] wrong indentation: expected 8 but found 6
port: 80
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
name: gateway-2
namespace: envoy-gateway
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http-2

Check failure on line 32 in internal/gatewayapi/testdata/merge-multiple-gateways.in.yaml

View workflow job for this annotation

GitHub Actions / lint

32:7 [indentation] wrong indentation: expected 8 but found 6
port: 8888
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
117 changes: 117 additions & 0 deletions internal/gatewayapi/testdata/merge-multiple-gateways.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
gateways:
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
creationTimestamp: null
name: gateway-1
namespace: envoy-gateway
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
port: 80
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
status:
listeners:
- attachedRoutes: 0
name: http
conditions:
- lastTransitionTime: null
message: Sending translated listener configuration to the data plane
reason: Programmed
status: "True"
type: Programmed
- lastTransitionTime: null
message: Listener has been successfully translated
reason: Accepted
status: "True"
type: Accepted
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
- group: gateway.networking.k8s.io
kind: GRPCRoute
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
creationTimestamp: null
name: gateway-2
namespace: envoy-gateway
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http-2
port: 8888
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
status:
listeners:
- attachedRoutes: 0
name: http-2
conditions:
- lastTransitionTime: null
message: Sending translated listener configuration to the data plane
reason: Programmed
status: "True"
type: Programmed
- lastTransitionTime: null
message: Listener has been successfully translated
reason: Accepted
status: "True"
type: Accepted
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
- group: gateway.networking.k8s.io
kind: GRPCRoute
infraIR:
envoy-gateway-class:
proxy:
config:
apiVersion: config.gateway.envoyproxy.io/v1alpha1
kind: EnvoyProxy
metadata:
creationTimestamp: null
name: test
namespace: envoy-gateway-system
spec:
mergeGateways: true
listeners:
- address: ""
ports:
- containerPort: 10080
name: http
protocol: HTTP
servicePort: 80
- containerPort: 8888
name: http-2
protocol: HTTP
servicePort: 8888
metadata:
labels:
gateway.envoyproxy.io/owning-gatewayclass: envoy-gateway-class
name: envoy-gateway-class
xdsIR:
envoy-gateway-class:
accessLog:
text:
- path: /dev/stdout
http:
- address: 0.0.0.0
hostnames:
- '*'
isHTTP2: false
name: envoy-gateway/gateway-1/http
port: 10080
- address: 0.0.0.0
hostnames:
- '*'
isHTTP2: false
name: envoy-gateway/gateway-2/http-2
port: 8888

29 changes: 29 additions & 0 deletions internal/gatewayapi/testdata/multiple-gateways.in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
gateways:
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
name: gateway-1
namespace: envoy-gateway
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http

Check failure on line 10 in internal/gatewayapi/testdata/multiple-gateways.in.yaml

View workflow job for this annotation

GitHub Actions / lint

10:7 [indentation] wrong indentation: expected 8 but found 6
port: 80
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
name: gateway-2
namespace: envoy-gateway
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http-2

Check failure on line 24 in internal/gatewayapi/testdata/multiple-gateways.in.yaml

View workflow job for this annotation

GitHub Actions / lint

24:7 [indentation] wrong indentation: expected 8 but found 6
port: 8888
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
123 changes: 123 additions & 0 deletions internal/gatewayapi/testdata/multiple-gateways.out.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
gateways:
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
creationTimestamp: null
name: gateway-1
namespace: envoy-gateway
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
port: 80
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
status:
listeners:
- attachedRoutes: 0
name: http
conditions:
- lastTransitionTime: null
message: Sending translated listener configuration to the data plane
reason: Programmed
status: "True"
type: Programmed
- lastTransitionTime: null
message: Listener has been successfully translated
reason: Accepted
status: "True"
type: Accepted
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
- group: gateway.networking.k8s.io
kind: GRPCRoute
- apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
creationTimestamp: null
name: gateway-2
namespace: envoy-gateway
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http-2
port: 8888
protocol: HTTP
allowedRoutes:
namespaces:
from: Same

Check failure on line 51 in internal/gatewayapi/testdata/multiple-gateways.out.yaml

View workflow job for this annotation

GitHub Actions / lint

51:21 [trailing-spaces] trailing spaces
status:
listeners:
- attachedRoutes: 0
name: http-2
conditions:
- lastTransitionTime: null
message: Sending translated listener configuration to the data plane
reason: Programmed
status: "True"
type: Programmed
- lastTransitionTime: null
message: Listener has been successfully translated
reason: Accepted
status: "True"
type: Accepted

Check failure on line 66 in internal/gatewayapi/testdata/multiple-gateways.out.yaml

View workflow job for this annotation

GitHub Actions / lint

66:23 [trailing-spaces] trailing spaces
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
- group: gateway.networking.k8s.io
kind: GRPCRoute

Check failure on line 71 in internal/gatewayapi/testdata/multiple-gateways.out.yaml

View workflow job for this annotation

GitHub Actions / lint

71:24 [trailing-spaces] trailing spaces
infraIR:
envoy-gateway/gateway-1:
proxy:
listeners:
- address: ""
ports:
- containerPort: 10080
name: http
protocol: HTTP
servicePort: 80
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-name: gateway-1
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
name: envoy-gateway/gateway-1
envoy-gateway/gateway-2:
proxy:
listeners:
- address: ""
ports:
- containerPort: 8888
name: http-2
protocol: HTTP
servicePort: 8888
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-name: gateway-2
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
name: envoy-gateway/gateway-2

Check failure on line 100 in internal/gatewayapi/testdata/multiple-gateways.out.yaml

View workflow job for this annotation

GitHub Actions / lint

100:36 [trailing-spaces] trailing spaces
xdsIR:
envoy-gateway/gateway-1:
accessLog:
text:
- path: /dev/stdout
http:
- address: 0.0.0.0
hostnames:
- '*'
isHTTP2: false
name: envoy-gateway/gateway-1/http
port: 10080
envoy-gateway/gateway-2:
accessLog:
text:
- path: /dev/stdout
http:
- address: 0.0.0.0
hostnames:
- '*'
isHTTP2: false
name: envoy-gateway/gateway-2/http-2
port: 8888

Check failure on line 123 in internal/gatewayapi/testdata/multiple-gateways.out.yaml

View workflow job for this annotation

GitHub Actions / lint

123:17 [new-line-at-end-of-file] no new line character at the end of file
Loading

0 comments on commit 152eae3

Please sign in to comment.