Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improvements for plugins #1629

Merged
merged 1 commit into from
Mar 27, 2024
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
6 changes: 6 additions & 0 deletions cmd/vcluster/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func ExecuteStart(ctx context.Context, options *StartOptions) error {
return err
}

// get current namespace
vConfig.ControlPlaneConfig, vConfig.ControlPlaneNamespace, vConfig.ControlPlaneService, vConfig.WorkloadConfig, vConfig.WorkloadNamespace, vConfig.WorkloadService, err = pro.GetRemoteClient(vConfig)
if err != nil {
return err
}

// init config
err = setup.InitConfig(vConfig)
if err != nil {
Expand Down
17 changes: 11 additions & 6 deletions pkg/plugin/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import "encoding/json"

// InitConfig is the config the syncer sends to the plugin
type InitConfig struct {
Pro InitConfigPro `json:"pro,omitempty"`
PhysicalClusterConfig []byte `json:"physicalClusterConfig,omitempty"`
SyncerConfig []byte `json:"syncerConfig,omitempty"`
CurrentNamespace string `json:"currentNamespace,omitempty"`
Pro InitConfigPro `json:"pro,omitempty"`
SyncerConfig []byte `json:"syncerConfig,omitempty"`

Config []byte `json:"config,omitempty"`
Options []byte `json:"options,omitempty"`
WorkloadConfig []byte `json:"workloadConfig,omitempty"`
ControlPlaneConfig []byte `json:"controlPlaneConfig,omitempty"`

Config []byte `json:"config,omitempty"`

WorkingDir string `json:"workingDir,omitempty"`

// Legacy fields we still need to support
Options []byte `json:"options,omitempty"`
CurrentNamespace string `json:"currentNamespace,omitempty"`
PhysicalClusterConfig []byte `json:"physicalClusterConfig,omitempty"`
}

// InitConfigPro is used to signal the plugin if vCluster.Pro is enabled and what features are allowed
Expand Down
30 changes: 23 additions & 7 deletions pkg/plugin/v2/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,22 @@ func (m *Manager) buildInitRequest(
if err != nil {
return nil, fmt.Errorf("convert physical client config: %w", err)
}
phyisicalConfigBytes, err := clientcmd.Write(rawPhysicalConfig)
workloadConfigBytes, err := clientcmd.Write(rawPhysicalConfig)
if err != nil {
return nil, fmt.Errorf("marshal physical client config: %w", err)
}
convertedControlPlaneConfig, err := kubeconfig.ConvertRestConfigToClientConfig(vConfig.ControlPlaneConfig)
if err != nil {
return nil, fmt.Errorf("convert control plane client config: %w", err)
}
rawControlPlaneConfig, err := convertedControlPlaneConfig.RawConfig()
if err != nil {
return nil, fmt.Errorf("convert control plane client config: %w", err)
}
controlPlaneConfigBytes, err := clientcmd.Write(rawControlPlaneConfig)
if err != nil {
return nil, fmt.Errorf("marshal control plane client config: %w", err)
}

// Syncer client config
syncerConfigBytes, err := clientcmd.Write(*syncerConfig)
Expand All @@ -281,12 +293,16 @@ func (m *Manager) buildInitRequest(
Enabled: len(m.ProFeatures) > 0,
Features: m.ProFeatures,
},
PhysicalClusterConfig: phyisicalConfigBytes,
SyncerConfig: syncerConfigBytes,
CurrentNamespace: vConfig.WorkloadNamespace,
Config: encodedConfig,
Options: encodedLegacyOptions,
WorkingDir: workingDir,
PhysicalClusterConfig: workloadConfigBytes,

WorkloadConfig: workloadConfigBytes,
ControlPlaneConfig: controlPlaneConfigBytes,

SyncerConfig: syncerConfigBytes,
CurrentNamespace: vConfig.WorkloadNamespace,
Config: encodedConfig,
Options: encodedLegacyOptions,
WorkingDir: workingDir,
})
if err != nil {
return nil, fmt.Errorf("error encoding init config: %w", err)
Expand Down
7 changes: 0 additions & 7 deletions pkg/setup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
vclusterconfig "github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/config"
"github.com/loft-sh/vcluster/pkg/k3s"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/util/translate"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -18,12 +17,6 @@ func InitConfig(vConfig *config.VirtualClusterConfig) error {
// set global vCluster name
translate.VClusterName = vConfig.Name

// get current namespace
vConfig.ControlPlaneConfig, vConfig.ControlPlaneNamespace, vConfig.ControlPlaneService, vConfig.WorkloadConfig, vConfig.WorkloadNamespace, vConfig.WorkloadService, err = pro.GetRemoteClient(vConfig)
if err != nil {
return err
}

// set workload namespace
err = os.Setenv("NAMESPACE", vConfig.WorkloadNamespace)
if err != nil {
Expand Down
Loading