2016-02-22 18:52:20 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-02-22 18:52:20 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-08-12 06:30:04 +00:00
|
|
|
package services
|
2016-02-22 18:52:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-06-10 02:25:36 +00:00
|
|
|
"math/rand"
|
2016-02-22 18:52:20 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-05-23 20:16:47 +00:00
|
|
|
"path"
|
2016-07-05 23:49:47 +00:00
|
|
|
"path/filepath"
|
2016-02-22 18:52:20 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2016-08-04 23:51:11 +00:00
|
|
|
"github.com/kardianos/osext"
|
2016-08-05 00:17:57 +00:00
|
|
|
|
2016-08-26 21:04:21 +00:00
|
|
|
utilconfig "k8s.io/kubernetes/pkg/util/config"
|
2016-08-05 00:17:57 +00:00
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2016-08-12 06:30:04 +00:00
|
|
|
"k8s.io/kubernetes/test/e2e_node/build"
|
2016-02-22 18:52:20 +00:00
|
|
|
)
|
|
|
|
|
2016-09-20 03:08:24 +00:00
|
|
|
// E2EServices starts and stops e2e services in a separate process. The test
|
|
|
|
// uses it to start and stop all e2e services.
|
2016-08-04 23:51:11 +00:00
|
|
|
type E2EServices struct {
|
|
|
|
services *server
|
2016-09-13 22:37:52 +00:00
|
|
|
kubelet *server
|
|
|
|
logFiles map[string]logFileData
|
|
|
|
}
|
|
|
|
|
|
|
|
// logFileData holds data about logfiles to fetch with a journalctl command or
|
|
|
|
// symlink from a node's file system.
|
|
|
|
type logFileData struct {
|
|
|
|
files []string
|
|
|
|
journalctlCommand []string
|
2016-08-04 23:51:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:08:24 +00:00
|
|
|
// NewE2EServices returns a new E2EServices instance.
|
2016-08-04 23:51:11 +00:00
|
|
|
func NewE2EServices() *E2EServices {
|
2016-09-13 22:37:52 +00:00
|
|
|
return &E2EServices{
|
|
|
|
// Special log files that need to be collected for additional debugging.
|
|
|
|
logFiles: map[string]logFileData{
|
|
|
|
"kern.log": {[]string{"/var/log/kern.log"}, []string{"-k"}},
|
|
|
|
"docker.log": {[]string{"/var/log/docker.log", "/var/log/upstart/docker.log"}, []string{"-u", "docker"}},
|
|
|
|
"cloud-init.log": {[]string{"/var/log/cloud-init.log"}, []string{"-u", "cloud*"}},
|
|
|
|
},
|
|
|
|
}
|
2016-08-04 23:51:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:08:24 +00:00
|
|
|
// Start starts the e2e services in another process by calling back into the
|
|
|
|
// test binary. Returns when all e2e services are ready or an error.
|
|
|
|
//
|
2016-08-04 23:51:11 +00:00
|
|
|
// We want to statically link e2e services into the test binary, but we don't
|
2016-09-20 03:08:24 +00:00
|
|
|
// want their glog output to pollute the test result. So we run the binary in
|
2016-09-13 22:37:52 +00:00
|
|
|
// run-services-mode to start e2e services in another process.
|
|
|
|
// The function starts 2 processes:
|
|
|
|
// * internal e2e services: services which statically linked in the test binary - apiserver, etcd and
|
|
|
|
// namespace controller.
|
|
|
|
// * kubelet: kubelet binary is outside. (We plan to move main kubelet start logic out when we have
|
|
|
|
// standard kubelet launcher)
|
2016-08-04 23:51:11 +00:00
|
|
|
func (e *E2EServices) Start() error {
|
|
|
|
var err error
|
2016-09-13 22:37:52 +00:00
|
|
|
// Start kubelet
|
2016-08-04 23:51:11 +00:00
|
|
|
// Create the manifest path for kubelet.
|
|
|
|
// TODO(random-liu): Remove related logic when we move kubelet starting logic out of the test.
|
|
|
|
framework.TestContext.ManifestPath, err = ioutil.TempDir("", "node-e2e-pod")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create static pod manifest directory: %v", err)
|
|
|
|
}
|
2016-09-13 22:37:52 +00:00
|
|
|
e.kubelet, err = e.startKubelet()
|
2016-08-04 23:51:11 +00:00
|
|
|
if err != nil {
|
2016-09-13 22:37:52 +00:00
|
|
|
return fmt.Errorf("failed to start kubelet: %v", err)
|
2016-08-04 23:51:11 +00:00
|
|
|
}
|
2016-09-13 22:37:52 +00:00
|
|
|
e.services, err = e.startInternalServices()
|
|
|
|
return err
|
2016-08-04 23:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the e2e services.
|
2016-09-13 22:37:52 +00:00
|
|
|
func (e *E2EServices) Stop() {
|
2016-08-04 23:51:11 +00:00
|
|
|
defer func() {
|
2016-09-13 22:37:52 +00:00
|
|
|
// Collect log files.
|
|
|
|
e.getLogFiles()
|
2016-08-04 23:51:11 +00:00
|
|
|
// Cleanup the manifest path for kubelet.
|
|
|
|
manifestPath := framework.TestContext.ManifestPath
|
|
|
|
if manifestPath != "" {
|
|
|
|
err := os.RemoveAll(manifestPath)
|
|
|
|
if err != nil {
|
2016-08-15 01:09:42 +00:00
|
|
|
glog.Errorf("Failed to delete static pod manifest directory %s: %v", manifestPath, err)
|
2016-08-04 23:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-09-13 22:37:52 +00:00
|
|
|
if e.services != nil {
|
|
|
|
if err := e.services.kill(); err != nil {
|
|
|
|
glog.Errorf("Failed to stop services: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e.kubelet != nil {
|
|
|
|
if err := e.kubelet.kill(); err != nil {
|
|
|
|
glog.Errorf("Failed to stop kubelet: %v", err)
|
|
|
|
}
|
2016-08-04 23:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunE2EServices actually start the e2e services. This function is used to
|
|
|
|
// start e2e services in current process. This is only used in run-services-mode.
|
|
|
|
func RunE2EServices() {
|
2016-08-26 21:04:21 +00:00
|
|
|
// Populate global DefaultFeatureGate with value from TestContext.FeatureGates.
|
|
|
|
// This way, statically-linked components see the same feature gate config as the test context.
|
|
|
|
utilconfig.DefaultFeatureGate.Set(framework.TestContext.FeatureGates)
|
2016-09-13 22:37:52 +00:00
|
|
|
e := newE2EServices()
|
2016-08-04 23:51:11 +00:00
|
|
|
if err := e.run(); err != nil {
|
|
|
|
glog.Fatalf("Failed to run e2e services: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 18:39:18 +00:00
|
|
|
const (
|
2016-09-13 22:37:52 +00:00
|
|
|
// services.log is the combined log of all services
|
|
|
|
servicesLogFile = "services.log"
|
|
|
|
// LOG_VERBOSITY_LEVEL is consistent with the level used in a cluster e2e test.
|
2016-06-23 18:39:18 +00:00
|
|
|
LOG_VERBOSITY_LEVEL = "4"
|
|
|
|
)
|
|
|
|
|
2016-09-13 22:37:52 +00:00
|
|
|
// startInternalServices starts the internal services in a separate process.
|
|
|
|
func (e *E2EServices) startInternalServices() (*server, error) {
|
|
|
|
testBin, err := osext.Executable()
|
2016-02-22 18:52:20 +00:00
|
|
|
if err != nil {
|
2016-09-13 22:37:52 +00:00
|
|
|
return nil, fmt.Errorf("can't get current binary: %v", err)
|
2016-02-22 18:52:20 +00:00
|
|
|
}
|
2016-09-13 22:37:52 +00:00
|
|
|
startCmd := exec.Command("sudo", testBin,
|
|
|
|
// TODO(mtaufen): Flags e.g. that target the TestContext need to be manually forwarded to the
|
|
|
|
// test binary when we start it up in run-services mode. This is not ideal.
|
|
|
|
// Very unintuitive because it prevents any flags NOT manually forwarded here
|
|
|
|
// from being set via TEST_ARGS when running tests from the command line.
|
|
|
|
"--run-services-mode",
|
|
|
|
"--server-start-timeout", serverStartTimeout.String(),
|
|
|
|
"--feature-gates", framework.TestContext.FeatureGates,
|
|
|
|
"--logtostderr",
|
|
|
|
"--vmodule=*="+LOG_VERBOSITY_LEVEL,
|
|
|
|
)
|
|
|
|
server := newServer("services", startCmd, nil, nil, getServicesHealthCheckURLs(), servicesLogFile, false)
|
|
|
|
return server, server.start()
|
2016-02-22 18:52:20 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:37:52 +00:00
|
|
|
const (
|
|
|
|
// Ports of different e2e services.
|
|
|
|
kubeletPort = "10250"
|
|
|
|
kubeletReadOnlyPort = "10255"
|
|
|
|
// Health check url of kubelet
|
|
|
|
kubeletHealthCheckURL = "http://127.0.0.1:" + kubeletReadOnlyPort + "/healthz"
|
|
|
|
)
|
2016-08-18 08:23:57 +00:00
|
|
|
|
2016-09-20 03:08:24 +00:00
|
|
|
// startKubelet starts the Kubelet in a separate process or returns an error
|
|
|
|
// if the Kubelet fails to start.
|
2016-09-13 22:37:52 +00:00
|
|
|
func (e *E2EServices) startKubelet() (*server, error) {
|
2016-09-20 03:08:24 +00:00
|
|
|
glog.Info("Starting kubelet")
|
2016-08-24 16:03:16 +00:00
|
|
|
var killCommand, restartCommand *exec.Cmd
|
2016-06-09 22:18:55 +00:00
|
|
|
cmdArgs := []string{}
|
|
|
|
if systemdRun, err := exec.LookPath("systemd-run"); err == nil {
|
|
|
|
// On systemd services, detection of a service / unit works reliably while
|
|
|
|
// detection of a process started from an ssh session does not work.
|
|
|
|
// Since kubelet will typically be run as a service it also makes more
|
|
|
|
// sense to test it that way
|
2016-06-10 02:25:36 +00:00
|
|
|
unitName := fmt.Sprintf("kubelet-%d.service", rand.Int31())
|
2016-08-12 06:30:04 +00:00
|
|
|
cmdArgs = append(cmdArgs, systemdRun, "--unit="+unitName, "--remain-after-exit", build.GetKubeletServerBin())
|
2016-08-04 23:51:11 +00:00
|
|
|
killCommand = exec.Command("sudo", "systemctl", "kill", unitName)
|
2016-08-24 16:03:16 +00:00
|
|
|
restartCommand = exec.Command("sudo", "systemctl", "restart", unitName)
|
2016-09-13 22:37:52 +00:00
|
|
|
e.logFiles["kubelet.log"] = logFileData{
|
2016-06-10 02:25:36 +00:00
|
|
|
journalctlCommand: []string{"-u", unitName},
|
|
|
|
}
|
2016-08-22 21:24:25 +00:00
|
|
|
framework.TestContext.EvictionHard = adjustConfigForSystemd(framework.TestContext.EvictionHard)
|
2016-06-09 22:18:55 +00:00
|
|
|
} else {
|
2016-08-12 06:30:04 +00:00
|
|
|
cmdArgs = append(cmdArgs, build.GetKubeletServerBin())
|
2016-08-02 00:21:12 +00:00
|
|
|
cmdArgs = append(cmdArgs,
|
|
|
|
"--runtime-cgroups=/docker-daemon",
|
|
|
|
"--kubelet-cgroups=/kubelet",
|
2016-08-11 00:47:11 +00:00
|
|
|
"--cgroup-root=/",
|
|
|
|
"--system-cgroups=/system",
|
2016-08-02 00:21:12 +00:00
|
|
|
)
|
2016-06-09 22:18:55 +00:00
|
|
|
}
|
|
|
|
cmdArgs = append(cmdArgs,
|
2016-08-21 00:41:34 +00:00
|
|
|
"--api-servers", getAPIServerClientURL(),
|
2016-02-26 23:06:25 +00:00
|
|
|
"--address", "0.0.0.0",
|
2016-08-04 23:51:11 +00:00
|
|
|
"--port", kubeletPort,
|
|
|
|
"--read-only-port", kubeletReadOnlyPort,
|
|
|
|
"--hostname-override", framework.TestContext.NodeName, // Required because hostname is inconsistent across hosts
|
2016-03-14 18:18:27 +00:00
|
|
|
"--volume-stats-agg-period", "10s", // Aggregate volumes frequently so tests don't need to wait as long
|
2016-04-07 00:11:13 +00:00
|
|
|
"--allow-privileged", "true",
|
2016-04-21 22:34:28 +00:00
|
|
|
"--serialize-image-pulls", "false",
|
2016-08-04 23:51:11 +00:00
|
|
|
"--config", framework.TestContext.ManifestPath,
|
2016-04-18 05:00:59 +00:00
|
|
|
"--file-check-frequency", "10s", // Check file frequently so tests won't wait too long
|
2016-06-29 18:25:49 +00:00
|
|
|
"--pod-cidr=10.180.0.0/24", // Assign a fixed CIDR to the node because there is no node controller.
|
2016-08-04 23:51:11 +00:00
|
|
|
"--eviction-hard", framework.TestContext.EvictionHard,
|
2016-07-28 19:05:59 +00:00
|
|
|
"--eviction-pressure-transition-period", "30s",
|
2016-08-26 21:04:21 +00:00
|
|
|
"--feature-gates", framework.TestContext.FeatureGates,
|
2016-09-13 22:37:52 +00:00
|
|
|
"--runtime-integration-type", framework.TestContext.RuntimeIntegrationType,
|
2016-09-19 23:25:22 +00:00
|
|
|
"--v", LOG_VERBOSITY_LEVEL, "--logtostderr",
|
2016-03-14 18:18:27 +00:00
|
|
|
)
|
2016-09-19 23:25:22 +00:00
|
|
|
if framework.TestContext.RuntimeIntegrationType != "" {
|
|
|
|
cmdArgs = append(cmdArgs, "--experimental-runtime-integration-type",
|
|
|
|
framework.TestContext.RuntimeIntegrationType) // Whether to use experimental cri integration.
|
|
|
|
}
|
2016-08-04 23:51:11 +00:00
|
|
|
if framework.TestContext.CgroupsPerQOS {
|
2016-08-11 21:06:06 +00:00
|
|
|
// TODO: enable this when the flag is stable and available in kubelet.
|
|
|
|
// cmdArgs = append(cmdArgs,
|
|
|
|
// "--cgroups-per-qos", "true",
|
|
|
|
// )
|
2016-06-27 18:46:20 +00:00
|
|
|
}
|
2016-08-04 23:51:11 +00:00
|
|
|
if !framework.TestContext.DisableKubenet {
|
2016-07-05 23:49:47 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmdArgs = append(cmdArgs,
|
|
|
|
"--network-plugin=kubenet",
|
2016-08-12 06:30:04 +00:00
|
|
|
// TODO(random-liu): Make sure the cni directory name is the same with that in remote/remote.go
|
|
|
|
"--network-plugin-dir", filepath.Join(cwd, "cni", "bin")) // Enable kubenet
|
2016-07-05 23:49:47 +00:00
|
|
|
}
|
2016-06-27 18:46:20 +00:00
|
|
|
|
2016-06-09 22:18:55 +00:00
|
|
|
cmd := exec.Command("sudo", cmdArgs...)
|
2016-08-04 23:51:11 +00:00
|
|
|
server := newServer(
|
|
|
|
"kubelet",
|
2016-02-26 23:06:25 +00:00
|
|
|
cmd,
|
2016-08-04 23:51:11 +00:00
|
|
|
killCommand,
|
2016-08-24 16:03:16 +00:00
|
|
|
restartCommand,
|
2016-08-04 23:51:11 +00:00
|
|
|
[]string{kubeletHealthCheckURL},
|
2016-08-24 16:03:16 +00:00
|
|
|
"kubelet.log",
|
2016-09-20 03:08:24 +00:00
|
|
|
true /* restartOnExit */)
|
2016-08-04 23:51:11 +00:00
|
|
|
return server, server.start()
|
|
|
|
}
|
|
|
|
|
2016-09-13 22:37:52 +00:00
|
|
|
func adjustConfigForSystemd(config string) string {
|
|
|
|
return strings.Replace(config, "%", "%%", -1)
|
2016-08-04 23:51:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 22:37:52 +00:00
|
|
|
// getLogFiles gets logs of interest either via journalctl or by creating sym
|
|
|
|
// links. Since we scp files from the remote directory, symlinks will be
|
|
|
|
// treated as normal files and file contents will be copied over.
|
|
|
|
func (e *E2EServices) getLogFiles() {
|
|
|
|
// Nothing to do if report dir is not specified.
|
|
|
|
if framework.TestContext.ReportDir == "" {
|
|
|
|
return
|
2016-08-24 16:03:16 +00:00
|
|
|
}
|
2016-09-13 22:37:52 +00:00
|
|
|
glog.Info("Fetching log files...")
|
|
|
|
journaldFound := isJournaldAvailable()
|
|
|
|
for targetFileName, logFileData := range e.logFiles {
|
|
|
|
targetLink := path.Join(framework.TestContext.ReportDir, targetFileName)
|
|
|
|
if journaldFound {
|
|
|
|
// Skip log files that do not have an equivalent in journald-based machines.
|
|
|
|
if len(logFileData.journalctlCommand) == 0 {
|
|
|
|
continue
|
2016-08-24 16:03:16 +00:00
|
|
|
}
|
2016-09-13 22:37:52 +00:00
|
|
|
glog.Infof("Get log file %q with journalctl command %v.", targetFileName, logFileData.journalctlCommand)
|
|
|
|
out, err := exec.Command("sudo", append([]string{"journalctl"}, logFileData.journalctlCommand...)...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("failed to get %q from journald: %v, %v", targetFileName, string(out), err)
|
|
|
|
} else {
|
|
|
|
if err = ioutil.WriteFile(targetLink, out, 0644); err != nil {
|
|
|
|
glog.Errorf("failed to write logs to %q: %v", targetLink, err)
|
2016-08-24 16:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 22:28:32 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-09-13 22:37:52 +00:00
|
|
|
for _, file := range logFileData.files {
|
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
// Expected file not found on this distro.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := copyLogFile(file, targetLink); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
} else {
|
|
|
|
break
|
2016-06-02 22:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 18:52:20 +00:00
|
|
|
}
|
2016-08-22 21:24:25 +00:00
|
|
|
|
2016-09-13 22:37:52 +00:00
|
|
|
// isJournaldAvailable returns whether the system executing the tests uses
|
|
|
|
// journald.
|
|
|
|
func isJournaldAvailable() bool {
|
|
|
|
_, err := exec.LookPath("journalctl")
|
|
|
|
return err == nil
|
2016-08-22 21:24:25 +00:00
|
|
|
}
|
2016-09-20 03:08:24 +00:00
|
|
|
|
|
|
|
func copyLogFile(src, target string) error {
|
|
|
|
// If not a journald based distro, then just symlink files.
|
|
|
|
if out, err := exec.Command("sudo", "cp", src, target).CombinedOutput(); err != nil {
|
|
|
|
return fmt.Errorf("failed to copy %q to %q: %v, %v", src, target, out, err)
|
|
|
|
}
|
|
|
|
if out, err := exec.Command("sudo", "chmod", "a+r", target).CombinedOutput(); err != nil {
|
|
|
|
return fmt.Errorf("failed to make log file %q world readable: %v, %v", target, out, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|