Skip to content

Commit

Permalink
Adds Support for Gateway Status Addresses
Browse files Browse the repository at this point in the history
Signed-off-by: danehans <daneyonhansen@gmail.com>
  • Loading branch information
danehans committed Sep 7, 2022
1 parent 4ce5d79 commit 3be8de6
Show file tree
Hide file tree
Showing 23 changed files with 292 additions and 100 deletions.
7 changes: 7 additions & 0 deletions internal/envoygateway/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
"github.com/envoyproxy/gateway/internal/log"
)

const (
// EnvoyGatewayNamespace is the namespace where envoy-gateway is running.
EnvoyGatewayNamespace = "envoy-gateway-system"
// EnvoyServiceName is the name of the Envoy Service.
EnvoyServiceName = "envoy"
)

// Server wraps the EnvoyGateway configuration and additional parameters
// used by Envoy Gateway server.
type Server struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/gatewayapi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func PathMatchTypePtr(pType v1beta1.PathMatchType) *v1beta1.PathMatchType {
return &pType
}

func GatewayAddressTypePtr(addr v1beta1.AddressType) *v1beta1.AddressType {
return &addr
}

func PathMatchTypeDerefOr(matchType *v1beta1.PathMatchType, defaultType v1beta1.PathMatchType) v1beta1.PathMatchType {
if matchType != nil {
return *matchType
Expand Down
16 changes: 13 additions & 3 deletions internal/gatewayapi/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runner
import (
"context"

"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/envoyproxy/gateway/internal/envoygateway/config"
Expand Down Expand Up @@ -72,11 +73,20 @@ func (r *Runner) subscribeAndTranslate(ctx context.Context) {
case gatewayClasses == nil:
// Envoy Gateway startup.
continue
case gatewayClasses[0] == nil:
case len(in.Gateways) == 1 && in.Gateways[0] == nil:
// No need to translate, publish empty IRs, e.g. delete operation.
r.XdsIR.Delete(r.Name())
// A nil ProxyInfra tells the Infra Manager to delete the managed proxy infra.
r.InfraIR.Store(r.Name(), &ir.Infra{Proxy: nil})
// Nil ProxyInfra listeners tells the Infra Manager to delete the managed proxy infra.
var gw types.NamespacedName
for nsName := range r.ProviderResources.Gateways.LoadAll() {
gw = nsName
}
infra := &ir.Infra{
Proxy: &ir.ProxyInfra{
Metadata: &ir.InfraMetadata{Labels: gatewayapi.GatewayOwnerLabels(gw.Namespace, gw.Name)},
},
}
r.InfraIR.Store(r.Name(), infra)
default:
// Translate and publish IRs.
t := &gatewayapi.Translator{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ xdsIR:
fullReplace: /redirected
infraIR:
proxy:
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-name: gateway-1
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
name: envoy-gateway-class
image: envoyproxy/envoy:v1.23-latest
listeners:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ xdsIR:
hostname: "redirected.com"
infraIR:
proxy:
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-name: gateway-1
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
name: envoy-gateway-class
image: envoyproxy/envoy:v1.23-latest
listeners:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ xdsIR:
statusCode: 500
infraIR:
proxy:
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-name: gateway-1
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
name: envoy-gateway-class
image: envoyproxy/envoy:v1.23-latest
listeners:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ xdsIR:
weight: 1
infraIR:
proxy:
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-name: gateway-1
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
name: envoy-gateway-class
image: envoyproxy/envoy:v1.23-latest
listeners:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ xdsIR:
weight: 1
infraIR:
proxy:
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-name: gateway-1
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
name: envoy-gateway-class
image: envoyproxy/envoy:v1.23-latest
listeners:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ xdsIR:
prefixMatchReplace: /redirected
infraIR:
proxy:
metadata:
labels:
gateway.envoyproxy.io/owning-gateway-name: gateway-1
gateway.envoyproxy.io/owning-gateway-namespace: envoy-gateway
name: envoy-gateway-class
image: envoyproxy/envoy:v1.23-latest
listeners:
Expand Down
10 changes: 5 additions & 5 deletions internal/gatewayapi/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (t *Translator) Translate(resources *Resources) *TranslateResult {
if resources != nil {
for i := range resources.Gateways {
if resources.Gateways[i] != nil {
infraIR.Proxy.Metadata.Labels = GatewayOwnerLabels(resources.Gateways[i])
infraIR.Proxy.Metadata.Labels = GatewayOwnerLabels(resources.Gateways[i].Namespace, resources.Gateways[i].Name)
}
}
}
Expand Down Expand Up @@ -983,10 +983,10 @@ func irTLSConfig(tlsSecret *v1.Secret) *ir.TLSListenerConfig {
}
}

// GatewayOwnerLabels returns owner labels for the provided Gateway.
func GatewayOwnerLabels(gw *v1beta1.Gateway) map[string]string {
// GatewayOwnerLabels returns owner labels for the provided ns/name.
func GatewayOwnerLabels(ns, name string) map[string]string {
return map[string]string{
OwningGatewayNameLabel: gw.Name,
OwningGatewayNamespaceLabel: gw.Namespace,
OwningGatewayNamespaceLabel: ns,
OwningGatewayNameLabel: name,
}
}
21 changes: 1 addition & 20 deletions internal/infrastructure/kubernetes/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (i *Infra) expectedDeployment(infra *ir.Infra) (*appsv1.Deployment, error)
return nil, err
}

podSelector := EnvoyPodSelector(infra.GetProxyInfra().Name)
podSelector := envoyPodSelector(infra.Proxy.Metadata.Labels)

deployment := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -284,22 +284,3 @@ func (i *Infra) deleteDeployment(ctx context.Context) error {

return nil
}

// EnvoyPodSelector returns a label selector using "control-plane: envoy-gateway" as the
// key/value pair.
//
// TODO: Update k/v pair to use gatewayclass controller name to distinguish between
// multiple Envoy Gateways.
func EnvoyPodSelector(gcName string) *metav1.LabelSelector {
return &metav1.LabelSelector{
MatchLabels: envoyLabels(gcName),
}
}

// envoyLabels returns the labels used for Envoy.
func envoyLabels(gcName string) map[string]string {
return map[string]string{
"gatewayClass": gcName,
"app": "envoy",
}
}
24 changes: 0 additions & 24 deletions internal/infrastructure/kubernetes/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,30 +188,6 @@ func TestCreateDeploymentIfNeeded(t *testing.T) {
}
}

func TestEnvoyPodSelector(t *testing.T) {
cases := []struct {
name string
gcName string
expected map[string]string
}{
{
name: "default",
gcName: "eg",
expected: map[string]string{
"gatewayClass": "eg",
"app": "envoy",
},
},
}

for _, tc := range cases {
t.Run("", func(t *testing.T) {
got := EnvoyPodSelector(tc.gcName)
require.Equal(t, tc.expected, got.MatchLabels)
})
}
}

func TestDeleteDeployment(t *testing.T) {
testCases := []struct {
name string
Expand Down
10 changes: 3 additions & 7 deletions internal/infrastructure/kubernetes/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ import (
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/utils/env"
)

const (
// envoyGatewayNamespace is the namespace where envoy-gateway is running.
envoyGatewayNamespace = "envoy-gateway-system"
)

// Infra holds all the translated Infra IR resources and provides
// the scaffolding for the managing Kubernetes infrastructure.
type Infra struct {
Expand All @@ -45,7 +41,7 @@ func NewInfra(cli client.Client) *Infra {
}

// Set the namespace used for the managed infra.
infra.Namespace = env.Lookup("ENVOY_GATEWAY_NAMESPACE", envoyGatewayNamespace)
infra.Namespace = env.Lookup("ENVOY_GATEWAY_NAMESPACE", config.EnvoyGatewayNamespace)

return infra
}
Expand Down Expand Up @@ -117,7 +113,7 @@ func (i *Infra) DeleteInfra(ctx context.Context, infra *ir.Infra) error {
return errors.New("infra ir is nil")
}

if err := i.deleteService(ctx); err != nil {
if err := i.deleteService(ctx, infra); err != nil {
return err
}

Expand Down
25 changes: 25 additions & 0 deletions internal/infrastructure/kubernetes/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kubernetes

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// envoyPodSelector returns a label selector used to select Envoy resources.
func envoyPodSelector(gwLabels map[string]string) *metav1.LabelSelector {
return &metav1.LabelSelector{
MatchLabels: envoyLabels(gwLabels),
}
}

// envoyLabels returns the labels applied to managed Envoy resources.
func envoyLabels(gwLabels map[string]string) map[string]string {
ret := map[string]string{
"app": "envoy",
}

for k, v := range gwLabels {
ret[k] = v
}

return ret
}
33 changes: 33 additions & 0 deletions internal/infrastructure/kubernetes/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package kubernetes

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestEnvoyPodSelector(t *testing.T) {
cases := []struct {
name string
in map[string]string
expected map[string]string
}{
{
name: "default",
in: map[string]string{
"foo": "bar",
},
expected: map[string]string{
"app": "envoy",
"foo": "bar",
},
},
}

for _, tc := range cases {
t.Run("", func(t *testing.T) {
got := envoyPodSelector(tc.in)
require.Equal(t, tc.expected, got.MatchLabels)
})
}
}
Loading

0 comments on commit 3be8de6

Please sign in to comment.