blob: ce9e78fdb05059ca525c6d53d5589df308328b8f [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 Brunfc5dbc62020-05-28 12:18:07 +020022 "fmt"
Serge Bazanski9104e382023-04-04 20:08:21 +020023 "io"
24 "strings"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020025
26 appsv1 "k8s.io/api/apps/v1"
Serge Bazanski9104e382023-04-04 20:08:21 +020027 batchv1 "k8s.io/api/batch/v1"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020028 corev1 "k8s.io/api/core/v1"
29 "k8s.io/apimachinery/pkg/api/resource"
30 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31 "k8s.io/apimachinery/pkg/util/intstr"
32 "k8s.io/client-go/kubernetes"
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020033)
34
Serge Bazanski216fe7b2021-05-21 18:36:16 +020035// makeTestDeploymentSpec generates a Deployment spec for a single pod running
36// NGINX with a readiness probe. This allows verifying that the control plane
37// is capable of scheduling simple pods and that kubelet works, its runtime is
38// set up well enough to run a simple container and the network to the pod can
39// pass readiness probe traffic.
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020040func makeTestDeploymentSpec(name string) *appsv1.Deployment {
41 return &appsv1.Deployment{
42 ObjectMeta: metav1.ObjectMeta{Name: name},
43 Spec: appsv1.DeploymentSpec{
44 Selector: &metav1.LabelSelector{MatchLabels: map[string]string{
45 "name": name,
46 }},
47 Template: corev1.PodTemplateSpec{
48 ObjectMeta: metav1.ObjectMeta{
49 Labels: map[string]string{
50 "name": name,
51 },
52 },
53 Spec: corev1.PodSpec{
54 Containers: []corev1.Container{
55 {
Serge Bazanski591d8082023-03-16 00:26:59 +010056 Name: "test",
57 ImagePullPolicy: corev1.PullNever,
58 Image: "bazel/metropolis/test/e2e/preseedtest:preseedtest_image",
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020059 ReadinessProbe: &corev1.Probe{
Lorenz Brund13c1c62022-03-30 19:58:58 +020060 ProbeHandler: corev1.ProbeHandler{
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020061 HTTPGet: &corev1.HTTPGetAction{Port: intstr.FromInt(80)},
62 },
63 },
64 },
65 },
66 },
67 },
68 },
69 }
70}
71
Serge Bazanski9104e382023-04-04 20:08:21 +020072// makeSelftestSpec generates a Job spec for the E2E self-test image.
73func makeSelftestSpec(name string) *batchv1.Job {
74 one := int32(1)
75 return &batchv1.Job{
76 ObjectMeta: metav1.ObjectMeta{Name: name},
77 Spec: batchv1.JobSpec{
78 BackoffLimit: &one,
79 Template: corev1.PodTemplateSpec{
80 ObjectMeta: metav1.ObjectMeta{
81 Labels: map[string]string{
82 "job-name": name,
83 },
84 },
85 Spec: corev1.PodSpec{
86 Containers: []corev1.Container{
87 {
88 Name: "test",
Lorenz Brun150f24a2023-07-13 20:11:06 +020089 ImagePullPolicy: corev1.PullIfNotPresent,
90 Image: "test.monogon.internal/metropolis/test/e2e/selftest/selftest_image",
Serge Bazanski9104e382023-04-04 20:08:21 +020091 },
92 },
93 RestartPolicy: corev1.RestartPolicyOnFailure,
94 },
95 },
96 },
97 }
98}
99
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200100// makeTestStatefulSet generates a StatefulSet spec
Lorenz Brun37050122021-03-30 14:00:27 +0200101func makeTestStatefulSet(name string, volumeMode corev1.PersistentVolumeMode) *appsv1.StatefulSet {
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200102 return &appsv1.StatefulSet{
103 ObjectMeta: metav1.ObjectMeta{Name: name},
104 Spec: appsv1.StatefulSetSpec{
105 Selector: &metav1.LabelSelector{MatchLabels: map[string]string{
106 "name": name,
107 }},
108 VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
109 {
110 ObjectMeta: metav1.ObjectMeta{Name: "www"},
111 Spec: corev1.PersistentVolumeClaimSpec{
112 AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
113 Resources: corev1.ResourceRequirements{
114 Requests: map[corev1.ResourceName]resource.Quantity{corev1.ResourceStorage: resource.MustParse("50Mi")},
115 },
Lorenz Brun37050122021-03-30 14:00:27 +0200116 VolumeMode: &volumeMode,
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200117 },
118 },
119 },
120 Template: corev1.PodTemplateSpec{
121 ObjectMeta: metav1.ObjectMeta{
122 Labels: map[string]string{
123 "name": name,
124 },
125 },
126 Spec: corev1.PodSpec{
127 Containers: []corev1.Container{
128 {
Serge Bazanski591d8082023-03-16 00:26:59 +0100129 Name: "test",
130 ImagePullPolicy: corev1.PullNever,
131 Image: "bazel/metropolis/test/e2e/preseedtest:preseedtest_image",
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200132 ReadinessProbe: &corev1.Probe{
Lorenz Brund13c1c62022-03-30 19:58:58 +0200133 ProbeHandler: corev1.ProbeHandler{
Lorenz Brunfc5dbc62020-05-28 12:18:07 +0200134 HTTPGet: &corev1.HTTPGetAction{Port: intstr.FromInt(80)},
135 },
136 },
137 },
138 },
139 },
140 },
141 },
142 }
143}
Serge Bazanski9104e382023-04-04 20:08:21 +0200144
145func getPodLogLines(ctx context.Context, cs kubernetes.Interface, podName string, nlines int64) ([]string, error) {
146 logsR := cs.CoreV1().Pods("default").GetLogs(podName, &corev1.PodLogOptions{
147 TailLines: &nlines,
148 })
149 logs, err := logsR.Stream(ctx)
150 if err != nil {
151 return nil, fmt.Errorf("stream failed: %w", err)
152 }
153 var buf bytes.Buffer
154 _, err = io.Copy(&buf, logs)
155 if err != nil {
156 return nil, fmt.Errorf("copy failed: %w", err)
157 }
158 lineStr := strings.Trim(buf.String(), "\n")
159 lines := strings.Split(lineStr, "\n")
160 lines = lines[len(lines)-int(nlines):]
161 return lines, nil
162}