forked from openshift/origin
/
config_manager.go
169 lines (144 loc) · 5.61 KB
/
config_manager.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package router
import (
"fmt"
"strings"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
e2e "k8s.io/kubernetes/test/e2e/framework"
routeclientset "github.com/openshift/client-go/route/clientset/versioned"
exutil "github.com/openshift/origin/test/extended/util"
)
const timeoutSeconds = 3 * 60
var _ = g.Describe("[Conformance][Area:Networking][Feature:Router]", func() {
defer g.GinkgoRecover()
var (
configPath = exutil.FixturePath("testdata", "router", "router-config-manager.yaml")
oc *exutil.CLI
ns string
)
// this hook must be registered before the framework namespace teardown
// hook
g.AfterEach(func() {
if g.CurrentGinkgoTestDescription().Failed {
client := routeclientset.NewForConfigOrDie(oc.AdminConfig()).RouteV1().Routes(ns)
if routes, _ := client.List(metav1.ListOptions{}); routes != nil {
outputIngress(routes.Items...)
}
exutil.DumpPodLogsStartingWith("router-", oc)
}
})
oc = exutil.NewCLI("router-config-manager", exutil.KubeConfigPath())
g.BeforeEach(func() {
ns = oc.Namespace()
routerImage, err := exutil.FindRouterImage(oc)
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.AsAdmin().Run("new-app").Args("-f", configPath, "-p", "IMAGE="+routerImage).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
})
g.Describe("The HAProxy router", func() {
g.It("should serve the correct routes when running with the haproxy config manager", func() {
g.Skip("TODO: This test is flaking, fix it")
ns := oc.KubeFramework().Namespace.Name
execPodName := exutil.CreateExecPodOrFail(oc.AdminKubeClient().CoreV1(), ns, "execpod")
defer func() { oc.AdminKubeClient().CoreV1().Pods(ns).Delete(execPodName, metav1.NewDeleteOptions(1)) }()
g.By(fmt.Sprintf("creating a router with haproxy config manager from a config file %q", configPath))
var routerIP string
err := wait.Poll(time.Second, timeoutSeconds*time.Second, func() (bool, error) {
pod, err := oc.KubeFramework().ClientSet.CoreV1().Pods(oc.KubeFramework().Namespace.Name).Get("router-haproxy-cfgmgr", metav1.GetOptions{})
if err != nil {
return false, err
}
if len(pod.Status.PodIP) == 0 {
return false, nil
}
routerIP = pod.Status.PodIP
return true, nil
})
o.Expect(err).NotTo(o.HaveOccurred())
g.By("waiting for the healthz endpoint to respond")
healthzURI := fmt.Sprintf("http://%s:1936/healthz", routerIP)
err = waitForRouterOKResponseExec(ns, execPodName, healthzURI, routerIP, timeoutSeconds)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("waiting for the valid routes to respond")
err = waitForRouteToRespond(ns, execPodName, "http", "insecure.hapcm.test", "/", routerIP, 0)
o.Expect(err).NotTo(o.HaveOccurred())
for _, host := range []string{"edge.allow.hapcm.test", "reencrypt.hapcm.test", "passthrough.hapcm.test"} {
err = waitForRouteToRespond(ns, execPodName, "https", host, "/", routerIP, 0)
o.Expect(err).NotTo(o.HaveOccurred())
}
g.By("mini stress test by adding (and removing) different routes and checking that they are exposed")
for i := 0; i < 16; i++ {
name := fmt.Sprintf("hapcm-stress-insecure-%d", i)
hostName := fmt.Sprintf("stress.insecure-%d.hapcm.test", i)
err := oc.AsAdmin().Run("expose").Args("service", "insecure-service", "--name", name, "--hostname", hostName, "--labels", "select=haproxy-cfgmgr").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = waitForRouteToRespond(ns, execPodName, "http", hostName, "/", routerIP, 0)
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.AsAdmin().Run("delete").Args("route", name).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
routeTypes := []string{"edge", "reencrypt", "passthrough"}
for _, t := range routeTypes {
name := fmt.Sprintf("hapcm-stress-%s-%d", t, i)
hostName := fmt.Sprintf("stress.%s-%d.hapcm.test", t, i)
serviceName := "secure-service"
if t == "edge" {
serviceName = "insecure-service"
}
err := oc.AsAdmin().Run("create").Args("route", t, name, "--service", serviceName, "--hostname", hostName).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.AsAdmin().Run("label").Args("route", name, "select=haproxy-cfgmgr").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = waitForRouteToRespond(ns, execPodName, "https", hostName, "/", routerIP, 0)
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.AsAdmin().Run("delete").Args("route", name).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
}
}
})
})
})
func waitForRouteToRespond(ns, execPodName, proto, host, abspath, ipaddr string, port int) error {
if port == 0 {
switch proto {
case "http":
port = 80
case "https":
port = 443
default:
port = 80
}
}
uri := fmt.Sprintf("%s://%s:%d%s", proto, host, port, abspath)
cmd := fmt.Sprintf(`
set -e
STOP=$(($(date '+%%s') + %d))
while [ $(date '+%%s') -lt $STOP ]; do
rc=0
code=$( curl -k -s -m 5 -o /dev/null -w '%%{http_code}\n' --resolve %s:%d:%s %q ) || rc=$?
if [[ "${rc:-0}" -eq 0 ]]; then
echo $code
if [[ $code -eq 200 ]]; then
exit 0
fi
if [[ $code -ne 503 ]]; then
exit 1
fi
else
echo "error ${rc}" 1>&2
fi
sleep 1
done
`, timeoutSeconds, host, port, ipaddr, uri)
output, err := e2e.RunHostCmd(ns, execPodName, cmd)
if err != nil {
return fmt.Errorf("host command failed: %v\n%s", err, output)
}
lines := strings.Split(strings.TrimSpace(output), "\n")
if lines[len(lines)-1] != "200" {
return fmt.Errorf("last response from server was not 200:\n%s", output)
}
return nil
}