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 560
/
util.go
148 lines (137 loc) · 4.39 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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)
}
// DumpNodes dumps information about nodes.
func DumpNodes() (string, error) {
cmd := exec.Command("oc", "get", "nodes", "-o", "wide")
printCmd(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to list nodes: %s", string(out))
return "", err
}
return string(out), nil
}
// DumpPods dumps the pods from all namespaces.
func DumpPods() (string, error) {
cmd := exec.Command("oc", "get", "pods", "--all-namespaces", "-o", "wide")
printCmd(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to list pods from all namespaces: %s", string(out))
return "", err
}
return string(out), nil
}
// RunDiagnostics runs the openshift diagnostics command.
func RunDiagnostics() (string, error) {
cmd := exec.Command("oc", "adm", "diagnostics")
printCmd(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to run diagnostics: %s", string(out))
return "", err
}
return string(out), nil
}
// FetchLogs returns logs for the provided kind/name in namespace.
func FetchLogs(kind, namespace, name string) string {
cmd := exec.Command("oc", "logs", fmt.Sprintf("%s/%s", kind, name), "-n", namespace)
printCmd(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Sprintf("Error trying to fetch logs from %s/%s in %s: %s", kind, name, namespace, string(out))
}
return string(out)
}