blob: 066c1c28c09abe36492d0c1cbe38e9bdc1dafbbd [file] [log] [blame]
Lorenz Brunfc5dbc62020-05-28 12:18:07 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package e2e
18
19import (
Serge Bazanski9104e382023-04-04 20:08:21 +020020 "bytes"
21 "context"
Lorenz Bruncc078df2021-12-23 11:51:55 +010022 "crypto/x509"
23 "encoding/pem"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020024 "fmt"
Serge Bazanski9104e382023-04-04 20:08:21 +020025 "io"
26 "strings"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020027
28 appsv1 "k8s.io/api/apps/v1"
Serge Bazanski9104e382023-04-04 20:08:21 +020029 batchv1 "k8s.io/api/batch/v1"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020030 corev1 "k8s.io/api/core/v1"
31 "k8s.io/apimachinery/pkg/api/resource"
32 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33 "k8s.io/apimachinery/pkg/util/intstr"
34 "k8s.io/client-go/kubernetes"
Lorenz Bruncc078df2021-12-23 11:51:55 +010035 "k8s.io/client-go/rest"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020036
Lorenz Bruncc078df2021-12-23 11:51:55 +010037 "source.monogon.dev/metropolis/test/launch/cluster"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020038)
39
Lorenz Bruncc078df2021-12-23 11:51:55 +010040// GetKubeClientSet gets a Kubernetes client set accessing the Metropolis
41// Kubernetes authenticating proxy using the cluster owner identity.
42// It currently has access to everything (i.e. the cluster-admin role)
43// via the owner-admin binding.
44func GetKubeClientSet(cluster *cluster.Cluster, port uint16) (kubernetes.Interface, error) {
45 pkcs8Key, err := x509.MarshalPKCS8PrivateKey(cluster.Owner.PrivateKey)
46 if err != nil {
47 // We explicitly pass an Ed25519 private key in, so this can't happen
48 panic(err)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020049 }
Lorenz Bruncc078df2021-12-23 11:51:55 +010050 var clientConfig = rest.Config{
51 Host: fmt.Sprintf("localhost:%v", port),
52 TLSClientConfig: rest.TLSClientConfig{
Lorenz Brun78cefca2022-06-20 12:59:55 +000053 ServerName: "kubernetes.default.svc",
Lorenz Bruncc078df2021-12-23 11:51:55 +010054 Insecure: true,
55 CertData: pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cluster.Owner.Certificate[0]}),
56 KeyData: pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: pkcs8Key}),
57 },
58 }
59 return kubernetes.NewForConfig(&clientConfig)
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020060}
61
Serge Bazanski216fe7b2021-05-21 18:36:16 +020062// makeTestDeploymentSpec generates a Deployment spec for a single pod running
63// NGINX with a readiness probe. This allows verifying that the control plane
64// is capable of scheduling simple pods and that kubelet works, its runtime is
65// set up well enough to run a simple container and the network to the pod can
66// pass readiness probe traffic.
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020067func makeTestDeploymentSpec(name string) *appsv1.Deployment {
68 return &appsv1.Deployment{
69 ObjectMeta: metav1.ObjectMeta{Name: name},
70 Spec: appsv1.DeploymentSpec{
71 Selector: &metav1.LabelSelector{MatchLabels: map[string]string{
72 "name": name,
73 }},
74 Template: corev1.PodTemplateSpec{
75 ObjectMeta: metav1.ObjectMeta{
76 Labels: map[string]string{
77 "name": name,
78 },
79 },
80 Spec: corev1.PodSpec{
81 Containers: []corev1.Container{
82 {
Serge Bazanski591d8082023-03-16 00:26:59 +010083 Name: "test",
84 ImagePullPolicy: corev1.PullNever,
85 Image: "bazel/metropolis/test/e2e/preseedtest:preseedtest_image",
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020086 ReadinessProbe: &corev1.Probe{
Lorenz Brund13c1c62022-03-30 19:58:58 +020087 ProbeHandler: corev1.ProbeHandler{
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020088 HTTPGet: &corev1.HTTPGetAction{Port: intstr.FromInt(80)},
89 },
90 },
91 },
92 },
93 },
94 },
95 },
96 }
97}
98
Serge Bazanski9104e382023-04-04 20:08:21 +020099// makeSelftestSpec generates a Job spec for the E2E self-test image.
100func makeSelftestSpec(name string) *batchv1.Job {
101 one := int32(1)
102 return &batchv1.Job{
103 ObjectMeta: metav1.ObjectMeta{Name: name},
104 Spec: batchv1.JobSpec{
105 BackoffLimit: &one,
106 Template: corev1.PodTemplateSpec{
107 ObjectMeta: metav1.ObjectMeta{
108 Labels: map[string]string{
109 "job-name": name,
110 },
111 },
112 Spec: corev1.PodSpec{
113 Containers: []corev1.Container{
114 {
115 Name: "test",
116 ImagePullPolicy: corev1.PullNever,
117 Image: "bazel/metropolis/test/e2e/selftest:selftest_image",
118 },
119 },
120 RestartPolicy: corev1.RestartPolicyOnFailure,
121 },
122 },
123 },
124 }
125}
126
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200127// makeTestStatefulSet generates a StatefulSet spec
Lorenz Brun37050122021-03-30 14:00:27 +0200128func makeTestStatefulSet(name string, volumeMode corev1.PersistentVolumeMode) *appsv1.StatefulSet {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200129 return &appsv1.StatefulSet{
130 ObjectMeta: metav1.ObjectMeta{Name: name},
131 Spec: appsv1.StatefulSetSpec{
132 Selector: &metav1.LabelSelector{MatchLabels: map[string]string{
133 "name": name,
134 }},
135 VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
136 {
137 ObjectMeta: metav1.ObjectMeta{Name: "www"},
138 Spec: corev1.PersistentVolumeClaimSpec{
139 AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
140 Resources: corev1.ResourceRequirements{
141 Requests: map[corev1.ResourceName]resource.Quantity{corev1.ResourceStorage: resource.MustParse("50Mi")},
142 },
Lorenz Brun37050122021-03-30 14:00:27 +0200143 VolumeMode: &volumeMode,
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200144 },
145 },
146 },
147 Template: corev1.PodTemplateSpec{
148 ObjectMeta: metav1.ObjectMeta{
149 Labels: map[string]string{
150 "name": name,
151 },
152 },
153 Spec: corev1.PodSpec{
154 Containers: []corev1.Container{
155 {
Serge Bazanski591d8082023-03-16 00:26:59 +0100156 Name: "test",
157 ImagePullPolicy: corev1.PullNever,
158 Image: "bazel/metropolis/test/e2e/preseedtest:preseedtest_image",
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200159 ReadinessProbe: &corev1.Probe{
Lorenz Brund13c1c62022-03-30 19:58:58 +0200160 ProbeHandler: corev1.ProbeHandler{
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200161 HTTPGet: &corev1.HTTPGetAction{Port: intstr.FromInt(80)},
162 },
163 },
164 },
165 },
166 },
167 },
168 },
169 }
170}
Serge Bazanski9104e382023-04-04 20:08:21 +0200171
172func getPodLogLines(ctx context.Context, cs kubernetes.Interface, podName string, nlines int64) ([]string, error) {
173 logsR := cs.CoreV1().Pods("default").GetLogs(podName, &corev1.PodLogOptions{
174 TailLines: &nlines,
175 })
176 logs, err := logsR.Stream(ctx)
177 if err != nil {
178 return nil, fmt.Errorf("stream failed: %w", err)
179 }
180 var buf bytes.Buffer
181 _, err = io.Copy(&buf, logs)
182 if err != nil {
183 return nil, fmt.Errorf("copy failed: %w", err)
184 }
185 lineStr := strings.Trim(buf.String(), "\n")
186 lines := strings.Split(lineStr, "\n")
187 lines = lines[len(lines)-int(nlines):]
188 return lines, nil
189}