Skip to content

Commit

Permalink
add goroutine back in and merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
johndietz committed Oct 24, 2023
2 parents 3a55d3b + 47cefec commit 283f280
Show file tree
Hide file tree
Showing 28 changed files with 187 additions and 293 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/google/go-github/v52 v52.0.0
github.com/hashicorp/vault/api v1.9.0
github.com/joho/godotenv v1.5.1
github.com/kubefirst/metrics-client v0.2.1
github.com/kubefirst/metrics-client v0.2.8
github.com/kubefirst/runtime v0.3.27
github.com/minio/minio-go/v7 v7.0.49
github.com/otiai10/copy v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubefirst/metrics-client v0.2.1 h1:KCzeeKQCq13ksj+l8cTrtHWKtraYTTnwWks2X4+xdFI=
github.com/kubefirst/metrics-client v0.2.1/go.mod h1:GR7wsMcyYhd+EU67PeuMCBYFE6OJ7P/j5OI5BLOoRMc=
github.com/kubefirst/metrics-client v0.2.8 h1:JfaeiBifZD/DpyYW2QVHcrhH/KWA98OmM+7c7M12qMc=
github.com/kubefirst/metrics-client v0.2.8/go.mod h1:GR7wsMcyYhd+EU67PeuMCBYFE6OJ7P/j5OI5BLOoRMc=
github.com/kubefirst/runtime v0.3.27 h1:U8u+ywqNbbSra8W4jZe+mHrkaZ0hE6JaHSWy3XQ/8GU=
github.com/kubefirst/runtime v0.3.27/go.mod h1:9egAgF5cF594mIzra5nTB1kfE57Hr/Nvl79tjLsAWCQ=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
Expand Down
15 changes: 5 additions & 10 deletions internal/controller/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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 Down Expand Up @@ -58,16 +57,14 @@ func (clctrl *ClusterController) InstallArgoCD() error {
argoCDInstallPath := fmt.Sprintf("github.com:kubefirst/manifests/argocd/cloud?ref=%s", pkg.KubefirstManifestRepoRef)
log.Infof("installing argocd")

segClient := segment.InitClient()
defer segClient.Client.Close()
telemetry.SendEvent(segClient, telemetry.ArgoCDInstallStarted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.ArgoCDInstallStarted, "")
err = argocd.ApplyArgoCDKustomize(kcfg.Clientset, argoCDInstallPath)
if err != nil {
telemetry.SendEvent(segClient, telemetry.ArgoCDInstallFailed, err.Error())
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.ArgoCDInstallFailed, err.Error())
return err
}

telemetry.SendEvent(segClient, telemetry.ArgoCDInstallCompleted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.ArgoCDInstallCompleted, "")

// Wait for ArgoCD to be ready
_, err = k8s.VerifyArgoCDReadiness(kcfg.Clientset, true, 300)
Expand Down Expand Up @@ -196,9 +193,7 @@ func (clctrl *ClusterController) DeployRegistryApplication() error {
}
}

segClient := segment.InitClient()
defer segClient.Client.Close()
telemetry.SendEvent(segClient, telemetry.CreateRegistryStarted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.CreateRegistryStarted, "")
argocdClient, err := argocdapi.NewForConfig(kcfg.RestConfig)
if err != nil {
return err
Expand Down Expand Up @@ -243,7 +238,7 @@ func (clctrl *ClusterController) DeployRegistryApplication() error {

_, _ = argocdClient.ArgoprojV1alpha1().Applications("argocd").Create(context.Background(), registryApplicationObject, metav1.CreateOptions{})

telemetry.SendEvent(segClient, telemetry.CreateRegistryCompleted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.CreateRegistryCompleted, "")

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "argocd_create_registry_check", true)
if err != nil {
Expand Down
11 changes: 4 additions & 7 deletions internal/controller/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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 @@ -51,9 +50,7 @@ func (clctrl *ClusterController) CreateCluster() error {
tfEntrypoint := clctrl.ProviderConfig.GitopsDir + fmt.Sprintf("/terraform/%s", clctrl.CloudProvider)
tfEnvs := map[string]string{}

segClient := segment.InitClient()
defer segClient.Client.Close()
telemetry.SendEvent(segClient, telemetry.CloudTerraformApplyStarted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.CloudTerraformApplyStarted, "")

log.Infof("creating %s cluster", clctrl.CloudProvider)

Expand Down Expand Up @@ -83,19 +80,19 @@ func (clctrl *ClusterController) CreateCluster() error {

err := terraformext.InitApplyAutoApprove(clctrl.ProviderConfig.TerraformClient, tfEntrypoint, tfEnvs)
if err != nil {
telemetry.SendEvent(segClient, telemetry.CloudTerraformApplyFailed, err.Error())
telemetry.SendEvent(clctrl.TelemetryEvent, 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 {
telemetry.SendEvent(clctrl.SegmentClient, telemetry.CloudTerraformApplyFailed, err.Error())
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.CloudTerraformApplyFailed, err.Error())
return err
}
return fmt.Errorf(msg)
}

log.Infof("created %s cloud resources", clctrl.CloudProvider)
telemetry.SendEvent(segClient, telemetry.CloudTerraformApplyCompleted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.CloudTerraformApplyCompleted, "")

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "cloud_terraform_apply_failed_check", false)
if err != nil {
Expand Down
56 changes: 18 additions & 38 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"os"
"time"

"github.com/denisbrodbeck/machineid"
"github.com/kubefirst/kubefirst-api/internal/constants"
"github.com/kubefirst/kubefirst-api/internal/db"
"github.com/kubefirst/kubefirst-api/internal/utils"
Expand All @@ -29,7 +28,6 @@ import (
"github.com/kubefirst/runtime/pkg/gitlab"
"github.com/kubefirst/runtime/pkg/k8s"
"github.com/kubefirst/runtime/pkg/services"
"github.com/segmentio/analytics-go"

log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand Down Expand Up @@ -105,7 +103,7 @@ type ClusterController struct {
MdbCl *db.MongoDBClient

// Telemetry
SegmentClient *telemetry.SegmentClient
TelemetryEvent telemetry.TelemetryEvent

// Provider clients
AwsClient *awsinternal.AWSConfiguration
Expand Down Expand Up @@ -145,42 +143,25 @@ func (clctrl *ClusterController) InitController(def *pkgtypes.ClusterDefinition)
clusterID = runtime.GenerateClusterID()
}

// Telemetry handle
machineID, _ := machineid.ID()

segClient := telemetry.SegmentClient{
TelemetryEvent: telemetry.TelemetryEvent{
CliVersion: os.Getenv("KUBEFIRST_VERSION"),
CloudProvider: clctrl.CloudProvider,
ClusterID: clusterID,
ClusterType: def.Type,
DomainName: def.DomainName,
GitProvider: def.GitProvider,
InstallMethod: os.Getenv("INSTALL_METHOD"),
KubefirstClient: "api",
KubefirstTeam: os.Getenv("KUBEFIRST_TEAM"),
KubefirstTeamInfo: os.Getenv("KUBEFIRST_TEAM_INFO"),
MachineID: machineID,
ErrorMessage: "",
UserId: machineID,
MetricName: telemetry.ClusterInstallStarted,
},
Client: analytics.New(telemetry.SegmentIOWriteKey),
telemetryEvent := telemetry.TelemetryEvent{
CliVersion: os.Getenv("KUBEFIRST_VERSION"),
CloudProvider: os.Getenv("CLOUD_PROVIDER"),
ClusterID: os.Getenv("CLUSTER_ID"),
ClusterType: os.Getenv("CLUSTER_TYPE"),
DomainName: os.Getenv("DOMAIN_NAME"),
ErrorMessage: "",
GitProvider: os.Getenv("GIT_PROVIDER"),
InstallMethod: os.Getenv("INSTALL_METHOD"),
KubefirstClient: "api",
KubefirstTeam: os.Getenv("KUBEFIRST_TEAM"),
KubefirstTeamInfo: os.Getenv("KUBEFIRST_TEAM_INFO"),
MachineID: os.Getenv("CLUSTER_ID"),
MetricName: telemetry.ClusterInstallStarted,
UserId: os.Getenv("CLUSTER_ID"),
}
clctrl.SegmentClient = &segClient
clctrl.TelemetryEvent = telemetryEvent

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, "")
}
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.ClusterInstallStarted, "")

//Copy Cluster Definiion to Cluster Controller
clctrl.AlertsEmail = def.AdminEmail
Expand Down Expand Up @@ -292,7 +273,6 @@ func (clctrl *ClusterController) InitController(def *pkgtypes.ClusterDefinition)
cl := pkgtypes.Cluster{
ID: primitive.NewObjectID(),
CreationTimestamp: fmt.Sprintf("%v", primitive.NewDateTimeFromTime(time.Now().UTC())),
UseTelemetry: clctrl.SegmentClient.UseTelemetry,
Status: constants.ClusterStatusProvisioning,
AlertsEmail: clctrl.AlertsEmail,
ClusterName: clctrl.ClusterName,
Expand Down
10 changes: 3 additions & 7 deletions internal/controller/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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 @@ -27,11 +26,8 @@ func (clctrl *ClusterController) DomainLivenessTest() error {
return err
}

segClient := segment.InitClient()
defer segClient.Client.Close()

if !cl.DomainLivenessCheck {
telemetry.SendEvent(segClient, telemetry.DomainLivenessStarted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.DomainLivenessStarted, "")

switch clctrl.DnsProvider {
case "aws":
Expand All @@ -50,7 +46,7 @@ func (clctrl *ClusterController) DomainLivenessTest() error {
// domain id
domainId, err := civoConf.GetDNSInfo(clctrl.DomainName, clctrl.CloudRegion)
if err != nil {
telemetry.SendEvent(segClient, telemetry.DomainLivenessFailed, err.Error())
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.DomainLivenessFailed, err.Error())
log.Info(err.Error())
}

Expand Down Expand Up @@ -125,7 +121,7 @@ func (clctrl *ClusterController) DomainLivenessTest() error {
return err
}

telemetry.SendEvent(segClient, telemetry.DomainLivenessCompleted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.DomainLivenessCompleted, "")

log.Infof("domain %s verified", clctrl.DomainName)
}
Expand Down
10 changes: 4 additions & 6 deletions internal/controller/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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 @@ -73,9 +72,8 @@ func (clctrl *ClusterController) RunGitTerraform() error {
}

// //* create teams and repositories in github
segClient := segment.InitClient()
defer segClient.Client.Close()
telemetry.SendEvent(segClient, telemetry.GitTerraformApplyStarted, "")

telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.GitTerraformApplyStarted, "")

log.Infof("Creating %s resources with terraform", clctrl.GitProvider)

Expand Down Expand Up @@ -116,12 +114,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(segClient, telemetry.GitTerraformApplyFailed, err.Error())
telemetry.SendEvent(clctrl.TelemetryEvent, 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(segClient, telemetry.GitTerraformApplyCompleted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.GitTerraformApplyCompleted, "")

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "git_terraform_apply_check", true)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/controller/kbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 @@ -32,12 +31,11 @@ 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(segClient, telemetry.KbotSetupFailed, err.Error())
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.KbotSetupFailed, err.Error())
return err
}

Expand Down
11 changes: 4 additions & 7 deletions internal/controller/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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 @@ -195,9 +194,7 @@ func (clctrl *ClusterController) RepositoryPush() error {
gitopsDir := clctrl.ProviderConfig.GitopsDir
metaphorDir := clctrl.ProviderConfig.MetaphorDir

segClient := segment.InitClient()
defer segClient.Client.Close()
telemetry.SendEvent(segClient, telemetry.GitopsRepoPushStarted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.GitopsRepoPushStarted, "")
gitopsRepo, err := git.PlainOpen(gitopsDir)
if err != nil {
log.Infof("error opening repo at: %s", gitopsDir)
Expand Down Expand Up @@ -253,7 +250,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(segClient, telemetry.GitopsRepoPushFailed, err.Error())
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.GitopsRepoPushFailed, err.Error())
return fmt.Errorf(msg)
}

Expand All @@ -269,14 +266,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(segClient, telemetry.GitopsRepoPushFailed, err.Error())
telemetry.SendEvent(clctrl.TelemetryEvent, 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(segClient, telemetry.GitopsRepoPushCompleted, "")
telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.GitopsRepoPushCompleted, "")

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "gitops_pushed_check", true)
if err != nil {
Expand Down
Loading

0 comments on commit 283f280

Please sign in to comment.