Skip to content

Commit

Permalink
#188 2nd try - fix tabs (#190)
Browse files Browse the repository at this point in the history
* #188 2nd try

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

* update gitlab

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

* update gitlab

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

* lock argocd version

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

* lock argocd version

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

* Error out on missing secrets

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

* try fix #192

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

* try fix #192

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>
  • Loading branch information
6za committed Aug 4, 2022
1 parent b678ff3 commit bc347d4
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 93 deletions.
3 changes: 3 additions & 0 deletions cmd/createUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func setArgocdCreds(dryRun bool) {
argocdSecretClient = clientset.CoreV1().Secrets("argocd")

argocdPassword := getSecretValue(argocdSecretClient, "argocd-initial-admin-secret", "password")
if argocdPassword == "" {
log.Panicf("Missing argocdPassword")
}

viper.Set("argocd.admin.password", argocdPassword)
viper.Set("argocd.admin.username", "admin")
Expand Down
11 changes: 6 additions & 5 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package cmd
import (
"bytes"
"fmt"
"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/reports"
"github.com/spf13/cobra"
"log"
"runtime"
"strings"

"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/reports"
"github.com/spf13/cobra"
)

// infoCmd represents the info command
Expand Down Expand Up @@ -38,11 +39,11 @@ var infoCmd = &cobra.Command{

err := configs.CheckKubefirstConfigFile(config)
if err != nil {
log.Panic(err)
log.Println("Config file check:", err)
}
err = configs.CheckKubefirstDir(config)
if err != nil {
log.Panic(err)
log.Println("Installer dir check:", err)
}
fmt.Printf("----------- \n")

Expand Down
5 changes: 3 additions & 2 deletions configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type Config struct {
HelmVersion string

// todo: move it back
KubefirstVersion string
KubefirstVersion string
ArgoCDChartHelmVersion string

CertsPath string
}
Expand Down Expand Up @@ -70,7 +71,7 @@ func ReadConfig() *Config {
config.HelmClientPath = fmt.Sprintf("%s/tools/helm", config.K1FolderPath)
config.CertsPath = fmt.Sprintf("%s/ssl", config.K1FolderPath)
config.TerraformVersion = "1.0.11"

config.ArgoCDChartHelmVersion = "4.10.5"
// todo adopt latest helmVersion := "v3.9.0"
config.HelmVersion = "v3.2.1"

Expand Down
5 changes: 3 additions & 2 deletions internal/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,18 @@ func GetArgocdAuthToken(dryRun bool) string {
},
}

x := 5
x := 20
for i := 0; i < x; i++ {
log.Printf("requesting auth token from argocd: attempt %d of %d", i+1, x)
time.Sleep(2 * time.Second)
time.Sleep(5 * time.Second)
res, err := client.Do(req)

if err != nil {
log.Print("error requesting auth token from argocd", err)
continue
} else {
defer res.Body.Close()
log.Printf("Request ArgoCD Token: Result HTTP Status %d", res.StatusCode)
if res.StatusCode != http.StatusOK {
log.Print("HTTP status NOK")
continue
Expand Down
114 changes: 57 additions & 57 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gitlab

import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
Expand All @@ -10,15 +9,13 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/ghodss/yaml"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
Expand All @@ -36,7 +33,7 @@ import (
"golang.org/x/crypto/ssh"
)

// GenerateKey generate public and private keys to be consumed by GitLab.
// GenerateKey - generate public and private keys to be consumed by GitLab.
func GenerateKey() (string, string, error) {
reader := rand.Reader
bitSize := 2048
Expand All @@ -61,6 +58,7 @@ func GenerateKey() (string, string, error) {
return publicKey, privateKey, nil
}

// GitlabGeneratePersonalAccessToken - Generate a Access Token for Gitlab
func GitlabGeneratePersonalAccessToken(gitlabPodName string) {
config := configs.ReadConfig()

Expand All @@ -80,6 +78,8 @@ func GitlabGeneratePersonalAccessToken(gitlabPodName string) {
log.Println("gitlab personal access token generated", gitlabToken)
}

// PushGitOpsToGitLab - Push GitOps to Gitlab repository
// Use repo loaded from `init``
func PushGitOpsToGitLab(dryRun bool) {
cfg := configs.ReadConfig()
if dryRun {
Expand Down Expand Up @@ -146,11 +146,17 @@ func PushGitOpsToGitLab(dryRun bool) {

}

// AwaitHost - Await for a Host to be avialable, it wait for 200 cycles.
// Prefer to use `AwaitHostNTimes` as it provide more control
func AwaitHost(appName string, dryRun bool) {
log.Println("AwaitHost called")
AwaitHostNTimes(appName, dryRun, 200)
}

// AwaitHostNTimes - Wait for a Host to be responsive
// - To return 200
// - To return true if host is ready, or false if dont.
// - Supports to pass numbr of cycles to test
func AwaitHostNTimes(appName string, dryRun bool, times int) bool {
log.Println("AwaitHostNTimes called")
if dryRun {
Expand All @@ -176,6 +182,7 @@ func AwaitHostNTimes(appName string, dryRun bool, times int) bool {
return hostReady
}

// ProduceGitlabTokens - Produce Gitlab token from argoCD secret
func ProduceGitlabTokens(dryRun bool) {
if dryRun {
log.Printf("[#99] Dry-run mode, ProduceGitlabTokens skipped.")
Expand All @@ -197,6 +204,9 @@ func ProduceGitlabTokens(dryRun bool) {
k8s.ArgocdSecretClient = clientset.CoreV1().Secrets("argocd")

argocdPassword := k8s.GetSecretValue(k8s.ArgocdSecretClient, "argocd-initial-admin-secret", "password")
if argocdPassword == "" {
log.Panicf("Missing argocdPassword")
}

viper.Set("argocd.admin.password", argocdPassword)
viper.WriteConfig()
Expand All @@ -218,7 +228,9 @@ func ProduceGitlabTokens(dryRun bool) {
}
}
gitlabRootPassword := k8s.GetSecretValue(k8s.GitlabSecretClient, gitlabRootPasswordSecretName, "password")

if gitlabRootPassword == "" {
log.Panicf("Missing gitlabRootPassword")
}
viper.Set("gitlab.podname", gitlabPodName)
viper.Set("gitlab.root.password", gitlabRootPassword)
viper.WriteConfig()
Expand All @@ -238,6 +250,9 @@ func ProduceGitlabTokens(dryRun bool) {

log.Println("getting gitlab runner token")
gitlabRunnerRegistrationToken := k8s.GetSecretValue(k8s.GitlabSecretClient, "gitlab-gitlab-runner-secret", "runner-registration-token")
if gitlabRunnerRegistrationToken == "" {
log.Panicf("Missing gitlabRunnerRegistrationToken")
}
viper.Set("gitlab.runnertoken", gitlabRunnerRegistrationToken)
viper.WriteConfig()
}
Expand Down Expand Up @@ -386,7 +401,6 @@ func ChangeRegistryToGitLab(dryRun bool) {

creds := ArgocdGitCreds{PersonalAccessToken: pat, URL: url, FullURL: fullurl}

var argocdRepositoryAccessTokenSecret *v1.Secret
k8sConfig, err := clientcmd.BuildConfigFromFlags("", config.KubeConfigPath)
if err != nil {
log.Panicf("error getting client from kubeconfig")
Expand All @@ -397,65 +411,51 @@ func ChangeRegistryToGitLab(dryRun bool) {
}
k8s.ArgocdSecretClient = clientset.CoreV1().Secrets("argocd")

var secrets bytes.Buffer

c, err := template.New("creds-gitlab").Parse(`
apiVersion: v1
data:
password: {{ .PersonalAccessToken }}
url: {{ .URL }}
username: cm9vdA==
kind: Secret
metadata:
annotations:
managed-by: argocd.argoproj.io
labels:
argocd.argoproj.io/secret-type: repo-creds
name: creds-gitlab
namespace: argocd
type: Opaque
`)
if err := c.Execute(&secrets, creds); err != nil {
log.Panicf("error executing golang template for git repository credentials template %s", err)
}

ba := []byte(secrets.String())
err = yaml.Unmarshal(ba, &argocdRepositoryAccessTokenSecret)
if err != nil {
log.Println("error unmarshalling yaml during argocd repository secret create", err)
argocdRepositoryAccessTokenSecret := &v1.Secret{
ObjectMeta: metaV1.ObjectMeta{
Name: "creds-gitlab",
Namespace: "argocd",
Labels: map[string]string{
"argocd.argoproj.io/secret-type": "repo-creds",
},
Annotations: map[string]string{
"managed-by": "argocd.argoproj.io",
},
},
Data: map[string][]byte{
"password": []byte(creds.PersonalAccessToken),
"url": []byte(creds.URL),
"username": []byte("cm9vdA=="),
},
Type: "Opaque",
}

_ = k8s.ArgocdSecretClient.Delete(context.TODO(), "creds-gitlab", metaV1.DeleteOptions{})
_, err = k8s.ArgocdSecretClient.Create(context.TODO(), argocdRepositoryAccessTokenSecret, metaV1.CreateOptions{})
if err != nil {
log.Panicf("error creating argocd repository credentials template %s", err)
}

var repoSecrets bytes.Buffer

c, err = template.New("repo-gitlab").Parse(`
apiVersion: v1
data:
project: ZGVmYXVsdA==
type: Z2l0
url: {{ .FullURL }}
kind: Secret
metadata:
annotations:
managed-by: argocd.argoproj.io
labels:
argocd.argoproj.io/secret-type: repository
name: repo-gitlab
namespace: argocd
type: Opaque
`)
if err := c.Execute(&repoSecrets, creds); err != nil {
log.Panicf("error executing golang template for gitops repository template %s", err)
argocdRepoSecret := &v1.Secret{
ObjectMeta: metaV1.ObjectMeta{
Name: "repo-gitlab",
Namespace: "argocd",
Labels: map[string]string{
"argocd.argoproj.io/secret-type": "repository",
},
Annotations: map[string]string{
"managed-by": "argocd.argoproj.io",
},
},
Data: map[string][]byte{
"project": []byte("ZGVmYXVsdA=="),
"type": []byte("Z2l0"),
"url": []byte(creds.FullURL),
},
Type: "Opaque",
}

ba = []byte(repoSecrets.String())
err = yaml.Unmarshal(ba, &argocdRepositoryAccessTokenSecret)

_, err = k8s.ArgocdSecretClient.Create(context.TODO(), argocdRepositoryAccessTokenSecret, metaV1.CreateOptions{})
_ = k8s.ArgocdSecretClient.Delete(context.TODO(), "repo-gitlab", metaV1.DeleteOptions{})
_, err = k8s.ArgocdSecretClient.Create(context.TODO(), argocdRepoSecret, metaV1.CreateOptions{})
if err != nil {
log.Panicf("error creating argocd repository connection secret %s", err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package helm

import (
"fmt"
"log"

"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/viper"
"log"
)

func InstallArgocd(dryRun bool) {
Expand All @@ -26,7 +27,7 @@ func InstallArgocd(dryRun bool) {
log.Panicf("error: could not helm repo update %s", err)
}

_, _, err = pkg.ExecShellReturnStrings(config.HelmClientPath, "--kubeconfig", config.KubeConfigPath, "upgrade", "--install", "argocd", "--namespace", "argocd", "--create-namespace", "--wait", "--values", fmt.Sprintf("%s/argocd-init-values.yaml", config.K1FolderPath), "argo/argo-cd")
_, _, err = pkg.ExecShellReturnStrings(config.HelmClientPath, "--kubeconfig", config.KubeConfigPath, "upgrade", "--install", "argocd", "--namespace", "argocd", "--create-namespace", "--version", config.ArgoCDChartHelmVersion, "--wait", "--values", fmt.Sprintf("%s/argocd-init-values.yaml", config.K1FolderPath), "argo/argo-cd")
if err != nil {
log.Panicf("error: could not helm install argocd command %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/k8s/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func DeletePodByName(podsClient coreV1Types.PodInterface, podName string) {
}
}
func DeletePodByLabel(podsClient coreV1Types.PodInterface, label string) {
err := podsClient.DeleteCollection(context.TODO(),metaV1.DeleteOptions{}, metaV1.ListOptions{LabelSelector: label})
err := podsClient.DeleteCollection(context.TODO(), metaV1.DeleteOptions{}, metaV1.ListOptions{LabelSelector: label})
if err != nil {
log.Println(err)
} else {
log.Printf("Success delete of pods with label(%s).",label)
log.Printf("Success delete of pods with label(%s).", label)
}
}

Expand Down

0 comments on commit bc347d4

Please sign in to comment.