Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 15 additions & 98 deletions cmd/hostpaths/hostpaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/loft-sh/vcluster/pkg/controllers/resources/namespaces"
podtranslate "github.com/loft-sh/vcluster/pkg/controllers/resources/pods/translate"
"github.com/loft-sh/vcluster/pkg/util/clienthelper"

Expand All @@ -24,7 +23,6 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -59,8 +57,7 @@ const (
// naming format <pod_name>_<namespace>_<container_name>-<containerdID(hash, with <docker/cri>:// prefix removed)>.log
ContainerSymlinkSourceTemplate = "%s_%s_%s-%s.log"

MultiNamespaceMode = "multi-namespace-mode"
SyncerContainer = "syncer"
SyncerContainer = "syncer"

optionsKey key = iota

Expand Down Expand Up @@ -191,7 +188,15 @@ func Start(ctx context.Context, options *VirtualClusterOptions, init bool) error
return fmt.Errorf("find vcluster mode: %w", err)
}

localManager, err := ctrl.NewManager(inClusterConfig, localManagerCtrlOptions(options))
localManager, err := ctrl.NewManager(inClusterConfig, ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: "0"},
LeaderElection: false,
NewClient: pluginhookclient.NewPhysicalPluginClientFactory(blockingcacheclient.NewCacheClient),
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{options.TargetNamespace: {}},
},
})
if err != nil {
return err
}
Expand Down Expand Up @@ -228,29 +233,6 @@ func Start(ctx context.Context, options *VirtualClusterOptions, init bool) error
return mapHostPaths(ctx, localManager, virtualClusterManager)
}

func getSyncerPodSpec(ctx context.Context, kubeClient kubernetes.Interface, vclusterName, vclusterNamespace string) (*corev1.PodSpec, error) {
// try looking for the stateful set first

vclusterSts, err := kubeClient.AppsV1().StatefulSets(vclusterNamespace).Get(ctx, vclusterName, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
// try looking for deployment - in case of eks/k8s
vclusterDeploy, err := kubeClient.AppsV1().Deployments(vclusterNamespace).Get(ctx, vclusterName, metav1.GetOptions{})
if kerrors.IsNotFound(err) {
klog.Errorf("could not find vcluster either in statefulset or deployment: %v", err)
return nil, err
} else if err != nil {
klog.Errorf("error looking for vcluster deployment: %v", err)
return nil, err
}

return &vclusterDeploy.Spec.Template.Spec, nil
} else if err != nil {
return nil, err
}

return &vclusterSts.Spec.Template.Spec, nil
}

func getVclusterConfigFromSecret(ctx context.Context, kubeClient kubernetes.Interface, vclusterName, vclusterNamespace string) (*config.Config, error) {
configSecret, err := kubeClient.CoreV1().Secrets(vclusterNamespace).Get(ctx, fmt.Sprintf(configSecretNameTemplate, vclusterName), metav1.GetOptions{})
if err != nil {
Expand All @@ -273,50 +255,12 @@ func getVclusterConfigFromSecret(ctx context.Context, kubeClient kubernetes.Inte
return rawConfig, nil
}

func setMultiNamespaceMode(options *VirtualClusterOptions) {
options.MultiNamespaceMode = true
translate.Default = translate.NewMultiNamespaceTranslator(options.TargetNamespace)
}

func localManagerCtrlOptions(options *VirtualClusterOptions) manager.Options {
controllerOptions := ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: "0"},
LeaderElection: false,
NewClient: pluginhookclient.NewPhysicalPluginClientFactory(blockingcacheclient.NewCacheClient),
}

if !options.MultiNamespaceMode {
controllerOptions.Cache.DefaultNamespaces = map[string]cache.Config{options.TargetNamespace: {}}
}

return controllerOptions
}

func findVclusterModeAndSetDefaultTranslation(ctx context.Context, kubeClient kubernetes.Interface, options *VirtualClusterOptions) error {
vClusterConfig, err := getVclusterConfigFromSecret(ctx, kubeClient, options.Name, options.TargetNamespace)
if err != nil && !kerrors.IsNotFound(err) {
return err
} else if vClusterConfig != nil && vClusterConfig.Experimental.MultiNamespaceMode.Enabled {
setMultiNamespaceMode(options)
return nil
}

vclusterPodSpec, err := getSyncerPodSpec(ctx, kubeClient, options.Name, options.TargetNamespace)
if err != nil {
return err
}

for _, container := range vclusterPodSpec.Containers {
if container.Name == SyncerContainer {
// iterate over command args
for _, arg := range container.Args {
if strings.Contains(arg, MultiNamespaceMode) {
setMultiNamespaceMode(options)
return nil
}
}
}
} else if vClusterConfig != nil && vClusterConfig.Sync.ToHost.Namespaces.Enabled {
return fmt.Errorf("unsupported vCluster config. Hostpathmapper is not compatible with toHost namespace syncing (sync.toHost.namespaces)")
}

translate.Default = translate.NewSingleNamespaceTranslator(options.TargetNamespace)
Expand Down Expand Up @@ -475,10 +419,7 @@ func getPhysicalPodMap(ctx context.Context, options *VirtualClusterOptions, pMan
FieldSelector: fields.SelectorFromSet(fields.Set{
NodeIndexName: os.Getenv(HostpathMapperSelfNodeNameEnvVar),
}),
}

if !options.MultiNamespaceMode {
podListOptions.Namespace = options.TargetNamespace
Namespace: options.TargetNamespace,
}

podList := &corev1.PodList{}
Expand All @@ -487,32 +428,8 @@ func getPhysicalPodMap(ctx context.Context, options *VirtualClusterOptions, pMan
return nil, fmt.Errorf("unable to list pods: %w", err)
}

var pods []corev1.Pod
if options.MultiNamespaceMode {
// find namespaces managed by the current vcluster
nsList := &corev1.NamespaceList{}
err = pManager.GetClient().List(ctx, nsList, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{
namespaces.VClusterNamespaceAnnotation: options.TargetNamespace,
}),
})
if err != nil {
return nil, fmt.Errorf("unable to list namespaces: %w", err)
}

vclusterNamespaces := make(map[string]struct{}, len(nsList.Items))
for _, ns := range nsList.Items {
vclusterNamespaces[ns.Name] = struct{}{}
}

// Limit Pods
pods = filter(ctx, podList.Items, vclusterNamespaces)
} else {
pods = podList.Items
}

podMappings := make(PhysicalPodMap, len(pods))
for _, pPod := range pods {
podMappings := make(PhysicalPodMap, len(podList.Items))
for _, pPod := range podList.Items {
lookupName := fmt.Sprintf("%s_%s_%s", pPod.Namespace, pPod.Name, pPod.UID)

ok, err := checkIfPathExists(lookupName)
Expand Down
38 changes: 19 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ toolchain go1.24.2

require (
github.com/go-openapi/loads v0.21.2
github.com/loft-sh/vcluster v0.25.0
github.com/loft-sh/vcluster v0.26.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.1
gotest.tools v2.2.0+incompatible
k8s.io/api v0.32.1
k8s.io/apimachinery v0.32.1
k8s.io/client-go v0.32.1
k8s.io/api v0.32.8
k8s.io/apimachinery v0.32.8
k8s.io/client-go v0.32.8
k8s.io/klog/v2 v2.130.1
sigs.k8s.io/controller-runtime v0.20.1
sigs.k8s.io/yaml v1.4.0
Expand Down Expand Up @@ -87,10 +87,10 @@ require (
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/loft-sh/admin-apis v0.0.0-20240203010124-3600c1c582a8 // indirect
github.com/loft-sh/agentapi/v4 v4.0.0-alpha.6.0.20240614131646-3359da6a4818 // indirect
github.com/loft-sh/api/v4 v4.0.0-alpha.6.0.20240614134907-f9ecc1668e9e // indirect
github.com/loft-sh/apiserver v0.0.0-20240607231110-634aeeab2b36 // indirect
github.com/loft-sh/admin-apis v0.0.0-20250429193833-4e0a455c33dd // indirect
github.com/loft-sh/agentapi/v4 v4.3.0-alpha.39 // indirect
github.com/loft-sh/api/v4 v4.3.0-alpha.32 // indirect
github.com/loft-sh/apiserver v0.0.0-20250206205835-422f1d472459 // indirect
github.com/loft-sh/log v0.0.0-20240219160058-26d83ffb46ac // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -109,7 +109,7 @@ require (
github.com/oklog/run v1.0.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/prometheus/client_golang v1.20.4 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down Expand Up @@ -142,15 +142,15 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6 // indirect
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/oauth2 v0.28.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/term v0.30.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.7.0 // indirect
golang.org/x/time v0.9.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240930140551-af27646dc61f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f // indirect
Expand All @@ -161,15 +161,15 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.32.1 // indirect
k8s.io/apiserver v0.32.1 // indirect
k8s.io/cli-runtime v0.32.1 // indirect
k8s.io/component-base v0.32.1 // indirect
k8s.io/component-helpers v0.32.1 // indirect
k8s.io/kube-aggregator v0.32.1 // indirect
k8s.io/apiextensions-apiserver v0.32.8 // indirect
k8s.io/apiserver v0.32.8 // indirect
k8s.io/cli-runtime v0.32.8 // indirect
k8s.io/component-base v0.32.8 // indirect
k8s.io/component-helpers v0.32.8 // indirect
k8s.io/kube-aggregator v0.32.8 // indirect
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
k8s.io/kubectl v0.32.1 // indirect
k8s.io/metrics v0.32.1 // indirect
k8s.io/kubectl v0.32.8 // indirect
k8s.io/metrics v0.32.8 // indirect
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.0 // indirect
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
Expand Down
Loading
Loading