/
router.go
167 lines (147 loc) · 5.5 KB
/
router.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
package router
import (
"net/http"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
kapierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
routeclientset "github.com/openshift/client-go/route/clientset/versioned"
exutil "github.com/openshift/origin/test/extended/util"
"github.com/openshift/origin/test/extended/util/url"
)
var _ = g.Describe("[Conformance][Area:Networking][Feature:Router]", func() {
defer g.GinkgoRecover()
var (
host, ns string
oc *exutil.CLI
configPath = exutil.FixturePath("testdata", "ingress.yaml")
)
// this hook must be registered before the framework namespace teardown
// hook
g.AfterEach(func() {
if g.CurrentGinkgoTestDescription().Failed {
currlabel := "router=router"
for _, ns := range []string{"default", "openshift-ingress", "tectonic-ingress"} {
//Search the router by label
if ns == "openshift-ingress" {
currlabel = "router=router-default"
}
exutil.DumpPodLogsStartingWithInNamespace("router", ns, oc.AsAdmin())
selector, err := labels.Parse(currlabel)
if err != nil {
panic(err)
}
exutil.DumpPodsCommand(oc.AdminKubeClient(), ns, selector, "cat /var/lib/haproxy/router/routes.json /var/lib/haproxy/conf/haproxy.config")
}
}
})
oc = exutil.NewCLI("router-stress", exutil.KubeConfigPath())
g.BeforeEach(func() {
var err error
host, err = waitForRouterServiceIP(oc)
o.Expect(err).NotTo(o.HaveOccurred())
ns = oc.KubeFramework().Namespace.Name
})
g.Describe("The HAProxy router", func() {
g.It("should respond with 503 to unrecognized hosts", func() {
t := url.NewTester(oc.AdminKubeClient(), ns)
defer t.Close()
t.Within(
time.Minute,
url.Expect("GET", "https://www.google.com").Through(host).SkipTLSVerification().HasStatusCode(503),
url.Expect("GET", "http://www.google.com").Through(host).HasStatusCode(503),
)
})
g.It("should serve routes that were created from an ingress", func() {
g.By("deploying an ingress rule")
err := oc.Run("create").Args("-f", configPath).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("waiting for the ingress rule to be converted to routes")
client := routeclientset.NewForConfigOrDie(oc.AdminConfig())
err = wait.Poll(time.Second, time.Minute, func() (bool, error) {
routes, err := client.RouteV1().Routes(ns).List(metav1.ListOptions{})
if err != nil {
return false, err
}
return len(routes.Items) == 4, nil
})
o.Expect(err).NotTo(o.HaveOccurred())
g.By("verifying the router reports the correct behavior")
t := url.NewTester(oc.AdminKubeClient(), ns)
defer t.Close()
t.Within(
3*time.Minute,
url.Expect("GET", "http://1.ingress-test.com/test").Through(host).HasStatusCode(200),
url.Expect("GET", "http://1.ingress-test.com/other/deep").Through(host).HasStatusCode(200),
url.Expect("GET", "http://1.ingress-test.com/").Through(host).HasStatusCode(503),
url.Expect("GET", "http://2.ingress-test.com/").Through(host).HasStatusCode(200),
url.Expect("GET", "https://3.ingress-test.com/").Through(host).SkipTLSVerification().HasStatusCode(200),
url.Expect("GET", "http://3.ingress-test.com/").Through(host).RedirectsTo("https://3.ingress-test.com/", http.StatusFound),
)
})
})
})
func waitForRouterInternalIP(oc *exutil.CLI) (string, error) {
return waitForNamedRouterServiceIP(oc, "router-internal-default")
}
func waitForRouterExternalIP(oc *exutil.CLI) (string, error) {
return waitForNamedRouterServiceIP(oc, "router-default")
}
func routerShouldHaveExternalService(oc *exutil.CLI) (bool, error) {
foundLoadBalancerServiceStrategyType := false
err := wait.PollImmediate(2*time.Second, 30*time.Second, func() (bool, error) {
ic, err := oc.AdminOperatorClient().OperatorV1().IngressControllers("openshift-ingress-operator").Get("default", metav1.GetOptions{})
if kapierrs.IsNotFound(err) {
return false, nil
}
o.Expect(err).NotTo(o.HaveOccurred())
if ic.Status.EndpointPublishingStrategy == nil {
return false, nil
}
if ic.Status.EndpointPublishingStrategy.Type == "LoadBalancerService" {
foundLoadBalancerServiceStrategyType = true
}
return true, nil
})
return foundLoadBalancerServiceStrategyType, err
}
func waitForRouterServiceIP(oc *exutil.CLI) (string, error) {
if useExternal, err := routerShouldHaveExternalService(oc); err != nil {
return "", err
} else if useExternal {
return waitForRouterExternalIP(oc)
}
return waitForRouterInternalIP(oc)
}
func waitForNamedRouterServiceIP(oc *exutil.CLI, name string) (string, error) {
_, ns, err := exutil.GetRouterPodTemplate(oc)
if err != nil {
return "", err
}
// wait for the service to show up
var host string
err = wait.PollImmediate(2*time.Second, 60*time.Second, func() (bool, error) {
svc, err := oc.AdminKubeClient().CoreV1().Services(ns).Get(name, metav1.GetOptions{})
if kapierrs.IsNotFound(err) {
// see if an older service named 'router' exists.
svc, err = oc.AdminKubeClient().CoreV1().Services(ns).Get("router", metav1.GetOptions{})
if kapierrs.IsNotFound(err) {
return false, nil
}
}
o.Expect(err).NotTo(o.HaveOccurred())
host = svc.Spec.ClusterIP
if svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
if len(svc.Status.LoadBalancer.Ingress) == 0 || len(svc.Status.LoadBalancer.Ingress[0].Hostname) == 0 {
return false, nil
}
host = svc.Status.LoadBalancer.Ingress[0].Hostname
}
return true, nil
})
return host, err
}