Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Jul 17, 2024
2 parents 0ca7e34 + 8bfe05c commit efd2428
Show file tree
Hide file tree
Showing 78 changed files with 595 additions and 799 deletions.
3 changes: 2 additions & 1 deletion cmd/vcluster/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/loft-sh/vcluster/pkg/scheme"
"github.com/loft-sh/vcluster/pkg/setup"
"github.com/loft-sh/vcluster/pkg/telemetry"
util "github.com/loft-sh/vcluster/pkg/util/context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -106,7 +107,7 @@ func ExecuteStart(ctx context.Context, options *StartOptions) error {
}

// start managers
syncers, err := setup.StartManagers(controllerCtx)
syncers, err := setup.StartManagers(util.ToRegisterContext(controllerCtx))
if err != nil {
return fmt.Errorf("start managers: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/controllers/generic/export_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ func createExporterFromConfig(ctx *synccontext.RegisterContext, config *vcluster

gvk := schema.FromAPIVersionAndKind(config.APIVersion, config.Kind)
controllerID := fmt.Sprintf("%s/%s/GenericExport", strings.ToLower(gvk.Kind), strings.ToLower(gvk.Group))

mapper, err := generic.NewNamespacedMapper(ctx, obj, translate.Default.PhysicalName)
mapper, err := generic.NewMapper(ctx, obj, translate.Default.PhysicalName)
if err != nil {
return nil, err
}
Expand Down
145 changes: 8 additions & 137 deletions pkg/controllers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,153 +9,24 @@ import (
"github.com/loft-sh/vcluster/pkg/config"
"github.com/loft-sh/vcluster/pkg/controllers/deploy"
"github.com/loft-sh/vcluster/pkg/controllers/generic"
"github.com/loft-sh/vcluster/pkg/controllers/resources/configmaps"
"github.com/loft-sh/vcluster/pkg/controllers/resources/csidrivers"
"github.com/loft-sh/vcluster/pkg/controllers/resources/csinodes"
"github.com/loft-sh/vcluster/pkg/controllers/resources/csistoragecapacities"
"github.com/loft-sh/vcluster/pkg/controllers/resources/endpoints"
"github.com/loft-sh/vcluster/pkg/controllers/resources/events"
"github.com/loft-sh/vcluster/pkg/controllers/resources/ingressclasses"
"github.com/loft-sh/vcluster/pkg/controllers/resources/ingresses"
"github.com/loft-sh/vcluster/pkg/controllers/resources/namespaces"
"github.com/loft-sh/vcluster/pkg/controllers/resources/networkpolicies"
"github.com/loft-sh/vcluster/pkg/controllers/resources/nodes"
"github.com/loft-sh/vcluster/pkg/controllers/resources/persistentvolumeclaims"
"github.com/loft-sh/vcluster/pkg/controllers/resources/persistentvolumes"
"github.com/loft-sh/vcluster/pkg/controllers/resources/poddisruptionbudgets"
"github.com/loft-sh/vcluster/pkg/controllers/resources/pods"
"github.com/loft-sh/vcluster/pkg/controllers/resources/priorityclasses"
"github.com/loft-sh/vcluster/pkg/controllers/resources/secrets"
"github.com/loft-sh/vcluster/pkg/controllers/resources/serviceaccounts"
"github.com/loft-sh/vcluster/pkg/controllers/resources/storageclasses"
"github.com/loft-sh/vcluster/pkg/controllers/resources/volumesnapshots/volumesnapshotclasses"
"github.com/loft-sh/vcluster/pkg/controllers/resources/volumesnapshots/volumesnapshotcontents"
"github.com/loft-sh/vcluster/pkg/controllers/resources/volumesnapshots/volumesnapshots"
"github.com/loft-sh/vcluster/pkg/controllers/servicesync"
"github.com/loft-sh/vcluster/pkg/controllers/syncer"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/util/blockingcacheclient"
util "github.com/loft-sh/vcluster/pkg/util/context"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

"github.com/loft-sh/vcluster/pkg/controllers/coredns"
"github.com/loft-sh/vcluster/pkg/controllers/k8sdefaultendpoint"
"github.com/loft-sh/vcluster/pkg/controllers/podsecurity"
"github.com/loft-sh/vcluster/pkg/controllers/resources/services"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/loghelper"
"github.com/pkg/errors"
)

// ExtraControllers that will be started as well
var ExtraControllers []BuildController

// BuildController is a function to build a new syncer
type BuildController func(ctx *synccontext.RegisterContext) (syncertypes.Object, error)

func getSyncers(ctx *config.ControllerContext) []BuildController {
return append([]BuildController{
isEnabled(ctx.Config.Sync.ToHost.Services.Enabled, services.New),
isEnabled(ctx.Config.Sync.ToHost.ConfigMaps.Enabled, configmaps.New),
isEnabled(ctx.Config.Sync.ToHost.Secrets.Enabled, secrets.New),
isEnabled(ctx.Config.Sync.ToHost.Endpoints.Enabled, endpoints.New),
isEnabled(ctx.Config.Sync.ToHost.Pods.Enabled, pods.New),
isEnabled(ctx.Config.Sync.FromHost.Events.Enabled, events.New),
isEnabled(ctx.Config.Sync.ToHost.PersistentVolumeClaims.Enabled, persistentvolumeclaims.New),
isEnabled(ctx.Config.Sync.ToHost.Ingresses.Enabled, ingresses.New),
isEnabled(ctx.Config.Sync.FromHost.IngressClasses.Enabled, ingressclasses.New),
isEnabled(ctx.Config.Sync.ToHost.StorageClasses.Enabled, storageclasses.New),
isEnabled(ctx.Config.Sync.FromHost.StorageClasses.Enabled == "true", storageclasses.NewHostStorageClassSyncer),
isEnabled(ctx.Config.Sync.ToHost.PriorityClasses.Enabled, priorityclasses.New),
isEnabled(ctx.Config.Sync.ToHost.PodDisruptionBudgets.Enabled, poddisruptionbudgets.New),
isEnabled(ctx.Config.Sync.ToHost.NetworkPolicies.Enabled, networkpolicies.New),
isEnabled(ctx.Config.Sync.ToHost.VolumeSnapshots.Enabled, volumesnapshotclasses.New),
isEnabled(ctx.Config.Sync.ToHost.VolumeSnapshots.Enabled, volumesnapshots.New),
isEnabled(ctx.Config.Sync.ToHost.VolumeSnapshots.Enabled, volumesnapshotcontents.New),
isEnabled(ctx.Config.Sync.ToHost.ServiceAccounts.Enabled, serviceaccounts.New),
isEnabled(ctx.Config.Sync.FromHost.CSINodes.Enabled == "true", csinodes.New),
isEnabled(ctx.Config.Sync.FromHost.CSIDrivers.Enabled == "true", csidrivers.New),
isEnabled(ctx.Config.Sync.FromHost.CSIStorageCapacities.Enabled == "true", csistoragecapacities.New),
isEnabled(ctx.Config.Experimental.MultiNamespaceMode.Enabled, namespaces.New),
persistentvolumes.New,
nodes.New,
}, ExtraControllers...)
}

func isEnabled(enabled bool, fn BuildController) BuildController {
if enabled {
return fn
}
return nil
}

func CreateSyncers(ctx *config.ControllerContext) ([]syncertypes.Object, error) {
registerContext := util.ToRegisterContext(ctx)

// register controllers for resource synchronization
syncers := []syncertypes.Object{}
for _, newSyncer := range getSyncers(ctx) {
if newSyncer == nil {
continue
}

createdController, err := newSyncer(registerContext)

name := ""
if createdController != nil {
name = createdController.Name()
}

if err != nil {
return nil, fmt.Errorf("register %s controller: %w", name, err)
}

loghelper.Infof("Start %s sync controller", name)
syncers = append(syncers, createdController)
}

return syncers, nil
}

func ExecuteInitializers(controllerCtx *config.ControllerContext, syncers []syncertypes.Object) error {
registerContext := util.ToRegisterContext(controllerCtx)

// execute the syncer init functions
for _, s := range syncers {
name := s.Name()
initializer, ok := s.(syncertypes.Initializer)
if ok {
klog.FromContext(controllerCtx.Context).V(1).Info("Execute syncer init", "syncer", name)
err := initializer.Init(registerContext)
if err != nil {
return errors.Wrapf(err, "ensure prerequisites for %s syncer", name)
}
}
}

return nil
}

func RegisterIndices(ctx *config.ControllerContext, syncers []syncertypes.Object) error {
registerContext := util.ToRegisterContext(ctx)
for _, s := range syncers {
indexRegisterer, ok := s.(syncertypes.IndicesRegisterer)
if ok {
err := indexRegisterer.RegisterIndices(registerContext)
if err != nil {
return errors.Wrapf(err, "register indices for %s syncer", s.Name())
}
}
}

return nil
}

func RegisterControllers(ctx *config.ControllerContext, syncers []syncertypes.Object) error {
registerContext := util.ToRegisterContext(ctx)

Expand All @@ -167,14 +38,14 @@ func RegisterControllers(ctx *config.ControllerContext, syncers []syncertypes.Ob

// register controller that maintains pod security standard check
if ctx.Config.Policies.PodSecurityStandard != "" {
err := RegisterPodSecurityController(ctx)
err := registerPodSecurityController(ctx)
if err != nil {
return err
}
}

// register controller that keeps CoreDNS NodeHosts config up to date
err = RegisterCoreDNSController(ctx)
err = registerCoreDNSController(ctx)
if err != nil {
return err
}
Expand All @@ -186,13 +57,13 @@ func RegisterControllers(ctx *config.ControllerContext, syncers []syncertypes.Ob
}

// register service syncer to map services between host and virtual cluster
err = RegisterServiceSyncControllers(ctx)
err = registerServiceSyncControllers(ctx)
if err != nil {
return err
}

// register generic sync controllers
err = RegisterGenericSyncController(ctx)
err = registerGenericSyncController(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -230,7 +101,7 @@ func RegisterControllers(ctx *config.ControllerContext, syncers []syncertypes.Ob
return nil
}

func RegisterGenericSyncController(ctx *config.ControllerContext) error {
func registerGenericSyncController(ctx *config.ControllerContext) error {
err := generic.CreateExporters(ctx)
if err != nil {
return err
Expand All @@ -244,7 +115,7 @@ func RegisterGenericSyncController(ctx *config.ControllerContext) error {
return nil
}

func RegisterServiceSyncControllers(ctx *config.ControllerContext) error {
func registerServiceSyncControllers(ctx *config.ControllerContext) error {
hostNamespace := ctx.Config.WorkloadTargetNamespace
if ctx.Config.Experimental.MultiNamespaceMode.Enabled {
hostNamespace = ctx.Config.WorkloadNamespace
Expand Down Expand Up @@ -371,7 +242,7 @@ func parseMapping(mappings []vclusterconfig.ServiceMapping, fromDefaultNamespace
return ret, nil
}

func RegisterCoreDNSController(ctx *config.ControllerContext) error {
func registerCoreDNSController(ctx *config.ControllerContext) error {
controller := &coredns.NodeHostsReconciler{
Client: ctx.VirtualManager.GetClient(),
Log: loghelper.New("corednsnodehosts-controller"),
Expand All @@ -383,7 +254,7 @@ func RegisterCoreDNSController(ctx *config.ControllerContext) error {
return nil
}

func RegisterPodSecurityController(ctx *config.ControllerContext) error {
func registerPodSecurityController(ctx *config.ControllerContext) error {
controller := &podsecurity.Reconciler{
Client: ctx.VirtualManager.GetClient(),
PodSecurityStandard: ctx.Config.Policies.PodSecurityStandard,
Expand Down
6 changes: 2 additions & 4 deletions pkg/controllers/resources/configmaps/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ func New(ctx *synccontext.RegisterContext) (syncer.Object, error) {
return &configMapSyncer{
NamespacedTranslator: translator.NewNamespacedTranslator(ctx, "configmap", &corev1.ConfigMap{}, mappings.ConfigMaps()),

syncAllConfigMaps: ctx.Config.Sync.ToHost.ConfigMaps.All,
multiNamespaceMode: ctx.Config.Experimental.MultiNamespaceMode.Enabled,
syncAllConfigMaps: ctx.Config.Sync.ToHost.ConfigMaps.All,
}, nil
}

type configMapSyncer struct {
translator.NamespacedTranslator

syncAllConfigMaps bool
multiNamespaceMode bool
syncAllConfigMaps bool
}

var _ syncer.IndicesRegisterer = &configMapSyncer{}
Expand Down
54 changes: 38 additions & 16 deletions pkg/controllers/resources/csistoragecapacities/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package csistoragecapacities
import (
"testing"

"github.com/loft-sh/vcluster/pkg/config"
testingutil "github.com/loft-sh/vcluster/pkg/util/testing"
"github.com/loft-sh/vcluster/pkg/util/translate"

"github.com/loft-sh/vcluster/pkg/controllers/resources/storageclasses"
Expand Down Expand Up @@ -92,7 +94,10 @@ func TestSyncHostStorageClass(t *testing.T) {
MaximumVolumeSize: resource.NewQuantity(202, resource.BinarySI),
}

generictesting.RunTests(t, []*generictesting.SyncTest{
generictesting.RunTestsWithContext(t, func(vConfig *config.VirtualClusterConfig, pClient *testingutil.FakeIndexClient, vClient *testingutil.FakeIndexClient) *synccontext.RegisterContext {
vConfig.Sync.FromHost.CSIStorageCapacities.Enabled = "true"
return generictesting.NewFakeRegisterContext(vConfig, pClient, vClient)
}, []*generictesting.SyncTest{
{
Name: "Sync Up",
InitialVirtualState: []runtime.Object{},
Expand Down Expand Up @@ -232,7 +237,10 @@ func TestSyncStorageClass(t *testing.T) {
MaximumVolumeSize: resource.NewQuantity(202, resource.BinarySI),
}

generictesting.RunTests(t, []*generictesting.SyncTest{
generictesting.RunTestsWithContext(t, func(vConfig *config.VirtualClusterConfig, pClient *testingutil.FakeIndexClient, vClient *testingutil.FakeIndexClient) *synccontext.RegisterContext {
vConfig.Sync.FromHost.CSIStorageCapacities.Enabled = "true"
return generictesting.NewFakeRegisterContext(vConfig, pClient, vClient)
}, []*generictesting.SyncTest{
{
Name: "Sync Up",
InitialVirtualState: []runtime.Object{vSCa, vSCb, labelledNode},
Expand All @@ -243,9 +251,11 @@ func TestSyncStorageClass(t *testing.T) {
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
storagev1.SchemeGroupVersion.WithKind(kind): {pObj},
},
AdjustConfig: func(vConfig *config.VirtualClusterConfig) {
vConfig.Sync.FromHost.StorageClasses.Enabled = "false"
vConfig.Sync.ToHost.StorageClasses.Enabled = true
},
Sync: func(ctx *synccontext.RegisterContext) {
ctx.Config.Sync.FromHost.StorageClasses.Enabled = "false"
ctx.Config.Sync.ToHost.StorageClasses.Enabled = true
var err error
syncCtx, sync := generictesting.FakeStartSyncer(t, ctx, storageclasses.New)
_, err = sync.(syncer.Syncer).SyncToHost(syncCtx, vSCa)
Expand All @@ -268,9 +278,11 @@ func TestSyncStorageClass(t *testing.T) {
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
storagev1.SchemeGroupVersion.WithKind(kind): {pObj},
},
AdjustConfig: func(vConfig *config.VirtualClusterConfig) {
vConfig.Sync.FromHost.StorageClasses.Enabled = "false"
vConfig.Sync.ToHost.StorageClasses.Enabled = true
},
Sync: func(ctx *synccontext.RegisterContext) {
ctx.Config.Sync.FromHost.StorageClasses.Enabled = "false"
ctx.Config.Sync.ToHost.StorageClasses.Enabled = true
var err error
syncCtx, sync := generictesting.FakeStartSyncer(t, ctx, storageclasses.New)
_, err = sync.(syncer.Syncer).SyncToHost(syncCtx, vSCa)
Expand All @@ -293,9 +305,11 @@ func TestSyncStorageClass(t *testing.T) {
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
storagev1.SchemeGroupVersion.WithKind(kind): {pObj},
},
AdjustConfig: func(vConfig *config.VirtualClusterConfig) {
vConfig.Sync.FromHost.StorageClasses.Enabled = "false"
vConfig.Sync.ToHost.StorageClasses.Enabled = true
},
Sync: func(ctx *synccontext.RegisterContext) {
ctx.Config.Sync.FromHost.StorageClasses.Enabled = "false"
ctx.Config.Sync.ToHost.StorageClasses.Enabled = true
var err error
syncCtx, sync := generictesting.FakeStartSyncer(t, ctx, storageclasses.New)
_, err = sync.(syncer.Syncer).SyncToHost(syncCtx, vSCa)
Expand All @@ -313,9 +327,11 @@ func TestSyncStorageClass(t *testing.T) {
InitialVirtualState: []runtime.Object{vObj, vSCa, vSCb, labelledNode},
ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{},
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{},
AdjustConfig: func(vConfig *config.VirtualClusterConfig) {
vConfig.Sync.FromHost.StorageClasses.Enabled = "false"
vConfig.Sync.ToHost.StorageClasses.Enabled = true
},
Sync: func(ctx *synccontext.RegisterContext) {
ctx.Config.Sync.FromHost.StorageClasses.Enabled = "false"
ctx.Config.Sync.ToHost.StorageClasses.Enabled = true
var err error
syncCtx, sync := generictesting.FakeStartSyncer(t, ctx, storageclasses.New)
_, err = sync.(syncer.Syncer).SyncToHost(syncCtx, vSCa)
Expand All @@ -338,9 +354,11 @@ func TestSyncStorageClass(t *testing.T) {
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
storagev1.SchemeGroupVersion.WithKind(kind): {pObjUpdated},
},
AdjustConfig: func(vConfig *config.VirtualClusterConfig) {
vConfig.Sync.FromHost.StorageClasses.Enabled = "false"
vConfig.Sync.ToHost.StorageClasses.Enabled = true
},
Sync: func(ctx *synccontext.RegisterContext) {
ctx.Config.Sync.FromHost.StorageClasses.Enabled = "false"
ctx.Config.Sync.ToHost.StorageClasses.Enabled = true
var err error
syncCtx, sync := generictesting.FakeStartSyncer(t, ctx, storageclasses.New)
_, err = sync.(syncer.Syncer).SyncToHost(syncCtx, vSCa)
Expand All @@ -363,9 +381,11 @@ func TestSyncStorageClass(t *testing.T) {
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
storagev1.SchemeGroupVersion.WithKind(kind): {pObj},
},
AdjustConfig: func(vConfig *config.VirtualClusterConfig) {
vConfig.Sync.FromHost.StorageClasses.Enabled = "false"
vConfig.Sync.ToHost.StorageClasses.Enabled = true
},
Sync: func(ctx *synccontext.RegisterContext) {
ctx.Config.Sync.FromHost.StorageClasses.Enabled = "false"
ctx.Config.Sync.ToHost.StorageClasses.Enabled = true
var err error
syncCtx, sync := generictesting.FakeStartSyncer(t, ctx, storageclasses.New)
_, err = sync.(syncer.Syncer).SyncToHost(syncCtx, vSCa)
Expand All @@ -388,9 +408,11 @@ func TestSyncStorageClass(t *testing.T) {
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
storagev1.SchemeGroupVersion.WithKind(kind): {pObj},
},
AdjustConfig: func(vConfig *config.VirtualClusterConfig) {
vConfig.Sync.FromHost.StorageClasses.Enabled = "false"
vConfig.Sync.ToHost.StorageClasses.Enabled = true
},
Sync: func(ctx *synccontext.RegisterContext) {
ctx.Config.Sync.FromHost.StorageClasses.Enabled = "false"
ctx.Config.Sync.ToHost.StorageClasses.Enabled = true
var err error
syncCtx, sync := generictesting.FakeStartSyncer(t, ctx, storageclasses.New)
_, err = sync.(syncer.Syncer).SyncToHost(syncCtx, vSCa)
Expand Down
Loading

0 comments on commit efd2428

Please sign in to comment.