Skip to content

Commit

Permalink
Merge pull request #1565 from FabianKramm/main
Browse files Browse the repository at this point in the history
refactor: allow dependency injection for pro
  • Loading branch information
FabianKramm authored Feb 28, 2024
2 parents 9010692 + 4c35b3a commit 81b8b54
Show file tree
Hide file tree
Showing 40 changed files with 732 additions and 143 deletions.
84 changes: 57 additions & 27 deletions cmd/vcluster/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package cmd
import (
"context"
"fmt"
"os"
"runtime/debug"

"github.com/loft-sh/vcluster/pkg/leaderelection"
"github.com/loft-sh/vcluster/pkg/options"
"github.com/loft-sh/vcluster/pkg/plugin"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/scheme"
"github.com/loft-sh/vcluster/pkg/setup"
"github.com/loft-sh/vcluster/pkg/telemetry"
"github.com/loft-sh/vcluster/pkg/util/blockingcacheclient"
"github.com/loft-sh/vcluster/pkg/util/clienthelper"
"github.com/loft-sh/vcluster/pkg/util/pluginhookclient"
"github.com/loft-sh/vcluster/pkg/util/translate"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
)

func NewStartCommand() *cobra.Command {
Expand Down Expand Up @@ -47,10 +46,16 @@ func NewStartCommand() *cobra.Command {
}

options.AddFlags(cmd.Flags(), vClusterOptions)
pro.AddProFlags(cmd.Flags(), vClusterOptions)
return cmd
}

func ExecuteStart(ctx context.Context, options *options.VirtualClusterOptions) error {
err := pro.ValidateProOptions(options)
if err != nil {
return err
}

// set suffix
translate.VClusterName = options.Name
if translate.VClusterName == "" {
Expand All @@ -66,64 +71,89 @@ func ExecuteStart(ctx context.Context, options *options.VirtualClusterOptions) e
}

// get current namespace
currentNamespace, err := clienthelper.CurrentNamespace()
controlPlaneConfig, controlPlaneNamespace, controlPlaneService, workloadConfig, workloadNamespace, workloadService, err := pro.GetRemoteClient(options)
if err != nil {
return err
}
options.ServiceName = workloadService
err = os.Setenv("NAMESPACE", workloadNamespace)
if err != nil {
return fmt.Errorf("set NAMESPACE env var: %w", err)
}

// init telemetry
telemetry.Collector.Init(controlPlaneConfig, controlPlaneNamespace, options)

// initialize feature gate from environment
err = pro.LicenseInit(ctx, controlPlaneConfig, controlPlaneNamespace, options.ProOptions.ProLicenseSecret)
if err != nil {
return fmt.Errorf("init license: %w", err)
}

// set features for plugins to recognize
plugin.DefaultManager.SetProFeatures(pro.LicenseFeatures())

// get host cluster config and tweak rate-limiting configuration
inClusterConfig := ctrl.GetConfigOrDie()
inClusterConfig.QPS = 40
inClusterConfig.Burst = 80
inClusterConfig.Timeout = 0
inClusterClient, err := kubernetes.NewForConfig(inClusterConfig)
workloadClient, err := kubernetes.NewForConfig(workloadConfig)
if err != nil {
return err
}
controlPlaneClient, err := kubernetes.NewForConfig(controlPlaneConfig)
if err != nil {
return err
}

// init telemetry
telemetry.Collector.Init(inClusterConfig, currentNamespace, options)

// check if we should create certs
err = setup.Initialize(
ctx,
inClusterClient,
inClusterClient,
currentNamespace,
currentNamespace,
workloadClient,
controlPlaneClient,
workloadNamespace,
controlPlaneNamespace,
translate.VClusterName,
options,
)
if err != nil {
return err
return fmt.Errorf("initialize: %w", err)
}

// build controller context
controllerCtx, err := setup.NewControllerContext(
ctx,
options,
currentNamespace,
inClusterConfig,
workloadNamespace,
workloadConfig,
scheme.Scheme,
pluginhookclient.NewPhysicalPluginClientFactory(blockingcacheclient.NewCacheClient),
pluginhookclient.NewVirtualPluginClientFactory(blockingcacheclient.NewCacheClient),
)
if err != nil {
return err
return fmt.Errorf("create controller context: %w", err)
}

// start proxy
err = setup.StartProxy(controllerCtx)
err = setup.StartProxy(
controllerCtx,
controlPlaneNamespace,
controlPlaneService,
controlPlaneClient,
)
if err != nil {
return err
return fmt.Errorf("start proxy: %w", err)
}

// start integrated coredns
if controllerCtx.Options.ProOptions.IntegratedCoredns {
err = pro.StartIntegratedCoreDNS(controllerCtx)
if err != nil {
return fmt.Errorf("start integrated core dns: %w", err)
}
}

// start leader election + controllers
err = StartLeaderElection(controllerCtx, func() error {
return setup.StartControllers(controllerCtx)
return setup.StartControllers(controllerCtx, controlPlaneNamespace, controlPlaneService, controlPlaneConfig)
})
if err != nil {
return err
return fmt.Errorf("start controllers: %w", err)
}

<-controllerCtx.StopChan
Expand Down
12 changes: 6 additions & 6 deletions cmd/vclusterctl/cmd/app/create/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/loft-sh/loftctl/v3/pkg/vcluster"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster-values/values"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/procli"
"github.com/loft-sh/vcluster/pkg/strvals"
"github.com/loft-sh/vcluster/pkg/telemetry"
"github.com/loft-sh/vcluster/pkg/upgrade"
Expand All @@ -39,7 +39,7 @@ const LoftChartRepo = "https://charts.loft.sh"

var AllowedDistros = []string{"k3s", "k0s", "k8s", "eks"}

func DeployProCluster(ctx context.Context, options *Options, proClient pro.Client, virtualClusterName, targetNamespace string, log log.Logger) error {
func DeployProCluster(ctx context.Context, options *Options, proClient procli.Client, virtualClusterName, targetNamespace string, log log.Logger) error {
// determine project & cluster name
var err error
options.Cluster, options.Project, err = helper.SelectProjectOrCluster(proClient, options.Cluster, options.Project, false, log)
Expand Down Expand Up @@ -133,7 +133,7 @@ func DeployProCluster(ctx context.Context, options *Options, proClient pro.Clien
return nil
}

func createWithoutTemplate(ctx context.Context, proClient pro.Client, options *Options, virtualClusterName, targetNamespace string, log log.Logger) (*managementv1.VirtualClusterInstance, error) {
func createWithoutTemplate(ctx context.Context, proClient procli.Client, options *Options, virtualClusterName, targetNamespace string, log log.Logger) (*managementv1.VirtualClusterInstance, error) {
err := validateNoTemplateOptions(options)
if err != nil {
return nil, err
Expand Down Expand Up @@ -214,7 +214,7 @@ func createWithoutTemplate(ctx context.Context, proClient pro.Client, options *O
return virtualClusterInstance, nil
}

func upgradeWithoutTemplate(ctx context.Context, proClient pro.Client, options *Options, virtualClusterInstance *managementv1.VirtualClusterInstance, log log.Logger) (*managementv1.VirtualClusterInstance, error) {
func upgradeWithoutTemplate(ctx context.Context, proClient procli.Client, options *Options, virtualClusterInstance *managementv1.VirtualClusterInstance, log log.Logger) (*managementv1.VirtualClusterInstance, error) {
err := validateNoTemplateOptions(options)
if err != nil {
return nil, err
Expand Down Expand Up @@ -514,7 +514,7 @@ func validateTemplateOptions(options *Options) error {
return nil
}

func mergeValues(proClient pro.Client, options *Options, log log.Logger) (string, error) {
func mergeValues(proClient procli.Client, options *Options, log log.Logger) (string, error) {
// merge values
chartOptions, err := toChartOptions(proClient, options, log)
if err != nil {
Expand Down Expand Up @@ -581,7 +581,7 @@ func parseString(str string) (map[string]interface{}, error) {
return out, nil
}

func toChartOptions(proClient pro.Client, options *Options, log log.Logger) (*values.ChartOptions, error) {
func toChartOptions(proClient procli.Client, options *Options, log log.Logger) (*values.ChartOptions, error) {
if !util.Contains(options.Distro, AllowedDistros) {
return nil, fmt.Errorf("unsupported distro %s, please select one of: %s", options.Distro, strings.Join(AllowedDistros, ", "))
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd/use"
proclient "github.com/loft-sh/loftctl/v3/pkg/client"
"github.com/loft-sh/loftctl/v3/pkg/vcluster"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/procli"
"github.com/loft-sh/vcluster/pkg/util/clihelper"
"github.com/pkg/errors"
"github.com/samber/lo"
Expand Down Expand Up @@ -134,15 +134,15 @@ func (cmd *ConnectCmd) Run(ctx context.Context, args []string) error {
vClusterName = args[0]
}

proClient, err := pro.CreateProClient()
proClient, err := procli.CreateProClient()
if err != nil {
cmd.Log.Debugf("Error creating pro client: %v", err)
}

return cmd.Connect(ctx, proClient, vClusterName, args[1:])
}

func (cmd *ConnectCmd) Connect(ctx context.Context, proClient pro.Client, vClusterName string, command []string) error {
func (cmd *ConnectCmd) Connect(ctx context.Context, proClient procli.Client, vClusterName string, command []string) error {
// validate flags
err := cmd.validateFlags()
if err != nil {
Expand All @@ -168,7 +168,7 @@ func (cmd *ConnectCmd) validateFlags() error {
return nil
}

func (cmd *ConnectCmd) connectPro(ctx context.Context, proClient proclient.Client, vCluster *pro.VirtualClusterInstanceProject, command []string) error {
func (cmd *ConnectCmd) connectPro(ctx context.Context, proClient proclient.Client, vCluster *procli.VirtualClusterInstanceProject, command []string) error {
err := cmd.validateProFlags()
if err != nil {
return err
Expand Down Expand Up @@ -397,7 +397,7 @@ func (cmd *ConnectCmd) prepare(ctx context.Context, vCluster *find.VCluster) err
return nil
}

func (cmd *ConnectCmd) getVClusterProKubeConfig(ctx context.Context, proClient proclient.Client, vCluster *pro.VirtualClusterInstanceProject) (*clientcmdapi.Config, error) {
func (cmd *ConnectCmd) getVClusterProKubeConfig(ctx context.Context, proClient proclient.Client, vCluster *procli.VirtualClusterInstanceProject) (*clientcmdapi.Config, error) {
contextOptions, err := use.CreateVirtualClusterInstanceOptions(ctx, proClient, "", vCluster.Project.Name, vCluster.VirtualCluster, false, false, cmd.Log)
if err != nil {
return nil, fmt.Errorf("prepare vCluster kube config: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/app/localkubernetes"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/find"
"github.com/loft-sh/vcluster/pkg/embed"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/procli"
"github.com/loft-sh/vcluster/pkg/util/cliconfig"
"github.com/loft-sh/vcluster/pkg/util/clihelper"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -137,7 +137,7 @@ func (cmd *CreateCmd) Run(ctx context.Context, args []string) error {

// check if we should create a pro cluster
if !cmd.DisablePro {
proClient, err := pro.CreateProClient()
proClient, err := procli.CreateProClient()
if err == nil {
// deploy pro cluster
err = create.DeployProCluster(ctx, &cmd.Options, proClient, args[0], cmd.Namespace, cmd.log)
Expand Down
6 changes: 3 additions & 3 deletions cmd/vclusterctl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
loftctlUtil "github.com/loft-sh/loftctl/v3/pkg/util"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/app/localkubernetes"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/find"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/procli"
"github.com/loft-sh/vcluster/pkg/util/clihelper"
"github.com/loft-sh/vcluster/pkg/util/translate"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -88,7 +88,7 @@ func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error {
ctx := cobraCmd.Context()

// get pro client
proClient, err := pro.CreateProClient()
proClient, err := procli.CreateProClient()
if err != nil {
cmd.log.Debugf("Error creating pro client: %v", err)
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error {
return nil
}

func (cmd *DeleteCmd) deleteProVCluster(ctx context.Context, proClient proclient.Client, vCluster *pro.VirtualClusterInstanceProject) error {
func (cmd *DeleteCmd) deleteProVCluster(ctx context.Context, proClient proclient.Client, vCluster *procli.VirtualClusterInstanceProject) error {
managementClient, err := proClient.Management()
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions cmd/vclusterctl/cmd/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/loft-sh/log"
"github.com/loft-sh/log/survey"
"github.com/loft-sh/log/terminal"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/procli"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/loft-sh/vcluster/pkg/constants"
Expand Down Expand Up @@ -67,7 +67,7 @@ func CurrentContext() (string, *clientcmdapi.Config, error) {
return rawConfig.CurrentContext, &rawConfig, nil
}

func GetVCluster(ctx context.Context, proClient pro.Client, context, name, namespace, project string, log log.Logger) (*VCluster, *pro.VirtualClusterInstanceProject, error) {
func GetVCluster(ctx context.Context, proClient procli.Client, context, name, namespace, project string, log log.Logger) (*VCluster, *procli.VirtualClusterInstanceProject, error) {
if name == "" {
return nil, nil, fmt.Errorf("please specify a name")
}
Expand Down Expand Up @@ -156,7 +156,7 @@ func FormatOptions(format string, options [][]string) []string {
return retOptions
}

func ListVClusters(ctx context.Context, proClient pro.Client, context, name, namespace, project string, log log.Logger) ([]VCluster, []pro.VirtualClusterInstanceProject, error) {
func ListVClusters(ctx context.Context, proClient procli.Client, context, name, namespace, project string, log log.Logger) ([]VCluster, []procli.VirtualClusterInstanceProject, error) {
var err error
if context == "" {
var err error
Expand All @@ -174,9 +174,9 @@ func ListVClusters(ctx context.Context, proClient pro.Client, context, name, nam
}
}

var proVClusters []pro.VirtualClusterInstanceProject
var proVClusters []procli.VirtualClusterInstanceProject
if proClient != nil {
proVClusters, err = pro.ListVClusters(ctx, proClient, name, project)
proVClusters, err = procli.ListVClusters(ctx, proClient, name, project)
if err != nil {
log.Warnf("Error retrieving pro vclusters: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/vclusterctl/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/loft-sh/log/terminal"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/find"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/procli"
"github.com/loft-sh/vcluster/pkg/util/compress"
"github.com/mgutz/ansi"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -56,7 +56,7 @@ vcluster import my-vcluster --cluster connected-cluster \
Long: description,
Args: loftctlUtil.VClusterNameOnlyValidator,
RunE: func(cobraCmd *cobra.Command, args []string) error {
proClient, err := pro.CreateProClient()
proClient, err := procli.CreateProClient()
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/vclusterctl/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime"

"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/procli"
"github.com/loft-sh/vcluster/pkg/telemetry"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -43,7 +43,7 @@ vcluster info
Arch: runtime.GOARCH,
MachineID: telemetry.GetMachineID(log.GetInstance()),
}
proClient, err := pro.CreateProClient()
proClient, err := procli.CreateProClient()
if err == nil {
infos.InstanceID = proClient.Self().Status.InstanceID
}
Expand Down
Loading

0 comments on commit 81b8b54

Please sign in to comment.