Skip to content

Commit

Permalink
fix: prevent vcluster crash when network policy syncing is enabled
Browse files Browse the repository at this point in the history
(cherry picked from commit 52df059)
  • Loading branch information
lizardruss committed Nov 22, 2024
1 parent f29818a commit 57afcf4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/resources/networkpolicies/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package networkpolicies
import (
"fmt"

"github.com/loft-sh/vcluster/pkg/mappings/generic"
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/syncer"
Expand All @@ -18,7 +18,7 @@ import (
)

func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
mapper, err := generic.NewMapper(ctx, &networkingv1.NetworkPolicy{}, translate.Default.HostName)
mapper, err := ctx.Mappings.ByGVK(mappings.NetworkingPolicies())
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/controllers/resources/networkpolicies/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package networkpolicies
import (
"testing"

"github.com/loft-sh/vcluster/pkg/config"
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
syncertesting "github.com/loft-sh/vcluster/pkg/syncer/testing"
testingutil "github.com/loft-sh/vcluster/pkg/util/testing"
"gotest.tools/assert"
"k8s.io/utils/ptr"

Expand Down Expand Up @@ -217,7 +219,10 @@ func TestSync(t *testing.T) {
},
}

syncertesting.RunTests(t, []*syncertesting.SyncTest{
syncertesting.RunTestsWithContext(t, func(vConfig *config.VirtualClusterConfig, pClient *testingutil.FakeIndexClient, vClient *testingutil.FakeIndexClient) *synccontext.RegisterContext {
vConfig.Sync.ToHost.NetworkPolicies.Enabled = true
return syncertesting.NewFakeRegisterContext(vConfig, pClient, vClient)
}, []*syncertesting.SyncTest{
{
Name: "Create forward",
InitialVirtualState: []runtime.Object{vBaseNetworkPolicy.DeepCopy()},
Expand Down
4 changes: 4 additions & 0 deletions pkg/mappings/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func Namespaces() schema.GroupVersionKind {
return corev1.SchemeGroupVersion.WithKind("Namespace")
}

func NetworkingPolicies() schema.GroupVersionKind {
return networkingv1.SchemeGroupVersion.WithKind("NetworkPolicy")
}

func Ingresses() schema.GroupVersionKind {
return networkingv1.SchemeGroupVersion.WithKind("Ingress")
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/mappings/resources/networkpolicies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package resources

import (
"github.com/loft-sh/vcluster/pkg/mappings/generic"
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
"github.com/loft-sh/vcluster/pkg/util/translate"
networkingv1 "k8s.io/api/networking/v1"
)

func CreateNetworkPoliciesMapper(ctx *synccontext.RegisterContext) (synccontext.Mapper, error) {
return generic.NewMapper(ctx, &networkingv1.NetworkPolicy{}, translate.Default.HostName)
}
1 change: 1 addition & 0 deletions pkg/mappings/resources/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func getMappers(ctx *synccontext.RegisterContext) []BuildMapper {
CreateEventsMapper,
isEnabled(ctx.Config.Sync.ToHost.Ingresses.Enabled, CreateIngressesMapper),
CreateNamespacesMapper,
isEnabled(ctx.Config.Sync.ToHost.NetworkPolicies.Enabled, CreateNetworkPoliciesMapper),
CreateNodesMapper,
CreatePersistentVolumeClaimsMapper,
isEnabled(ctx.Config.Sync.ToHost.ServiceAccounts.Enabled, CreateServiceAccountsMapper),
Expand Down

0 comments on commit 57afcf4

Please sign in to comment.