-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6260c64
commit fe98a15
Showing
9 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package runtimeclasses | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/loft-sh/vcluster/pkg/mappings/generic" | ||
"github.com/loft-sh/vcluster/pkg/patcher" | ||
"github.com/loft-sh/vcluster/pkg/syncer" | ||
"github.com/loft-sh/vcluster/pkg/syncer/synccontext" | ||
syncertypes "github.com/loft-sh/vcluster/pkg/syncer/types" | ||
"github.com/loft-sh/vcluster/pkg/util/translate" | ||
nodev1 "k8s.io/api/node/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func New(_ *synccontext.RegisterContext) (syncertypes.Object, error) { | ||
mapper, err := generic.NewMirrorMapper(&nodev1.RuntimeClass{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &runtimeClassSyncer{ | ||
Mapper: mapper, | ||
}, nil | ||
} | ||
|
||
type runtimeClassSyncer struct { | ||
synccontext.Mapper | ||
} | ||
|
||
func (i *runtimeClassSyncer) Name() string { | ||
return "runtimeclass" | ||
} | ||
|
||
func (i *runtimeClassSyncer) Resource() client.Object { | ||
return &nodev1.RuntimeClass{} | ||
} | ||
|
||
var _ syncertypes.Syncer = &runtimeClassSyncer{} | ||
|
||
func (i *runtimeClassSyncer) Syncer() syncertypes.Sync[client.Object] { | ||
return syncer.ToGenericSyncer[*nodev1.RuntimeClass](i) | ||
} | ||
|
||
func (i *runtimeClassSyncer) SyncToVirtual(ctx *synccontext.SyncContext, event *synccontext.SyncToVirtualEvent[*nodev1.RuntimeClass]) (ctrl.Result, error) { | ||
vObj := translate.CopyObjectWithName(event.Host, types.NamespacedName{Name: event.Host.Name, Namespace: event.Host.Namespace}, false) | ||
ctx.Log.Infof("create runtime class %s, because it does not exist in virtual cluster", vObj.Name) | ||
return ctrl.Result{}, ctx.VirtualClient.Create(ctx, vObj) | ||
} | ||
|
||
func (i *runtimeClassSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.SyncEvent[*nodev1.RuntimeClass]) (_ ctrl.Result, retErr error) { | ||
patch, err := patcher.NewSyncerPatcher(ctx, event.Host, event.Virtual) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("new syncer patcher: %w", err) | ||
} | ||
defer func() { | ||
if err := patch.Patch(ctx, event.Host, event.Virtual); err != nil { | ||
retErr = utilerrors.NewAggregate([]error{retErr, err}) | ||
} | ||
}() | ||
|
||
event.Virtual.Annotations = event.Host.Annotations | ||
event.Virtual.Labels = event.Host.Labels | ||
event.Virtual.Handler = event.Host.Handler | ||
event.Virtual.Overhead = event.Host.Overhead | ||
event.Virtual.Scheduling = event.Host.Scheduling | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (i *runtimeClassSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.SyncToHostEvent[*nodev1.RuntimeClass]) (ctrl.Result, error) { | ||
ctx.Log.Infof("delete virtual runtime class %s, because physical object is missing", event.Virtual.Name) | ||
return ctrl.Result{}, ctx.VirtualClient.Delete(ctx, event.Virtual) | ||
} |
137 changes: 137 additions & 0 deletions
137
pkg/controllers/resources/runtimeclasses/syncer_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package runtimeclasses | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/loft-sh/vcluster/pkg/syncer/synccontext" | ||
syncertesting "github.com/loft-sh/vcluster/pkg/syncer/testing" | ||
"github.com/loft-sh/vcluster/pkg/util/translate" | ||
"gotest.tools/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
nodev1 "k8s.io/api/node/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
func TestSync(t *testing.T) { | ||
vObjectMeta := metav1.ObjectMeta{ | ||
Name: "test-ingc", | ||
Annotations: map[string]string{ | ||
translate.NameAnnotation: "test-runtimec", | ||
translate.UIDAnnotation: "", | ||
translate.KindAnnotation: nodev1.SchemeGroupVersion.WithKind("RuntimeClass").String(), | ||
}, | ||
} | ||
|
||
vObj := &nodev1.RuntimeClass{ | ||
ObjectMeta: vObjectMeta, | ||
Scheduling: &nodev1.Scheduling{ | ||
NodeSelector: map[string]string{"stuff": "stuff"}, | ||
}, | ||
Handler: "somehandler", | ||
Overhead: &nodev1.Overhead{ | ||
PodFixed: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, | ||
} | ||
|
||
pObj := &nodev1.RuntimeClass{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: vObjectMeta.Name, | ||
Labels: map[string]string{ | ||
translate.MarkerLabel: translate.VClusterName, | ||
}, | ||
Annotations: map[string]string{ | ||
translate.NameAnnotation: "test-runtimec", | ||
translate.UIDAnnotation: "", | ||
translate.KindAnnotation: nodev1.SchemeGroupVersion.WithKind("RuntimeClass").String(), | ||
}, | ||
}, | ||
Scheduling: &nodev1.Scheduling{ | ||
NodeSelector: map[string]string{"stuff": "stuff"}, | ||
}, | ||
Handler: "somehandler", | ||
Overhead: &nodev1.Overhead{ | ||
PodFixed: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, | ||
} | ||
|
||
vObjUpdated := &nodev1.RuntimeClass{ | ||
ObjectMeta: vObjectMeta, | ||
Scheduling: &nodev1.Scheduling{ | ||
NodeSelector: map[string]string{"stuff": "stuff2"}, | ||
}, | ||
Handler: "somehandler", | ||
Overhead: &nodev1.Overhead{ | ||
PodFixed: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, | ||
} | ||
|
||
pObjUpdated := &nodev1.RuntimeClass{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: translate.Default.HostNameCluster(vObjectMeta.Name), | ||
Labels: map[string]string{ | ||
translate.MarkerLabel: translate.VClusterName, | ||
}, | ||
Annotations: map[string]string{ | ||
translate.NameAnnotation: "test-runtimec", | ||
translate.UIDAnnotation: "", | ||
translate.KindAnnotation: nodev1.SchemeGroupVersion.WithKind("RuntimeClass").String(), | ||
}, | ||
}, | ||
Scheduling: &nodev1.Scheduling{ | ||
NodeSelector: map[string]string{"stuff": "stuff2"}, | ||
}, | ||
Handler: "somehandler", | ||
Overhead: &nodev1.Overhead{ | ||
PodFixed: corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("1")}, | ||
}, | ||
} | ||
|
||
syncertesting.RunTests(t, []*syncertesting.SyncTest{ | ||
{ | ||
Name: "Import", | ||
InitialVirtualState: []runtime.Object{}, | ||
InitialPhysicalState: []runtime.Object{pObj}, | ||
ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{ | ||
nodev1.SchemeGroupVersion.WithKind("RuntimeClass"): {vObj}, | ||
}, | ||
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{ | ||
nodev1.SchemeGroupVersion.WithKind("RuntimeClass"): {pObj}, | ||
}, | ||
Sync: func(ctx *synccontext.RegisterContext) { | ||
syncCtx, syncer := syncertesting.FakeStartSyncer(t, ctx, New) | ||
_, err := syncer.(*runtimeClassSyncer).SyncToVirtual(syncCtx, synccontext.NewSyncToVirtualEvent(pObj)) | ||
assert.NilError(t, err) | ||
}, | ||
}, | ||
{ | ||
Name: "Delete virtual", | ||
InitialVirtualState: []runtime.Object{vObj}, | ||
ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{}, | ||
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{}, | ||
Sync: func(ctx *synccontext.RegisterContext) { | ||
syncCtx, syncer := syncertesting.FakeStartSyncer(t, ctx, New) | ||
_, err := syncer.(*runtimeClassSyncer).SyncToHost(syncCtx, synccontext.NewSyncToHostEvent(vObj)) | ||
assert.NilError(t, err) | ||
}, | ||
}, | ||
{ | ||
Name: "Sync", | ||
InitialVirtualState: []runtime.Object{vObj}, | ||
InitialPhysicalState: []runtime.Object{pObjUpdated}, | ||
ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{ | ||
nodev1.SchemeGroupVersion.WithKind("RuntimeClass"): {vObjUpdated}, | ||
}, | ||
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{ | ||
nodev1.SchemeGroupVersion.WithKind("RuntimeClass"): {pObjUpdated}, | ||
}, | ||
Sync: func(ctx *synccontext.RegisterContext) { | ||
syncCtx, syncer := syncertesting.FakeStartSyncer(t, ctx, New) | ||
_, err := syncer.(*runtimeClassSyncer).Sync(syncCtx, synccontext.NewSyncEvent(pObjUpdated, vObj)) | ||
assert.NilError(t, err) | ||
}, | ||
}, | ||
}) | ||
} |