Skip to content

Commit

Permalink
fixup! fix: Separate volumesnapshot mappers into individual configura…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
ThomasK33 committed Oct 16, 2024
1 parent 52af8c5 commit c7a6f4a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 24 deletions.
26 changes: 18 additions & 8 deletions pkg/config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ var allowedPodSecurityStandards = map[string]bool{
"restricted": true,
}

var (
verbs = []string{"get", "list", "create", "update", "patch", "watch", "delete", "deletecollection"}
)
var verbs = []string{"get", "list", "create", "update", "patch", "watch", "delete", "deletecollection"}

func ValidateConfigAndSetDefaults(config *VirtualClusterConfig) error {
// check the value of pod security standard
Expand Down Expand Up @@ -61,12 +59,24 @@ func ValidateConfigAndSetDefaults(config *VirtualClusterConfig) error {

// check if nodes controller needs to be enabled
if config.ControlPlane.Advanced.VirtualScheduler.Enabled && !config.Sync.FromHost.Nodes.Enabled {
return fmt.Errorf("sync.fromHost.nodes.enabled is false, but required if using virtual scheduler")
return errors.New("sync.fromHost.nodes.enabled is false, but required if using virtual scheduler")
}

// check if storage classes and host storage classes are enabled at the same time
if config.Sync.FromHost.StorageClasses.Enabled == "true" && config.Sync.ToHost.StorageClasses.Enabled {
return fmt.Errorf("you cannot enable both sync.fromHost.storageClasses.enabled and sync.toHost.storageClasses.enabled at the same time. Choose only one of them")
return errors.New("you cannot enable both sync.fromHost.storageClasses.enabled and sync.toHost.storageClasses.enabled at the same time. Choose only one of them")
}

if config.Sync.FromHost.PriorityClasses.Enabled && config.Sync.ToHost.PriorityClasses.Enabled {
return errors.New("cannot sync priorityclasses to and from host at the same time")
}

// volumesnapshots and volumesnapshotcontents are dependant on each other
if config.Sync.ToHost.VolumeSnapshotContents.Enabled && !config.Sync.ToHost.VolumeSnapshots.Enabled {
return errors.New("when syncing volume snapshots contents to the host, one must set sync.toHost.volumeSnapshots.enabled to true")
}
if config.Sync.ToHost.VolumeSnapshots.Enabled && !config.Sync.ToHost.VolumeSnapshotContents.Enabled {
return errors.New("when syncing volume snapshots to the host, one must set sync.toHost.volumeSnapshotContents.enabled to true")
}

// validate central admission control
Expand Down Expand Up @@ -122,13 +132,13 @@ func ValidateConfigAndSetDefaults(config *VirtualClusterConfig) error {

func validateDistro(config *VirtualClusterConfig) error {
enabledDistros := 0
if config.Config.ControlPlane.Distro.K3S.Enabled {
if config.ControlPlane.Distro.K3S.Enabled {
enabledDistros++
}
if config.Config.ControlPlane.Distro.K0S.Enabled {
if config.ControlPlane.Distro.K0S.Enabled {
enabledDistros++
}
if config.Config.ControlPlane.Distro.K8S.Enabled {
if config.ControlPlane.Distro.K8S.Enabled {
enabledDistros++
}

Expand Down
15 changes: 10 additions & 5 deletions pkg/controllers/resources/volumesnapshotclasses/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ func TestSync(t *testing.T) {
vMoreParamsVSC := vBaseVSC.DeepCopy()
vMoreParamsVSC.Parameters["additional"] = "param"

syncertesting.RunTestsWithContext(t, func(vConfig *config.VirtualClusterConfig, pClient *testingutil.FakeIndexClient, vClient *testingutil.FakeIndexClient) *synccontext.RegisterContext {
vConfig.Sync.ToHost.VolumeSnapshots.Enabled = true
return syncertesting.NewFakeRegisterContext(vConfig, pClient, vClient)
}, []*syncertesting.SyncTest{
tests := []*syncertesting.SyncTest{
{
Name: "Create backward",
InitialVirtualState: []runtime.Object{},
Expand Down Expand Up @@ -96,5 +93,13 @@ func TestSync(t *testing.T) {
assert.NilError(t, err)
},
},
})
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test.Run(t, syncertesting.NewContextFunc(func(vConfig *config.VirtualClusterConfig, pClient *testingutil.FakeIndexClient, vClient *testingutil.FakeIndexClient) *synccontext.RegisterContext {
vConfig.Sync.ToHost.VolumeSnapshots.Enabled = true
return syncertesting.NewFakeRegisterContext(vConfig, pClient, vClient)
}))
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func TestSync(t *testing.T) {

createContext := func(vConfig *config.VirtualClusterConfig, pClient *testingutil.FakeIndexClient, vClient *testingutil.FakeIndexClient) *synccontext.RegisterContext {
vConfig.Sync.ToHost.VolumeSnapshots.Enabled = true
vConfig.Sync.ToHost.VolumeSnapshotContents.Enabled = true
return syncertesting.NewFakeRegisterContext(vConfig, pClient, vClient)
}

Expand Down
18 changes: 14 additions & 4 deletions pkg/controllers/resources/volumesnapshots/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@ func TestSync(t *testing.T) {
vWithStatus := vPVSourceSnapshot.DeepCopy()
vWithStatus.Status = pWithStatus.Status

syncertesting.RunTestsWithContext(t, func(vConfig *config.VirtualClusterConfig, pClient *testingutil.FakeIndexClient, vClient *testingutil.FakeIndexClient) *synccontext.RegisterContext {
createContext := syncertesting.NewContextFunc(func(vConfig *config.VirtualClusterConfig, pClient *testingutil.FakeIndexClient, vClient *testingutil.FakeIndexClient) *synccontext.RegisterContext {
vConfig.Sync.ToHost.VolumeSnapshots.Enabled = true
vConfig.Sync.ToHost.VolumeSnapshotContents.Enabled = true
return syncertesting.NewFakeRegisterContext(vConfig, pClient, vClient)
}, []*syncertesting.SyncTest{
})

tests := []*syncertesting.SyncTest{
{
Name: "Create with PersistentVolume source",
InitialVirtualState: []runtime.Object{vPVSourceSnapshot.DeepCopy()},
Expand Down Expand Up @@ -229,12 +232,19 @@ func TestSync(t *testing.T) {
volumesnapshotv1.SchemeGroupVersion.WithKind("VolumeSnapshot"): {vDeletingSnapshot},
},
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
volumesnapshotv1.SchemeGroupVersion.WithKind("VolumeSnapshot"): {}},
volumesnapshotv1.SchemeGroupVersion.WithKind("VolumeSnapshot"): {},
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := syncertesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*volumeSnapshotSyncer).Sync(syncCtx, synccontext.NewSyncEvent(pPVSourceSnapshot.DeepCopy(), vDeletingSnapshot))
assert.NilError(t, err)
},
},
})
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test.Run(t, createContext)
})
}
}
2 changes: 1 addition & 1 deletion pkg/mappings/generic/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func tryToMatchHostNameShort(ctx *synccontext.SyncContext, req types.NamespacedN
}

vNamespace := nameMapping.VirtualName.Namespace
vName := strings.Replace(req.Name, nameMapping.HostName.Name, nameMapping.VirtualName.Name, -1)
vName := strings.ReplaceAll(req.Name, nameMapping.HostName.Name, nameMapping.VirtualName.Name)
klog.FromContext(ctx).V(1).Info("Translated back name/namespace via single-namespace mode method", "req", req.String(), "ret", types.NamespacedName{
Namespace: vNamespace,
Name: vName,
Expand Down
4 changes: 2 additions & 2 deletions pkg/mappings/generic/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (n *mirrorMapper) Migrate(_ *synccontext.RegisterContext, _ synccontext.Map
return nil
}

func (n *mirrorMapper) VirtualToHost(ctx *synccontext.SyncContext, req types.NamespacedName, _ client.Object) (retName types.NamespacedName) {
func (n *mirrorMapper) VirtualToHost(ctx *synccontext.SyncContext, req types.NamespacedName, _ client.Object) types.NamespacedName {
pNamespace := req.Namespace
if pNamespace != "" {
pNamespace = translate.Default.HostNamespace(ctx, pNamespace)
Expand All @@ -47,7 +47,7 @@ func (n *mirrorMapper) VirtualToHost(ctx *synccontext.SyncContext, req types.Nam
}
}

func (n *mirrorMapper) HostToVirtual(_ *synccontext.SyncContext, req types.NamespacedName, pObj client.Object) (retName types.NamespacedName) {
func (n *mirrorMapper) HostToVirtual(_ *synccontext.SyncContext, req types.NamespacedName, pObj client.Object) types.NamespacedName {
if pObj != nil {
pAnnotations := pObj.GetAnnotations()
if pAnnotations != nil && pAnnotations[translate.NameAnnotation] != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/mappings/generic/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type recorder struct {
}

func (n *recorder) Migrate(ctx *synccontext.RegisterContext, mapper synccontext.Mapper) error {
gvk := n.Mapper.GroupVersionKind()
gvk := n.GroupVersionKind()
listGvk := schema.GroupVersionKind{
Group: gvk.Group,
Version: gvk.Version,
Expand Down
6 changes: 3 additions & 3 deletions pkg/syncer/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ type SyncTest struct {
ExpectedVirtualState map[schema.GroupVersionKind][]runtime.Object
Sync func(ctx *synccontext.RegisterContext)
Compare Compare
Name string
InitialPhysicalState []runtime.Object
InitialVirtualState []runtime.Object
AdjustConfig func(vConfig *config.VirtualClusterConfig)
pClient *testingutil.FakeIndexClient
vClient *testingutil.FakeIndexClient
vConfig *config.VirtualClusterConfig
Name string
InitialPhysicalState []runtime.Object
InitialVirtualState []runtime.Object
}

func RunTests(t *testing.T, tests []*SyncTest) {
Expand Down

0 comments on commit c7a6f4a

Please sign in to comment.