2016-07-22 21:24:05 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 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 dockershim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-27 22:35:23 +00:00
|
|
|
"io"
|
2016-11-04 18:50:51 +00:00
|
|
|
"net/http"
|
2017-05-02 00:17:19 +00:00
|
|
|
"strconv"
|
2017-04-05 00:31:54 +00:00
|
|
|
"sync"
|
2017-02-03 02:28:19 +00:00
|
|
|
"time"
|
2016-07-22 21:24:05 +00:00
|
|
|
|
2017-02-03 02:28:19 +00:00
|
|
|
"github.com/blang/semver"
|
|
|
|
dockertypes "github.com/docker/engine-api/types"
|
2016-10-29 00:01:06 +00:00
|
|
|
"github.com/golang/glog"
|
2016-11-02 03:20:13 +00:00
|
|
|
|
2017-05-02 00:17:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-02-03 21:11:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
2016-10-25 03:42:20 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/componentconfig"
|
2017-02-23 00:05:05 +00:00
|
|
|
internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri"
|
2017-06-07 08:54:28 +00:00
|
|
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2016-12-08 08:59:41 +00:00
|
|
|
kubecm "k8s.io/kubernetes/pkg/kubelet/cm"
|
2016-07-22 21:24:05 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2016-10-10 20:56:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/dockershim/cm"
|
2017-02-14 19:13:29 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/dockershim/errors"
|
2016-10-25 03:42:20 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/network"
|
|
|
|
"k8s.io/kubernetes/pkg/kubelet/network/cni"
|
2017-02-03 21:11:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/network/hostport"
|
2016-10-25 03:42:20 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/network/kubenet"
|
2016-10-26 23:34:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/server/streaming"
|
2017-02-03 02:28:19 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/util/cache"
|
2017-05-03 17:46:35 +00:00
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
|
2016-07-22 21:24:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dockerRuntimeName = "docker"
|
|
|
|
kubeAPIVersion = "0.1.0"
|
|
|
|
|
|
|
|
// String used to detect docker host mode for various namespaces (e.g.
|
|
|
|
// networking). Must match the value returned by docker inspect -f
|
|
|
|
// '{{.HostConfig.NetworkMode}}'.
|
|
|
|
namespaceModeHost = "host"
|
|
|
|
|
2016-07-25 23:25:36 +00:00
|
|
|
dockerNetNSFmt = "/proc/%v/ns/net"
|
|
|
|
|
2016-07-22 21:24:05 +00:00
|
|
|
defaultSeccompProfile = "unconfined"
|
2016-07-25 23:25:36 +00:00
|
|
|
|
|
|
|
// Internal docker labels used to identify whether a container is a sandbox
|
|
|
|
// or a regular container.
|
|
|
|
// TODO: This is not backward compatible with older containers. We will
|
|
|
|
// need to add filtering based on names.
|
|
|
|
containerTypeLabelKey = "io.kubernetes.docker.type"
|
|
|
|
containerTypeLabelSandbox = "podsandbox"
|
|
|
|
containerTypeLabelContainer = "container"
|
2016-10-14 18:52:18 +00:00
|
|
|
containerLogPathLabelKey = "io.kubernetes.container.logpath"
|
2016-09-16 00:31:51 +00:00
|
|
|
sandboxIDLabelKey = "io.kubernetes.sandbox.id"
|
2016-08-25 20:45:38 +00:00
|
|
|
|
2017-02-03 02:28:19 +00:00
|
|
|
// The expiration time of version cache.
|
|
|
|
versionCacheTTL = 60 * time.Second
|
|
|
|
|
2017-02-07 21:57:30 +00:00
|
|
|
defaultCgroupDriver = "cgroupfs"
|
|
|
|
|
2016-08-25 20:45:38 +00:00
|
|
|
// TODO: https://github.com/kubernetes/kubernetes/pull/31169 provides experimental
|
|
|
|
// defaulting of host user namespace that may be enabled when the docker daemon
|
|
|
|
// is using remapped UIDs.
|
|
|
|
// Dockershim should provide detection support for a remapping environment .
|
|
|
|
// This should be included in the feature proposal. Defaulting may still occur according
|
|
|
|
// to kubelet behavior and system settings in addition to any API flags that may be introduced.
|
2016-07-22 21:24:05 +00:00
|
|
|
)
|
|
|
|
|
2016-11-02 04:39:46 +00:00
|
|
|
// NetworkPluginSettings is the subset of kubelet runtime args we pass
|
2016-10-25 03:42:20 +00:00
|
|
|
// to the container runtime shim so it can probe for network plugins.
|
|
|
|
// In the future we will feed these directly to a standalone container
|
|
|
|
// runtime process.
|
|
|
|
type NetworkPluginSettings struct {
|
|
|
|
// HairpinMode is best described by comments surrounding the kubelet arg
|
|
|
|
HairpinMode componentconfig.HairpinMode
|
|
|
|
// NonMasqueradeCIDR is the range of ips which should *not* be included
|
|
|
|
// in any MASQUERADE rules applied by the plugin
|
|
|
|
NonMasqueradeCIDR string
|
|
|
|
// PluginName is the name of the plugin, runtime shim probes for
|
|
|
|
PluginName string
|
|
|
|
// PluginBinDir is the directory in which the binaries for the plugin with
|
|
|
|
// PluginName is kept. The admin is responsible for provisioning these
|
|
|
|
// binaries before-hand.
|
|
|
|
PluginBinDir string
|
|
|
|
// PluginConfDir is the directory in which the admin places a CNI conf.
|
|
|
|
// Depending on the plugin, this may be an optional field, eg: kubenet
|
|
|
|
// generates its own plugin conf.
|
|
|
|
PluginConfDir string
|
|
|
|
// MTU is the desired MTU for network devices created by the plugin.
|
|
|
|
MTU int
|
|
|
|
|
|
|
|
// RuntimeHost is an interface that serves as a trap-door from plugin back
|
|
|
|
// into the kubelet.
|
2016-10-29 00:01:06 +00:00
|
|
|
// TODO: This shouldn't be required, remove once we move host ports into CNI
|
|
|
|
// and figure out bandwidth shaping. See corresponding comments above
|
|
|
|
// network.Host interface.
|
|
|
|
LegacyRuntimeHost network.LegacyHost
|
2016-10-25 03:42:20 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 21:11:01 +00:00
|
|
|
// namespaceGetter is a wrapper around the dockerService that implements
|
|
|
|
// the network.NamespaceGetter interface.
|
|
|
|
type namespaceGetter struct {
|
|
|
|
ds *dockerService
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *namespaceGetter) GetNetNS(containerID string) (string, error) {
|
|
|
|
return n.ds.GetNetNS(containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// portMappingGetter is a wrapper around the dockerService that implements
|
|
|
|
// the network.PortMappingGetter interface.
|
|
|
|
type portMappingGetter struct {
|
|
|
|
ds *dockerService
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *portMappingGetter) GetPodPortMappings(containerID string) ([]*hostport.PortMapping, error) {
|
|
|
|
return p.ds.GetPodPortMappings(containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dockerNetworkHost implements network.Host by wrapping the legacy host passed in by the kubelet
|
|
|
|
// and dockerServices which implementes the rest of the network host interfaces.
|
|
|
|
// The legacy host methods are slated for deletion.
|
|
|
|
type dockerNetworkHost struct {
|
|
|
|
network.LegacyHost
|
|
|
|
*namespaceGetter
|
|
|
|
*portMappingGetter
|
|
|
|
}
|
|
|
|
|
2016-10-14 18:52:18 +00:00
|
|
|
var internalLabelKeys []string = []string{containerTypeLabelKey, containerLogPathLabelKey, sandboxIDLabelKey}
|
2016-09-16 00:31:51 +00:00
|
|
|
|
2016-09-26 07:46:29 +00:00
|
|
|
// NOTE: Anything passed to DockerService should be eventually handled in another way when we switch to running the shim as a different process.
|
2017-05-03 17:46:35 +00:00
|
|
|
func NewDockerService(client libdocker.Interface, seccompProfileRoot string, podSandboxImage string, streamingConfig *streaming.Config,
|
2017-05-01 22:57:19 +00:00
|
|
|
pluginSettings *NetworkPluginSettings, cgroupsName string, kubeCgroupDriver string, execHandlerName, dockershimRootDir string, disableSharedPID bool) (DockerService, error) {
|
2017-05-03 17:46:35 +00:00
|
|
|
c := libdocker.NewInstrumentedInterface(client)
|
2017-04-11 19:57:49 +00:00
|
|
|
checkpointHandler, err := NewPersistentCheckpointHandler(dockershimRootDir)
|
2017-02-17 22:17:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-01 22:57:19 +00:00
|
|
|
var execHandler ExecHandler
|
|
|
|
switch execHandlerName {
|
|
|
|
case "native":
|
|
|
|
execHandler = &NativeExecHandler{}
|
|
|
|
case "nsenter":
|
|
|
|
execHandler = &NsenterExecHandler{}
|
|
|
|
default:
|
|
|
|
glog.Warningf("Unknown Docker exec handler %q; defaulting to native", execHandlerName)
|
|
|
|
execHandler = &NativeExecHandler{}
|
|
|
|
}
|
|
|
|
|
2016-10-26 23:34:45 +00:00
|
|
|
ds := &dockerService{
|
2016-09-26 07:46:29 +00:00
|
|
|
seccompProfileRoot: seccompProfileRoot,
|
2016-10-10 20:56:53 +00:00
|
|
|
client: c,
|
2016-10-14 18:52:18 +00:00
|
|
|
os: kubecontainer.RealOS{},
|
2016-09-26 14:18:06 +00:00
|
|
|
podSandboxImage: podSandboxImage,
|
2016-10-26 23:34:45 +00:00
|
|
|
streamingRuntime: &streamingRuntime{
|
2017-01-20 00:23:48 +00:00
|
|
|
client: client,
|
|
|
|
execHandler: execHandler,
|
2016-10-26 23:34:45 +00:00
|
|
|
},
|
2017-01-11 23:40:01 +00:00
|
|
|
containerManager: cm.NewContainerManager(cgroupsName, client),
|
2017-02-17 22:17:11 +00:00
|
|
|
checkpointHandler: checkpointHandler,
|
2017-04-19 01:29:50 +00:00
|
|
|
disableSharedPID: disableSharedPID,
|
2017-04-05 00:31:54 +00:00
|
|
|
networkReady: make(map[string]bool),
|
2016-07-22 21:24:05 +00:00
|
|
|
}
|
2017-03-15 01:28:06 +00:00
|
|
|
|
|
|
|
// check docker version compatibility.
|
|
|
|
if err = ds.checkVersionCompatibility(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create streaming server if configured.
|
2016-10-26 23:34:45 +00:00
|
|
|
if streamingConfig != nil {
|
|
|
|
var err error
|
|
|
|
ds.streamingServer, err = streaming.NewServer(*streamingConfig, ds.streamingRuntime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 03:42:20 +00:00
|
|
|
// dockershim currently only supports CNI plugins.
|
|
|
|
cniPlugins := cni.ProbeNetworkPlugins(pluginSettings.PluginConfDir, pluginSettings.PluginBinDir)
|
|
|
|
cniPlugins = append(cniPlugins, kubenet.NewPlugin(pluginSettings.PluginBinDir))
|
2016-10-29 00:01:06 +00:00
|
|
|
netHost := &dockerNetworkHost{
|
|
|
|
pluginSettings.LegacyRuntimeHost,
|
|
|
|
&namespaceGetter{ds},
|
2017-02-03 21:11:01 +00:00
|
|
|
&portMappingGetter{ds},
|
2016-10-29 00:01:06 +00:00
|
|
|
}
|
|
|
|
plug, err := network.InitNetworkPlugin(cniPlugins, pluginSettings.PluginName, netHost, pluginSettings.HairpinMode, pluginSettings.NonMasqueradeCIDR, pluginSettings.MTU)
|
2016-10-25 03:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("didn't find compatible CNI plugin with given settings %+v: %v", pluginSettings, err)
|
|
|
|
}
|
2016-12-13 22:19:04 +00:00
|
|
|
ds.network = network.NewPluginManager(plug)
|
2016-10-29 00:01:06 +00:00
|
|
|
glog.Infof("Docker cri networking managed by %v", plug.Name())
|
2016-12-08 08:59:41 +00:00
|
|
|
|
|
|
|
// NOTE: cgroup driver is only detectable in docker 1.11+
|
2017-02-07 21:57:30 +00:00
|
|
|
cgroupDriver := defaultCgroupDriver
|
2016-12-08 08:59:41 +00:00
|
|
|
dockerInfo, err := ds.client.Info()
|
|
|
|
if err != nil {
|
2017-02-07 21:57:30 +00:00
|
|
|
glog.Errorf("Failed to execute Info() call to the Docker client: %v", err)
|
|
|
|
glog.Warningf("Falling back to use the default driver: %q", cgroupDriver)
|
|
|
|
} else if len(dockerInfo.CgroupDriver) == 0 {
|
|
|
|
glog.Warningf("No cgroup driver is set in Docker")
|
|
|
|
glog.Warningf("Falling back to use the default driver: %q", cgroupDriver)
|
2016-12-08 08:59:41 +00:00
|
|
|
} else {
|
|
|
|
cgroupDriver = dockerInfo.CgroupDriver
|
|
|
|
}
|
2017-02-07 21:57:30 +00:00
|
|
|
if len(kubeCgroupDriver) != 0 && kubeCgroupDriver != cgroupDriver {
|
|
|
|
return nil, fmt.Errorf("misconfiguration: kubelet cgroup driver: %q is different from docker cgroup driver: %q", kubeCgroupDriver, cgroupDriver)
|
|
|
|
}
|
|
|
|
glog.Infof("Setting cgroupDriver to %s", cgroupDriver)
|
2016-12-08 08:59:41 +00:00
|
|
|
ds.cgroupDriver = cgroupDriver
|
2017-02-03 02:28:19 +00:00
|
|
|
ds.versionCache = cache.NewObjectCache(
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return ds.getDockerVersion()
|
|
|
|
},
|
|
|
|
versionCacheTTL,
|
|
|
|
)
|
2016-10-26 23:34:45 +00:00
|
|
|
return ds, nil
|
2016-07-22 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 18:36:13 +00:00
|
|
|
// DockerService is an interface that embeds the new RuntimeService and
|
|
|
|
// ImageService interfaces.
|
2016-10-04 03:49:19 +00:00
|
|
|
type DockerService interface {
|
2016-11-30 07:27:27 +00:00
|
|
|
internalapi.RuntimeService
|
|
|
|
internalapi.ImageManagerService
|
2016-10-10 20:56:53 +00:00
|
|
|
Start() error
|
2016-11-04 18:50:51 +00:00
|
|
|
// For serving streaming calls.
|
|
|
|
http.Handler
|
2016-10-04 03:49:19 +00:00
|
|
|
}
|
2016-07-22 21:24:05 +00:00
|
|
|
|
|
|
|
type dockerService struct {
|
2016-09-26 07:46:29 +00:00
|
|
|
seccompProfileRoot string
|
2017-05-03 17:46:35 +00:00
|
|
|
client libdocker.Interface
|
2016-10-14 18:52:18 +00:00
|
|
|
os kubecontainer.OSInterface
|
2016-09-26 14:18:06 +00:00
|
|
|
podSandboxImage string
|
2016-10-26 23:34:45 +00:00
|
|
|
streamingRuntime *streamingRuntime
|
|
|
|
streamingServer streaming.Server
|
2017-04-05 00:31:54 +00:00
|
|
|
|
|
|
|
network *network.PluginManager
|
|
|
|
// Map of podSandboxID :: network-is-ready
|
|
|
|
networkReady map[string]bool
|
|
|
|
networkReadyLock sync.Mutex
|
|
|
|
|
|
|
|
containerManager cm.ContainerManager
|
2016-12-08 08:59:41 +00:00
|
|
|
// cgroup driver used by Docker runtime.
|
2017-01-11 23:40:01 +00:00
|
|
|
cgroupDriver string
|
|
|
|
checkpointHandler CheckpointHandler
|
2017-01-31 02:57:03 +00:00
|
|
|
// legacyCleanup indicates whether legacy cleanup has finished or not.
|
|
|
|
legacyCleanup legacyCleanupFlag
|
2017-02-03 02:28:19 +00:00
|
|
|
// caches the version of the runtime.
|
|
|
|
// To be compatible with multiple docker versions, we need to perform
|
|
|
|
// version checking for some operations. Use this cache to avoid querying
|
|
|
|
// the docker daemon every time we need to do such checks.
|
|
|
|
versionCache *cache.ObjectCache
|
2017-04-19 01:29:50 +00:00
|
|
|
// This option provides an escape hatch to override the new default behavior for Docker under
|
|
|
|
// the CRI to use a shared PID namespace for all pods. It is temporary and will be removed.
|
|
|
|
// See proposals/pod-pid-namespace.md for details.
|
|
|
|
// TODO: Remove once the escape hatch is no longer used (https://issues.k8s.io/41938)
|
|
|
|
disableSharedPID bool
|
2016-07-22 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the runtime name, runtime version and runtime API version
|
2016-11-30 07:27:27 +00:00
|
|
|
func (ds *dockerService) Version(_ string) (*runtimeapi.VersionResponse, error) {
|
2017-02-03 02:28:19 +00:00
|
|
|
v, err := ds.getDockerVersion()
|
2016-07-22 21:24:05 +00:00
|
|
|
if err != nil {
|
2017-02-03 02:28:19 +00:00
|
|
|
return nil, err
|
2016-07-22 21:24:05 +00:00
|
|
|
}
|
2016-11-30 07:27:27 +00:00
|
|
|
return &runtimeapi.VersionResponse{
|
2017-02-03 02:28:19 +00:00
|
|
|
Version: kubeAPIVersion,
|
|
|
|
RuntimeName: dockerRuntimeName,
|
2017-01-20 01:55:37 +00:00
|
|
|
RuntimeVersion: v.Version,
|
2017-02-03 02:28:19 +00:00
|
|
|
RuntimeApiVersion: v.APIVersion,
|
2016-07-22 21:24:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2016-10-07 19:32:57 +00:00
|
|
|
|
2017-02-03 02:28:19 +00:00
|
|
|
// dockerVersion gets the version information from docker.
|
|
|
|
func (ds *dockerService) getDockerVersion() (*dockertypes.Version, error) {
|
|
|
|
v, err := ds.client.Version()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get docker version: %v", err)
|
|
|
|
}
|
|
|
|
// Docker API version (e.g., 1.23) is not semver compatible. Add a ".0"
|
|
|
|
// suffix to remedy this.
|
|
|
|
v.APIVersion = fmt.Sprintf("%s.0", v.APIVersion)
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2016-10-28 23:53:33 +00:00
|
|
|
// UpdateRuntimeConfig updates the runtime config. Currently only handles podCIDR updates.
|
2016-11-30 07:27:27 +00:00
|
|
|
func (ds *dockerService) UpdateRuntimeConfig(runtimeConfig *runtimeapi.RuntimeConfig) (err error) {
|
2016-10-28 23:53:33 +00:00
|
|
|
if runtimeConfig == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
glog.Infof("docker cri received runtime config %+v", runtimeConfig)
|
2016-12-13 22:19:04 +00:00
|
|
|
if ds.network != nil && runtimeConfig.NetworkConfig.PodCidr != "" {
|
2016-10-28 23:53:33 +00:00
|
|
|
event := make(map[string]interface{})
|
2017-01-20 01:55:37 +00:00
|
|
|
event[network.NET_PLUGIN_EVENT_POD_CIDR_CHANGE_DETAIL_CIDR] = runtimeConfig.NetworkConfig.PodCidr
|
2016-12-13 22:19:04 +00:00
|
|
|
ds.network.Event(network.NET_PLUGIN_EVENT_POD_CIDR_CHANGE, event)
|
2016-10-28 23:53:33 +00:00
|
|
|
}
|
|
|
|
return
|
2016-10-07 19:32:57 +00:00
|
|
|
}
|
2016-10-29 00:01:06 +00:00
|
|
|
|
|
|
|
// GetNetNS returns the network namespace of the given containerID. The ID
|
|
|
|
// supplied is typically the ID of a pod sandbox. This getter doesn't try
|
|
|
|
// to map non-sandbox IDs to their respective sandboxes.
|
|
|
|
func (ds *dockerService) GetNetNS(podSandboxID string) (string, error) {
|
|
|
|
r, err := ds.client.InspectContainer(podSandboxID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-03-30 22:33:58 +00:00
|
|
|
return getNetworkNamespace(r)
|
2016-10-29 00:01:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 21:11:01 +00:00
|
|
|
// GetPodPortMappings returns the port mappings of the given podSandbox ID.
|
|
|
|
func (ds *dockerService) GetPodPortMappings(podSandboxID string) ([]*hostport.PortMapping, error) {
|
|
|
|
// TODO: get portmappings from docker labels for backward compatibility
|
|
|
|
checkpoint, err := ds.checkpointHandler.GetCheckpoint(podSandboxID)
|
2017-02-14 19:13:29 +00:00
|
|
|
// Return empty portMappings if checkpoint is not found
|
2017-02-03 21:11:01 +00:00
|
|
|
if err != nil {
|
2017-02-14 19:13:29 +00:00
|
|
|
if err == errors.CheckpointNotFoundError {
|
2017-03-06 02:46:26 +00:00
|
|
|
glog.Warningf("Failed to retrieve checkpoint for sandbox %q: %v", podSandboxID, err)
|
2017-02-14 19:13:29 +00:00
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-03 21:11:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 15:25:56 +00:00
|
|
|
portMappings := make([]*hostport.PortMapping, 0, len(checkpoint.Data.PortMappings))
|
2017-02-03 21:11:01 +00:00
|
|
|
for _, pm := range checkpoint.Data.PortMappings {
|
|
|
|
proto := toAPIProtocol(*pm.Protocol)
|
|
|
|
portMappings = append(portMappings, &hostport.PortMapping{
|
|
|
|
HostPort: *pm.HostPort,
|
|
|
|
ContainerPort: *pm.ContainerPort,
|
|
|
|
Protocol: proto,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return portMappings, nil
|
2016-10-29 00:01:06 +00:00
|
|
|
}
|
2016-10-10 20:56:53 +00:00
|
|
|
|
|
|
|
// Start initializes and starts components in dockerService.
|
|
|
|
func (ds *dockerService) Start() error {
|
2017-01-31 02:57:03 +00:00
|
|
|
// Initialize the legacy cleanup flag.
|
|
|
|
ds.LegacyCleanupInit()
|
2016-10-10 20:56:53 +00:00
|
|
|
return ds.containerManager.Start()
|
|
|
|
}
|
2016-11-02 03:20:13 +00:00
|
|
|
|
|
|
|
// Status returns the status of the runtime.
|
|
|
|
// TODO(random-liu): Set network condition accordingly here.
|
2016-11-30 07:27:27 +00:00
|
|
|
func (ds *dockerService) Status() (*runtimeapi.RuntimeStatus, error) {
|
|
|
|
runtimeReady := &runtimeapi.RuntimeCondition{
|
2017-01-20 01:55:37 +00:00
|
|
|
Type: runtimeapi.RuntimeReady,
|
|
|
|
Status: true,
|
2016-11-02 03:20:13 +00:00
|
|
|
}
|
2016-11-30 07:27:27 +00:00
|
|
|
networkReady := &runtimeapi.RuntimeCondition{
|
2017-01-20 01:55:37 +00:00
|
|
|
Type: runtimeapi.NetworkReady,
|
|
|
|
Status: true,
|
2016-11-02 03:20:13 +00:00
|
|
|
}
|
2016-11-30 07:27:27 +00:00
|
|
|
conditions := []*runtimeapi.RuntimeCondition{runtimeReady, networkReady}
|
2016-11-03 01:23:57 +00:00
|
|
|
if _, err := ds.client.Version(); err != nil {
|
2017-01-20 01:55:37 +00:00
|
|
|
runtimeReady.Status = false
|
|
|
|
runtimeReady.Reason = "DockerDaemonNotReady"
|
|
|
|
runtimeReady.Message = fmt.Sprintf("docker: failed to get docker version: %v", err)
|
2016-11-02 03:20:13 +00:00
|
|
|
}
|
2016-12-13 22:19:04 +00:00
|
|
|
if err := ds.network.Status(); err != nil {
|
2017-01-20 01:55:37 +00:00
|
|
|
networkReady.Status = false
|
|
|
|
networkReady.Reason = "NetworkPluginNotReady"
|
|
|
|
networkReady.Message = fmt.Sprintf("docker: network plugin is not ready: %v", err)
|
2016-11-03 01:23:57 +00:00
|
|
|
}
|
2016-11-30 07:27:27 +00:00
|
|
|
return &runtimeapi.RuntimeStatus{Conditions: conditions}, nil
|
2016-11-02 03:20:13 +00:00
|
|
|
}
|
2016-11-04 18:50:51 +00:00
|
|
|
|
|
|
|
func (ds *dockerService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if ds.streamingServer != nil {
|
|
|
|
ds.streamingServer.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 08:59:41 +00:00
|
|
|
|
|
|
|
// GenerateExpectedCgroupParent returns cgroup parent in syntax expected by cgroup driver
|
|
|
|
func (ds *dockerService) GenerateExpectedCgroupParent(cgroupParent string) (string, error) {
|
|
|
|
if len(cgroupParent) > 0 {
|
|
|
|
// if docker uses the systemd cgroup driver, it expects *.slice style names for cgroup parent.
|
|
|
|
// if we configured kubelet to use --cgroup-driver=cgroupfs, and docker is configured to use systemd driver
|
|
|
|
// docker will fail to launch the container because the name we provide will not be a valid slice.
|
|
|
|
// this is a very good thing.
|
|
|
|
if ds.cgroupDriver == "systemd" {
|
|
|
|
systemdCgroupParent, err := kubecm.ConvertCgroupFsNameToSystemd(cgroupParent)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
cgroupParent = systemdCgroupParent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glog.V(3).Infof("Setting cgroup parent to: %q", cgroupParent)
|
|
|
|
return cgroupParent, nil
|
|
|
|
}
|
2017-02-03 02:28:19 +00:00
|
|
|
|
2017-03-15 01:28:06 +00:00
|
|
|
// checkVersionCompatibility verifies whether docker is in a compatible version.
|
|
|
|
func (ds *dockerService) checkVersionCompatibility() error {
|
|
|
|
apiVersion, err := ds.getDockerAPIVersion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:46:35 +00:00
|
|
|
minAPIVersion, err := semver.Parse(libdocker.MinimumDockerAPIVersion)
|
2017-03-15 01:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the docker version.
|
|
|
|
result := apiVersion.Compare(minAPIVersion)
|
|
|
|
if result < 0 {
|
2017-05-03 17:46:35 +00:00
|
|
|
return fmt.Errorf("docker API version is older than %s", libdocker.MinimumDockerAPIVersion)
|
2017-03-15 01:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-03 02:28:19 +00:00
|
|
|
// getDockerAPIVersion gets the semver-compatible docker api version.
|
|
|
|
func (ds *dockerService) getDockerAPIVersion() (*semver.Version, error) {
|
|
|
|
var dv *dockertypes.Version
|
|
|
|
var err error
|
|
|
|
if ds.versionCache != nil {
|
|
|
|
dv, err = ds.getDockerVersionFromCache()
|
|
|
|
} else {
|
|
|
|
dv, err = ds.getDockerVersion()
|
|
|
|
}
|
2017-04-04 16:38:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-03 02:28:19 +00:00
|
|
|
|
|
|
|
apiVersion, err := semver.Parse(dv.APIVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &apiVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *dockerService) getDockerVersionFromCache() (*dockertypes.Version, error) {
|
|
|
|
// We only store on key in the cache.
|
|
|
|
const dummyKey = "version"
|
|
|
|
value, err := ds.versionCache.Get(dummyKey)
|
|
|
|
dv := value.(*dockertypes.Version)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return dv, nil
|
|
|
|
}
|
2017-02-03 21:11:01 +00:00
|
|
|
|
|
|
|
func toAPIProtocol(protocol Protocol) v1.Protocol {
|
|
|
|
switch protocol {
|
|
|
|
case protocolTCP:
|
|
|
|
return v1.ProtocolTCP
|
|
|
|
case protocolUDP:
|
|
|
|
return v1.ProtocolUDP
|
|
|
|
}
|
|
|
|
glog.Warningf("Unknown protocol %q: defaulting to TCP", protocol)
|
|
|
|
return v1.ProtocolTCP
|
|
|
|
}
|
2017-02-27 22:35:23 +00:00
|
|
|
|
|
|
|
// DockerLegacyService interface embeds some legacy methods for backward compatibility.
|
|
|
|
type DockerLegacyService interface {
|
|
|
|
// GetContainerLogs gets logs for a specific container.
|
|
|
|
GetContainerLogs(*v1.Pod, kubecontainer.ContainerID, *v1.PodLogOptions, io.Writer, io.Writer) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// dockerLegacyService implements the DockerLegacyService. We add this for non json-log driver
|
|
|
|
// support. (See #41996)
|
|
|
|
type dockerLegacyService struct {
|
2017-05-03 17:46:35 +00:00
|
|
|
client libdocker.Interface
|
2017-02-27 22:35:23 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:46:35 +00:00
|
|
|
func NewDockerLegacyService(client libdocker.Interface) DockerLegacyService {
|
2017-02-27 22:35:23 +00:00
|
|
|
return &dockerLegacyService{client: client}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContainerLogs get container logs directly from docker daemon.
|
|
|
|
func (d *dockerLegacyService) GetContainerLogs(pod *v1.Pod, containerID kubecontainer.ContainerID, logOptions *v1.PodLogOptions, stdout, stderr io.Writer) error {
|
|
|
|
container, err := d.client.InspectContainer(containerID.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-02 00:17:19 +00:00
|
|
|
|
|
|
|
var since int64
|
|
|
|
if logOptions.SinceSeconds != nil {
|
|
|
|
t := metav1.Now().Add(-time.Duration(*logOptions.SinceSeconds) * time.Second)
|
|
|
|
since = t.Unix()
|
|
|
|
}
|
|
|
|
if logOptions.SinceTime != nil {
|
|
|
|
since = logOptions.SinceTime.Unix()
|
|
|
|
}
|
|
|
|
opts := dockertypes.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Since: strconv.FormatInt(since, 10),
|
|
|
|
Timestamps: logOptions.Timestamps,
|
|
|
|
Follow: logOptions.Follow,
|
|
|
|
}
|
|
|
|
if logOptions.TailLines != nil {
|
|
|
|
opts.Tail = strconv.FormatInt(*logOptions.TailLines, 10)
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:46:35 +00:00
|
|
|
sopts := libdocker.StreamOptions{
|
2017-05-02 00:17:19 +00:00
|
|
|
OutputStream: stdout,
|
|
|
|
ErrorStream: stderr,
|
|
|
|
RawTerminal: container.Config.Tty,
|
|
|
|
}
|
|
|
|
return d.client.Logs(containerID.ID, opts, sopts)
|
2017-02-27 22:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// criSupportedLogDrivers are log drivers supported by native CRI integration.
|
|
|
|
var criSupportedLogDrivers = []string{"json-file"}
|
|
|
|
|
|
|
|
// IsCRISupportedLogDriver checks whether the logging driver used by docker is
|
|
|
|
// suppoted by native CRI integration.
|
2017-05-03 17:46:35 +00:00
|
|
|
func IsCRISupportedLogDriver(client libdocker.Interface) (bool, error) {
|
2017-02-27 22:35:23 +00:00
|
|
|
info, err := client.Info()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to get docker info: %v", err)
|
|
|
|
}
|
|
|
|
for _, driver := range criSupportedLogDrivers {
|
|
|
|
if info.LoggingDriver == driver {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|