forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
276 lines (233 loc) · 8.39 KB
/
helpers.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
267
268
269
270
271
272
273
274
275
276
package templates
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
kapi "k8s.io/kubernetes/pkg/api"
kapiv1 "k8s.io/kubernetes/pkg/api/v1"
e2e "k8s.io/kubernetes/test/e2e/framework"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/pkg/config/cmd"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
"github.com/openshift/origin/pkg/template/controller"
osbclient "github.com/openshift/origin/pkg/templateservicebroker/openservicebroker/client"
userapi "github.com/openshift/origin/pkg/user/apis/user"
restutil "github.com/openshift/origin/pkg/util/rest"
exutil "github.com/openshift/origin/test/extended/util"
)
func createUser(cli *exutil.CLI, name, role string) *userapi.User {
name = cli.Namespace() + "-" + name
user, err := cli.AdminUserClient().User().Users().Create(&userapi.User{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
})
o.Expect(err).NotTo(o.HaveOccurred())
if role != "" {
_, err = cli.AdminAuthorizationClient().Authorization().RoleBindings(cli.Namespace()).Create(&authorizationapi.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s-binding", name, role),
},
RoleRef: kapi.ObjectReference{
Name: role,
},
Subjects: []kapi.ObjectReference{
{
Kind: authorizationapi.UserKind,
Name: name,
},
},
})
o.Expect(err).NotTo(o.HaveOccurred())
}
return user
}
func createGroup(cli *exutil.CLI, name, role string) *userapi.Group {
name = cli.Namespace() + "-" + name
group, err := cli.AdminUserClient().User().Groups().Create(&userapi.Group{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
})
o.Expect(err).NotTo(o.HaveOccurred())
if role != "" {
_, err = cli.AdminAuthorizationClient().Authorization().RoleBindings(cli.Namespace()).Create(&authorizationapi.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s-binding", name, role),
},
RoleRef: kapi.ObjectReference{
Name: role,
},
Subjects: []kapi.ObjectReference{
{
Kind: authorizationapi.GroupKind,
Name: name,
},
},
})
o.Expect(err).NotTo(o.HaveOccurred())
}
return group
}
func addUserToGroup(cli *exutil.CLI, username, groupname string) {
group, err := cli.AdminUserClient().User().Groups().Get(groupname, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
if group != nil {
group.Users = append(group.Users, username)
_, err = cli.AdminUserClient().User().Groups().Update(group)
o.Expect(err).NotTo(o.HaveOccurred())
}
}
func deleteGroup(cli *exutil.CLI, group *userapi.Group) {
err := cli.AdminUserClient().User().Groups().Delete(group.Name, nil)
o.Expect(err).NotTo(o.HaveOccurred())
}
func deleteUser(cli *exutil.CLI, user *userapi.User) {
err := cli.AdminUserClient().User().Users().Delete(user.Name, nil)
o.Expect(err).NotTo(o.HaveOccurred())
}
func setUser(cli *exutil.CLI, user *userapi.User) {
if user == nil {
g.By("testing as system:admin user")
*cli = *cli.AsAdmin()
} else {
g.By(fmt.Sprintf("testing as %s user", user.Name))
cli.ChangeUser(user.Name)
}
}
func setEmptyNodeSelector(tsbOC *exutil.CLI) {
namespace, err := tsbOC.AdminKubeClient().CoreV1().Namespaces().Get(tsbOC.Namespace(), metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
namespace.Annotations["openshift.io/node-selector"] = ""
_, err = tsbOC.AdminKubeClient().CoreV1().Namespaces().Update(namespace)
o.Expect(err).NotTo(o.HaveOccurred())
}
// EnsureTSB makes sure a TSB is present where expected and returns a client to
// speak to it and a close method which provides the proxy. The caller must
// call the close method, usually done in AfterEach
func EnsureTSB(tsbOC *exutil.CLI) (osbclient.Client, func() error) {
{
configPath := exutil.FixturePath("..", "..", "install", "templateservicebroker", "rbac-template.yaml")
stdout, _, err := tsbOC.WithoutNamespace().Run("process").Args("-f", configPath, "-p", "NAMESPACE="+tsbOC.Namespace()).Outputs()
if err != nil {
e2e.Logf("Error processing TSB template at %s: %v \n", configPath, err)
}
err = tsbOC.WithoutNamespace().AsAdmin().Run("create").Args("-f", "-").InputString(stdout).Execute()
if err != nil {
// If template tests run in parallel this could be created twice, we don't really care.
e2e.Logf("Error creating TSB resources: %v \n", err)
}
}
// Set an empty node selector on our namespace. For now this ensures we
// don't trigger a spinning state (see bz1494709) with the DaemonSet if
// projectConfig.defaultNodeSelector is set in the master config and some
// nodes don't match the nodeSelector. The spinning state wastes CPU and
// fills the node logs, but otherwise isn't particularly harmful.
setEmptyNodeSelector(tsbOC)
configPath := exutil.FixturePath("..", "..", "install", "templateservicebroker", "apiserver-template.yaml")
err := tsbOC.AsAdmin().Run("new-app").Args(configPath, "-p", "LOGLEVEL=4", "-p", "NAMESPACE="+tsbOC.Namespace()).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
var pod *kapiv1.Pod
err = wait.Poll(e2e.Poll, 10*time.Minute, func() (bool, error) {
pods, err := tsbOC.KubeClient().CoreV1().Pods(tsbOC.Namespace()).List(metav1.ListOptions{})
if err != nil {
return false, err
}
pod = nil
for _, p := range pods.Items {
if strings.HasPrefix(p.Name, "apiserver-") {
pod = &p
break
}
}
if pod == nil {
return false, nil
}
for _, c := range pod.Status.Conditions {
if c.Type == kapiv1.PodReady && c.Status == kapiv1.ConditionTrue {
return true, nil
}
}
return false, nil
})
o.Expect(err).NotTo(o.HaveOccurred())
// we're trying to test the TSB, not the service. We're outside all the normal networks. Run a portforward to a particular
// pod and test that
portForwardCmd, stdout, err := tsbOC.Run("port-forward").Args(pod.Name, ":8443").BackgroundRC()
o.Expect(err).NotTo(o.HaveOccurred())
// read in the local address the port-forwarder is listening on
br := bufio.NewReader(stdout)
portline, err := br.ReadString('\n')
o.Expect(err).NotTo(o.HaveOccurred())
s := regexp.MustCompile(`Forwarding from (.*) ->`).FindStringSubmatch(portline)
o.Expect(s).To(o.HaveLen(2))
go io.Copy(ioutil.Discard, br)
tsbclient := osbclient.NewClient(&http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}, "https://"+s[1]+templateapi.ServiceBrokerRoot)
return tsbclient, func() error {
return portForwardCmd.Process.Kill()
}
}
func dumpObjectReadiness(oc *exutil.CLI, templateInstance *templateapi.TemplateInstance) error {
restmapper := restutil.DefaultMultiRESTMapper()
_, config, err := configapi.GetInternalKubeClient(exutil.KubeConfigPath(), nil)
if err != nil {
return err
}
fmt.Fprintf(g.GinkgoWriter, "dumping object readiness for %s/%s\n", templateInstance.Namespace, templateInstance.Name)
for _, object := range templateInstance.Status.Objects {
if !controller.CanCheckReadiness(object.Ref) {
continue
}
mapping, err := restmapper.RESTMapping(object.Ref.GroupVersionKind().GroupKind())
if err != nil {
return err
}
cli, err := cmd.ClientMapperFromConfig(config).ClientForMapping(mapping)
if err != nil {
return err
}
obj, err := cli.Get().Resource(mapping.Resource).NamespaceIfScoped(object.Ref.Namespace, mapping.Scope.Name() == meta.RESTScopeNameNamespace).Name(object.Ref.Name).Do().Get()
if err != nil {
return err
}
meta, err := meta.Accessor(obj)
if err != nil {
return err
}
if meta.GetUID() != object.Ref.UID {
return kerrors.NewNotFound(schema.GroupResource{Group: mapping.GroupVersionKind.Group, Resource: mapping.Resource}, object.Ref.Name)
}
if strings.ToLower(meta.GetAnnotations()[templateapi.WaitForReadyAnnotation]) != "true" {
continue
}
ready, failed, err := controller.CheckReadiness(oc.BuildClient(), object.Ref, obj)
if err != nil {
return err
}
fmt.Fprintf(g.GinkgoWriter, "%s %s/%s: ready %v, failed %v\n", object.Ref.Kind, object.Ref.Namespace, object.Ref.Name, ready, failed)
if !ready || failed {
fmt.Fprintf(g.GinkgoWriter, "object: %#v\n", obj)
}
}
return nil
}