Skip to content

Commit

Permalink
review comments, status deepcopy, check routes in ns
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Chauhan <shubham@tetrate.io>
  • Loading branch information
chauhanshubham committed Oct 10, 2022
1 parent 991b869 commit b54f2fe
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
24 changes: 21 additions & 3 deletions internal/gatewayapi/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,18 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR XdsIRMap
break
}

// With TLS Passthrough, partial wildcards are not allowed in xDS config, so "*", "*w.abc.com" are
// invalid configurations.
if listener.Hostname == nil || *listener.Hostname == "" {
listener.SetCondition(
v1beta1.ListenerConditionReady,
metav1.ConditionFalse,
v1beta1.ListenerReasonInvalid,
"Hostname must not be empty with TLS mode Passthrough.",
)
break
}

if len(listener.TLS.CertificateRefs) > 0 {
listener.SetCondition(
v1beta1.ListenerConditionReady,
Expand Down Expand Up @@ -559,10 +571,16 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR XdsIRMap
SNIs: []string{},
},
}
if listener.Hostname != nil {
if listener.Hostname == nil || *listener.Hostname == "" {
listener.SetCondition(
v1beta1.ListenerConditionReady,
metav1.ConditionFalse,
v1beta1.ListenerReasonInvalid,
"Listener is invalid, see other Conditions for details.",
)
}
if listener.Hostname != nil && *listener.Hostname != "" {
irListener.TLS.SNIs = append(irListener.TLS.SNIs, string(*listener.Hostname))
} else {
irListener.TLS.SNIs = append(irListener.TLS.SNIs, "*")
}
gwXdsIR.TCP = append(gwXdsIR.TCP, irListener)
}
Expand Down
14 changes: 0 additions & 14 deletions internal/gatewayapi/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -77,19 +76,6 @@ func TestTranslate(t *testing.T) {

got := translator.Translate(resources)

envoyGatewayNsName := "envoy-gateway-gateway-1"
sort.Slice(got.XdsIR[envoyGatewayNsName].HTTP, func(i, j int) bool {
return got.XdsIR[envoyGatewayNsName].HTTP[i].Name < got.XdsIR[envoyGatewayNsName].HTTP[j].Name
})
sort.Slice(got.XdsIR[envoyGatewayNsName].TCP, func(i, j int) bool {
return got.XdsIR[envoyGatewayNsName].TCP[i].Name < got.XdsIR[envoyGatewayNsName].TCP[j].Name
})
// Only 1 listener is supported
sort.Slice(got.InfraIR[envoyGatewayNsName].Proxy.Listeners[0].Ports,
func(i, j int) bool {
return got.InfraIR[envoyGatewayNsName].Proxy.Listeners[0].Ports[i].Name < got.InfraIR[envoyGatewayNsName].Proxy.Listeners[0].Ports[j].Name
})

opts := cmpopts.IgnoreFields(metav1.Condition{}, "LastTransitionTime")
require.Empty(t, cmp.Diff(want, got, opts))
})
Expand Down
20 changes: 20 additions & 0 deletions internal/provider/kubernetes/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwapiv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

Expand Down Expand Up @@ -54,3 +55,22 @@ func validateParentRefs(ctx context.Context, client client.Client, namespace str

return ret, nil
}

// isRoutePresentInNamespace checks if any kind of Routes - HTTPRoute, TLSRoute
// exists in the namespace ns.
func isRoutePresentInNamespace(ctx context.Context, c client.Client, ns string) (bool, error) {
tlsRouteList := &gwapiv1a2.TLSRouteList{}
if err := c.List(ctx, tlsRouteList, &client.ListOptions{Namespace: ns}); err != nil {
return false, fmt.Errorf("error listing tlsroutes")
}

httpRouteList := &gwapiv1b1.HTTPRouteList{}
if err := c.List(ctx, httpRouteList, &client.ListOptions{Namespace: ns}); err != nil {
return false, fmt.Errorf("error listing httproutes")
}

if len(tlsRouteList.Items)+len(httpRouteList.Items) > 0 {
return true, nil
}
return false, nil
}
10 changes: 5 additions & 5 deletions internal/provider/kubernetes/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ func (r *httpRouteReconciler) Reconcile(ctx context.Context, request reconcile.R
log.Info("deleted httproute from resource map")

// Delete the Namespace and Service from the resource maps if no other
// routes exist in the namespace.
routeList = &gwapiv1b1.HTTPRouteList{}
if err := r.client.List(ctx, routeList, &client.ListOptions{Namespace: request.Namespace}); err != nil {
return reconcile.Result{}, fmt.Errorf("error listing httproutes")
// routes (TLSRoute or HTTPRoute) exist in the namespace.
found, err := isRoutePresentInNamespace(ctx, r.client, request.NamespacedName.Namespace)
if err != nil {
return reconcile.Result{}, err
}
if len(routeList.Items) == 0 {
if !found {
r.resources.Namespaces.Delete(request.Namespace)
log.Info("deleted namespace from resource map")
r.resources.Services.Delete(request.NamespacedName)
Expand Down
17 changes: 10 additions & 7 deletions internal/provider/kubernetes/tlsroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ func (r *tlsRouteReconciler) Reconcile(ctx context.Context, request reconcile.Re
log.Info("deleted tlsroute from resource map")

// Delete the Namespace and Service from the resource maps if no other
// routes exist in the namespace.
routeList = &gwapiv1a2.TLSRouteList{}
if err := r.client.List(ctx, routeList, &client.ListOptions{Namespace: request.Namespace}); err != nil {
return reconcile.Result{}, fmt.Errorf("error listing tlsroutes")
// routes (TLSRoute or HTTPRoute) exist in the namespace.
found, err := isRoutePresentInNamespace(ctx, r.client, request.NamespacedName.Namespace)
if err != nil {
return reconcile.Result{}, err
}
if len(routeList.Items) == 0 {
if !found {
r.resources.Namespaces.Delete(request.Namespace)
log.Info("deleted namespace from resource map")
r.resources.Services.Delete(request.NamespacedName)
Expand Down Expand Up @@ -311,10 +311,13 @@ func (r *tlsRouteReconciler) subscribeAndUpdateStatus(ctx context.Context) {
NamespacedName: key,
Resource: new(gwapiv1a2.TLSRoute),
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
if _, ok := obj.(*gwapiv1a2.TLSRoute); !ok {
t, ok := obj.(*gwapiv1a2.TLSRoute)
if !ok {
panic(fmt.Sprintf("unsupported object type %T", obj))
}
return val
tCopy := t.DeepCopy()
tCopy.Status.Parents = val.Status.Parents
return tCopy
}),
})
}
Expand Down

0 comments on commit b54f2fe

Please sign in to comment.