Skip to content

Commit

Permalink
Implement Watch Namespaces (#1610)
Browse files Browse the repository at this point in the history
* Implement Watch Namespaces

Relates to #1117

Signed-off-by: Arko Dasgupta <[email protected]>
  • Loading branch information
arkodg authored Jul 4, 2023
1 parent 9c9c82a commit 3892d90
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/provider/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func New(cfg *rest.Config, svr *config.Server, resources *message.ProviderResour
LeaderElectionID: "5b9825d2.gateway.envoyproxy.io",
MetricsBindAddress: ":8080",
}

if svr.EnvoyGateway.Provider != nil &&
svr.EnvoyGateway.Provider.Kubernetes != nil &&
(svr.EnvoyGateway.Provider.Kubernetes.Watch != nil) &&
(len(svr.EnvoyGateway.Provider.Kubernetes.Watch.Namespaces) > 0) {
mgrOpts.Cache.Namespaces = svr.EnvoyGateway.Provider.Kubernetes.Watch.Namespaces
}

mgr, err := ctrl.NewManager(cfg, mgrOpts)
if err != nil {
return nil, fmt.Errorf("failed to create manager: %w", err)
Expand Down
60 changes: 60 additions & 0 deletions internal/provider/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1664,3 +1664,63 @@ func testServiceCleanupForMultipleRoutes(ctx context.Context, t *testing.T, prov
return true
}, defaultWait, defaultTick)
}

func TestNamespacedProvider(t *testing.T) {
// Setup the test environment.
testEnv, cliCfg, err := startEnv()
require.NoError(t, err)

// Setup and start the kube provider.
svr, err := config.New()
require.NoError(t, err)
// config to watch a subset of namespaces
svr.EnvoyGateway.Provider.Kubernetes = &egcfgv1a1.EnvoyGatewayKubernetesProvider{
Watch: &egcfgv1a1.KubernetesWatchMode{
Namespaces: []string{"ns1", "ns2"},
},
}

resources := new(message.ProviderResources)
provider, err := New(cliCfg, svr, resources)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
go func() {
require.NoError(t, provider.Start(ctx))
}()

// Make sure a cluster scoped gatewayclass can be reconciled
testGatewayClassController(ctx, t, provider, resources)

cli := provider.manager.GetClient()
gcName := "gc-watch-ns"
gc := test.GetGatewayClass(gcName, egcfgv1a1.GatewayControllerName)
require.NoError(t, cli.Create(ctx, gc))

// Create the namespaces.
ns1 := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "ns1"}}
require.NoError(t, cli.Create(ctx, ns1))
ns2 := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "ns2"}}
require.NoError(t, cli.Create(ctx, ns2))
ns3 := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "ns3"}}
require.NoError(t, cli.Create(ctx, ns3))

// Create the gateways
gw1 := test.GetGateway(types.NamespacedName{Name: "gw-ns1", Namespace: "ns1"}, gcName)
require.NoError(t, cli.Create(ctx, gw1))
gw2 := test.GetGateway(types.NamespacedName{Name: "gw-ns2", Namespace: "ns2"}, gcName)
require.NoError(t, cli.Create(ctx, gw2))
gw3 := test.GetGateway(types.NamespacedName{Name: "gw-ns3", Namespace: "ns3"}, gcName)
require.NoError(t, cli.Create(ctx, gw3))

// Ensure only 2 gateways are reconciled
gatewayList := &gwapiv1b1.GatewayList{}
require.NoError(t, cli.List(ctx, gatewayList))
assert.Equal(t, len(gatewayList.Items), 2)

// Stop the kube provider.
defer func() {
cancel()
require.NoError(t, testEnv.Stop())
}()

}

0 comments on commit 3892d90

Please sign in to comment.