Skip to content

Commit

Permalink
fix: potential nil pointer in telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Dec 12, 2023
1 parent d55f103 commit aaa79f5
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pkg/telemetry/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/loft-sh/analytics-client/client"
managementv1 "github.com/loft-sh/api/v3/pkg/apis/management/v1"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/registry"
"github.com/loft-sh/vcluster/pkg/setup/options"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/loft-sh/vcluster/pkg/util/clihelper"
Expand Down Expand Up @@ -101,7 +102,7 @@ type DefaultCollector struct {
vClusterID cachedValue[string]
hostClusterVersion cachedValue[*KubernetesVersion]
virtualClusterVersion cachedValue[*KubernetesVersion]
chartInfo cachedValue[*ChartInfo]
chartInfo cachedValue[*registry.ChartInfo]

// everything below will be set during runtime
virtualClient *kubernetes.Clientset
Expand Down Expand Up @@ -193,15 +194,18 @@ func (d *DefaultCollector) RecordStatus(ctx context.Context) {
}

func (d *DefaultCollector) RecordStart(ctx context.Context) {
chartInfo := d.getChartInfo(ctx)
properties := map[string]interface{}{
"vcluster_version": SyncerVersion,
"vcluster_k8s_distro": d.getChartInfo(ctx).Name,
"vcluster_k8s_distro_version": d.getVirtualClusterVersion(),
"host_cluster_k8s_version": d.getHostClusterVersion(),
"os_arch": runtime.GOOS + "/" + runtime.GOARCH,
"helm_values": d.getChartInfo(ctx).Values,
"creation_method": d.config.InstanceCreator,
}
if chartInfo != nil {
properties["vcluster_k8s_distro"] = chartInfo.Name
properties["helm_values"] = chartInfo.Values
}

// build the event and record
propertiesRaw, _ := json.Marshal(properties)
Expand Down Expand Up @@ -231,7 +235,10 @@ func (d *DefaultCollector) RecordError(ctx context.Context, severity ErrorSeveri

// if panic or fatal we add the helm values
if severity == PanicSeverity || severity == FatalSeverity {
properties["helm_values"] = d.getChartInfo(ctx).Values
chartInfo := d.getChartInfo(ctx)
if chartInfo != nil {
properties["helm_values"] = chartInfo.Values
}
}

// build the event and record
Expand Down Expand Up @@ -271,9 +278,9 @@ func (d *DefaultCollector) getHostClusterVersion() *KubernetesVersion {
return hostVersion
}

func (d *DefaultCollector) getChartInfo(ctx context.Context) *ChartInfo {
chartInfo, err := d.chartInfo.Get(func() (*ChartInfo, error) {
return getChartInfo(ctx, d.hostClient, d.hostNamespace)
func (d *DefaultCollector) getChartInfo(ctx context.Context) *registry.ChartInfo {
chartInfo, err := d.chartInfo.Get(func() (*registry.ChartInfo, error) {
return registry.GetChartInfo(ctx, d.hostClient, d.hostNamespace)
})
if err != nil {
klog.V(1).ErrorS(err, "Error retrieving chart info")
Expand Down

0 comments on commit aaa79f5

Please sign in to comment.