Skip to content

Commit 7626274

Browse files
authored
Mvp/fixes for vault (#136)
* remove hardcoded aws profile, change default name * fix for dns test logic and add vault skips
1 parent 4852398 commit 7626274

File tree

3 files changed

+118
-117
lines changed

3 files changed

+118
-117
lines changed

cmd/createUtils.go

+92-78
Original file line numberDiff line numberDiff line change
@@ -100,32 +100,37 @@ func waitVaultToBeRunning(dryRun bool) {
100100
log.Printf("[#99] Dry-run mode, waitVaultToBeRunning skipped.")
101101
return
102102
}
103-
config := configs.ReadConfig()
104-
x := 50
105-
for i := 0; i < x; i++ {
106-
_, _, err := pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "get", "namespace/vault")
107-
if err != nil {
108-
log.Println("Waiting vault to be born")
109-
time.Sleep(10 * time.Second)
110-
} else {
111-
log.Println("vault namespace found, continuing")
112-
time.Sleep(25 * time.Second)
113-
break
103+
token := viper.GetString("vault.token")
104+
if len(token) == 0 {
105+
config := configs.ReadConfig()
106+
x := 50
107+
for i := 0; i < x; i++ {
108+
_, _, err := pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "get", "namespace/vault")
109+
if err != nil {
110+
log.Println("Waiting vault to be born")
111+
time.Sleep(10 * time.Second)
112+
} else {
113+
log.Println("vault namespace found, continuing")
114+
time.Sleep(25 * time.Second)
115+
break
116+
}
114117
}
115-
}
116118

117-
//! failing
118-
x = 50
119-
for i := 0; i < x; i++ {
120-
_, _, err := pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "vault", "get", "pods", "-l", "app.kubernetes.io/instance=vault")
121-
if err != nil {
122-
log.Println("Waiting vault pods to create")
123-
time.Sleep(10 * time.Second)
124-
} else {
125-
log.Println("vault pods found, continuing")
126-
time.Sleep(15 * time.Second)
127-
break
119+
//! failing
120+
x = 50
121+
for i := 0; i < x; i++ {
122+
_, _, err := pkg.ExecShellReturnStrings(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "vault", "get", "pods", "-l", "app.kubernetes.io/instance=vault")
123+
if err != nil {
124+
log.Println("Waiting vault pods to create")
125+
time.Sleep(10 * time.Second)
126+
} else {
127+
log.Println("vault pods found, continuing")
128+
time.Sleep(15 * time.Second)
129+
break
130+
}
128131
}
132+
} else {
133+
log.Println("vault token arleady exists, skipping vault health checks waitVaultToBeRunning")
129134
}
130135
}
131136

@@ -134,45 +139,49 @@ func loopUntilPodIsReady(dryRun bool) {
134139
log.Printf("[#99] Dry-run mode, loopUntilPodIsReady skipped.")
135140
return
136141
}
142+
token := viper.GetString("vault.token")
143+
if len(token) == 0 {
137144

138-
x := 50
139-
url := "http://localhost:8200/v1/sys/health"
140-
for i := 0; i < x; i++ {
141-
log.Println("vault is not ready yet, sleeping and checking again")
142-
time.Sleep(10 * time.Second)
145+
x := 50
146+
url := "http://localhost:8200/v1/sys/health"
147+
for i := 0; i < x; i++ {
148+
log.Println("vault is not ready yet, sleeping and checking again")
149+
time.Sleep(10 * time.Second)
143150

144-
req, _ := http.NewRequest("GET", url, nil)
151+
req, _ := http.NewRequest("GET", url, nil)
145152

146-
req.Header.Add("Content-Type", "application/json")
153+
req.Header.Add("Content-Type", "application/json")
147154

148-
res, err := http.DefaultClient.Do(req)
149-
if err != nil {
150-
log.Println("error with http request Do, vault is not available", err)
151-
continue
152-
}
155+
res, err := http.DefaultClient.Do(req)
156+
if err != nil {
157+
log.Println("error with http request Do, vault is not available", err)
158+
continue
159+
}
153160

154-
defer res.Body.Close()
155-
body, err := ioutil.ReadAll(res.Body)
156-
if err != nil {
157-
log.Println("vault is availbale but the body is not what is expected ", err)
158-
continue
159-
}
160-
log.Println(string(body))
161+
defer res.Body.Close()
162+
body, err := ioutil.ReadAll(res.Body)
163+
if err != nil {
164+
log.Println("vault is availbale but the body is not what is expected ", err)
165+
continue
166+
}
161167

162-
var responseJson map[string]interface{}
168+
var responseJson map[string]interface{}
163169

164-
if err := json.Unmarshal(body, &responseJson); err != nil {
165-
log.Printf("vault is availbale but the body is not what is expected %s", err)
166-
continue
167-
}
170+
if err := json.Unmarshal(body, &responseJson); err != nil {
171+
log.Printf("vault is availbale but the body is not what is expected %s", err)
172+
continue
173+
}
168174

169-
_, ok := responseJson["initialized"]
170-
if ok {
171-
log.Printf("vault is initialized and is in the expected state")
172-
return
175+
_, ok := responseJson["initialized"]
176+
if ok {
177+
log.Printf("vault is initialized and is in the expected state")
178+
return
179+
}
180+
log.Panic("vault was never initialized")
173181
}
182+
} else {
183+
log.Println("vault token arleady exists, skipping vault health checks loopUntilPodIsReady")
174184
}
175-
log.Panic("vault was never initialized")
176185
}
177186

178187
type VaultInitResponse struct {
@@ -206,41 +215,46 @@ func initializeVaultAndAutoUnseal(dryRun bool) {
206215
return
207216
}
208217

209-
time.Sleep(time.Second * 10)
210-
url := "http://127.0.0.1:8200/v1/sys/init"
218+
token := viper.GetString("vault.token")
219+
if len(token) == 0 {
211220

212-
payload := strings.NewReader("{\n\t\"stored_shares\": 3,\n\t\"recovery_threshold\": 3,\n\t\"recovery_shares\": 5\n}")
221+
time.Sleep(time.Second * 10)
222+
url := "http://127.0.0.1:8200/v1/sys/init"
213223

214-
req, err := http.NewRequest("POST", url, payload)
215-
if err != nil {
216-
log.Panic(err)
217-
}
224+
payload := strings.NewReader("{\n\t\"stored_shares\": 3,\n\t\"recovery_threshold\": 3,\n\t\"recovery_shares\": 5\n}")
218225

219-
req.Header.Add("Content-Type", "application/json")
226+
req, err := http.NewRequest("POST", url, payload)
227+
if err != nil {
228+
log.Panic(err)
229+
}
220230

221-
res, err := http.DefaultClient.Do(req)
222-
if err != nil {
223-
log.Println("error in Do http client request", err)
224-
}
231+
req.Header.Add("Content-Type", "application/json")
225232

226-
defer res.Body.Close()
227-
body, err := ioutil.ReadAll(res.Body)
228-
if err != nil {
229-
log.Panic(err)
230-
}
233+
res, err := http.DefaultClient.Do(req)
234+
if err != nil {
235+
log.Println("error in Do http client request", err)
236+
}
231237

232-
log.Println(string(body))
238+
defer res.Body.Close()
239+
body, err := ioutil.ReadAll(res.Body)
240+
if err != nil {
241+
log.Panic(err)
242+
}
233243

244+
log.Println(string(body))
234245

235-
vaultResponse := VaultUnsealResponse{}
236-
err = json.Unmarshal(body, &vaultResponse)
237-
if err != nil {
238-
log.Panic(err)
239-
}
246+
vaultResponse := VaultUnsealResponse{}
247+
err = json.Unmarshal(body, &vaultResponse)
248+
if err != nil {
249+
log.Panic(err)
250+
}
240251

241-
viper.Set("vault.token", vaultResponse.RootToken)
242-
viper.Set("vault.unseal-keys", vaultResponse)
243-
viper.WriteConfig()
252+
viper.Set("vault.token", vaultResponse.RootToken)
253+
viper.Set("vault.unseal-keys", vaultResponse)
254+
viper.WriteConfig()
255+
} else {
256+
log.Println("vault token already exists, continuing")
257+
}
244258
}
245259

246260
func waitGitlabToBeReady(dryRun bool) {

internal/aws/aws.go

+24-37
Original file line numberDiff line numberDiff line change
@@ -124,49 +124,36 @@ func TestHostedZoneLiveness(dryRun bool, hostedZoneName, hostedZoneId string) {
124124
log.Println("checking to see if record", route53RecordName, "exists")
125125
log.Println("hostedZoneId", hostedZoneId)
126126
log.Println("route53RecordName", route53RecordName)
127-
128-
recordList, err := route53Client.ListResourceRecordSets(context.TODO(), &route53.ListResourceRecordSetsInput{
129-
HostedZoneId: aws.String(hostedZoneId),
130-
StartRecordName: aws.String(route53RecordName),
131-
StartRecordType: "TXT",
132-
})
133-
if err != nil {
134-
log.Println("failed read route53 ", err.Error())
135-
os.Exit(1)
136-
}
137-
138-
if len(recordList.ResourceRecordSets) == 0 {
139-
if !dryRun {
140-
record, err := route53Client.ChangeResourceRecordSets(context.TODO(), &route53.ChangeResourceRecordSetsInput{
141-
ChangeBatch: &types.ChangeBatch{
142-
Changes: []types.Change{
143-
{
144-
Action: "CREATE",
145-
ResourceRecordSet: &types.ResourceRecordSet{
146-
Name: aws.String(route53RecordName),
147-
Type: "TXT",
148-
ResourceRecords: []types.ResourceRecord{
149-
{
150-
Value: aws.String(strconv.Quote(route53RecordValue)),
151-
},
127+
if !dryRun {
128+
record, err := route53Client.ChangeResourceRecordSets(context.TODO(), &route53.ChangeResourceRecordSetsInput{
129+
ChangeBatch: &types.ChangeBatch{
130+
Changes: []types.Change{
131+
{
132+
Action: "UPSERT",
133+
ResourceRecordSet: &types.ResourceRecordSet{
134+
Name: aws.String(route53RecordName),
135+
Type: "TXT",
136+
ResourceRecords: []types.ResourceRecord{
137+
{
138+
Value: aws.String(strconv.Quote(route53RecordValue)),
152139
},
153-
TTL: aws.Int64(10),
154-
Weight: aws.Int64(100),
155-
SetIdentifier: aws.String("CREATE sanity check for kubefirst installation"),
156140
},
141+
TTL: aws.Int64(10),
142+
Weight: aws.Int64(100),
143+
SetIdentifier: aws.String("CREATE sanity check for kubefirst installation"),
157144
},
158145
},
159-
Comment: aws.String("CREATE sanity check dns record."),
160146
},
161-
HostedZoneId: aws.String(hostedZoneId),
162-
})
163-
if err != nil {
164-
log.Println(err)
165-
}
166-
log.Println("record creation status is ", record.ChangeInfo.Status)
167-
} else {
168-
log.Printf("[#99] Dry-run mode, route53 creation/update skipped: %s", route53RecordName)
147+
Comment: aws.String("CREATE sanity check dns record."),
148+
},
149+
HostedZoneId: aws.String(hostedZoneId),
150+
})
151+
if err != nil {
152+
log.Println(err)
169153
}
154+
log.Println("record creation status is ", record.ChangeInfo.Status)
155+
} else {
156+
log.Printf("[#99] Dry-run mode, route53 creation/update skipped: %s", route53RecordName)
170157
}
171158
count := 0
172159
// todo need to exit after n number of minutes and tell them to check ns records

internal/vault/vault.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func ConfigureVault(dryRun bool) {
6767
err := kPortForward.Start()
6868
defer kPortForward.Process.Signal(syscall.SIGTERM)
6969
if err != nil {
70-
log.Println("Commad Execution STDOUT: %s", kPortForwardOutb.String())
71-
log.Println("Commad Execution STDERR: %s", kPortForwardErrb.String())
70+
log.Printf("Commad Execution STDOUT: %s", kPortForwardOutb.String())
71+
log.Printf("Commad Execution STDERR: %s", kPortForwardErrb.String())
7272
log.Panicf("error: failed to port-forward to vault namespce svc/vault %s", err)
7373
}
7474

0 commit comments

Comments
 (0)