Skip to content

Commit

Permalink
adjust events signature (#218)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
jarededwards and johndietz authored Oct 22, 2023
1 parent 9355fe4 commit eeb0e87
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 74 deletions.
18 changes: 11 additions & 7 deletions internal/controller/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -38,6 +39,7 @@ func (clctrl *ClusterController) InstallArgoCD() error {
}

if !cl.ArgoCDInstallCheck {

var kcfg *k8s.KubernetesClient

switch clctrl.CloudProvider {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions internal/controller/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 9 additions & 16 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
10 changes: 7 additions & 3 deletions internal/controller/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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":
Expand All @@ -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())
}

Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 6 additions & 3 deletions internal/controller/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion internal/controller/kbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down
11 changes: 7 additions & 4 deletions internal/controller/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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 {
Expand Down
Loading

0 comments on commit eeb0e87

Please sign in to comment.