test_context.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework
  14. import (
  15. "flag"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "sort"
  20. "strings"
  21. "time"
  22. "github.com/onsi/ginkgo/config"
  23. "github.com/pkg/errors"
  24. restclient "k8s.io/client-go/rest"
  25. "k8s.io/client-go/tools/clientcmd"
  26. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  27. cliflag "k8s.io/component-base/cli/flag"
  28. "k8s.io/klog"
  29. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  30. )
  31. const (
  32. defaultHost = "http://127.0.0.1:8080"
  33. // DefaultNumNodes is the number of nodes. If not specified, then number of nodes is auto-detected
  34. DefaultNumNodes = -1
  35. )
  36. // TestContextType contains test settings and global state. Due to
  37. // historic reasons, it is a mixture of items managed by the test
  38. // framework itself, cloud providers and individual tests.
  39. // The goal is to move anything not required by the framework
  40. // into the code which uses the settings.
  41. //
  42. // The recommendation for those settings is:
  43. // - They are stored in their own context structure or local
  44. // variables.
  45. // - The standard `flag` package is used to register them.
  46. // The flag name should follow the pattern <part1>.<part2>....<partn>
  47. // where the prefix is unlikely to conflict with other tests or
  48. // standard packages and each part is in lower camel case. For
  49. // example, test/e2e/storage/csi/context.go could define
  50. // storage.csi.numIterations.
  51. // - framework/config can be used to simplify the registration of
  52. // multiple options with a single function call:
  53. // var storageCSI {
  54. // NumIterations `default:"1" usage:"number of iterations"`
  55. // }
  56. // _ config.AddOptions(&storageCSI, "storage.csi")
  57. // - The direct use Viper in tests is possible, but discouraged because
  58. // it only works in test suites which use Viper (which is not
  59. // required) and the supported options cannot be
  60. // discovered by a test suite user.
  61. //
  62. // Test suite authors can use framework/viper to make all command line
  63. // parameters also configurable via a configuration file.
  64. type TestContextType struct {
  65. KubeConfig string
  66. KubeContext string
  67. KubeAPIContentType string
  68. KubeVolumeDir string
  69. CertDir string
  70. Host string
  71. // TODO: Deprecating this over time... instead just use gobindata_util.go , see #23987.
  72. RepoRoot string
  73. DockershimCheckpointDir string
  74. // ListImages will list off all images that are used then quit
  75. ListImages bool
  76. // Provider identifies the infrastructure provider (gce, gke, aws)
  77. Provider string
  78. // Tooling is the tooling in use (e.g. kops, gke). Provider is the cloud provider and might not uniquely identify the tooling.
  79. Tooling string
  80. CloudConfig CloudConfig
  81. KubectlPath string
  82. OutputDir string
  83. ReportDir string
  84. ReportPrefix string
  85. Prefix string
  86. MinStartupPods int
  87. // Timeout for waiting for system pods to be running
  88. SystemPodsStartupTimeout time.Duration
  89. EtcdUpgradeStorage string
  90. EtcdUpgradeVersion string
  91. GCEUpgradeScript string
  92. ContainerRuntime string
  93. ContainerRuntimeEndpoint string
  94. ContainerRuntimeProcessName string
  95. ContainerRuntimePidFile string
  96. // SystemdServices are comma separated list of systemd services the test framework
  97. // will dump logs for.
  98. SystemdServices string
  99. // DumpSystemdJournal controls whether to dump the full systemd journal.
  100. DumpSystemdJournal bool
  101. ImageServiceEndpoint string
  102. MasterOSDistro string
  103. NodeOSDistro string
  104. VerifyServiceAccount bool
  105. DeleteNamespace bool
  106. DeleteNamespaceOnFailure bool
  107. AllowedNotReadyNodes int
  108. CleanStart bool
  109. // If set to 'true' or 'all' framework will start a goroutine monitoring resource usage of system add-ons.
  110. // It will read the data every 30 seconds from all Nodes and print summary during afterEach. If set to 'master'
  111. // only master Node will be monitored.
  112. GatherKubeSystemResourceUsageData string
  113. GatherLogsSizes bool
  114. GatherMetricsAfterTest string
  115. GatherSuiteMetricsAfterTest bool
  116. MaxNodesToGather int
  117. AllowGatheringProfiles bool
  118. // If set to 'true' framework will gather ClusterAutoscaler metrics when gathering them for other components.
  119. IncludeClusterAutoscalerMetrics bool
  120. // Currently supported values are 'hr' for human-readable and 'json'. It's a comma separated list.
  121. OutputPrintType string
  122. // NodeSchedulableTimeout is the timeout for waiting for all nodes to be schedulable.
  123. NodeSchedulableTimeout time.Duration
  124. // SystemDaemonsetStartupTimeout is the timeout for waiting for all system daemonsets to be ready.
  125. SystemDaemonsetStartupTimeout time.Duration
  126. // CreateTestingNS is responsible for creating namespace used for executing e2e tests.
  127. // It accepts namespace base name, which will be prepended with e2e prefix, kube client
  128. // and labels to be applied to a namespace.
  129. CreateTestingNS CreateTestingNSFn
  130. // If set to true test will dump data about the namespace in which test was running.
  131. DumpLogsOnFailure bool
  132. // Disables dumping cluster log from master and nodes after all tests.
  133. DisableLogDump bool
  134. // Path to the GCS artifacts directory to dump logs from nodes. Logexporter gets enabled if this is non-empty.
  135. LogexporterGCSPath string
  136. // featureGates is a map of feature names to bools that enable or disable alpha/experimental features.
  137. FeatureGates map[string]bool
  138. // Node e2e specific test context
  139. NodeTestContextType
  140. // Indicates what path the kubernetes-anywhere is installed on
  141. KubernetesAnywherePath string
  142. // The DNS Domain of the cluster.
  143. ClusterDNSDomain string
  144. // The configuration of NodeKiller.
  145. NodeKiller NodeKillerConfig
  146. // The Default IP Family of the cluster ("ipv4" or "ipv6")
  147. IPFamily string
  148. // NonblockingTaints is the comma-delimeted string given by the user to specify taints which should not stop the test framework from running tests.
  149. NonblockingTaints string
  150. // ProgressReportURL is the URL which progress updates will be posted to as tests complete. If empty, no updates are sent.
  151. ProgressReportURL string
  152. // SriovdpConfigMapFile is the path to the ConfigMap to configure the SRIOV device plugin on this host.
  153. SriovdpConfigMapFile string
  154. }
  155. // NodeKillerConfig describes configuration of NodeKiller -- a utility to
  156. // simulate node failures.
  157. type NodeKillerConfig struct {
  158. // Enabled determines whether NodeKill should do anything at all.
  159. // All other options below are ignored if Enabled = false.
  160. Enabled bool
  161. // FailureRatio is a percentage of all nodes that could fail simultinously.
  162. FailureRatio float64
  163. // Interval is time between node failures.
  164. Interval time.Duration
  165. // JitterFactor is factor used to jitter node failures.
  166. // Node will be killed between [Interval, Interval + (1.0 + JitterFactor)].
  167. JitterFactor float64
  168. // SimulatedDowntime is a duration between node is killed and recreated.
  169. SimulatedDowntime time.Duration
  170. // NodeKillerStopCh is a channel that is used to notify NodeKiller to stop killing nodes.
  171. NodeKillerStopCh chan struct{}
  172. }
  173. // NodeTestContextType is part of TestContextType, it is shared by all node e2e test.
  174. type NodeTestContextType struct {
  175. // NodeE2E indicates whether it is running node e2e.
  176. NodeE2E bool
  177. // Name of the node to run tests on.
  178. NodeName string
  179. // NodeConformance indicates whether the test is running in node conformance mode.
  180. NodeConformance bool
  181. // PrepullImages indicates whether node e2e framework should prepull images.
  182. PrepullImages bool
  183. // KubeletConfig is the kubelet configuration the test is running against.
  184. KubeletConfig kubeletconfig.KubeletConfiguration
  185. // ImageDescription is the description of the image on which the test is running.
  186. ImageDescription string
  187. // SystemSpecName is the name of the system spec (e.g., gke) that's used in
  188. // the node e2e test. If empty, the default one (system.DefaultSpec) is
  189. // used. The system specs are in test/e2e_node/system/specs/.
  190. SystemSpecName string
  191. // ExtraEnvs is a map of environment names to values.
  192. ExtraEnvs map[string]string
  193. }
  194. // CloudConfig holds the cloud configuration for e2e test suites.
  195. type CloudConfig struct {
  196. APIEndpoint string
  197. ProjectID string
  198. Zone string // for multizone tests, arbitrarily chosen zone
  199. Region string
  200. MultiZone bool
  201. MultiMaster bool
  202. Cluster string
  203. MasterName string
  204. NodeInstanceGroup string // comma-delimited list of groups' names
  205. NumNodes int
  206. ClusterIPRange string
  207. ClusterTag string
  208. Network string
  209. ConfigFile string // for azure and openstack
  210. NodeTag string
  211. MasterTag string
  212. Provider ProviderInterface
  213. }
  214. // TestContext should be used by all tests to access common context data.
  215. var TestContext TestContextType
  216. // ClusterIsIPv6 returns true if the cluster is IPv6
  217. func (tc TestContextType) ClusterIsIPv6() bool {
  218. return tc.IPFamily == "ipv6"
  219. }
  220. // RegisterCommonFlags registers flags common to all e2e test suites.
  221. // The flag set can be flag.CommandLine (if desired) or a custom
  222. // flag set that then gets passed to viperconfig.ViperizeFlags.
  223. //
  224. // The other Register*Flags methods below can be used to add more
  225. // test-specific flags. However, those settings then get added
  226. // regardless whether the test is actually in the test suite.
  227. //
  228. // For tests that have been converted to registering their
  229. // options themselves, copy flags from test/e2e/framework/config
  230. // as shown in HandleFlags.
  231. func RegisterCommonFlags(flags *flag.FlagSet) {
  232. // Turn on verbose by default to get spec names
  233. config.DefaultReporterConfig.Verbose = true
  234. // Turn on EmitSpecProgress to get spec progress (especially on interrupt)
  235. config.GinkgoConfig.EmitSpecProgress = true
  236. // Randomize specs as well as suites
  237. config.GinkgoConfig.RandomizeAllSpecs = true
  238. flags.StringVar(&TestContext.GatherKubeSystemResourceUsageData, "gather-resource-usage", "false", "If set to 'true' or 'all' framework will be monitoring resource usage of system all add-ons in (some) e2e tests, if set to 'master' framework will be monitoring master node only, if set to 'none' of 'false' monitoring will be turned off.")
  239. flags.BoolVar(&TestContext.GatherLogsSizes, "gather-logs-sizes", false, "If set to true framework will be monitoring logs sizes on all machines running e2e tests.")
  240. flags.IntVar(&TestContext.MaxNodesToGather, "max-nodes-to-gather-from", 20, "The maximum number of nodes to gather extended info from on test failure.")
  241. flags.StringVar(&TestContext.GatherMetricsAfterTest, "gather-metrics-at-teardown", "false", "If set to 'true' framework will gather metrics from all components after each test. If set to 'master' only master component metrics would be gathered.")
  242. flags.BoolVar(&TestContext.GatherSuiteMetricsAfterTest, "gather-suite-metrics-at-teardown", false, "If set to true framwork will gather metrics from all components after the whole test suite completes.")
  243. flags.BoolVar(&TestContext.AllowGatheringProfiles, "allow-gathering-profiles", true, "If set to true framework will allow to gather CPU/memory allocation pprof profiles from the master.")
  244. flags.BoolVar(&TestContext.IncludeClusterAutoscalerMetrics, "include-cluster-autoscaler", false, "If set to true, framework will include Cluster Autoscaler when gathering metrics.")
  245. flags.StringVar(&TestContext.OutputPrintType, "output-print-type", "json", "Format in which summaries should be printed: 'hr' for human readable, 'json' for JSON ones.")
  246. flags.BoolVar(&TestContext.DumpLogsOnFailure, "dump-logs-on-failure", true, "If set to true test will dump data about the namespace in which test was running.")
  247. flags.BoolVar(&TestContext.DisableLogDump, "disable-log-dump", false, "If set to true, logs from master and nodes won't be gathered after test run.")
  248. flags.StringVar(&TestContext.LogexporterGCSPath, "logexporter-gcs-path", "", "Path to the GCS artifacts directory to dump logs from nodes. Logexporter gets enabled if this is non-empty.")
  249. flags.BoolVar(&TestContext.DeleteNamespace, "delete-namespace", true, "If true tests will delete namespace after completion. It is only designed to make debugging easier, DO NOT turn it off by default.")
  250. flags.BoolVar(&TestContext.DeleteNamespaceOnFailure, "delete-namespace-on-failure", true, "If true, framework will delete test namespace on failure. Used only during test debugging.")
  251. flags.IntVar(&TestContext.AllowedNotReadyNodes, "allowed-not-ready-nodes", 0, "If non-zero, framework will allow for that many non-ready nodes when checking for all ready nodes.")
  252. flags.StringVar(&TestContext.Host, "host", "", fmt.Sprintf("The host, or apiserver, to connect to. Will default to %s if this argument and --kubeconfig are not set", defaultHost))
  253. flags.StringVar(&TestContext.ReportPrefix, "report-prefix", "", "Optional prefix for JUnit XML reports. Default is empty, which doesn't prepend anything to the default name.")
  254. flags.StringVar(&TestContext.ReportDir, "report-dir", "", "Path to the directory where the JUnit XML reports should be saved. Default is empty, which doesn't generate these reports.")
  255. flags.Var(cliflag.NewMapStringBool(&TestContext.FeatureGates), "feature-gates", "A set of key=value pairs that describe feature gates for alpha/experimental features.")
  256. flags.StringVar(&TestContext.ContainerRuntime, "container-runtime", "docker", "The container runtime of cluster VM instances (docker/remote).")
  257. flags.StringVar(&TestContext.ContainerRuntimeEndpoint, "container-runtime-endpoint", "unix:///var/run/dockershim.sock", "The container runtime endpoint of cluster VM instances.")
  258. flags.StringVar(&TestContext.ContainerRuntimeProcessName, "container-runtime-process-name", "dockerd", "The name of the container runtime process.")
  259. flags.StringVar(&TestContext.ContainerRuntimePidFile, "container-runtime-pid-file", "/var/run/docker.pid", "The pid file of the container runtime.")
  260. flags.StringVar(&TestContext.SystemdServices, "systemd-services", "docker", "The comma separated list of systemd services the framework will dump logs for.")
  261. flags.BoolVar(&TestContext.DumpSystemdJournal, "dump-systemd-journal", false, "Whether to dump the full systemd journal.")
  262. flags.StringVar(&TestContext.ImageServiceEndpoint, "image-service-endpoint", "", "The image service endpoint of cluster VM instances.")
  263. flags.StringVar(&TestContext.DockershimCheckpointDir, "dockershim-checkpoint-dir", "/var/lib/dockershim/sandbox", "The directory for dockershim to store sandbox checkpoints.")
  264. flags.StringVar(&TestContext.KubernetesAnywherePath, "kubernetes-anywhere-path", "/workspace/k8s.io/kubernetes-anywhere", "Which directory kubernetes-anywhere is installed to.")
  265. flags.StringVar(&TestContext.NonblockingTaints, "non-blocking-taints", `node-role.kubernetes.io/master`, "Nodes with taints in this comma-delimited list will not block the test framework from starting tests.")
  266. flags.BoolVar(&TestContext.ListImages, "list-images", false, "If true, will show list of images used for runnning tests.")
  267. flags.StringVar(&TestContext.KubectlPath, "kubectl-path", "kubectl", "The kubectl binary to use. For development, you might use 'cluster/kubectl.sh' here.")
  268. flags.StringVar(&TestContext.ProgressReportURL, "progress-report-url", "", "The URL to POST progress updates to as the suite runs to assist in aiding integrations. If empty, no messages sent.")
  269. }
  270. // RegisterClusterFlags registers flags specific to the cluster e2e test suite.
  271. func RegisterClusterFlags(flags *flag.FlagSet) {
  272. flags.BoolVar(&TestContext.VerifyServiceAccount, "e2e-verify-service-account", true, "If true tests will verify the service account before running.")
  273. flags.StringVar(&TestContext.KubeConfig, clientcmd.RecommendedConfigPathFlag, os.Getenv(clientcmd.RecommendedConfigPathEnvVar), "Path to kubeconfig containing embedded authinfo.")
  274. flags.StringVar(&TestContext.KubeContext, clientcmd.FlagContext, "", "kubeconfig context to use/override. If unset, will use value from 'current-context'")
  275. flags.StringVar(&TestContext.KubeAPIContentType, "kube-api-content-type", "application/vnd.kubernetes.protobuf", "ContentType used to communicate with apiserver")
  276. flags.StringVar(&TestContext.KubeVolumeDir, "volume-dir", "/var/lib/kubelet", "Path to the directory containing the kubelet volumes.")
  277. flags.StringVar(&TestContext.CertDir, "cert-dir", "", "Path to the directory containing the certs. Default is empty, which doesn't use certs.")
  278. flags.StringVar(&TestContext.RepoRoot, "repo-root", "../../", "Root directory of kubernetes repository, for finding test files.")
  279. flags.StringVar(&TestContext.Provider, "provider", "", "The name of the Kubernetes provider (gce, gke, local, skeleton (the fallback if not set), etc.)")
  280. flags.StringVar(&TestContext.Tooling, "tooling", "", "The tooling in use (kops, gke, etc.)")
  281. flags.StringVar(&TestContext.OutputDir, "e2e-output-dir", "/tmp", "Output directory for interesting/useful test data, like performance data, benchmarks, and other metrics.")
  282. flags.StringVar(&TestContext.Prefix, "prefix", "e2e", "A prefix to be added to cloud resources created during testing.")
  283. flags.StringVar(&TestContext.MasterOSDistro, "master-os-distro", "debian", "The OS distribution of cluster master (debian, ubuntu, gci, coreos, or custom).")
  284. flags.StringVar(&TestContext.NodeOSDistro, "node-os-distro", "debian", "The OS distribution of cluster VM instances (debian, ubuntu, gci, coreos, or custom).")
  285. flags.StringVar(&TestContext.ClusterDNSDomain, "dns-domain", "cluster.local", "The DNS Domain of the cluster.")
  286. // TODO: Flags per provider? Rename gce-project/gce-zone?
  287. cloudConfig := &TestContext.CloudConfig
  288. flags.StringVar(&cloudConfig.MasterName, "kube-master", "", "Name of the kubernetes master. Only required if provider is gce or gke")
  289. flags.StringVar(&cloudConfig.APIEndpoint, "gce-api-endpoint", "", "The GCE APIEndpoint being used, if applicable")
  290. flags.StringVar(&cloudConfig.ProjectID, "gce-project", "", "The GCE project being used, if applicable")
  291. flags.StringVar(&cloudConfig.Zone, "gce-zone", "", "GCE zone being used, if applicable")
  292. flags.StringVar(&cloudConfig.Region, "gce-region", "", "GCE region being used, if applicable")
  293. flags.BoolVar(&cloudConfig.MultiZone, "gce-multizone", false, "If true, start GCE cloud provider with multizone support.")
  294. flags.BoolVar(&cloudConfig.MultiMaster, "gce-multimaster", false, "If true, the underlying GCE/GKE cluster is assumed to be multi-master.")
  295. flags.StringVar(&cloudConfig.Cluster, "gke-cluster", "", "GKE name of cluster being used, if applicable")
  296. flags.StringVar(&cloudConfig.NodeInstanceGroup, "node-instance-group", "", "Name of the managed instance group for nodes. Valid only for gce, gke or aws. If there is more than one group: comma separated list of groups.")
  297. flags.StringVar(&cloudConfig.Network, "network", "e2e", "The cloud provider network for this e2e cluster.")
  298. flags.IntVar(&cloudConfig.NumNodes, "num-nodes", DefaultNumNodes, fmt.Sprintf("Number of nodes in the cluster. If the default value of '%q' is used the number of schedulable nodes is auto-detected.", DefaultNumNodes))
  299. flags.StringVar(&cloudConfig.ClusterIPRange, "cluster-ip-range", "10.64.0.0/14", "A CIDR notation IP range from which to assign IPs in the cluster.")
  300. flags.StringVar(&cloudConfig.NodeTag, "node-tag", "", "Network tags used on node instances. Valid only for gce, gke")
  301. flags.StringVar(&cloudConfig.MasterTag, "master-tag", "", "Network tags used on master instances. Valid only for gce, gke")
  302. flags.StringVar(&cloudConfig.ClusterTag, "cluster-tag", "", "Tag used to identify resources. Only required if provider is aws.")
  303. flags.StringVar(&cloudConfig.ConfigFile, "cloud-config-file", "", "Cloud config file. Only required if provider is azure or vsphere.")
  304. flags.IntVar(&TestContext.MinStartupPods, "minStartupPods", 0, "The number of pods which we need to see in 'Running' state with a 'Ready' condition of true, before we try running tests. This is useful in any cluster which needs some base pod-based services running before it can be used.")
  305. flags.DurationVar(&TestContext.SystemPodsStartupTimeout, "system-pods-startup-timeout", 10*time.Minute, "Timeout for waiting for all system pods to be running before starting tests.")
  306. flags.DurationVar(&TestContext.NodeSchedulableTimeout, "node-schedulable-timeout", 30*time.Minute, "Timeout for waiting for all nodes to be schedulable.")
  307. flags.DurationVar(&TestContext.SystemDaemonsetStartupTimeout, "system-daemonsets-startup-timeout", 5*time.Minute, "Timeout for waiting for all system daemonsets to be ready.")
  308. flags.StringVar(&TestContext.EtcdUpgradeStorage, "etcd-upgrade-storage", "", "The storage version to upgrade to (either 'etcdv2' or 'etcdv3') if doing an etcd upgrade test.")
  309. flags.StringVar(&TestContext.EtcdUpgradeVersion, "etcd-upgrade-version", "", "The etcd binary version to upgrade to (e.g., '3.0.14', '2.3.7') if doing an etcd upgrade test.")
  310. flags.StringVar(&TestContext.GCEUpgradeScript, "gce-upgrade-script", "", "Script to use to upgrade a GCE cluster.")
  311. flags.BoolVar(&TestContext.CleanStart, "clean-start", false, "If true, purge all namespaces except default and system before running tests. This serves to Cleanup test namespaces from failed/interrupted e2e runs in a long-lived cluster.")
  312. nodeKiller := &TestContext.NodeKiller
  313. flags.BoolVar(&nodeKiller.Enabled, "node-killer", false, "Whether NodeKiller should kill any nodes.")
  314. flags.Float64Var(&nodeKiller.FailureRatio, "node-killer-failure-ratio", 0.01, "Percentage of nodes to be killed")
  315. flags.DurationVar(&nodeKiller.Interval, "node-killer-interval", 1*time.Minute, "Time between node failures.")
  316. flags.Float64Var(&nodeKiller.JitterFactor, "node-killer-jitter-factor", 60, "Factor used to jitter node failures.")
  317. flags.DurationVar(&nodeKiller.SimulatedDowntime, "node-killer-simulated-downtime", 10*time.Minute, "A delay between node death and recreation")
  318. }
  319. func createKubeConfig(clientCfg *restclient.Config) *clientcmdapi.Config {
  320. clusterNick := "cluster"
  321. userNick := "user"
  322. contextNick := "context"
  323. configCmd := clientcmdapi.NewConfig()
  324. credentials := clientcmdapi.NewAuthInfo()
  325. credentials.Token = clientCfg.BearerToken
  326. credentials.TokenFile = clientCfg.BearerTokenFile
  327. credentials.ClientCertificate = clientCfg.TLSClientConfig.CertFile
  328. if len(credentials.ClientCertificate) == 0 {
  329. credentials.ClientCertificateData = clientCfg.TLSClientConfig.CertData
  330. }
  331. credentials.ClientKey = clientCfg.TLSClientConfig.KeyFile
  332. if len(credentials.ClientKey) == 0 {
  333. credentials.ClientKeyData = clientCfg.TLSClientConfig.KeyData
  334. }
  335. configCmd.AuthInfos[userNick] = credentials
  336. cluster := clientcmdapi.NewCluster()
  337. cluster.Server = clientCfg.Host
  338. cluster.CertificateAuthority = clientCfg.CAFile
  339. if len(cluster.CertificateAuthority) == 0 {
  340. cluster.CertificateAuthorityData = clientCfg.CAData
  341. }
  342. cluster.InsecureSkipTLSVerify = clientCfg.Insecure
  343. configCmd.Clusters[clusterNick] = cluster
  344. context := clientcmdapi.NewContext()
  345. context.Cluster = clusterNick
  346. context.AuthInfo = userNick
  347. configCmd.Contexts[contextNick] = context
  348. configCmd.CurrentContext = contextNick
  349. return configCmd
  350. }
  351. // AfterReadingAllFlags makes changes to the context after all flags
  352. // have been read.
  353. func AfterReadingAllFlags(t *TestContextType) {
  354. // Only set a default host if one won't be supplied via kubeconfig
  355. if len(t.Host) == 0 && len(t.KubeConfig) == 0 {
  356. // Check if we can use the in-cluster config
  357. if clusterConfig, err := restclient.InClusterConfig(); err == nil {
  358. if tempFile, err := ioutil.TempFile(os.TempDir(), "kubeconfig-"); err == nil {
  359. kubeConfig := createKubeConfig(clusterConfig)
  360. clientcmd.WriteToFile(*kubeConfig, tempFile.Name())
  361. t.KubeConfig = tempFile.Name()
  362. klog.Infof("Using a temporary kubeconfig file from in-cluster config : %s", tempFile.Name())
  363. }
  364. }
  365. if len(t.KubeConfig) == 0 {
  366. klog.Warningf("Unable to find in-cluster config, using default host : %s", defaultHost)
  367. t.Host = defaultHost
  368. }
  369. }
  370. // Allow 1% of nodes to be unready (statistically) - relevant for large clusters.
  371. if t.AllowedNotReadyNodes == 0 {
  372. t.AllowedNotReadyNodes = t.CloudConfig.NumNodes / 100
  373. }
  374. klog.Infof("Tolerating taints %q when considering if nodes are ready", TestContext.NonblockingTaints)
  375. // Make sure that all test runs have a valid TestContext.CloudConfig.Provider.
  376. // TODO: whether and how long this code is needed is getting discussed
  377. // in https://github.com/kubernetes/kubernetes/issues/70194.
  378. if TestContext.Provider == "" {
  379. // Some users of the e2e.test binary pass --provider=.
  380. // We need to support that, changing it would break those usages.
  381. Logf("The --provider flag is not set. Continuing as if --provider=skeleton had been used.")
  382. TestContext.Provider = "skeleton"
  383. }
  384. var err error
  385. TestContext.CloudConfig.Provider, err = SetupProviderConfig(TestContext.Provider)
  386. if err != nil {
  387. if os.IsNotExist(errors.Cause(err)) {
  388. // Provide a more helpful error message when the provider is unknown.
  389. var providers []string
  390. for _, name := range GetProviders() {
  391. // The empty string is accepted, but looks odd in the output below unless we quote it.
  392. if name == "" {
  393. name = `""`
  394. }
  395. providers = append(providers, name)
  396. }
  397. sort.Strings(providers)
  398. klog.Errorf("Unknown provider %q. The following providers are known: %v", TestContext.Provider, strings.Join(providers, " "))
  399. } else {
  400. klog.Errorf("Failed to setup provider config for %q: %v", TestContext.Provider, err)
  401. }
  402. os.Exit(1)
  403. }
  404. }