Skip to content

Commit

Permalink
chore: move get github user away from authenticate (#687)
Browse files Browse the repository at this point in the history
* feat: enable github token to be accepted via env. variable

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* chore: clean up

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* fix: add get github data (#680)

Signed-off-by: João Vanzuita <joao@kubeshop.io>

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* Change reference to github token (#681)

Signed-off-by: Jessica Marinho <jessica@kubeshop.io>

Signed-off-by: Jessica Marinho <jessica@kubeshop.io>
Co-authored-by: Jessica Marinho <jessica@kubeshop.io>

* chore: clean up (#683)

Signed-off-by: João Vanzuita <joao@kubeshop.io>

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* revert break of handoff (#684)

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

* Fix 664 (#685)

* revert break of handoff

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

* re-enable addon to local

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

* chore: move get github user away from authenticate

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* chore: clean up

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* chore: remove github.org , add github.owner

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* feat: ask for github token on github aws create

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* chore: clean up

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* remove local from cloud cli (#689)

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

* feat: ask for github token on github aws init

Signed-off-by: João Vanzuita <joao@kubeshop.io>

* Set github owner with config file (#693)

Signed-off-by: Jessica Marinho <jessica@kubeshop.io>

Signed-off-by: Jessica Marinho <jessica@kubeshop.io>
Co-authored-by: Jessica Marinho <jessica@kubeshop.io>

Signed-off-by: João Vanzuita <joao@kubeshop.io>
Signed-off-by: Jessica Marinho <jessica@kubeshop.io>
Signed-off-by: 6za <53096417+6za@users.noreply.github.com>
Co-authored-by: Jéssica Marinho <jlmarinhocosta@gmail.com>
Co-authored-by: Jessica Marinho <jessica@kubeshop.io>
Co-authored-by: Cesar Filho <53096417+6za@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 9, 2022
1 parent 0414cb8 commit a9f0de0
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 271 deletions.
256 changes: 75 additions & 181 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package cmd

import (
"context"
"errors"
"fmt"
"net/http"
"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 @@ -60,6 +53,13 @@ cluster provisioning process spinning up the services, and validates the livenes
)
}

if viper.GetString("cloud") != flagset.CloudAws {
log.Println("Not cloud mode attempt to create using cloud cli")
if err != nil {
return fmt.Errorf("not support mode of install via this command, only cloud install supported")
}
}

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

Expand Down Expand Up @@ -94,106 +94,97 @@ cluster provisioning process spinning up the services, and validates the livenes
}
}

token := os.Getenv("KUBEFIRST_GITHUB_AUTH_TOKEN")
if len(token) == 0 {
errors.New("GitHub token not provided")
httpClient := http.DefaultClient
gitHubService := services.NewGitHubService(httpClient)
gitHubHandler := handlers.NewGitHubHandler(gitHubService)

providerValue := viper.GetString("gitprovider")

config := configs.ReadConfig()
gitHubAccessToken := config.GitHubPersonalAccessToken
if providerValue == pkg.GitHubProviderName && gitHubAccessToken == "" {

gitHubAccessToken, err = gitHubHandler.AuthenticateUser()
if err != nil {
return err
}

if gitHubAccessToken == "" {
return errors.New("cannot create a cluster without a github auth token. please export your " +
"KUBEFIRST_GITHUB_AUTH_TOKEN in your terminal",
)
}

// todo: set common way to load env. values (viper->struct->load-env)
if err := os.Setenv("KUBEFIRST_GITHUB_AUTH_TOKEN", gitHubAccessToken); err != nil {
return err
}
log.Println("\nKUBEFIRST_GITHUB_AUTH_TOKEN set via OAuth")
}

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)
// get GitHub data to set user and owner based on the provided token
githubUser := gitHubHandler.GetGitHubUser(gitHubAccessToken)
viper.Set("github.user", githubUser)
err = viper.WriteConfig()
if err != nil {
return err
}

if !viper.GetBool("kubefirst.done") {
if viper.GetString("gitprovider") == "github" {
log.Println("Installing Github version of Kubefirst")
viper.Set("git.mode", "github")
if viper.GetString("cloud") == flagset.CloudLocal {
// if not local it is AWS for now
err := createGithubK3dCmd.RunE(cmd, args)
if err != nil {
return err
}
} else {
// if not local it is AWS for now
err := createGithubCmd.RunE(cmd, args)
if err != nil {
return err
}
// if not local it is AWS for now
err := createGithubCmd.RunE(cmd, args)
if err != nil {
return err
}

} else {
log.Println("Installing GitLab version of Kubefirst")
viper.Set("git.mode", "gitlab")
if viper.GetString("cloud") == flagset.CloudLocal {
// We don't support gitlab on local yet
return errors.New("gitlab is not supported on kubefirst local")

} else {
// if not local it is AWS for now
err := createGitlabCmd.RunE(cmd, args)
if err != nil {
return err
}
// if not local it is AWS for now
err := createGitlabCmd.RunE(cmd, args)
if err != nil {
return err
}

}
viper.Set("kubefirst.done", true)
viper.WriteConfig()
} else {
log.Println("already executed create command, continuing for readiness checks")
}

if viper.GetString("cloud") == flagset.CloudLocal {
if !viper.GetBool("chartmuseum.host.resolved") {

//* 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")
// 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.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)
}

} 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)
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.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
}
log.Println(err)
} else {
break
}
}

Expand All @@ -212,103 +203,6 @@ cluster provisioning process spinning up the services, and validates the livenes
}
}

//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.ReplaceTerraformS3Backend()
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.UpdateLocalTerraformFilesAndPush(
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")

if globalFlags.UseTelemetry {
Expand Down
2 changes: 1 addition & 1 deletion cmd/githubAdd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var githubAddCmd = &cobra.Command{
return nil
}

log.Println("Org used:", viper.GetString("github.org"))
log.Println("Org used:", viper.GetString("github.owner"))
log.Println("dry-run:", globalFlags.DryRun)

if !viper.GetBool("github.terraformapplied.gitops") {
Expand Down
2 changes: 1 addition & 1 deletion cmd/githubRemove.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var githubRemoveCmd = &cobra.Command{
return nil
}

log.Println("Org used:", viper.GetString("github.org"))
log.Println("Org used:", viper.GetString("github.owner"))
log.Println("dry-run:", globalFlags.DryRun)

if viper.GetBool("github.terraformapplied.gitops") {
Expand Down
Loading

0 comments on commit a9f0de0

Please sign in to comment.