Skip to content

Commit

Permalink
Merge pull request #1379 from FabianKramm/main
Browse files Browse the repository at this point in the history
refactor: telemetry & helm values
  • Loading branch information
FabianKramm authored Nov 28, 2023
2 parents d02b61e + 2e880ea commit d974e95
Show file tree
Hide file tree
Showing 1,117 changed files with 53,600 additions and 9,600 deletions.
4 changes: 3 additions & 1 deletion charts/eks/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,6 @@ multiNamespaceMode:
telemetry:
disabled: false
instanceCreator: "helm"
instanceCreatorUID: ""
platformUserID: ""
platformInstanceID: ""
machineID: ""
4 changes: 3 additions & 1 deletion charts/k0s/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,6 @@ multiNamespaceMode:
telemetry:
disabled: false
instanceCreator: "helm"
instanceCreatorUID: ""
platformUserID: ""
platformInstanceID: ""
machineID: ""
4 changes: 3 additions & 1 deletion charts/k3s/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,9 @@ multiNamespaceMode:
telemetry:
disabled: false
instanceCreator: "helm"
instanceCreatorUID: ""
platformUserID: ""
platformInstanceID: ""
machineID: ""

noopSyncer:
enabled: false
Expand Down
4 changes: 3 additions & 1 deletion charts/k8s/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,6 @@ multiNamespaceMode:
telemetry:
disabled: false
instanceCreator: "helm"
instanceCreatorUID: ""
platformUserID: ""
platformInstanceID: ""
machineID: ""
29 changes: 21 additions & 8 deletions cmd/vcluster/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package cmd

import (
"context"
"fmt"
"runtime/debug"

volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
"github.com/loft-sh/vcluster/pkg/apis"
"github.com/loft-sh/vcluster/pkg/leaderelection"
"github.com/loft-sh/vcluster/pkg/setup"
"github.com/loft-sh/vcluster/pkg/setup/options"
"github.com/loft-sh/vcluster/pkg/telemetry"
telemetrytypes "github.com/loft-sh/vcluster/pkg/telemetry/types"
"github.com/loft-sh/vcluster/pkg/util/clienthelper"
"github.com/loft-sh/vcluster/pkg/util/translate"

Expand Down Expand Up @@ -49,22 +50,31 @@ func NewStartCommand() *cobra.Command {
Use: "start",
Short: "Execute the vcluster",
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
RunE: func(cobraCmd *cobra.Command, args []string) (err error) {
// start telemetry
telemetry.Start(false)
defer telemetry.Collector.Flush()

// capture errors
defer func() {
if r := recover(); r != nil {
telemetry.Collector.RecordError(cobraCmd.Context(), telemetry.PanicSeverity, fmt.Errorf("panic: %v %s", r, string(debug.Stack())))
panic(r)
} else if err != nil {
telemetry.Collector.RecordError(cobraCmd.Context(), telemetry.FatalSeverity, err)
}
}()

// execute command
return ExecuteStart(cobraCmd.Context(), vClusterOptions)
},
}

options.AddFlags(cmd.Flags(), vClusterOptions)
telemetry.Collector.SetStartCommand(cmd)
return cmd
}

func ExecuteStart(ctx context.Context, options *options.VirtualClusterOptions) error {
if telemetry.Collector.IsEnabled() {
// TODO: add code that will force events upload immediately? (in case of panic/Fail/Exit initiated from the code)
telemetry.Collector.RecordEvent(telemetry.Collector.NewEvent(telemetrytypes.EventSyncerStarted))
}

// set suffix
translate.Suffix = options.Name
if translate.Suffix == "" {
Expand Down Expand Up @@ -95,6 +105,9 @@ func ExecuteStart(ctx context.Context, options *options.VirtualClusterOptions) e
return err
}

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

// check if we should create certs
err = setup.Initialize(
ctx,
Expand Down
17 changes: 13 additions & 4 deletions cmd/vcluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"os"

"github.com/go-logr/logr"
loftlogr "github.com/loft-sh/log/logr"
"github.com/loft-sh/vcluster/cmd/vcluster/cmd"
"github.com/loft-sh/vcluster/pkg/util/log"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -24,13 +25,21 @@ import (
func main() {
// set global logger
if os.Getenv("DEBUG") == "true" {
ctrl.SetLogger(log.NewLog(0))
_ = os.Setenv("LOFT_LOG_LEVEL", "debug")
} else {
ctrl.SetLogger(log.NewLog(2))
_ = os.Setenv("LOFT_LOG_LEVEL", "info")
}

// set global logger
logger, err := loftlogr.NewLogger("vcluster")
if err != nil {
klog.Fatal(err)
}
ctrl.SetLogger(logger)
ctx := logr.NewContext(context.Background(), logger)

// create a new command and execute
err := cmd.BuildRoot().ExecuteContext(context.Background())
err = cmd.BuildRoot().ExecuteContext(ctx)
if err != nil {
klog.Fatal(err)
}
Expand Down
39 changes: 16 additions & 23 deletions cmd/vclusterctl/cmd/app/create/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"github.com/loft-sh/loftctl/v3/pkg/config"
"github.com/loft-sh/loftctl/v3/pkg/vcluster"
"github.com/loft-sh/log"
helmUtils "github.com/loft-sh/utils/pkg/helm"
"github.com/loft-sh/utils/pkg/helm/values"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/strvals"
"github.com/loft-sh/vcluster/pkg/telemetry"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/loft-sh/vcluster/pkg/util"
"github.com/loft-sh/vcluster/pkg/util/cliconfig"
"github.com/loft-sh/vcluster/pkg/values"
"golang.org/x/mod/semver"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
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 proclient.Client, virtualClusterName, targetNamespace string, log log.Logger) error {
func DeployProCluster(ctx context.Context, options *Options, proClient pro.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,14 +133,14 @@ func DeployProCluster(ctx context.Context, options *Options, proClient proclient
return nil
}

func createWithoutTemplate(ctx context.Context, proClient proclient.Client, options *Options, virtualClusterName, targetNamespace string, log log.Logger) (*managementv1.VirtualClusterInstance, error) {
func createWithoutTemplate(ctx context.Context, proClient pro.Client, options *Options, virtualClusterName, targetNamespace string, log log.Logger) (*managementv1.VirtualClusterInstance, error) {
err := validateNoTemplateOptions(options)
if err != nil {
return nil, err
}

// merge values
helmValues, err := mergeValues(options, log)
helmValues, err := mergeValues(proClient, options, log)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -202,14 +202,14 @@ func createWithoutTemplate(ctx context.Context, proClient proclient.Client, opti
return virtualClusterInstance, nil
}

func upgradeWithoutTemplate(ctx context.Context, proClient proclient.Client, options *Options, virtualClusterInstance *managementv1.VirtualClusterInstance, log log.Logger) (*managementv1.VirtualClusterInstance, error) {
func upgradeWithoutTemplate(ctx context.Context, proClient pro.Client, options *Options, virtualClusterInstance *managementv1.VirtualClusterInstance, log log.Logger) (*managementv1.VirtualClusterInstance, error) {
err := validateNoTemplateOptions(options)
if err != nil {
return nil, err
}

// merge values
helmValues, err := mergeValues(options, log)
helmValues, err := mergeValues(proClient, options, log)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -464,9 +464,9 @@ func validateTemplateOptions(options *Options) error {
return nil
}

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

func toChartOptions(options *Options, log log.Logger) (*helmUtils.ChartOptions, error) {
func toChartOptions(proClient pro.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 All @@ -540,16 +540,7 @@ func toChartOptions(options *Options, log log.Logger) (*helmUtils.ChartOptions,
options.ChartName += "-" + options.Distro
}

cliConf, err := cliconfig.GetConfig()
if err != nil {
log.Debugf("Failed to load local configuration file: %v", err.Error())
}
instanceCreatorUID := ""
if !cliConf.TelemetryDisabled {
instanceCreatorUID = telemetry.GetInstanceCreatorUID()
}

version := helmUtils.Version{}
version := values.Version{}
if options.KubernetesVersion != "" {
if options.KubernetesVersion[0] != 'v' {
options.KubernetesVersion = "v" + options.KubernetesVersion
Expand Down Expand Up @@ -578,15 +569,17 @@ func toChartOptions(options *Options, log log.Logger) (*helmUtils.ChartOptions,
options.ChartVersion = ""
}

return &helmUtils.ChartOptions{
return &values.ChartOptions{
ChartName: options.ChartName,
ChartRepo: options.ChartRepo,
ChartVersion: options.ChartVersion,
DisableIngressSync: options.DisableIngressSync,
Isolate: options.Isolate,
KubernetesVersion: version,
DisableTelemetry: cliConf.TelemetryDisabled,
DisableTelemetry: cliconfig.GetConfig(log).TelemetryDisabled,
InstanceCreatorType: "vclusterctl",
InstanceCreatorUID: instanceCreatorUID,
PlatformInstanceID: telemetry.GetPlatformInstanceID(proClient.Self()),
PlatformUserID: telemetry.GetPlatformUserID(proClient.Self()),
MachineID: telemetry.GetMachineID(log),
}, nil
}
11 changes: 6 additions & 5 deletions cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
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/util/clihelper"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -148,7 +149,7 @@ func (cmd *ConnectCmd) Run(ctx context.Context, args []string) error {
return cmd.Connect(ctx, proClient, vClusterName, args[1:])
}

func (cmd *ConnectCmd) Connect(ctx context.Context, proClient proclient.Client, vClusterName string, command []string) error {
func (cmd *ConnectCmd) Connect(ctx context.Context, proClient pro.Client, vClusterName string, command []string) error {
// validate flags
err := cmd.validateFlags()
if err != nil {
Expand Down Expand Up @@ -288,7 +289,7 @@ func (cmd *ConnectCmd) writeKubeConfig(kubeConfig *api.Config, vClusterName stri
authConfig = a
}

err = updateKubeConfig(cmd.KubeConfigContextName, clusterConfig, authConfig, true)
err = clihelper.UpdateKubeConfig(cmd.KubeConfigContextName, clusterConfig, authConfig, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -354,7 +355,7 @@ func (cmd *ConnectCmd) writeKubeConfig(kubeConfig *api.Config, vClusterName stri

func (cmd *ConnectCmd) prepare(ctx context.Context, vCluster *find.VCluster) error {
if cmd.LocalPort == 0 {
cmd.LocalPort = randomPort()
cmd.LocalPort = clihelper.RandomPort()
}

var (
Expand Down Expand Up @@ -442,7 +443,7 @@ func (cmd *ConnectCmd) getVClusterProKubeConfig(ctx context.Context, proClient p
}

// build kube config
kubeConfig, err := GetProKubeConfig(contextOptions)
kubeConfig, err := clihelper.GetProKubeConfig(contextOptions)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -508,7 +509,7 @@ func (cmd *ConnectCmd) getVClusterKubeConfig(ctx context.Context, vclusterName s
}

// get the kube config from the Secret
kubeConfig, err := GetKubeConfig(ctx, cmd.kubeClient, vclusterName, cmd.Namespace, cmd.Log)
kubeConfig, err := clihelper.GetKubeConfig(ctx, cmd.kubeClient, vclusterName, cmd.Namespace, cmd.Log)
if err != nil {
return nil, errors.Wrap(err, "failed to parse kube config")
}
Expand Down
27 changes: 9 additions & 18 deletions cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ import (
"github.com/loft-sh/vcluster/pkg/embed"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/loft-sh/vcluster/pkg/util/cliconfig"
"github.com/loft-sh/vcluster/pkg/util/clihelper"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"

loftctlUtil "github.com/loft-sh/loftctl/v3/pkg/util"
"github.com/loft-sh/utils/pkg/helm/values"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/app/create"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/loft-sh/vcluster/pkg/util"
"github.com/loft-sh/vcluster/pkg/util/servicecidr"
"github.com/loft-sh/vcluster/pkg/values"
"golang.org/x/mod/semver"

"github.com/loft-sh/log"
helmUtils "github.com/loft-sh/utils/pkg/helm"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/helm"
"github.com/loft-sh/vcluster/pkg/telemetry"
Expand Down Expand Up @@ -184,7 +184,7 @@ func (cmd *CreateCmd) Run(ctx context.Context, args []string) error {
}

output, err := exec.Command(helmBinaryPath, "version", "--client").CombinedOutput()
if errHelm := CheckHelmVersion(string(output)); errHelm != nil {
if errHelm := clihelper.CheckHelmVersion(string(output)); errHelm != nil {
return errHelm
} else if err != nil {
return err
Expand All @@ -202,7 +202,7 @@ func (cmd *CreateCmd) Run(ctx context.Context, args []string) error {
}

// load the default values
chartOptions, err := cmd.ToChartOptions(kubernetesVersion)
chartOptions, err := cmd.ToChartOptions(kubernetesVersion, cmd.log)
if err != nil {
return err
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func (cmd *CreateCmd) deployChart(ctx context.Context, vClusterName, chartValues
return nil
}

func (cmd *CreateCmd) ToChartOptions(kubernetesVersion *version.Info) (*helmUtils.ChartOptions, error) {
func (cmd *CreateCmd) ToChartOptions(kubernetesVersion *version.Info, log log.Logger) (*values.ChartOptions, error) {
if !util.Contains(cmd.Distro, create.AllowedDistros) {
return nil, fmt.Errorf("unsupported distro %s, please select one of: %s", cmd.Distro, strings.Join(create.AllowedDistros, ", "))
}
Expand All @@ -424,16 +424,7 @@ func (cmd *CreateCmd) ToChartOptions(kubernetesVersion *version.Info) (*helmUtil
cmd.localCluster = true
}

cliConf, err := cliconfig.GetConfig()
if err != nil {
cmd.log.Debugf("Failed to load local configuration file: %v", err.Error())
}
instanceCreatorUID := ""
if !cliConf.TelemetryDisabled {
instanceCreatorUID = telemetry.GetInstanceCreatorUID()
}

return &helmUtils.ChartOptions{
return &values.ChartOptions{
ChartName: cmd.ChartName,
ChartRepo: cmd.ChartRepo,
ChartVersion: cmd.ChartVersion,
Expand All @@ -443,13 +434,13 @@ func (cmd *CreateCmd) ToChartOptions(kubernetesVersion *version.Info) (*helmUtil
SyncNodes: cmd.localCluster,
NodePort: cmd.localCluster,
Isolate: cmd.Isolate,
KubernetesVersion: helmUtils.Version{
KubernetesVersion: values.Version{
Major: kubernetesVersion.Major,
Minor: kubernetesVersion.Minor,
},
DisableTelemetry: cliConf.TelemetryDisabled,
DisableTelemetry: cliconfig.GetConfig(log).TelemetryDisabled,
InstanceCreatorType: "vclusterctl",
InstanceCreatorUID: instanceCreatorUID,
MachineID: telemetry.GetMachineID(log),
}, nil
}

Expand Down
Loading

0 comments on commit d974e95

Please sign in to comment.