Skip to content

Commit

Permalink
Merge pull request #2178 from FabianKramm/main
Browse files Browse the repository at this point in the history
fix: pv syncing
  • Loading branch information
FabianKramm authored Sep 24, 2024
2 parents 4165065 + a7928fc commit 359b6e5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
11 changes: 10 additions & 1 deletion pkg/controllers/resources/persistentvolumes/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"testing"
"time"

"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/apimachinery/pkg/api/resource"

Expand Down Expand Up @@ -188,7 +190,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.PersistentVolumes.Enabled = true
return syncertesting.NewFakeRegisterContext(vConfig, pClient, vClient)
}, []*syncertesting.SyncTest{
{
Name: "Create Backward",
InitialVirtualState: []runtime.Object{basePvc},
Expand All @@ -202,6 +207,10 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncContext, syncer := newFakeSyncer(t, ctx)

vName := syncer.HostToVirtual(syncContext, types.NamespacedName{Name: basePPv.Name}, basePPv)
assert.Equal(t, vName.Name, baseVPv.Name)

_, err := syncer.SyncToVirtual(syncContext, synccontext.NewSyncToVirtualEvent(basePPv))
assert.NilError(t, err)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
syncertesting "github.com/loft-sh/vcluster/pkg/syncer/testing"
testingutil "github.com/loft-sh/vcluster/pkg/util/testing"
"gotest.tools/assert"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"

volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
Expand Down Expand Up @@ -177,6 +178,10 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := newFakeSyncer(t, ctx)

vName := syncer.HostToVirtual(syncCtx, types.NamespacedName{Name: pDynamic.Name}, pDynamic)
assert.Equal(t, vName.Name, pDynamic.Name)

_, err := syncer.SyncToVirtual(syncCtx, synccontext.NewSyncToVirtualEvent(pDynamic.DeepCopy()))
assert.NilError(t, err)
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/mappings/generic/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewMapperWithObject(ctx *synccontext.RegisterContext, obj client.Object, tr
return newMapper(ctx, obj, true, translateName)
}

// NewMapperWithoutRecorder creates a new mapper with a recorder to store mappings in the mappings store
// NewMapperWithoutRecorder creates a new mapper without a recorder to store mappings in the mappings store
func NewMapperWithoutRecorder(ctx *synccontext.RegisterContext, obj client.Object, translateName PhysicalNameWithObjectFunc) (synccontext.Mapper, error) {
return newMapper(ctx, obj, false, translateName)
}
Expand Down
26 changes: 23 additions & 3 deletions pkg/mappings/resources/persistentvolumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,36 @@ func CreatePersistentVolumesMapper(ctx *synccontext.RegisterContext) (synccontex
return generic.NewMirrorMapper(&corev1.PersistentVolume{})
}

return generic.NewMapperWithObject(ctx, &corev1.PersistentVolume{}, func(_ *synccontext.SyncContext, name, _ string, vObj client.Object) types.NamespacedName {
mapper, err := generic.NewMapperWithoutRecorder(ctx, &corev1.PersistentVolume{}, func(_ *synccontext.SyncContext, vName, _ string, vObj client.Object) types.NamespacedName {
if vObj == nil {
return types.NamespacedName{Name: name}
return types.NamespacedName{Name: vName}
}

vPv, ok := vObj.(*corev1.PersistentVolume)
if !ok || vPv.Annotations == nil || vPv.Annotations[constants.HostClusterPersistentVolumeAnnotation] == "" {
return types.NamespacedName{Name: translate.Default.HostNameCluster(name)}
return types.NamespacedName{Name: translate.Default.HostNameCluster(vName)}
}

return types.NamespacedName{Name: vPv.Annotations[constants.HostClusterPersistentVolumeAnnotation]}
})
if err != nil {
return nil, err
}

return generic.WithRecorder(&persistentVolumeMapper{
Mapper: mapper,
}), nil
}

type persistentVolumeMapper struct {
synccontext.Mapper
}

func (p *persistentVolumeMapper) HostToVirtual(ctx *synccontext.SyncContext, req types.NamespacedName, pObj client.Object) types.NamespacedName {
vName := p.Mapper.HostToVirtual(ctx, req, pObj)
if vName.Name != "" {
return vName
}

return types.NamespacedName{Name: req.Name}
}
26 changes: 23 additions & 3 deletions pkg/mappings/resources/volumesnapshotcontents.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,36 @@ func CreateVolumeSnapshotContentsMapper(ctx *synccontext.RegisterContext) (syncc
return nil, err
}

return generic.NewMapperWithObject(ctx, &volumesnapshotv1.VolumeSnapshotContent{}, func(_ *synccontext.SyncContext, name, _ string, vObj client.Object) types.NamespacedName {
mapper, err := generic.NewMapperWithoutRecorder(ctx, &volumesnapshotv1.VolumeSnapshotContent{}, func(_ *synccontext.SyncContext, vName, _ string, vObj client.Object) types.NamespacedName {
if vObj == nil {
return types.NamespacedName{Name: name}
return types.NamespacedName{Name: vName}
}

vVSC, ok := vObj.(*volumesnapshotv1.VolumeSnapshotContent)
if !ok || vVSC.Annotations == nil || vVSC.Annotations[constants.HostClusterVSCAnnotation] == "" {
return types.NamespacedName{Name: translate.Default.HostNameCluster(name)}
return types.NamespacedName{Name: translate.Default.HostNameCluster(vName)}
}

return types.NamespacedName{Name: vVSC.Annotations[constants.HostClusterVSCAnnotation]}
})
if err != nil {
return nil, err
}

return generic.WithRecorder(&volumeSnapshotContentMapper{
Mapper: mapper,
}), nil
}

type volumeSnapshotContentMapper struct {
synccontext.Mapper
}

func (p *volumeSnapshotContentMapper) HostToVirtual(ctx *synccontext.SyncContext, req types.NamespacedName, pObj client.Object) types.NamespacedName {
vName := p.Mapper.HostToVirtual(ctx, req, pObj)
if vName.Name != "" {
return vName
}

return types.NamespacedName{Name: req.Name}
}

0 comments on commit 359b6e5

Please sign in to comment.