This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
forked from istio/istio
/
kubernetes_utils.go
373 lines (328 loc) · 11.3 KB
/
kubernetes_utils.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright 2017 Istio 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 integration
import (
"crypto/x509"
"fmt"
"time"
"k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth" // to avoid 'No Auth Provider found for name "gcp"'
"k8s.io/client-go/tools/clientcmd"
"istio.io/istio/pkg/log"
"istio.io/istio/security/pkg/pki/ca/controller"
"istio.io/istio/security/pkg/pki/util"
)
var (
immediate int64
)
const (
testNamespacePrefix = "istio-ca-integration-"
kubernetesWaitTimeout = 300 * time.Second
)
// CreateClientset creates a new Clientset for the given kubeconfig.
func CreateClientset(kubeconfig string) (*kubernetes.Clientset, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, fmt.Errorf("failed to create config object from kube-config file: %q (error: %v)",
kubeconfig, err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("failed to create clientset object (error: %v)", err)
}
return clientset, nil
}
// createTestNamespace creates a namespace for test. Returns name of the namespace on success, and error if there is any.
func createTestNamespace(clientset kubernetes.Interface, prefix string) (string, error) {
template := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: prefix,
},
}
namespace, err := clientset.CoreV1().Namespaces().Create(template)
if err != nil {
return "", fmt.Errorf("failed to create a namespace (error: %v)", err)
}
log.Infof("namespace %v is created", namespace.GetName())
// Create Role
err = createIstioCARole(clientset, namespace.GetName())
if err != nil {
_ = deleteTestNamespace(clientset, namespace.GetName())
return "", fmt.Errorf("failed to create a role (error: %v)", err)
}
// Create RoleBinding
err = createIstioCARoleBinding(clientset, namespace.GetName())
if err != nil {
_ = deleteTestNamespace(clientset, namespace.GetName())
return "", fmt.Errorf("failed to create a rolebinding (error: %v)", err)
}
return namespace.GetName(), nil
}
// deleteTestNamespace deletes a namespace for test.
func deleteTestNamespace(clientset kubernetes.Interface, namespace string) error {
if err := clientset.CoreV1().Namespaces().Delete(namespace, &metav1.DeleteOptions{GracePeriodSeconds: &immediate}); err != nil {
return fmt.Errorf("failed to delete namespace %q (error: %v)", namespace, err)
}
log.Infof("namespace %v is deleted", namespace)
return nil
}
// createService creates a service object and returns a pointer pointing to this object on success.
func createService(clientset kubernetes.Interface, namespace string, name string, port int32,
serviceType v1.ServiceType, labels map[string]string, selector map[string]string,
annotation map[string]string) (*v1.Service, error) {
_, err := clientset.CoreV1().Services(namespace).Create(&v1.Service{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Name: name,
Annotations: annotation,
},
Spec: v1.ServiceSpec{
Type: serviceType,
Selector: selector,
Ports: []v1.ServicePort{
{
Port: port,
},
},
},
})
if err != nil {
return nil, err
}
return clientset.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
}
// deleteService deletes a service.
func deleteService(clientset kubernetes.Interface, namespace string, name string) error {
return clientset.CoreV1().Services(namespace).Delete(name, &metav1.DeleteOptions{GracePeriodSeconds: &immediate})
}
// createPod creates a pod object and returns a pointer pointing to this object on success.
func createPod(clientset kubernetes.Interface, namespace string, image string, name string,
labels map[string]string, command []string, args []string) (*v1.Pod, error) {
env := []v1.EnvVar{
{
Name: "NAMESPACE",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.namespace",
},
},
},
}
spec := v1.PodSpec{
Containers: []v1.Container{
{
Env: env,
Name: fmt.Sprintf("%v-pod-container", name),
Image: image,
},
},
}
if len(command) > 0 {
spec.Containers[0].Command = command
if len(args) > 0 {
spec.Containers[0].Args = args
}
}
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Name: name,
},
Spec: spec,
}
if _, err := clientset.CoreV1().Pods(namespace).Create(pod); err != nil {
return nil, err
}
return clientset.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
}
// deletePod deletes a pod.
func deletePod(clientset kubernetes.Interface, namespace string, name string) error {
return clientset.CoreV1().Pods(namespace).Delete(name, &metav1.DeleteOptions{GracePeriodSeconds: &immediate})
}
// createIstioCARole creates a role object named "istio-ca-role".
func createIstioCARole(clientset kubernetes.Interface, namespace string) error {
role := rbac.Role{
TypeMeta: metav1.TypeMeta{
APIVersion: "rbac.authorization.k8s.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "istio-ca-role",
Namespace: namespace,
},
Rules: []rbac.PolicyRule{
{
Verbs: []string{"create", "get", "watch", "list", "update"},
APIGroups: []string{"core", ""},
Resources: []string{"secrets"},
},
{
Verbs: []string{"get", "watch", "list"},
APIGroups: []string{"core", ""},
Resources: []string{"serviceaccounts", "services", "pods"},
},
},
}
if _, err := clientset.RbacV1beta1().Roles(namespace).Create(&role); err != nil {
return fmt.Errorf("failed to create role (error: %v)", err)
}
return nil
}
// createIstioCARoleBinding binds role "istio-ca-role" to default service account.
func createIstioCARoleBinding(clientset kubernetes.Interface, namespace string) error {
rolebinding := rbac.RoleBinding{
TypeMeta: metav1.TypeMeta{
APIVersion: "rbac.authorization.k8s.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "istio-ca-role-binding",
Namespace: namespace,
},
Subjects: []rbac.Subject{
{
Kind: "ServiceAccount",
Name: "default",
Namespace: namespace,
},
},
RoleRef: rbac.RoleRef{
Kind: "Role",
Name: "istio-ca-role",
APIGroup: "rbac.authorization.k8s.io",
},
}
_, err := clientset.RbacV1beta1().RoleBindings(namespace).Create(&rolebinding)
return err
}
func waitForServiceExternalIPAddress(clientset kubernetes.Interface, namespace string, uuid string,
timeToWait time.Duration) error {
selectors := labels.Set{"uuid": uuid}.AsSelectorPreValidated()
listOptions := metav1.ListOptions{
LabelSelector: selectors.String(),
}
watch, err := clientset.CoreV1().Services(namespace).Watch(listOptions)
if err != nil {
return fmt.Errorf("failed to set up a watch for service (error: %v)", err)
}
events := watch.ResultChan()
startTime := time.Now()
for {
select {
case event := <-events:
svc := event.Object.(*v1.Service)
if len(svc.Status.LoadBalancer.Ingress) > 0 {
log.Infof("LoadBalancer for %v/%v is ready. IP: %v", namespace, svc.GetName(),
svc.Status.LoadBalancer.Ingress[0].IP)
return nil
}
case <-time.After(timeToWait - time.Since(startTime)):
return fmt.Errorf("pod is not in running phase within %v", timeToWait)
}
}
}
func waitForPodRunning(clientset kubernetes.Interface, namespace string, uuid string,
timeToWait time.Duration) error {
selectors := labels.Set{"uuid": uuid}.AsSelectorPreValidated()
listOptions := metav1.ListOptions{
LabelSelector: selectors.String(),
}
watch, err := clientset.CoreV1().Pods(namespace).Watch(listOptions)
if err != nil {
return fmt.Errorf("failed to set up a watch for pod (error: %v)", err)
}
events := watch.ResultChan()
startTime := time.Now()
for {
select {
case event := <-events:
pod := event.Object.(*v1.Pod)
if pod.Status.Phase == v1.PodRunning {
log.Infof("pod %v/%v is in Running phase", namespace, pod.GetName())
return nil
}
case <-time.After(timeToWait - time.Since(startTime)):
return fmt.Errorf("pod is not in running phase within %v", timeToWait)
}
}
}
func getServiceExternalIPAddress(clientset kubernetes.Interface, namespace string, name string) (string, error) {
service, err := clientset.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get service: %v err: %v", name, err)
}
if len(service.Status.LoadBalancer.Ingress) > 0 {
return service.Status.LoadBalancer.Ingress[0].IP, nil
}
return "", fmt.Errorf("external ip address for the service %v is not ready", name)
}
// WaitForSecretExist takes name of a secret and watches the secret. Returns the requested secret
// if it exists, or error on timeouts.
func WaitForSecretExist(clientset kubernetes.Interface, namespace string, secretName string,
timeToWait time.Duration) (*v1.Secret, error) {
watch, err := clientset.CoreV1().Secrets(namespace).Watch(metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to set up watch for secret (error: %v)", err)
}
events := watch.ResultChan()
startTime := time.Now()
for {
select {
case event := <-events:
secret := event.Object.(*v1.Secret)
if secret.GetName() == secretName {
return secret, nil
}
case <-time.After(timeToWait - time.Since(startTime)):
return nil, fmt.Errorf("secret %v/%v did not become existent within %v",
namespace, secretName, timeToWait)
}
}
}
// ExamineSecret examines the content of an Istio secret to make sure that
// * Secret type is correctly set;
// * Key, certificate and CA root are correctly saved in the data section;
func ExamineSecret(secret *v1.Secret) error {
if secret.Type != controller.IstioSecretType {
return fmt.Errorf(`unexpected value for the "type" annotation: expecting %v but got %v`,
controller.IstioSecretType, secret.Type)
}
for _, key := range []string{controller.CertChainID, controller.RootCertID, controller.PrivateKeyID} {
if _, exists := secret.Data[key]; !exists {
return fmt.Errorf("%v does not exist in the data section", key)
}
}
expectedID, err := util.GenSanURI(secret.GetNamespace(), "default")
if err != nil {
return err
}
verifyFields := &util.VerifyFields{
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
IsCA: false,
}
if err := util.VerifyCertificate(secret.Data[controller.PrivateKeyID],
secret.Data[controller.CertChainID], secret.Data[controller.RootCertID],
expectedID, verifyFields); err != nil {
return fmt.Errorf("certificate verification failed: %v", err)
}
return nil
}
// DeleteSecret deletes a secret.
func DeleteSecret(clientset kubernetes.Interface, namespace string, name string) error {
return clientset.CoreV1().Secrets(namespace).Delete(name, &metav1.DeleteOptions{GracePeriodSeconds: &immediate})
}