From eeb0e87e1ac5c48d1a7c29dda4c700b382811a7c Mon Sep 17 00:00:00 2001 From: Jared Edwards Date: Sun, 22 Oct 2023 01:48:22 -0600 Subject: [PATCH] adjust events signature (#218) * adjust SendEvent errors * two missed errors on telemetry send * first stab at test after new clients * fix error handling telemetry in vault * missed error adjustments * adding segment client instance --------- Co-authored-by: John Dietz --- internal/controller/argocd.go | 18 +++++++++++------- internal/controller/cluster.go | 9 ++++++--- internal/controller/controller.go | 25 +++++++++---------------- internal/controller/domain.go | 10 +++++++--- internal/controller/git.go | 9 ++++++--- internal/controller/kbot.go | 5 ++++- internal/controller/repository.go | 11 +++++++---- internal/controller/state.go | 24 ++++++++++++++++-------- internal/controller/users.go | 9 ++++++--- internal/controller/vault.go | 17 +++++++++++------ internal/router/api/v1/telemetry.go | 2 +- internal/telemetry/heartbeat.go | 14 +++++++++----- providers/aws/create.go | 5 ++++- providers/aws/delete.go | 7 +++++-- providers/civo/create.go | 5 ++++- providers/civo/delete.go | 7 +++++-- providers/digitalocean/create.go | 5 ++++- providers/digitalocean/delete.go | 5 ++++- providers/google/create.go | 5 ++++- providers/google/delete.go | 7 +++++-- providers/vultr/create.go | 5 ++++- providers/vultr/delete.go | 7 +++++-- 22 files changed, 137 insertions(+), 74 deletions(-) diff --git a/internal/controller/argocd.go b/internal/controller/argocd.go index 1a9e3002..472cece5 100644 --- a/internal/controller/argocd.go +++ b/internal/controller/argocd.go @@ -13,6 +13,7 @@ import ( argocdapi "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned" awsext "github.com/kubefirst/kubefirst-api/extensions/aws" + "github.com/kubefirst/kubefirst-api/pkg/segment" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg" "github.com/kubefirst/runtime/pkg/argocd" @@ -38,6 +39,7 @@ func (clctrl *ClusterController) InstallArgoCD() error { } if !cl.ArgoCDInstallCheck { + var kcfg *k8s.KubernetesClient switch clctrl.CloudProvider { @@ -56,16 +58,16 @@ func (clctrl *ClusterController) InstallArgoCD() error { argoCDInstallPath := fmt.Sprintf("github.com:kubefirst/manifests/argocd/cloud?ref=%s", pkg.KubefirstManifestRepoRef) log.Infof("installing argocd") - telemetry.SendEvent(clctrl.SegmentClient, telemetry.ArgoCDInstallStarted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.ArgoCDInstallStarted, "") err = argocd.ApplyArgoCDKustomize(kcfg.Clientset, argoCDInstallPath) if err != nil { - - telemetry.SendEvent(clctrl.SegmentClient, telemetry.ArgoCDInstallFailed, err.Error()) - + telemetry.SendEvent(segClient, telemetry.ArgoCDInstallFailed, err.Error()) return err } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.ArgoCDInstallCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.ArgoCDInstallCompleted, "") // Wait for ArgoCD to be ready _, err = k8s.VerifyArgoCDReadiness(kcfg.Clientset, true, 300) @@ -194,7 +196,9 @@ func (clctrl *ClusterController) DeployRegistryApplication() error { } } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.CreateRegistryStarted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.CreateRegistryStarted, "") argocdClient, err := argocdapi.NewForConfig(kcfg.RestConfig) if err != nil { return err @@ -239,7 +243,7 @@ func (clctrl *ClusterController) DeployRegistryApplication() error { _, _ = argocdClient.ArgoprojV1alpha1().Applications("argocd").Create(context.Background(), registryApplicationObject, metav1.CreateOptions{}) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.CreateRegistryCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.CreateRegistryCompleted, "") err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "argocd_create_registry_check", true) if err != nil { diff --git a/internal/controller/cluster.go b/internal/controller/cluster.go index 8feedb7c..1aadf709 100644 --- a/internal/controller/cluster.go +++ b/internal/controller/cluster.go @@ -20,6 +20,7 @@ import ( vultrext "github.com/kubefirst/kubefirst-api/extensions/vultr" gitShim "github.com/kubefirst/kubefirst-api/internal/gitShim" "github.com/kubefirst/kubefirst-api/pkg/providerConfigs" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" @@ -50,7 +51,9 @@ func (clctrl *ClusterController) CreateCluster() error { tfEntrypoint := clctrl.ProviderConfig.GitopsDir + fmt.Sprintf("/terraform/%s", clctrl.CloudProvider) tfEnvs := map[string]string{} - telemetry.SendEvent(clctrl.SegmentClient, telemetry.CloudTerraformApplyStarted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.CloudTerraformApplyStarted, "") log.Infof("creating %s cluster", clctrl.CloudProvider) @@ -80,18 +83,18 @@ func (clctrl *ClusterController) CreateCluster() error { err := terraformext.InitApplyAutoApprove(clctrl.ProviderConfig.TerraformClient, tfEntrypoint, tfEnvs) if err != nil { + telemetry.SendEvent(segClient, telemetry.CloudTerraformApplyFailed, err.Error()) msg := fmt.Sprintf("error creating %s resources with terraform %s: %s", clctrl.CloudProvider, tfEntrypoint, err) log.Error(msg) err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "cloud_terraform_apply_failed_check", true) if err != nil { return err } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.CloudTerraformApplyFailed, err.Error()) return fmt.Errorf(msg) } log.Infof("created %s cloud resources", clctrl.CloudProvider) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.CloudTerraformApplyCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.CloudTerraformApplyCompleted, "") err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "cloud_terraform_apply_failed_check", false) if err != nil { diff --git a/internal/controller/controller.go b/internal/controller/controller.go index 73568ca3..fe537c0d 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -145,12 +145,6 @@ func (clctrl *ClusterController) InitController(def *pkgtypes.ClusterDefinition) clusterID = runtime.GenerateClusterID() } - if os.Getenv("USE_TELEMETRY") == "false" { - clctrl.SegmentClient.UseTelemetry = false - } else { - clctrl.SegmentClient.UseTelemetry = true - } - // Telemetry handle machineID, _ := machineid.ID() @@ -173,15 +167,20 @@ func (clctrl *ClusterController) InitController(def *pkgtypes.ClusterDefinition) }, Client: analytics.New(telemetry.SegmentIOWriteKey), } - if err != nil { - log.Warn(err) + clctrl.SegmentClient = &segClient + + if os.Getenv("USE_TELEMETRY") == "false" { + clctrl.SegmentClient.UseTelemetry = false + } else { + clctrl.SegmentClient.UseTelemetry = true } + log.Info(segClient) + defer segClient.Client.Close() if clctrl.SegmentClient.UseTelemetry { - telemetry.SendEvent(&segClient, telemetry.ClusterInstallStarted, err.Error()) + telemetry.SendEvent(&segClient, telemetry.ClusterInstallStarted, "") } - clctrl.SegmentClient = &segClient //Copy Cluster Definiion to Cluster Controller clctrl.AlertsEmail = def.AdminEmail @@ -289,12 +288,6 @@ func (clctrl *ClusterController) InitController(def *pkgtypes.ClusterDefinition) } - if os.Getenv("USE_TELEMETRY") == "false" { - clctrl.SegmentClient.UseTelemetry = false - } else { - clctrl.SegmentClient.UseTelemetry = true - } - // Write cluster record if it doesn't exist cl := pkgtypes.Cluster{ ID: primitive.NewObjectID(), diff --git a/internal/controller/domain.go b/internal/controller/domain.go index c1fe52fb..8968a750 100644 --- a/internal/controller/domain.go +++ b/internal/controller/domain.go @@ -11,6 +11,7 @@ import ( "fmt" cloudflare_api "github.com/cloudflare/cloudflare-go" + "github.com/kubefirst/kubefirst-api/pkg/segment" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/civo" "github.com/kubefirst/runtime/pkg/digitalocean" @@ -26,8 +27,11 @@ func (clctrl *ClusterController) DomainLivenessTest() error { return err } + segClient := segment.InitClient() + defer segClient.Client.Close() + if !cl.DomainLivenessCheck { - telemetry.SendEvent(clctrl.SegmentClient, telemetry.DomainLivenessStarted, err.Error()) + telemetry.SendEvent(segClient, telemetry.DomainLivenessStarted, "") switch clctrl.DnsProvider { case "aws": @@ -46,7 +50,7 @@ func (clctrl *ClusterController) DomainLivenessTest() error { // domain id domainId, err := civoConf.GetDNSInfo(clctrl.DomainName, clctrl.CloudRegion) if err != nil { - telemetry.SendEvent(clctrl.SegmentClient, telemetry.DomainLivenessFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.DomainLivenessFailed, err.Error()) log.Info(err.Error()) } @@ -121,7 +125,7 @@ func (clctrl *ClusterController) DomainLivenessTest() error { return err } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.DomainLivenessCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.DomainLivenessCompleted, "") log.Infof("domain %s verified", clctrl.DomainName) } diff --git a/internal/controller/git.go b/internal/controller/git.go index f2668ee5..b3266fdf 100644 --- a/internal/controller/git.go +++ b/internal/controller/git.go @@ -17,6 +17,7 @@ import ( terraformext "github.com/kubefirst/kubefirst-api/extensions/terraform" vultrext "github.com/kubefirst/kubefirst-api/extensions/vultr" gitShim "github.com/kubefirst/kubefirst-api/internal/gitShim" + "github.com/kubefirst/kubefirst-api/pkg/segment" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/gitlab" log "github.com/sirupsen/logrus" @@ -72,7 +73,9 @@ func (clctrl *ClusterController) RunGitTerraform() error { } // //* create teams and repositories in github - telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitTerraformApplyStarted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.GitTerraformApplyStarted, "") log.Infof("Creating %s resources with terraform", clctrl.GitProvider) @@ -113,12 +116,12 @@ func (clctrl *ClusterController) RunGitTerraform() error { if err != nil { msg := fmt.Sprintf("error creating %s resources with terraform %s: %s", clctrl.GitProvider, tfEntrypoint, err) log.Error(msg) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitTerraformApplyFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.GitTerraformApplyFailed, err.Error()) return fmt.Errorf(msg) } log.Infof("created git projects and groups for %s.com/%s", clctrl.GitProvider, clctrl.GitAuth.Owner) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitTerraformApplyCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.GitTerraformApplyCompleted, "") err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "git_terraform_apply_check", true) if err != nil { diff --git a/internal/controller/kbot.go b/internal/controller/kbot.go index 16c89a64..66b2b120 100644 --- a/internal/controller/kbot.go +++ b/internal/controller/kbot.go @@ -9,6 +9,7 @@ package controller import ( "os" + "github.com/kubefirst/kubefirst-api/pkg/segment" "github.com/kubefirst/metrics-client/pkg/telemetry" internalssh "github.com/kubefirst/runtime/pkg/ssh" log "github.com/sirupsen/logrus" @@ -31,10 +32,12 @@ func (clctrl *ClusterController) InitializeBot() error { } if !cl.KbotSetupCheck { + segClient := segment.InitClient() + defer segClient.Client.Close() clctrl.GitAuth.PrivateKey, clctrl.GitAuth.PublicKey, err = internalssh.CreateSshKeyPair() if err != nil { log.Errorf("error generating ssh keys: %s", err) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.KbotSetupFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.KbotSetupFailed, err.Error()) return err } diff --git a/internal/controller/repository.go b/internal/controller/repository.go index bce252b3..1aeb6df3 100644 --- a/internal/controller/repository.go +++ b/internal/controller/repository.go @@ -15,6 +15,7 @@ import ( githttps "github.com/go-git/go-git/v5/plumbing/transport/http" google "github.com/kubefirst/kubefirst-api/pkg/google" "github.com/kubefirst/kubefirst-api/pkg/providerConfigs" + "github.com/kubefirst/kubefirst-api/pkg/segment" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/civo" "github.com/kubefirst/runtime/pkg/digitalocean" @@ -194,7 +195,9 @@ func (clctrl *ClusterController) RepositoryPush() error { gitopsDir := clctrl.ProviderConfig.GitopsDir metaphorDir := clctrl.ProviderConfig.MetaphorDir - telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitopsRepoPushStarted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.GitopsRepoPushStarted, "") gitopsRepo, err := git.PlainOpen(gitopsDir) if err != nil { log.Infof("error opening repo at: %s", gitopsDir) @@ -250,7 +253,7 @@ func (clctrl *ClusterController) RepositoryPush() error { ) if err != nil { msg := fmt.Sprintf("error pushing detokenized gitops repository to remote %s: %s", clctrl.ProviderConfig.DestinationGitopsRepoURL, err) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitopsRepoPushFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.GitopsRepoPushFailed, err.Error()) return fmt.Errorf(msg) } @@ -266,14 +269,14 @@ func (clctrl *ClusterController) RepositoryPush() error { ) if err != nil { msg := fmt.Sprintf("error pushing detokenized metaphor repository to remote %s: %s", clctrl.ProviderConfig.DestinationMetaphorRepoURL, err) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitopsRepoPushFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.GitopsRepoPushFailed, err.Error()) return fmt.Errorf(msg) } log.Infof("successfully pushed gitops and metaphor repositories to git@%s/%s", clctrl.GitHost, clctrl.GitAuth.Owner) // todo delete the local gitops repo and re-clone it // todo that way we can stop worrying about which origin we're going to push to - telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitopsRepoPushCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.GitopsRepoPushCompleted, "") err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "gitops_pushed_check", true) if err != nil { diff --git a/internal/controller/state.go b/internal/controller/state.go index 371e6ce6..c08e38c8 100644 --- a/internal/controller/state.go +++ b/internal/controller/state.go @@ -12,6 +12,7 @@ import ( "os" "strings" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/civo" @@ -37,7 +38,9 @@ func (clctrl *ClusterController) StateStoreCredentials() error { } var stateStoreData pkgtypes.StateStoreCredentials - telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCredentialsCreateStarted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.StateStoreCredentialsCreateStarted, "") if !cl.StateStoreCredsCheck { switch clctrl.CloudProvider { @@ -66,6 +69,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error { Name: clctrl.KubefirstStateStoreBucketName, }) if err != nil { + telemetry.SendEvent(segClient, telemetry.StateStoreCredentialsCreateFailed, err.Error()) return err } case "civo": @@ -76,6 +80,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error { creds, err := civoConf.GetAccessCredentials(clctrl.KubefirstStateStoreBucketName, clctrl.CloudRegion) if err != nil { + telemetry.SendEvent(segClient, telemetry.StateStoreCredentialsCreateFailed, err.Error()) log.Error(err.Error()) } @@ -100,7 +105,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error { if err != nil { msg := fmt.Sprintf("error creating spaces bucket %s: %s", clctrl.KubefirstStateStoreBucketName, err) log.Error(msg) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCredentialsCreateFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.StateStoreCredentialsCreateFailed, err.Error()) return fmt.Errorf(msg) } @@ -126,7 +131,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error { if err != nil { msg := fmt.Sprintf("error creating google bucket %s: %s", clctrl.KubefirstStateStoreBucketName, err) log.Error() - telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateFailed, msg) + telemetry.SendEvent(segClient, telemetry.StateStoreCreateFailed, msg) return fmt.Errorf(msg) } @@ -141,7 +146,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error { objst, err := vultrConf.CreateObjectStorage(clctrl.KubefirstStateStoreBucketName) if err != nil { - telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.StateStoreCreateFailed, err.Error()) log.Error(err.Error()) return err } @@ -151,6 +156,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error { Endpoint: objst.S3Hostname, }, clctrl.KubefirstStateStoreBucketName) if err != nil { + telemetry.SendEvent(segClient, telemetry.StateStoreCredentialsCreateFailed, err.Error()) return fmt.Errorf("error creating vultr state storage bucket: %s", err) } @@ -180,7 +186,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error { if err != nil { return err } - + telemetry.SendEvent(segClient, telemetry.CloudCredentialsCheckCompleted, "") log.Infof("%s object storage credentials created and set", clctrl.CloudProvider) } @@ -197,19 +203,21 @@ func (clctrl *ClusterController) StateStoreCreate() error { if !cl.StateStoreCreateCheck { switch clctrl.CloudProvider { case "civo": + segClient := segment.InitClient() + defer segClient.Client.Close() civoConf := civo.CivoConfiguration{ Client: civo.NewCivo(cl.CivoAuth.Token, cl.CloudRegion), Context: context.Background(), } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateStarted, err.Error()) + telemetry.SendEvent(segClient, telemetry.StateStoreCreateStarted, "") accessKeyId := cl.StateStoreCredentials.AccessKeyID log.Infof("access key id %s", accessKeyId) bucket, err := civoConf.CreateStorageBucket(accessKeyId, clctrl.KubefirstStateStoreBucketName, clctrl.CloudRegion) if err != nil { - telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.StateStoreCreateFailed, err.Error()) log.Error(err.Error()) return err } @@ -229,7 +237,7 @@ func (clctrl *ClusterController) StateStoreCreate() error { return err } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.StateStoreCreateCompleted, "") log.Infof("%s state store bucket created", clctrl.CloudProvider) } } diff --git a/internal/controller/users.go b/internal/controller/users.go index 73179403..49cdf816 100644 --- a/internal/controller/users.go +++ b/internal/controller/users.go @@ -15,6 +15,7 @@ import ( googleext "github.com/kubefirst/kubefirst-api/extensions/google" terraformext "github.com/kubefirst/kubefirst-api/extensions/terraform" vultrext "github.com/kubefirst/kubefirst-api/extensions/vultr" + "github.com/kubefirst/kubefirst-api/pkg/segment" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" log "github.com/sirupsen/logrus" @@ -52,7 +53,9 @@ func (clctrl *ClusterController) RunUsersTerraform() error { } } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.UsersTerraformApplyStarted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.UsersTerraformApplyStarted, "") log.Info("applying users terraform") tfEnvs := map[string]string{} @@ -80,11 +83,11 @@ func (clctrl *ClusterController) RunUsersTerraform() error { err = terraformext.InitApplyAutoApprove(terraformClient, tfEntrypoint, tfEnvs) if err != nil { log.Errorf("error applying users terraform: %s", err) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.UsersTerraformApplyFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.UsersTerraformApplyFailed, err.Error()) return err } log.Info("executed users terraform successfully") - telemetry.SendEvent(clctrl.SegmentClient, telemetry.UsersTerraformApplyCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.UsersTerraformApplyCompleted, "") clctrl.VaultAuth.RootToken = tfEnvs["VAULT_TOKEN"] err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "vault_auth.root_token", clctrl.VaultAuth.RootToken) diff --git a/internal/controller/vault.go b/internal/controller/vault.go index 691cdbee..901bb152 100644 --- a/internal/controller/vault.go +++ b/internal/controller/vault.go @@ -22,6 +22,7 @@ import ( googleext "github.com/kubefirst/kubefirst-api/extensions/google" terraformext "github.com/kubefirst/kubefirst-api/extensions/terraform" vultrext "github.com/kubefirst/kubefirst-api/extensions/vultr" + "github.com/kubefirst/kubefirst-api/pkg/segment" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" vault "github.com/kubefirst/runtime/pkg/vault" @@ -96,7 +97,9 @@ func (clctrl *ClusterController) InitializeVault() error { } } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultInitializationStarted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.VaultInitializationStarted, "") switch clctrl.CloudProvider { case "aws", "google": @@ -150,11 +153,11 @@ func (clctrl *ClusterController) InitializeVault() error { _, err = k8s.WaitForJobComplete(kcfg.Clientset, job, 240) if err != nil { msg := fmt.Sprintf("could not run vault unseal job: %s", err) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultInitializationFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.VaultInitializationFailed, err.Error()) log.Error(msg) } } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultInitializationCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.VaultInitializationCompleted, "") err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "vault_initialized_check", true) if err != nil { @@ -196,7 +199,9 @@ func (clctrl *ClusterController) RunVaultTerraform() error { return err } } - telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultTerraformApplyStarted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.VaultTerraformApplyStarted, "") tfEnvs := map[string]string{} @@ -250,12 +255,12 @@ func (clctrl *ClusterController) RunVaultTerraform() error { err = terraformext.InitApplyAutoApprove(terraformClient, tfEntrypoint, tfEnvs) if err != nil { log.Errorf("error applying vault terraform: %s", err) - telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultTerraformApplyFailed, err.Error()) + telemetry.SendEvent(segClient, telemetry.VaultTerraformApplyFailed, err.Error()) return err } log.Info("vault terraform executed successfully") - telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultTerraformApplyCompleted, err.Error()) + telemetry.SendEvent(segClient, telemetry.VaultTerraformApplyCompleted, "") err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "vault_terraform_apply_check", true) if err != nil { diff --git a/internal/router/api/v1/telemetry.go b/internal/router/api/v1/telemetry.go index d30c0a7a..e9b6e45d 100644 --- a/internal/router/api/v1/telemetry.go +++ b/internal/router/api/v1/telemetry.go @@ -93,7 +93,7 @@ func PostTelemetry(c *gin.Context) { return } - telemetry.SendEvent(&segClient, req.Event, err.Error()) + telemetry.SendEvent(&segClient, req.Event, "") c.JSON(http.StatusOK, true) } diff --git a/internal/telemetry/heartbeat.go b/internal/telemetry/heartbeat.go index 43c58b5c..15496f01 100644 --- a/internal/telemetry/heartbeat.go +++ b/internal/telemetry/heartbeat.go @@ -5,22 +5,26 @@ import ( "github.com/kubefirst/kubefirst-api/internal/constants" "github.com/kubefirst/kubefirst-api/internal/db" + "github.com/kubefirst/kubefirst-api/pkg/segment" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/segmentio/analytics-go" ) func Heartbeat(segmentClient *telemetry.SegmentClient, dbClient *db.MongoDBClient) { - telemetry.SendEvent(segmentClient, telemetry.KubefirstHeartbeat, "") - HeartbeatWorkloadClusters(segmentClient, dbClient) + + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.KubefirstHeartbeat, "") + HeartbeatWorkloadClusters(segClient, dbClient) for range time.Tick(time.Second * 30) { - telemetry.SendEvent(segmentClient, telemetry.KubefirstHeartbeat, "") - HeartbeatWorkloadClusters(segmentClient, dbClient) + telemetry.SendEvent(segClient, telemetry.KubefirstHeartbeat, "") + HeartbeatWorkloadClusters(segClient, dbClient) } } func HeartbeatWorkloadClusters(segmentClient *telemetry.SegmentClient, dbClient *db.MongoDBClient) error { - + clusters, _ := dbClient.GetClusters() for _, cluster := range clusters { diff --git a/providers/aws/create.go b/providers/aws/create.go index 911a2083..2bcdc24a 100644 --- a/providers/aws/create.go +++ b/providers/aws/create.go @@ -12,6 +12,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/controller" "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/services" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" awsinternal "github.com/kubefirst/runtime/pkg/aws" @@ -256,7 +257,9 @@ func CreateAWSCluster(definition *pkgtypes.ClusterDefinition) error { log.Info("cluster creation complete") - telemetry.SendEvent(ctrl.SegmentClient, telemetry.ClusterInstallCompleted, err.Error()) + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.ClusterInstallCompleted, "") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName) diff --git a/providers/aws/delete.go b/providers/aws/delete.go index c7e35fd2..75430416 100644 --- a/providers/aws/delete.go +++ b/providers/aws/delete.go @@ -20,6 +20,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/errors" "github.com/kubefirst/kubefirst-api/pkg/providerConfigs" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg" @@ -32,6 +33,8 @@ import ( // DeleteAWSCluster func DeleteAWSCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentClient) error { + segClient := segment.InitClient() + defer segClient.Client.Close() // Logging handler // Logs to stdout to maintain compatibility with event streaming log.SetFormatter(&log.TextFormatter{ @@ -41,7 +44,7 @@ func DeleteAWSCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentClie log.SetReportCaller(false) log.SetOutput(os.Stdout) - telemetry.SendEvent(segmentClient, telemetry.ClusterDeleteStarted, "") + telemetry.SendEvent(segClient, telemetry.ClusterDeleteStarted, "") // Instantiate aws config config := providerConfigs.GetConfig(cl.ClusterName, cl.DomainName, cl.GitProvider, cl.GitAuth.Owner, cl.GitProtocol, cl.CloudflareAuth.APIToken, cl.CloudflareAuth.OriginCaIssuerKey) @@ -253,7 +256,7 @@ func DeleteAWSCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentClie } } - telemetry.SendEvent(segmentClient, telemetry.ClusterDeleteCompleted, "") + telemetry.SendEvent(segClient, telemetry.ClusterDeleteCompleted, "") err = db.Client.UpdateCluster(cl.ClusterName, "status", constants.ClusterStatusDeleted) if err != nil { diff --git a/providers/civo/create.go b/providers/civo/create.go index 9f41bd2c..4f97353a 100644 --- a/providers/civo/create.go +++ b/providers/civo/create.go @@ -13,6 +13,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/controller" "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/services" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" @@ -236,7 +237,9 @@ func CreateCivoCluster(definition *pkgtypes.ClusterDefinition) error { log.Info("cluster creation complete") - telemetry.SendEvent(ctrl.SegmentClient, telemetry.ClusterInstallCompleted, "") + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.ClusterInstallCompleted, "") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName) diff --git a/providers/civo/delete.go b/providers/civo/delete.go index 3db58127..a1f09d1c 100644 --- a/providers/civo/delete.go +++ b/providers/civo/delete.go @@ -21,6 +21,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/errors" "github.com/kubefirst/kubefirst-api/pkg/providerConfigs" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg" @@ -32,6 +33,8 @@ import ( // DeleteCivoCluster func DeleteCivoCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentClient) error { + segClient := segment.InitClient() + defer segClient.Client.Close() // Logging handler // Logs to stdout to maintain compatibility with event streaming log.SetFormatter(&log.TextFormatter{ @@ -41,7 +44,7 @@ func DeleteCivoCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentCli log.SetReportCaller(false) log.SetOutput(os.Stdout) - telemetry.SendEvent(segmentClient, telemetry.ClusterDeleteStarted, "") + telemetry.SendEvent(segClient, telemetry.ClusterDeleteStarted, "") // Instantiate civo config config := providerConfigs.GetConfig(cl.ClusterName, cl.DomainName, cl.GitProvider, cl.GitAuth.Owner, cl.GitProtocol, cl.CloudflareAuth.APIToken, cl.CloudflareAuth.OriginCaIssuerKey) @@ -249,7 +252,7 @@ func DeleteCivoCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentCli } } - telemetry.SendEvent(segmentClient, telemetry.ClusterDeleteCompleted, "") + telemetry.SendEvent(segClient, telemetry.ClusterDeleteCompleted, "") err = db.Client.UpdateCluster(cl.ClusterName, "status", constants.ClusterStatusDeleted) if err != nil { diff --git a/providers/digitalocean/create.go b/providers/digitalocean/create.go index 8fa915b2..0bb49b30 100644 --- a/providers/digitalocean/create.go +++ b/providers/digitalocean/create.go @@ -13,6 +13,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/controller" "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/services" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" @@ -231,7 +232,9 @@ func CreateDigitaloceanCluster(definition *pkgtypes.ClusterDefinition) error { // Telemetry handler - telemetry.SendEvent(ctrl.SegmentClient, telemetry.ClusterInstallCompleted, "") + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.ClusterInstallCompleted, "") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName) diff --git a/providers/digitalocean/delete.go b/providers/digitalocean/delete.go index 3f2863f6..c72c4d6f 100644 --- a/providers/digitalocean/delete.go +++ b/providers/digitalocean/delete.go @@ -21,6 +21,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/errors" "github.com/kubefirst/kubefirst-api/pkg/providerConfigs" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg" @@ -33,6 +34,8 @@ import ( // DeleteDigitaloceanCluster func DeleteDigitaloceanCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentClient) error { + segClient := segment.InitClient() + defer segClient.Client.Close() // Logging handler // Logs to stdout to maintain compatibility with event streaming log.SetFormatter(&log.TextFormatter{ @@ -42,7 +45,7 @@ func DeleteDigitaloceanCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.Se log.SetReportCaller(false) log.SetOutput(os.Stdout) - telemetry.SendEvent(segmentClient, telemetry.ClusterDeleteStarted, "") + telemetry.SendEvent(segClient, telemetry.ClusterDeleteStarted, "") // Instantiate digitalocean config config := providerConfigs.GetConfig(cl.ClusterName, cl.DomainName, cl.GitProvider, cl.GitAuth.Owner, cl.GitProtocol, cl.CloudflareAuth.Token, "") diff --git a/providers/google/create.go b/providers/google/create.go index b6cc545b..fd753c6d 100644 --- a/providers/google/create.go +++ b/providers/google/create.go @@ -15,6 +15,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/services" "github.com/kubefirst/kubefirst-api/pkg/google" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" @@ -253,7 +254,9 @@ func CreateGoogleCluster(definition *pkgtypes.ClusterDefinition) error { log.Info("cluster creation complete") - telemetry.SendEvent(ctrl.SegmentClient, telemetry.ClusterInstallCompleted, "") + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.ClusterInstallCompleted, "") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName) diff --git a/providers/google/delete.go b/providers/google/delete.go index 0648a9d1..347a82f5 100644 --- a/providers/google/delete.go +++ b/providers/google/delete.go @@ -22,6 +22,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/errors" "github.com/kubefirst/kubefirst-api/pkg/google" "github.com/kubefirst/kubefirst-api/pkg/providerConfigs" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg" @@ -33,6 +34,8 @@ import ( // DeleteGoogleCluster func DeleteGoogleCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentClient) error { + segClient := segment.InitClient() + defer segClient.Client.Close() // Logging handler // Logs to stdout to maintain compatibility with event streaming log.SetFormatter(&log.TextFormatter{ @@ -42,7 +45,7 @@ func DeleteGoogleCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentC log.SetReportCaller(false) log.SetOutput(os.Stdout) - telemetry.SendEvent(segmentClient, telemetry.ClusterDeleteStarted, "") + telemetry.SendEvent(segClient, telemetry.ClusterDeleteStarted, "") // Instantiate google config config := providerConfigs.GetConfig(cl.ClusterName, cl.DomainName, cl.GitProvider, cl.GitAuth.Owner, cl.GitProtocol, cl.CloudflareAuth.Token, "") @@ -251,7 +254,7 @@ func DeleteGoogleCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentC } } - telemetry.SendEvent(segmentClient, telemetry.ClusterDeleteCompleted, "") + telemetry.SendEvent(segClient, telemetry.ClusterDeleteCompleted, "") err = db.Client.UpdateCluster(cl.ClusterName, "status", constants.ClusterStatusDeleted) if err != nil { diff --git a/providers/vultr/create.go b/providers/vultr/create.go index 0518ff1a..8c443e4b 100644 --- a/providers/vultr/create.go +++ b/providers/vultr/create.go @@ -13,6 +13,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/controller" "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/services" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" @@ -227,7 +228,9 @@ func CreateVultrCluster(definition *pkgtypes.ClusterDefinition) error { log.Info("cluster creation complete") - telemetry.SendEvent(ctrl.SegmentClient, telemetry.ClusterInstallCompleted, "") + segClient := segment.InitClient() + defer segClient.Client.Close() + telemetry.SendEvent(segClient, telemetry.ClusterInstallCompleted, "") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName) diff --git a/providers/vultr/delete.go b/providers/vultr/delete.go index 083a2775..d13cec73 100644 --- a/providers/vultr/delete.go +++ b/providers/vultr/delete.go @@ -21,6 +21,7 @@ import ( "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/errors" "github.com/kubefirst/kubefirst-api/pkg/providerConfigs" + "github.com/kubefirst/kubefirst-api/pkg/segment" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" "github.com/kubefirst/metrics-client/pkg/telemetry" runtime "github.com/kubefirst/runtime/pkg" @@ -33,6 +34,8 @@ import ( // DeleteVultrCluster func DeleteVultrCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentClient) error { + segClient := segment.InitClient() + defer segClient.Client.Close() // Logging handler // Logs to stdout to maintain compatibility with event streaming log.SetFormatter(&log.TextFormatter{ @@ -42,7 +45,7 @@ func DeleteVultrCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentCl log.SetReportCaller(false) log.SetOutput(os.Stdout) - telemetry.SendEvent(segmentClient, telemetry.ClusterDeleteStarted, "") + telemetry.SendEvent(segClient, telemetry.ClusterDeleteStarted, "") // Instantiate vultr config config := providerConfigs.GetConfig(cl.ClusterName, cl.DomainName, cl.GitProvider, cl.GitAuth.Owner, cl.GitProtocol, cl.CloudflareAuth.Token, "") @@ -279,7 +282,7 @@ func DeleteVultrCluster(cl *pkgtypes.Cluster, segmentClient *telemetry.SegmentCl } } - telemetry.SendEvent(segmentClient, telemetry.ClusterDeleteCompleted, "") + telemetry.SendEvent(segClient, telemetry.ClusterDeleteCompleted, "") err = db.Client.UpdateCluster(cl.ClusterName, "status", constants.ClusterStatusDeleted) if err != nil {