forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
63 lines (54 loc) · 1.53 KB
/
common.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
package db
import (
"fmt"
"os/exec"
"strings"
"github.com/openshift/origin/test/extended/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kcoreclient "k8s.io/client-go/kubernetes/typed/core/v1"
)
// PodConfig holds configuration for a pod.
type PodConfig struct {
Container string
Env map[string]string
}
func getPodConfig(c kcoreclient.PodInterface, podName string) (conf *PodConfig, err error) {
pod, err := c.Get(podName, metav1.GetOptions{})
if err != nil {
return nil, err
}
env := make(map[string]string)
for _, container := range pod.Spec.Containers {
for _, e := range container.Env {
env[e.Name] = e.Value
}
}
return &PodConfig{pod.Spec.Containers[0].Name, env}, nil
}
func firstContainerName(c kcoreclient.PodInterface, podName string) (string, error) {
pod, err := c.Get(podName, metav1.GetOptions{})
if err != nil {
return "", err
}
return pod.Spec.Containers[0].Name, nil
}
func isReady(oc *util.CLI, podName string, pingCommand, expectedOutput string) (bool, error) {
out, err := executeShellCommand(oc, podName, pingCommand)
ok := strings.Contains(out, expectedOutput)
if !ok {
err = fmt.Errorf("Expected output: %q but actual: %q", expectedOutput, out)
}
return ok, err
}
func executeShellCommand(oc *util.CLI, podName string, command string) (string, error) {
out, err := oc.Run("exec").Args(podName, "--", "bash", "-c", command).Output()
if err != nil {
switch err.(type) {
case *util.ExitError, *exec.ExitError:
return "", nil
default:
return "", err
}
}
return out, nil
}