Skip to content

Commit

Permalink
refactor: controller context
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Jan 26, 2024
1 parent c406db5 commit dd9e732
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 108 deletions.
2 changes: 1 addition & 1 deletion pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (m *manager) Start(
return fmt.Errorf("start legacy plugins: %w", err)
}

err = m.pluginManager.Start(ctx, currentNamespace, targetNamespace, virtualKubeConfig, physicalKubeConfig, syncerConfig, options)
err = m.pluginManager.Start(ctx, currentNamespace, physicalKubeConfig, syncerConfig, options)
if err != nil {
return fmt.Errorf("start plugins: %w", err)
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/plugin/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import "encoding/json"

// InitConfig is the config the syncer sends to the plugin
type InitConfig struct {
VirtualClusterConfig []byte `json:"virtualClusterConfig,omitempty"`
PhysicalClusterConfig []byte `json:"physicalClusterConfig,omitempty"`
SyncerConfig []byte `json:"syncerConfig,omitempty"`
TargetNamespace string `json:"targetNamespace,omitempty"`
CurrentNamespace string `json:"currentNamespace,omitempty"`
Options []byte `json:"options,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
Expand Down
25 changes: 3 additions & 22 deletions pkg/plugin/v2/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ type vClusterPlugin struct {

func (m *Manager) Start(
ctx context.Context,
currentNamespace, targetNamespace string,
virtualKubeConfig *rest.Config,
currentNamespace string,
physicalKubeConfig *rest.Config,
syncerConfig *clientcmdapi.Config,
options *options.VirtualClusterOptions,
Expand All @@ -89,7 +88,7 @@ func (m *Manager) Start(
// after loading all plugins we start them
for _, vClusterPlugin := range m.Plugins {
// build the start request
initRequest, err := m.buildInitRequest(filepath.Dir(vClusterPlugin.Path), currentNamespace, targetNamespace, virtualKubeConfig, physicalKubeConfig, syncerConfig, options)
initRequest, err := m.buildInitRequest(filepath.Dir(vClusterPlugin.Path), currentNamespace, physicalKubeConfig, syncerConfig, options)
if err != nil {
return fmt.Errorf("build start request: %w", err)
}
Expand Down Expand Up @@ -233,9 +232,7 @@ func (m *Manager) registerClientHooks(vClusterPlugin *vClusterPlugin, clientHook

func (m *Manager) buildInitRequest(
workingDir,
currentNamespace,
targetNamespace string,
virtualKubeConfig *rest.Config,
currentNamespace string,
physicalKubeConfig *rest.Config,
syncerConfig *clientcmdapi.Config,
options *options.VirtualClusterOptions,
Expand All @@ -246,20 +243,6 @@ func (m *Manager) buildInitRequest(
return nil, fmt.Errorf("marshal options: %w", err)
}

// Virtual client config
convertedVirtualConfig, err := kubeconfig.ConvertRestConfigToClientConfig(virtualKubeConfig)
if err != nil {
return nil, fmt.Errorf("convert virtual client config: %w", err)
}
rawVirtualConfig, err := convertedVirtualConfig.RawConfig()
if err != nil {
return nil, fmt.Errorf("convert virtual client config: %w", err)
}
virtualConfigBytes, err := clientcmd.Write(rawVirtualConfig)
if err != nil {
return nil, fmt.Errorf("marshal virtual client config: %w", err)
}

// Physical client config
convertedPhysicalConfig, err := kubeconfig.ConvertRestConfigToClientConfig(physicalKubeConfig)
if err != nil {
Expand All @@ -282,10 +265,8 @@ func (m *Manager) buildInitRequest(

// marshal init config
initConfig, err := json.Marshal(&InitConfig{
VirtualClusterConfig: virtualConfigBytes,
PhysicalClusterConfig: phyisicalConfigBytes,
SyncerConfig: syncerConfigBytes,
TargetNamespace: targetNamespace,
CurrentNamespace: currentNamespace,
Options: encodedOptions,
WorkingDir: workingDir,
Expand Down
83 changes: 10 additions & 73 deletions pkg/setup/controller_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/loft-sh/vcluster/pkg/setup/options"
"github.com/loft-sh/vcluster/pkg/telemetry"
"github.com/loft-sh/vcluster/pkg/util/blockingcacheclient"
"github.com/loft-sh/vcluster/pkg/util/kubeconfig"
"github.com/loft-sh/vcluster/pkg/util/pluginhookclient"
"github.com/loft-sh/vcluster/pkg/util/toleration"
"github.com/loft-sh/vcluster/pkg/util/translate"
Expand Down Expand Up @@ -51,8 +50,8 @@ func NewControllerContext(
return nil, err
}

// init managers
localManager, virtualClusterManager, virtualRawConfig, err := InitManagers(
// create controller context
return InitManagers(
ctx,
options,
currentNamespace,
Expand All @@ -61,12 +60,6 @@ func NewControllerContext(
pluginhookclient.NewPhysicalPluginClientFactory(blockingcacheclient.NewCacheClient),
pluginhookclient.NewVirtualPluginClientFactory(blockingcacheclient.NewCacheClient),
)
if err != nil {
return nil, err
}

// create controller context
return InitControllerContext(ctx, currentNamespace, localManager, virtualClusterManager, virtualRawConfig, options)
}

func InitManagers(
Expand All @@ -77,11 +70,11 @@ func InitManagers(
scheme *runtime.Scheme,
newPhysicalClient client.NewClientFunc,
newVirtualClient client.NewClientFunc,
) (ctrl.Manager, ctrl.Manager, *clientcmdapi.Config, error) {
) (*options.ControllerContext, error) {
// load virtual config
virtualConfig, virtualRawConfig, err := LoadVirtualConfig(ctx, options)
if err != nil {
return nil, nil, nil, err
return nil, err
}

// is multi namespace mode?
Expand All @@ -102,7 +95,7 @@ func InitManagers(
// start plugins
err = StartPlugins(ctx, currentNamespace, inClusterConfig, virtualConfig, virtualRawConfig, options)
if err != nil {
return nil, nil, nil, err
return nil, err
}

// create physical manager
Expand All @@ -115,7 +108,7 @@ func InitManagers(
NewClient: newPhysicalClient,
})
if err != nil {
return nil, nil, nil, err
return nil, err
}

// create virtual manager
Expand All @@ -126,10 +119,11 @@ func InitManagers(
NewClient: newVirtualClient,
})
if err != nil {
return nil, nil, nil, err
return nil, err
}

return localManager, virtualClusterManager, virtualRawConfig, nil
// init controller context
return InitControllerContext(ctx, currentNamespace, localManager, virtualClusterManager, virtualRawConfig, options)
}

func StartPlugins(
Expand All @@ -140,6 +134,7 @@ func StartPlugins(
virtualRawConfig *clientcmdapi.Config,
options *options.VirtualClusterOptions,
) error {
// start plugins only if they are not disabled
if !options.DisablePlugins {
klog.Infof("Start Plugins Manager...")
syncerConfig, err := CreateVClusterKubeConfig(virtualRawConfig, options)
Expand Down Expand Up @@ -309,64 +304,6 @@ func CreateVClusterKubeConfig(config *clientcmdapi.Config, options *options.Virt
return config, nil
}

func WriteKubeConfigToSecret(ctx context.Context, currentNamespace string, currentNamespaceClient client.Client, options *options.VirtualClusterOptions, config *clientcmdapi.Config, isRemote bool) error {
config, err := CreateVClusterKubeConfig(config, options)
if err != nil {
return err
}

if options.KubeConfigContextName != "" {
config.CurrentContext = options.KubeConfigContextName
// update authInfo
for k := range config.AuthInfos {
config.AuthInfos[options.KubeConfigContextName] = config.AuthInfos[k]
if k != options.KubeConfigContextName {
delete(config.AuthInfos, k)
}
break
}

// update cluster
for k := range config.Clusters {
config.Clusters[options.KubeConfigContextName] = config.Clusters[k]
if k != options.KubeConfigContextName {
delete(config.Clusters, k)
}
break
}

// update context
for k := range config.Contexts {
tmpCtx := config.Contexts[k]
tmpCtx.Cluster = options.KubeConfigContextName
tmpCtx.AuthInfo = options.KubeConfigContextName
config.Contexts[options.KubeConfigContextName] = tmpCtx
if k != options.KubeConfigContextName {
delete(config.Contexts, k)
}
break
}
}

// check if we need to write the kubeconfig secrete to the default location as well
if options.KubeConfigSecret != "" {
// which namespace should we create the additional secret in?
secretNamespace := options.KubeConfigSecretNamespace
if secretNamespace == "" {
secretNamespace = currentNamespace
}

// write the extra secret
err = kubeconfig.WriteKubeConfig(ctx, currentNamespaceClient, options.KubeConfigSecret, secretNamespace, config, isRemote)
if err != nil {
return fmt.Errorf("creating %s secret in the %s ns failed: %w", options.KubeConfigSecret, secretNamespace, err)
}
}

// write the default Secret
return kubeconfig.WriteKubeConfig(ctx, currentNamespaceClient, kubeconfig.GetDefaultSecretName(translate.VClusterName), currentNamespace, config, isRemote)
}

func InitControllerContext(
ctx context.Context,
currentNamespace string,
Expand Down
60 changes: 60 additions & 0 deletions pkg/setup/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import (
"github.com/loft-sh/vcluster/pkg/setup/options"
"github.com/loft-sh/vcluster/pkg/specialservices"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/kubeconfig"
"github.com/loft-sh/vcluster/pkg/util/loghelper"
"github.com/loft-sh/vcluster/pkg/util/translate"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -203,3 +205,61 @@ func RegisterOrDeregisterAPIService(ctx *options.ControllerContext) {
klog.Errorf("Error registering metrics apiservice: %v", err)
}
}

func WriteKubeConfigToSecret(ctx context.Context, currentNamespace string, currentNamespaceClient client.Client, options *options.VirtualClusterOptions, config *clientcmdapi.Config, isRemote bool) error {
config, err := CreateVClusterKubeConfig(config, options)
if err != nil {
return err
}

if options.KubeConfigContextName != "" {
config.CurrentContext = options.KubeConfigContextName
// update authInfo
for k := range config.AuthInfos {
config.AuthInfos[options.KubeConfigContextName] = config.AuthInfos[k]
if k != options.KubeConfigContextName {
delete(config.AuthInfos, k)
}
break
}

// update cluster
for k := range config.Clusters {
config.Clusters[options.KubeConfigContextName] = config.Clusters[k]
if k != options.KubeConfigContextName {
delete(config.Clusters, k)
}
break
}

// update context
for k := range config.Contexts {
tmpCtx := config.Contexts[k]
tmpCtx.Cluster = options.KubeConfigContextName
tmpCtx.AuthInfo = options.KubeConfigContextName
config.Contexts[options.KubeConfigContextName] = tmpCtx
if k != options.KubeConfigContextName {
delete(config.Contexts, k)
}
break
}
}

// check if we need to write the kubeconfig secrete to the default location as well
if options.KubeConfigSecret != "" {
// which namespace should we create the additional secret in?
secretNamespace := options.KubeConfigSecretNamespace
if secretNamespace == "" {
secretNamespace = currentNamespace
}

// write the extra secret
err = kubeconfig.WriteKubeConfig(ctx, currentNamespaceClient, options.KubeConfigSecret, secretNamespace, config, isRemote)
if err != nil {
return fmt.Errorf("creating %s secret in the %s ns failed: %w", options.KubeConfigSecret, secretNamespace, err)
}
}

// write the default Secret
return kubeconfig.WriteKubeConfig(ctx, currentNamespaceClient, kubeconfig.GetDefaultSecretName(translate.VClusterName), currentNamespace, config, isRemote)
}
21 changes: 11 additions & 10 deletions pkg/setup/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,27 @@ func initialize(
options *options.VirtualClusterOptions,
certificatesDir string,
) error {
var err error
distro := constants.GetVClusterDistro()

var serviceCIDR, warning string
// retrieve service cidr
var serviceCIDR string
if distro != constants.K0SDistro {
var warning string
serviceCIDR, warning = servicecidr.GetServiceCIDR(ctx, currentNamespaceClient, currentNamespace)
if warning != "" {
klog.Warning(warning)
}
}

// check what distro are we running
switch distro {
case constants.K0SDistro:
serviceCIDR, err = servicecidr.EnsureServiceCIDRInK0sSecret(ctx, workspaceNamespaceClient, currentNamespaceClient, workspaceNamespace, currentNamespace, vClusterName)
// ensure service cidr
_, err := servicecidr.EnsureServiceCIDRInK0sSecret(ctx, workspaceNamespaceClient, currentNamespaceClient, workspaceNamespace, currentNamespace, vClusterName)
if err != nil {
return err
}

// start k0s
go func() {
// we need to run this with the parent ctx as otherwise this context will be cancelled by the wait
Expand Down Expand Up @@ -123,17 +127,14 @@ func initialize(
case constants.K8SDistro, constants.EKSDistro:
if certificatesDir != "" {
// generate k8s certificates
err = GenerateK8sCerts(ctx, currentNamespaceClient, vClusterName, currentNamespace, serviceCIDR, certificatesDir, options.ClusterDomain)
err := GenerateK8sCerts(ctx, currentNamespaceClient, vClusterName, currentNamespace, serviceCIDR, certificatesDir, options.ClusterDomain)
if err != nil {
return err
}
}
serviceCIDR, warning := servicecidr.GetServiceCIDR(ctx, currentNamespaceClient, currentNamespace)
if warning != "" {
klog.Warning(warning)
}
apiUp := make(chan struct{})

// start k8s
apiUp := make(chan struct{})
go func() {
// we need to run this with the parent ctx as otherwise this context will be cancelled by the wait
// loop in Initialize
Expand All @@ -147,7 +148,7 @@ func initialize(
case constants.Unknown:
if certificatesDir != "" {
// generate k8s certificates
err = GenerateK8sCerts(ctx, currentNamespaceClient, vClusterName, currentNamespace, serviceCIDR, certificatesDir, options.ClusterDomain)
err := GenerateK8sCerts(ctx, currentNamespaceClient, vClusterName, currentNamespace, serviceCIDR, certificatesDir, options.ClusterDomain)
if err != nil {
return err
}
Expand Down

0 comments on commit dd9e732

Please sign in to comment.