Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix retrial logic #187

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ logs/
/tmp
lint_log.txt
credentials
kubefirst
25 changes: 19 additions & 6 deletions internal/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/spf13/viper"
"io/ioutil"
"log"
"net/http"

"github.com/kubefirst/kubefirst/pkg"
"github.com/spf13/viper"

"strings"
"time"

"github.com/kubefirst/kubefirst/pkg"
)

type ArgoCDConfig struct {
Expand Down Expand Up @@ -197,27 +199,38 @@ func GetArgocdAuthToken(dryRun bool) string {
},
}

x := 3
x := 5
for i := 0; i < x; i++ {
log.Printf("requesting auth token from argocd: attempt %d of %d", i+1, x)
time.Sleep(1 * time.Second)
time.Sleep(2 * 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()
if res.StatusCode != http.StatusOK {
log.Print("HTTP status NOK")
continue
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Print("error sending POST request to get argocd auth token:", err)
continue
}

var dat map[string]interface{}

if body == nil {
log.Print("body object is nil")
continue
}
if err := json.Unmarshal(body, &dat); err != nil {
log.Print("error unmarshalling %s", err)
log.Printf("error unmarshalling %s", err)
continue
}
if dat == nil {
log.Print("dat object is nil")
continue
}
token := dat["token"]
Expand Down
170 changes: 87 additions & 83 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,99 +373,103 @@ func ChangeRegistryToGitLab(dryRun bool) {
return
}

type ArgocdGitCreds struct {
PersonalAccessToken string
URL string
FullURL string
}
if !viper.GetBool("gitlab.changeregistry.gitlab") {
type ArgocdGitCreds struct {
PersonalAccessToken string
URL string
FullURL string
}

pat := b64.StdEncoding.EncodeToString([]byte(viper.GetString("gitlab.token")))
url := b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("https://gitlab.%s/kubefirst/", viper.GetString("aws.hostedzonename"))))
fullurl := b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("https://gitlab.%s/kubefirst/gitops.git", viper.GetString("aws.hostedzonename"))))
pat := b64.StdEncoding.EncodeToString([]byte(viper.GetString("gitlab.token")))
url := b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("https://gitlab.%s/kubefirst/", viper.GetString("aws.hostedzonename"))))
fullurl := b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("https://gitlab.%s/kubefirst/gitops.git", viper.GetString("aws.hostedzonename"))))

creds := ArgocdGitCreds{PersonalAccessToken: pat, URL: url, FullURL: fullurl}
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")
}
clientset, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
log.Panicf("error getting kubeconfig for clientset")
}
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)
}
var argocdRepositoryAccessTokenSecret *v1.Secret
k8sConfig, err := clientcmd.BuildConfigFromFlags("", config.KubeConfigPath)
if err != nil {
log.Panicf("error getting client from kubeconfig")
}
clientset, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
log.Panicf("error getting kubeconfig for clientset")
}
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)
}
ba := []byte(secrets.String())
err = yaml.Unmarshal(ba, &argocdRepositoryAccessTokenSecret)
if err != nil {
log.Println("error unmarshalling yaml during argocd repository secret create", err)
}

_, err = k8s.ArgocdSecretClient.Create(context.TODO(), argocdRepositoryAccessTokenSecret, metaV1.CreateOptions{})
if err != nil {
log.Panicf("error creating argocd repository credentials template %s", err)
}
_, 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)
}
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)
}

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

_, err = k8s.ArgocdSecretClient.Create(context.TODO(), argocdRepositoryAccessTokenSecret, metaV1.CreateOptions{})
if err != nil {
log.Panicf("error creating argocd repository connection secret %s", err)
}
_, err = k8s.ArgocdSecretClient.Create(context.TODO(), argocdRepositoryAccessTokenSecret, metaV1.CreateOptions{})
if err != nil {
log.Panicf("error creating argocd repository connection secret %s", err)
}

// curl -X 'DELETE' \
// 'https://$ARGO_ADDRESS/api/v1/applications/registry?cascade=false' \
// -H 'accept: application/json'
// curl -X 'DELETE' \
// 'https://$ARGO_ADDRESS/api/v1/applications/registry?cascade=false' \
// -H 'accept: application/json'

_, _, err = pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "argocd", "apply", "-f", fmt.Sprintf("%s/gitops/components/gitlab/argocd-adopts-gitlab.yaml", config.K1FolderPath))
if err != nil {
log.Panicf("failed to call execute kubectl apply of argocd patch to adopt gitlab: %s", err)
_, _, err = pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "argocd", "apply", "-f", fmt.Sprintf("%s/gitops/components/gitlab/argocd-adopts-gitlab.yaml", config.K1FolderPath))
if err != nil {
log.Panicf("failed to call execute kubectl apply of argocd patch to adopt gitlab: %s", err)
}
viper.Set("gitlab.changeregistry.gitlab", true)
viper.WriteConfig()
}

}
Expand Down