Skip to content

Commit

Permalink
Vault unseal tweak 0718 (#121)
Browse files Browse the repository at this point in the history
* allow the retry to work as expected
* re-enable dry-run create

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>
  • Loading branch information
6za authored Jul 18, 2022
1 parent 2e6c948 commit bb5745e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
6 changes: 3 additions & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ to quickly create a Cobra application.`,
log.Panicf("error: failed to port-forward to vault in main thread %s", err)
}
}
loopUntilPodIsReady()
initializeVaultAndAutoUnseal()
loopUntilPodIsReady(dryRun)
initializeVaultAndAutoUnseal(dryRun)
informUser(fmt.Sprintf("Vault available at %s", viper.GetString("vault.local.service")))
progressPrinter.IncrementTracker("step-gitlab", 1)

Expand Down Expand Up @@ -252,7 +252,7 @@ to quickly create a Cobra application.`,
viper.Set("gitlab.gitops-pushed", true)
viper.WriteConfig()
}
if !viper.GetBool("argocd.oidc-patched") {
if !dryRun && !viper.GetBool("argocd.oidc-patched") {
cfg := configs.ReadConfig()
config, err := clientcmd.BuildConfigFromFlags("", cfg.KubeConfigPath)
if err != nil {
Expand Down
14 changes: 11 additions & 3 deletions cmd/createUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ func waitVaultToBeRunning(dryRun bool) {
}
}

func loopUntilPodIsReady() {
func loopUntilPodIsReady(dryRun bool) {
if dryRun {
log.Printf("[#99] Dry-run mode, loopUntilPodIsReady skipped.")
return
}

x := 50
url := "http://localhost:8200/v1/sys/health"
Expand All @@ -153,7 +157,7 @@ func loopUntilPodIsReady() {
log.Println("vault is availbale but the body is not what is expected ", err)
continue
}
fmt.Println(string(body))
log.Println(string(body))

var responseJson map[string]interface{}

Expand Down Expand Up @@ -196,7 +200,11 @@ type VaultUnsealResponse struct {
KeysB64 []string `json:"keys_base64"`
}

func initializeVaultAndAutoUnseal() {
func initializeVaultAndAutoUnseal(dryRun bool) {
if dryRun {
log.Printf("[#99] Dry-run mode, initializeVaultAndAutoUnseal skipped.")
return
}
url := "http://127.0.0.1:8200/v1/sys/init"

payload := strings.NewReader("{\n\t\"stored_shares\": 3,\n\t\"recovery_threshold\": 3,\n\t\"recovery_shares\": 5\n}")
Expand Down
19 changes: 14 additions & 5 deletions internal/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,26 @@ func GetArgocdAuthToken(dryRun bool) string {

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

if err != nil {
log.Panic("error requesting auth token from argocd", err)
} else {
defer res.Body.Close()
log.Print("error requesting auth token from argocd", err)
continue
} else {
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Panic("error sending POST request to get argocd auth token :", err)
log.Print("error sending POST request to get argocd auth token:", err)
continue
}

var dat map[string]interface{}

if err := json.Unmarshal(body, &dat); err != nil {
log.Panicf("error unmarshalling %s", err)
log.Print("error unmarshalling %s", err)
continue
}
token := dat["token"]
viper.Set("argocd.admin.apitoken", token)
Expand All @@ -168,6 +174,9 @@ func GetArgocdAuthToken(dryRun bool) string {
return token.(string)
}
}
log.Panic("Fail to get a token")
// This code is unreacheble, as in absence of token we want to fail the install.
// I kept is to avoid compiler to complain.
return ""
}

Expand Down
8 changes: 4 additions & 4 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func AwaitGitlab(dryRun bool) {
}

func ProduceGitlabTokens(dryRun bool) {
if dryRun {
log.Printf("[#99] Dry-run mode, ProduceGitlabTokens skipped.")
return
}
//TODO: Should this step be skipped if already executed?
config := configs.ReadConfig()
k8sConfig, err := clientcmd.BuildConfigFromFlags("", config.KubeConfigPath)
Expand All @@ -180,10 +184,6 @@ func ProduceGitlabTokens(dryRun bool) {
log.Panic(err.Error())
}
log.Println("discovering gitlab toolbox pod")
if dryRun {
log.Printf("[#99] Dry-run mode, ProduceGitlabTokens skipped.")
return
}
time.Sleep(30 * time.Second)
// todo: move it to config
k8s.ArgocdSecretClient = clientset.CoreV1().Secrets("argocd")
Expand Down
4 changes: 2 additions & 2 deletions internal/k8s/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var ArgocdSecretClient coreV1Types.SecretInterface
func GetPodNameByLabel(podsClient coreV1Types.PodInterface, label string) string {
pods, err := podsClient.List(context.TODO(), metaV1.ListOptions{LabelSelector: label})
if err != nil {
fmt.Println(err)
log.Println(err)
}

gitlabToolboxPodName = pods.Items[0].Name
Expand All @@ -42,7 +42,7 @@ func GetPodNameByLabel(podsClient coreV1Types.PodInterface, label string) string
func DeletePodByName(podsClient coreV1Types.PodInterface, podName string) {
err := podsClient.Delete(context.TODO(), podName, metaV1.DeleteOptions{})
if err != nil {
fmt.Println(err)
log.Println(err)
}
}

Expand Down

0 comments on commit bb5745e

Please sign in to comment.