forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 3
/
serviceloadbalancers.go
266 lines (229 loc) · 7.9 KB
/
serviceloadbalancers.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"fmt"
"net/http"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
"k8s.io/kubernetes/test/e2e/framework"
"k8s.io/kubernetes/test/e2e/generated"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
// getLoadBalancerControllers returns a list of LBCtesters.
func getLoadBalancerControllers(client clientset.Interface) []LBCTester {
return []LBCTester{
&haproxyControllerTester{
name: "haproxy",
cfg: "test/e2e/testing-manifests/serviceloadbalancer/haproxyrc.yaml",
client: client,
},
}
}
// getIngManagers returns a list of ingManagers.
func getIngManagers(client clientset.Interface) []*ingManager {
return []*ingManager{
{
name: "netexec",
rcCfgPaths: []string{"test/e2e/testing-manifests/serviceloadbalancer/netexecrc.yaml"},
svcCfgPaths: []string{"test/e2e/testing-manifests/serviceloadbalancer/netexecsvc.yaml"},
svcNames: []string{},
client: client,
},
}
}
// LBCTester is an interface used to test loadbalancer controllers.
type LBCTester interface {
// start starts the loadbalancer controller in the given namespace
start(namespace string) error
// lookup returns the address (ip/hostname) associated with ingressKey
lookup(ingressKey string) string
// stop stops the loadbalancer controller
stop() error
// name returns the name of the loadbalancer
getName() string
}
// haproxyControllerTester implements LBCTester for bare metal haproxy LBs.
type haproxyControllerTester struct {
client clientset.Interface
cfg string
rcName string
rcNamespace string
name string
address []string
}
func (h *haproxyControllerTester) getName() string {
return h.name
}
func (h *haproxyControllerTester) start(namespace string) (err error) {
// Create a replication controller with the given configuration.
rc := rcFromManifest(h.cfg)
rc.Namespace = namespace
rc.Spec.Template.Labels["name"] = rc.Name
// Add the --namespace arg.
// TODO: Remove this when we have proper namespace support.
for i, c := range rc.Spec.Template.Spec.Containers {
rc.Spec.Template.Spec.Containers[i].Args = append(
c.Args, fmt.Sprintf("--namespace=%v", namespace))
framework.Logf("Container args %+v", rc.Spec.Template.Spec.Containers[i].Args)
}
rc, err = h.client.Core().ReplicationControllers(rc.Namespace).Create(rc)
if err != nil {
return
}
if err = framework.WaitForControlledPodsRunning(h.client, namespace, rc.Name, api.Kind("ReplicationController")); err != nil {
return
}
h.rcName = rc.Name
h.rcNamespace = rc.Namespace
// Find the pods of the rc we just created.
labelSelector := labels.SelectorFromSet(
labels.Set(map[string]string{"name": h.rcName}))
options := metav1.ListOptions{LabelSelector: labelSelector.String()}
pods, err := h.client.Core().Pods(h.rcNamespace).List(options)
if err != nil {
return err
}
// Find the external addresses of the nodes the pods are running on.
for _, p := range pods.Items {
wait.Poll(pollInterval, framework.ServiceRespondingTimeout, func() (bool, error) {
address, err := framework.GetHostExternalAddress(h.client, &p)
if err != nil {
framework.Logf("%v", err)
return false, nil
}
h.address = append(h.address, address)
return true, nil
})
}
if len(h.address) == 0 {
return fmt.Errorf("No external ips found for loadbalancer %v", h.getName())
}
return nil
}
func (h *haproxyControllerTester) stop() error {
return h.client.Core().ReplicationControllers(h.rcNamespace).Delete(h.rcName, nil)
}
func (h *haproxyControllerTester) lookup(ingressKey string) string {
// The address of a service is the address of the lb/servicename, currently.
return fmt.Sprintf("http://%v/%v", h.address[0], ingressKey)
}
// ingManager starts an rc and the associated service.
type ingManager struct {
rcCfgPaths []string
svcCfgPaths []string
ingCfgPath string
name string
namespace string
client clientset.Interface
svcNames []string
}
func (s *ingManager) getName() string {
return s.name
}
func (s *ingManager) start(namespace string) (err error) {
// Create rcs
for _, rcPath := range s.rcCfgPaths {
rc := rcFromManifest(rcPath)
rc.Namespace = namespace
rc.Spec.Template.Labels["name"] = rc.Name
rc, err = s.client.Core().ReplicationControllers(rc.Namespace).Create(rc)
if err != nil {
return
}
if err = framework.WaitForControlledPodsRunning(s.client, rc.Namespace, rc.Name, api.Kind("ReplicationController")); err != nil {
return
}
}
// Create services.
// Note that it's up to the caller to make sure the service actually matches
// the pods of the rc.
for _, svcPath := range s.svcCfgPaths {
svc := svcFromManifest(svcPath)
svc.Namespace = namespace
svc, err = s.client.Core().Services(svc.Namespace).Create(svc)
if err != nil {
return
}
// TODO: This is short term till we have an Ingress.
s.svcNames = append(s.svcNames, svc.Name)
}
s.name = s.svcNames[0]
s.namespace = namespace
return nil
}
func (s *ingManager) test(path string) error {
url := fmt.Sprintf("%v/hostName", path)
httpClient := &http.Client{}
return wait.Poll(pollInterval, framework.ServiceRespondingTimeout, func() (bool, error) {
body, err := framework.SimpleGET(httpClient, url, "")
if err != nil {
framework.Logf("%v\n%v\n%v", url, body, err)
return false, nil
}
return true, nil
})
}
var _ = framework.KubeDescribe("ServiceLoadBalancer [Feature:ServiceLoadBalancer]", func() {
// These variables are initialized after framework's beforeEach.
var ns string
var client clientset.Interface
f := framework.NewDefaultFramework("servicelb")
BeforeEach(func() {
client = f.ClientSet
ns = f.Namespace.Name
})
It("should support simple GET on Ingress ips", func() {
for _, t := range getLoadBalancerControllers(client) {
By(fmt.Sprintf("Starting loadbalancer controller %v in namespace %v", t.getName(), ns))
Expect(t.start(ns)).NotTo(HaveOccurred())
for _, s := range getIngManagers(client) {
By(fmt.Sprintf("Starting ingress manager %v in namespace %v", s.getName(), ns))
Expect(s.start(ns)).NotTo(HaveOccurred())
for _, sName := range s.svcNames {
path := t.lookup(sName)
framework.Logf("Testing path %v", path)
Expect(s.test(path)).NotTo(HaveOccurred())
}
}
Expect(t.stop()).NotTo(HaveOccurred())
}
})
})
// rcFromManifest reads a .json/yaml file and returns the rc in it.
func rcFromManifest(fileName string) *v1.ReplicationController {
var controller v1.ReplicationController
framework.Logf("Parsing rc from %v", fileName)
data := generated.ReadOrDie(fileName)
json, err := utilyaml.ToJSON(data)
Expect(err).NotTo(HaveOccurred())
Expect(runtime.DecodeInto(api.Codecs.UniversalDecoder(), json, &controller)).NotTo(HaveOccurred())
return &controller
}
// svcFromManifest reads a .json/yaml file and returns the rc in it.
func svcFromManifest(fileName string) *v1.Service {
var svc v1.Service
framework.Logf("Parsing service from %v", fileName)
data := generated.ReadOrDie(fileName)
json, err := utilyaml.ToJSON(data)
Expect(err).NotTo(HaveOccurred())
Expect(runtime.DecodeInto(api.Codecs.UniversalDecoder(), json, &svc)).NotTo(HaveOccurred())
return &svc
}