Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into ci-github-flavor
Browse files Browse the repository at this point in the history
  • Loading branch information
marinhojess committed Oct 27, 2022
2 parents a9db76b + 35f515e commit c2bc477
Show file tree
Hide file tree
Showing 48 changed files with 1,883 additions and 494 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ k3d-linux-amd64.1
k3d-linux-amd64
my.test
go.test
kubefirst.yaml
# kubefirst # <- this is causing files in docs to not commit, need a more explicit path ignored

2 changes: 1 addition & 1 deletion cmd/addCi.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var addCiCmd = &cobra.Command{
return err
}

if !viper.GetBool("github.enabled") {
if viper.GetString("gitprovider") == "gitlab" {
ciTools.DeployOnGitlab(globalFlags, bucketName)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/checktools.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var checktoolsCmd = &cobra.Command{

kubectlVersion, kubectlStdErr, errKubectl := pkg.ExecShellReturnStrings(config.KubectlClientPath, "version", "--client", "--short")
fmt.Printf("-> kubectl version:\n\t%s\n\t%s\n", kubectlVersion, kubectlStdErr)
terraformVersion, terraformStdErr, errTerraform := pkg.ExecShellReturnStrings(config.TerraformPath, "version")
terraformVersion, terraformStdErr, errTerraform := pkg.ExecShellReturnStrings(config.TerraformClientPath, "version")
fmt.Printf("-> terraform version:\n\t%s\n\t%s\n", terraformVersion, terraformStdErr)
helmVersion, helmStdErr, errHelm := pkg.ExecShellReturnStrings(config.HelmClientPath, "version", "--client", "--short")
fmt.Printf("-> helm version:\n\t%s\n\t%s\n", helmVersion, helmStdErr)
Expand Down
215 changes: 181 additions & 34 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package cmd

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"syscall"

"github.com/go-git/go-git/v5/plumbing"
"github.com/kubefirst/kubefirst/internal/gitClient"
"github.com/kubefirst/kubefirst/internal/githubWrapper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"log"
"time"
Expand Down Expand Up @@ -50,8 +60,10 @@ cluster provisioning process spinning up the services, and validates the livenes
)
}

// todo remove this dependency from create.go
hostedZoneName := viper.GetString("aws.hostedzonename")

//* telemetry
if globalFlags.UseTelemetry {
// Instantiates a SegmentIO client to send messages to the segment API.
segmentIOClientStart := analytics.New(pkg.SegmentIOWriteKey)
Expand Down Expand Up @@ -82,8 +94,23 @@ cluster provisioning process spinning up the services, and validates the livenes
}
}

token := os.Getenv("GITHUB_AUTH_TOKEN")
if len(token) == 0 {
token = viper.GetString("github.token")
err := os.Setenv("GITHUB_AUTH_TOKEN", token)
if err != nil {
return err
}
}

if viper.GetString("cloud") == flagset.CloudK3d {
// todo need to add go channel to control when ngrok should close
go pkg.RunNgrok(context.TODO(), pkg.LocalAtlantisURL)
time.Sleep(5 * time.Second)
}

if !viper.GetBool("kubefirst.done") {
if viper.GetBool("github.enabled") {
if viper.GetString("gitprovider") == "github" {
log.Println("Installing Github version of Kubefirst")
viper.Set("git.mode", "github")
if viper.GetString("cloud") == flagset.CloudLocal {
Expand Down Expand Up @@ -117,54 +144,173 @@ cluster provisioning process spinning up the services, and validates the livenes
}
viper.Set("kubefirst.done", true)
viper.WriteConfig()
} else {
log.Println("already executed create command, continuing for readiness checks")
}

if viper.GetString("cloud") == flagset.CloudLocal {
log.Println("Hard break as we are still testing this mode")
return nil
}
// Relates to issue: https://github.com/kubefirst/kubefirst/issues/386
// Metaphor needs chart museum for CI works
informUser("Waiting chartmuseum", globalFlags.SilentMode)
for i := 1; i < 10; i++ {
chartMuseum := gitlab.AwaitHostNTimes("chartmuseum", globalFlags.DryRun, 20)
if chartMuseum {
informUser("Chartmuseum DNS is ready", globalFlags.SilentMode)
break
}
}
if !viper.GetBool("chartmuseum.host.resolved") {

informUser("Removing self-signed Argo certificate", globalFlags.SilentMode)
clientset, err := k8s.GetClientSet(globalFlags.DryRun)
if err != nil {
log.Printf("Failed to get clientset for k8s : %s", err)
return err
}
argocdPodClient := clientset.CoreV1().Pods("argocd")
err = k8s.RemoveSelfSignedCertArgoCD(argocdPodClient)
if err != nil {
log.Printf("Error removing self-signed certificate from ArgoCD: %s", err)
}
//* establish port-forward
var kPortForwardChartMuseum *exec.Cmd
kPortForwardChartMuseum, err = k8s.PortForward(globalFlags.DryRun, "chartmuseum", "svc/chartmuseum", "8181:8080")
defer func() {
err = kPortForwardChartMuseum.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Println("Error closing kPortForwardChartMuseum")
}
}()
pkg.AwaitHostNTimes("http://localhost:8181/health", 5, 5)
viper.Set("chartmuseum.host.resolved", true)
viper.WriteConfig()
} else {
log.Println("already resolved host for chartmuseum, continuing")
}

informUser("Checking if cluster is ready for use by metaphor apps", globalFlags.SilentMode)
for i := 1; i < 10; i++ {
err = k1ReadyCmd.RunE(cmd, args)
} else {
// Relates to issue: https://github.com/kubefirst/kubefirst/issues/386
// Metaphor needs chart museum for CI works
informUser("Waiting chartmuseum", globalFlags.SilentMode)
for i := 1; i < 10; i++ {
chartMuseum := gitlab.AwaitHostNTimes("chartmuseum", globalFlags.DryRun, 20)
if chartMuseum {
informUser("Chartmuseum DNS is ready", globalFlags.SilentMode)
break
}
}
informUser("Removing self-signed Argo certificate", globalFlags.SilentMode)
clientset, err := k8s.GetClientSet(globalFlags.DryRun)
if err != nil {
log.Println(err)
} else {
break
log.Printf("Failed to get clientset for k8s : %s", err)
return err
}
argocdPodClient := clientset.CoreV1().Pods("argocd")
err = k8s.RemoveSelfSignedCertArgoCD(argocdPodClient)
if err != nil {
log.Printf("Error removing self-signed certificate from ArgoCD: %s", err)
}

informUser("Checking if cluster is ready for use by metaphor apps", globalFlags.SilentMode)
for i := 1; i < 10; i++ {
err = k1ReadyCmd.RunE(cmd, args)
if err != nil {
log.Println(err)
} else {
break
}
}
}

informUser("Deploying metaphor applications", globalFlags.SilentMode)
err = deployMetaphorCmd.RunE(cmd, args)
if err != nil {
informUser("Error deploy metaphor applications", globalFlags.SilentMode)
log.Println("Error running deployMetaphorCmd")
return err
}
err = state.UploadKubefirstToStateStore(globalFlags.DryRun)
if err != nil {
log.Println(err)

if viper.GetString("cloud") == flagset.CloudAws {
err = state.UploadKubefirstToStateStore(globalFlags.DryRun)
if err != nil {
log.Println(err)
}
}

//kPortForwardAtlantis, err := k8s.PortForward(globalFlags.DryRun, "atlantis", "svc/atlantis", "4141:80")
//defer func() {
// err = kPortForwardAtlantis.Process.Signal(syscall.SIGTERM)
// if err != nil {
// log.Println("error closing kPortForwardAtlantis")
// }
//}()

// ---
// todo: (start) we can remove it, the secrets are now coming from Vault (run a full installation after removing to confirm)
if viper.GetString("cloud") == flagset.CloudK3d {
clientset, err := k8s.GetClientSet(false)
atlantisSecrets, err := clientset.CoreV1().Secrets("atlantis").Get(context.TODO(), "atlantis-secrets", metav1.GetOptions{})
if err != nil {
return err
}

// todo: hardcoded
atlantisSecrets.Data["TF_VAR_vault_addr"] = []byte("http://vault.vault.svc.cluster.local:8200")
atlantisSecrets.Data["VAULT_ADDR"] = []byte("http://vault.vault.svc.cluster.local:8200")

_, err = clientset.CoreV1().Secrets("atlantis").Update(context.TODO(), atlantisSecrets, metav1.UpdateOptions{})
if err != nil {
return err
}

err = clientset.CoreV1().Pods("atlantis").Delete(context.TODO(), "atlantis-0", metav1.DeleteOptions{})
if err != nil {
log.Fatal(err)
}
log.Println("---debug---")
log.Println("sleeping after kill atlantis pod")
log.Println("---debug---")

time.Sleep(10 * time.Second)

log.Println("---debug---")
log.Println("new port forward atlantis")
log.Println("---debug---")
kPortForwardAtlantis, err := k8s.PortForward(false, "atlantis", "svc/atlantis", "4141:80")
defer func() {
err = kPortForwardAtlantis.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Println("error closing kPortForwardAtlantis")
}
}()
// todo: (end)

// todo: wire it up in the architecture / files / folder

// update terraform s3 backend to internal k8s dns (s3/minio bucket)
err = pkg.ReplaceS3Backend()
if err != nil {
return err
}

// create a new branch and push changes
githubHost := viper.GetString("github.host")
githubOwner := viper.GetString("github.owner")
remoteName := "github"
localRepo := "gitops"
branchName := "update-s3-backend"
branchNameRef := plumbing.ReferenceName("refs/heads/" + branchName)

gitClient.UpdateLocalTFFilesAndPush(
githubHost,
githubOwner,
localRepo,
remoteName,
branchNameRef,
)

fmt.Println("sleeping after commit...")
time.Sleep(3 * time.Second)

// create a PR, atlantis will identify it's a terraform change/file update and,
// trigger atlantis plan
g := githubWrapper.New()
err = g.CreatePR(branchName)
if err != nil {
fmt.Println(err)
}
log.Println("sleeping after create PR...")
time.Sleep(5 * time.Second)
log.Println("sleeping... atlantis plan should be running")
time.Sleep(5 * time.Second)

fmt.Println("sleeping before apply...")
time.Sleep(120 * time.Second)

// after 120 seconds, it will comment in the PR with atlantis plan
err = g.CommentPR(1, "atlantis apply")
if err != nil {
fmt.Println(err)
}
}

log.Println("sending mgmt cluster install completed metric")
Expand Down Expand Up @@ -202,6 +348,7 @@ cluster provisioning process spinning up the services, and validates the livenes
log.Println("Kubefirst installation finished successfully")
informUser("Kubefirst installation finished successfully", globalFlags.SilentMode)

// todo: temporary code to enable console for localhost
err = postInstallCmd.RunE(cmd, args)
if err != nil {
informUser("Error starting apps from post-install", globalFlags.SilentMode)
Expand Down
Loading

0 comments on commit c2bc477

Please sign in to comment.