Skip to content

Commit

Permalink
Merge pull request #281 from ngrok/nikolay/isolate-tunnels-namespace
Browse files Browse the repository at this point in the history
Create tunnels per namespace/service
  • Loading branch information
nikolay-ngrok authored Jul 13, 2023
2 parents 07b9aa1 + 18bc117 commit a3b8d54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
golang.ngrok.com/ngrok v1.3.1
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
golang.org/x/sync v0.1.0
k8s.io/api v0.26.0
k8s.io/apimachinery v0.26.0
Expand Down Expand Up @@ -81,7 +82,7 @@ require (
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.ngrok.com/muxado/v2 v2.0.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand All @@ -621,8 +623,8 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
20 changes: 14 additions & 6 deletions internal/store/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/go-logr/logr"
"golang.org/x/exp/maps"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -453,11 +454,18 @@ func (d *Driver) calculateHTTPSEdges() []ingressv1alpha1.HTTPSEdge {
func (d *Driver) calculateTunnels() []ingressv1alpha1.Tunnel {
// Tunnels should be unique on a service and port basis so if they are referenced more than once, we
// only create one tunnel per service and port.
tunnelMap := make(map[string]ingressv1alpha1.Tunnel)
var tunnels = map[string]map[string]ingressv1alpha1.Tunnel{}

ingresses := d.ListNgrokIngressesV1()
for _, ingress := range ingresses {
namespace := ingress.Namespace

namespaceTunnels, namespaceOk := tunnels[namespace]
if !namespaceOk {
namespaceTunnels = make(map[string]ingressv1alpha1.Tunnel)
tunnels[namespace] = namespaceTunnels
}

for _, rule := range ingress.Spec.Rules {
for _, path := range rule.HTTP.Paths {
// We only support service backends right now. TODO: support resource backends
Expand All @@ -474,7 +482,7 @@ func (d *Driver) calculateTunnels() []ingressv1alpha1.Tunnel {
tunnelAddr := fmt.Sprintf("%s.%s.%s:%d", serviceName, namespace, clusterDomain, servicePort)
tunnelName := fmt.Sprintf("%s-%d", serviceName, servicePort)

tunnelMap[tunnelName] = ingressv1alpha1.Tunnel{
namespaceTunnels[tunnelName] = ingressv1alpha1.Tunnel{
ObjectMeta: metav1.ObjectMeta{
Name: tunnelName,
Namespace: ingress.Namespace,
Expand All @@ -491,11 +499,11 @@ func (d *Driver) calculateTunnels() []ingressv1alpha1.Tunnel {
}
}

tunnels := make([]ingressv1alpha1.Tunnel, 0, len(tunnelMap))
for _, tunnel := range tunnelMap {
tunnels = append(tunnels, tunnel)
var allTunnels []ingressv1alpha1.Tunnel
for _, namespaceTunnels := range maps.Values(tunnels) {
allTunnels = append(allTunnels, maps.Values(namespaceTunnels)...)
}
return tunnels
return allTunnels
}

func (d *Driver) calculateIngressLoadBalancerIPStatus(ing *netv1.Ingress, c client.Reader) []netv1.IngressLoadBalancerIngress {
Expand Down

0 comments on commit a3b8d54

Please sign in to comment.