This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 558
/
Copy pathutil.go
101 lines (94 loc) · 3.03 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package util
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
)
func printCmd(cmd *exec.Cmd) {
fmt.Printf("\n$ %s\n", strings.Join(cmd.Args, " "))
}
// CreateFromTemplate processes and creates the provided templateName/templateNamespace template
// in the provided namespace.
func CreateFromTemplate(templateName, templateNamespace, namespace string) error {
processCmd := exec.Command("oc", "process", templateName, "-n", templateNamespace)
printCmd(processCmd)
out, err := processCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("cannot process template %s: %v\noutput: %s", templateName, err, string(out))
}
if err := ioutil.WriteFile(templateName, out, 0644); err != nil {
return fmt.Errorf("cannot create tempfile for processed template %s: %v", templateName, err)
}
defer os.Remove(templateName)
createCmd := exec.Command("oc", "create", "-n", namespace, "-f", templateName)
printCmd(createCmd)
out, err = createCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("cannot create processed template %s: %v\noutput: %s", templateName, err, string(out))
}
return nil
}
// WaitForDeploymentConfig waits until the provided deploymentconfig namespace/name
// gets deployed.
func WaitForDeploymentConfig(name, namespace string) error {
cmd := exec.Command("oc", "rollout", "status", fmt.Sprintf("dc/%s", name), "-n", namespace)
printCmd(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to see the rollout status of dc/%s: %s", name, string(out))
return err
}
return nil
}
// GetHost expects the name and namespace of a route in order to
// return its host.
func GetHost(name, namespace string) (string, error) {
cmd := exec.Command("oc", "get", fmt.Sprintf("route/%s", name), "-n", namespace, "-o", "jsonpath={.spec.host}")
printCmd(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to get the hostname of route/%s: %s", name, string(out))
return "", err
}
return string(out), nil
}
// TestHost tries to access host and retries maxRetries times with a retryDelay
// that is doubled on every retry.
func TestHost(host string, maxRetries int, retryDelay time.Duration) error {
backoff := retryDelay
url := fmt.Sprintf("http://%s", host)
resp, err := http.Get(url)
if err == nil && resp.StatusCode == http.StatusOK {
return nil
}
if err == nil {
log.Printf("got status %q while trying to access %s", resp.Status, host)
resp.Body.Close()
} else {
log.Printf("error while trying to access %s: %v", host, err)
}
for retries := 1; retries <= maxRetries; retries++ {
log.Printf("Retry #%d to access %s", retries, host)
resp, err = http.Get(url)
if err != nil {
log.Printf("error while trying to access %s: %v", host, err)
continue
}
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
log.Printf("got status %q while trying to access %s", resp.Status, host)
time.Sleep(backoff)
backoff *= 2
}
if err != nil {
return err
}
return fmt.Errorf("unexpected response status: %v", resp.Status)
}