Skip to content

Commit

Permalink
add support for using gateway.spec.addresses as gateway service exter…
Browse files Browse the repository at this point in the history
…nal ip

Signed-off-by: Shawnh2 <[email protected]>
  • Loading branch information
shawnh2 committed Apr 17, 2023
1 parent 4d4b1ca commit 00351d0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 44 deletions.
40 changes: 40 additions & 0 deletions internal/gatewayapi/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package gatewayapi

import (
"net/netip"

"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/gateway-api/apis/v1beta1"
)

var _ AddressesTranslator = (*Translator)(nil)

type AddressesTranslator interface {
ProcessAddresses(gateways []*GatewayContext, xdsIR XdsIRMap, infraIR InfraIRMap, resources *Resources)
}

func (t *Translator) ProcessAddresses(gateways []*GatewayContext, xdsIR XdsIRMap, infraIR InfraIRMap, resources *Resources) {
for _, gateway := range gateways {
// Infra IR already exist
irKey := irStringKey(gateway.Gateway)
gwInfraIR := infraIR[irKey]

ipAddr := sets.Set[string]{}

for _, addr := range gateway.Spec.Addresses {
switch *addr.Type {
case v1beta1.IPAddressType:
if _, err := netip.ParseAddr(addr.Value); err == nil {
ipAddr.Insert(addr.Value)
}
}
}

gwInfraIR.Proxy.Addresses = sets.List(ipAddr)
}
}
22 changes: 0 additions & 22 deletions internal/gatewayapi/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,3 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR XdsIRMap
}
}
}

func newProxyAddresses(as []v1beta1.GatewayAddress) []ir.ProxyAddress {
if len(as) == 0 {
return nil
}

out := make([]ir.ProxyAddress, len(as))
for i, a := range as {
if a.Type == nil {
continue
}
switch *a.Type {
case v1beta1.HostnameAddressType:
out[i] = ir.ProxyHostname(a.Value)
case v1beta1.IPAddressType:
out[i] = ir.ProxyIPAddress(a.Value)
case v1beta1.NamedAddressType:
out[i] = ir.ProxyNamedAddress(a.Value)
}
}
return out
}
4 changes: 4 additions & 0 deletions internal/gatewayapi/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type TranslatorManager interface {

RoutesTranslator
ListenersTranslator
AddressesTranslator
FiltersTranslator
}

Expand Down Expand Up @@ -119,6 +120,9 @@ func (t *Translator) Translate(resources *Resources) *TranslateResult {
// Process all Listeners for all relevant Gateways.
t.ProcessListeners(gateways, xdsIR, infraIR, resources)

// Process all Addresses for all relevant Gateways.
t.ProcessAddresses(gateways, xdsIR, infraIR, resources)

// Process all relevant HTTPRoutes.
httpRoutes := t.ProcessHTTPRoutes(resources.HTTPRoutes, gateways, resources, xdsIR)

Expand Down
5 changes: 5 additions & 0 deletions internal/infrastructure/kubernetes/proxy_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ func (i *Infra) expectedProxyService(infra *ir.Infra) (*corev1.Service, error) {
if envoyServiceConfig.Annotations != nil {
annotations = envoyServiceConfig.Annotations
}

// Set the spec of envoy gateway service
serviceSpec := expectedServiceSpec(envoyServiceConfig.Type)
serviceSpec.Ports = ports
serviceSpec.Selector = getSelector(labels).MatchLabels
if len(infra.Proxy.Addresses) > 0 {
serviceSpec.ExternalIPs = infra.Proxy.Addresses
}

svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down
19 changes: 1 addition & 18 deletions internal/ir/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ type ProxyInfra struct {
Config *v1alpha1.EnvoyProxy
// Listeners define the listeners exposed by the proxy infrastructure.
Listeners []ProxyListener

// Addresses contain the external addresses this gateway has been
// requested to be available at.
Addresses []ProxyAddress
Addresses []string
}

// InfraMetadata defines metadata for the managed proxy infrastructure.
Expand All @@ -59,22 +58,6 @@ type ProxyListener struct {
Ports []ListenerPort
}

type ProxyAddress interface {
isProxyAddress()
}

type ProxyHostname string

func (ProxyHostname) isProxyAddress() {}

type ProxyIPAddress string

func (ProxyIPAddress) isProxyAddress() {}

type ProxyNamedAddress string

func (ProxyNamedAddress) isProxyAddress() {}

// ListenerPort defines a network port of a listener.
// +k8s:deepcopy-gen=true
type ListenerPort struct {
Expand Down
9 changes: 5 additions & 4 deletions internal/status/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func UpdateGatewayStatusProgrammedCondition(gw *gwapiv1b1.Gateway, svc *corev1.S
}
}

for _, eip := range svc.Spec.ExternalIPs {
addresses = append(addresses, eip)
}

var gwAddresses []gwapiv1b1.GatewayAddress
for i := range addresses {
addr := gwapiv1b1.GatewayAddress{
Expand All @@ -68,11 +72,8 @@ func UpdateGatewayStatusProgrammedCondition(gw *gwapiv1b1.Gateway, svc *corev1.S
}

gw.Status.Addresses = gwAddresses
if len(gw.Status.Addresses) == 0 {
gw.Status.Addresses = gw.Spec.Addresses
}
} else {
gw.Status.Addresses = gw.Spec.Addresses
gw.Status.Addresses = nil
}
// Update the programmed condition.
gw.Status.Conditions = MergeConditions(gw.Status.Conditions, computeGatewayProgrammedCondition(gw, deployment))
Expand Down

0 comments on commit 00351d0

Please sign in to comment.