-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
run.go
283 lines (253 loc) · 8.99 KB
/
run.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
/*
Copyright 2020 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 kubetest implmements kubetest functionality.
package kubetest
import (
"context"
"fmt"
"os"
"os/user"
"path"
"runtime"
"strconv"
"strings"
"github.com/onsi/ginkgo/v2"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/yaml"
"sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/test/framework/ginkgoextensions"
"sigs.k8s.io/cluster-api/test/infrastructure/container"
)
const (
standardImage = "registry.k8s.io/conformance"
ciArtifactImage = "gcr.io/k8s-staging-ci-images/conformance"
)
// Export Ginkgo constants.
const (
DefaultGinkgoNodes = 1
DefaultGinkoSlowSpecThreshold = 120
)
type RunInput struct {
// ClusterProxy is a clusterctl test framework proxy for the workload cluster
// for which to run kubetest against
ClusterProxy framework.ClusterProxy
// NumberOfNodes is the number of cluster nodes that exist for kubetest
// to be aware of
NumberOfNodes int
// ArtifactsDirectory is where conformance suite output will go
ArtifactsDirectory string
// Path to the kubetest e2e config file
ConfigFilePath string
// GinkgoNodes is the number of Ginkgo nodes to use
GinkgoNodes int
// GinkgoSlowSpecThreshold is time in s before spec is marked as slow
GinkgoSlowSpecThreshold int
// KubernetesVersion is the version of Kubernetes to test (if not specified, then an attempt to discover the server version is made)
KubernetesVersion string
// ConformanceImage is an optional field to specify an exact conformance image
ConformanceImage string
// KubeTestRepoListPath is optional file for specifying custom image repositories
// https://github.com/kubernetes/kubernetes/blob/master/test/images/README.md#testing-the-new-image
KubeTestRepoListPath string
}
// Run executes kube-test given an artifact directory, and sets settings
// required for kubetest to work with Cluster API. JUnit files are
// also gathered for inclusion in Prow.
func Run(ctx context.Context, input RunInput) error {
if input.ClusterProxy == nil {
return errors.New("ClusterProxy must be provided")
}
if input.GinkgoNodes == 0 {
input.GinkgoNodes = DefaultGinkgoNodes
}
if input.GinkgoSlowSpecThreshold == 0 {
input.GinkgoSlowSpecThreshold = 120
}
if input.NumberOfNodes == 0 {
numNodes, err := countClusterNodes(ctx, input.ClusterProxy)
if err != nil {
return errors.Wrap(err, "Unable to count number of cluster nodes")
}
input.NumberOfNodes = numNodes
}
if input.KubernetesVersion == "" && input.ConformanceImage == "" {
discoveredVersion, err := discoverClusterKubernetesVersion(input.ClusterProxy)
if err != nil {
return errors.Wrap(err, "Unable to discover server's Kubernetes version")
}
input.KubernetesVersion = discoveredVersion
}
input.ArtifactsDirectory = framework.ResolveArtifactsDirectory(input.ArtifactsDirectory)
reportDir := path.Join(input.ArtifactsDirectory, "kubetest")
outputDir := path.Join(reportDir, "e2e-output")
kubetestConfigDir := path.Join(reportDir, "config")
if err := os.MkdirAll(outputDir, 0o750); err != nil {
return err
}
if err := os.MkdirAll(kubetestConfigDir, 0o750); err != nil {
return err
}
ginkgoVars := map[string]string{
"nodes": strconv.Itoa(input.GinkgoNodes),
"slowSpecThreshold": strconv.Itoa(input.GinkgoSlowSpecThreshold),
}
config, err := parseKubetestConfig(input.ConfigFilePath)
if err != nil {
return err
}
tmpKubeConfigPath, err := dockeriseKubeconfig(kubetestConfigDir, input.ClusterProxy.GetKubeconfigPath())
if err != nil {
return err
}
e2eVars := map[string]string{
"kubeconfig": "/tmp/kubeconfig",
"provider": "skeleton",
"report-dir": "/output",
"e2e-output-dir": "/output/e2e-output",
"dump-logs-on-failure": "false",
"report-prefix": "kubetest.",
"num-nodes": strconv.FormatInt(int64(input.NumberOfNodes), 10),
}
ginkgoArgs := buildArgs(ginkgoVars, "-")
e2eArgs := buildArgs(e2eVars, "--")
if input.ConformanceImage == "" {
input.ConformanceImage = versionToConformanceImage(input.KubernetesVersion)
}
volumeMounts := map[string]string{
tmpKubeConfigPath: "/tmp/kubeconfig",
reportDir: "/output",
}
user, err := user.Current()
if err != nil {
return errors.Wrap(err, "unable to determine current user")
}
env := map[string]string{}
if input.KubeTestRepoListPath != "" {
tmpKubeTestRepoListPath := path.Join(kubetestConfigDir, "repo_list.yaml")
if err := copyFile(input.KubeTestRepoListPath, tmpKubeTestRepoListPath); err != nil {
return err
}
dest := "/tmp/repo_list.yaml"
env["KUBE_TEST_REPO_LIST"] = dest
volumeMounts[tmpKubeTestRepoListPath] = dest
}
// Formulate our command arguments
var args []string
args = append(args, ginkgoArgs...)
args = append(
args,
"/usr/local/bin/e2e.test",
"--")
args = append(args, e2eArgs...)
args = append(args, config.toFlags()...)
// Get our current working directory. Just for information, so we don't need
// to worry about errors at this point.
cwd, _ := os.Getwd()
ginkgoextensions.Byf("Running e2e test: dir=%s, command=%q", cwd, args)
containerRuntime, err := container.NewDockerClient()
if err != nil {
return errors.Wrap(err, "Unable to run conformance tests")
}
ctx = container.RuntimeInto(ctx, containerRuntime)
err = containerRuntime.RunContainer(ctx, &container.RunContainerInput{
Image: input.ConformanceImage,
Network: "kind",
User: user.Uid,
Group: user.Gid,
Volumes: volumeMounts,
EnvironmentVars: env,
CommandArgs: args,
Entrypoint: []string{"/usr/local/bin/ginkgo"},
// We don't want the conformance test container to restart once ginkgo exits.
RestartPolicy: "no",
}, ginkgo.GinkgoWriter)
if err != nil {
return errors.Wrap(err, "Unable to run conformance tests")
}
return framework.GatherJUnitReports(reportDir, input.ArtifactsDirectory)
}
type kubetestConfig map[string]string
func (c kubetestConfig) toFlags() []string {
return buildArgs(c, "-")
}
func parseKubetestConfig(kubetestConfigFile string) (kubetestConfig, error) {
conf := make(kubetestConfig)
data, err := os.ReadFile(kubetestConfigFile) //nolint:gosec
if err != nil {
return nil, fmt.Errorf("unable to read kubetest config file %s: %w", kubetestConfigFile, err)
}
if err := yaml.Unmarshal(data, &conf); err != nil {
return nil, fmt.Errorf("unable to parse kubetest config file %s as valid, non-nested YAML: %w", kubetestConfigFile, err)
}
return conf, nil
}
func isUsingCIArtifactsVersion(k8sVersion string) bool {
return strings.Contains(k8sVersion, "-")
}
func discoverClusterKubernetesVersion(proxy framework.ClusterProxy) (string, error) {
config := proxy.GetRESTConfig()
discoverClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return "", err
}
serverVersionInfo, err := discoverClient.ServerVersion()
if err != nil {
return "", err
}
return serverVersionInfo.String(), nil
}
func dockeriseKubeconfig(kubetestConfigDir string, kubeConfigPath string) (string, error) {
kubeConfig, err := clientcmd.LoadFromFile(kubeConfigPath)
if err != nil {
return "", err
}
newPath := path.Join(kubetestConfigDir, "kubeconfig")
// On CAPD, if not running on Linux, we need to use Docker's proxy to connect back to the host
// to the CAPD cluster. Moby on Linux doesn't use the host.docker.internal DNS name.
if runtime.GOOS != "linux" {
for i := range kubeConfig.Clusters {
kubeConfig.Clusters[i].Server = strings.ReplaceAll(kubeConfig.Clusters[i].Server, "127.0.0.1", "host.docker.internal")
}
}
if err := clientcmd.WriteToFile(*kubeConfig, newPath); err != nil {
return "", err
}
return newPath, nil
}
func countClusterNodes(ctx context.Context, proxy framework.ClusterProxy) (int, error) {
nodeList, err := proxy.GetClientSet().CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return 0, errors.Wrap(err, "Unable to count nodes")
}
return len(nodeList.Items), nil
}
func versionToConformanceImage(kubernetesVersion string) string {
k8sVersion := strings.ReplaceAll(kubernetesVersion, "+", "_")
if isUsingCIArtifactsVersion(kubernetesVersion) {
return ciArtifactImage + ":" + k8sVersion
}
return standardImage + ":" + k8sVersion
}
// buildArgs converts a string map to the format --key=value.
func buildArgs(kv map[string]string, flagMarker string) []string {
args := make([]string, len(kv))
i := 0
for k, v := range kv {
args[i] = flagMarker + k + "=" + v
i++
}
return args
}