mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-09 13:12:20 +00:00
Introduce a type for docker container ids
This commit is contained in:
@@ -61,6 +61,9 @@ type DockerInterface interface {
|
|||||||
StopContainer(id string, timeout uint) error
|
StopContainer(id string, timeout uint) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Type to make it clear when we're working with docker container Ids
|
||||||
|
type DockerContainerId string
|
||||||
|
|
||||||
//Interface for testability
|
//Interface for testability
|
||||||
type DockerPuller interface {
|
type DockerPuller interface {
|
||||||
Pull(image string) error
|
Pull(image string) error
|
||||||
@@ -170,8 +173,8 @@ func (kl *Kubelet) LogEvent(event *api.Event) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return a map of docker containers that we manage. The map key is the docker container ID
|
// Return a map of docker containers that we manage. The map key is the docker container ID
|
||||||
func (kl *Kubelet) getDockerContainers() (map[string]docker.APIContainers, error) {
|
func (kl *Kubelet) getDockerContainers() (map[DockerContainerId]docker.APIContainers, error) {
|
||||||
result := map[string]docker.APIContainers{}
|
result := map[DockerContainerId]docker.APIContainers{}
|
||||||
containerList, err := kl.DockerClient.ListContainers(docker.ListContainersOptions{})
|
containerList, err := kl.DockerClient.ListContainers(docker.ListContainersOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
@@ -182,21 +185,21 @@ func (kl *Kubelet) getDockerContainers() (map[string]docker.APIContainers, error
|
|||||||
if !strings.HasPrefix(value.Names[0], "/"+containerNamePrefix+"--") {
|
if !strings.HasPrefix(value.Names[0], "/"+containerNamePrefix+"--") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
result[value.ID] = value
|
result[DockerContainerId(value.ID)] = value
|
||||||
}
|
}
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return Docker's container ID for a manifest's container. Returns an empty string if it doesn't exist
|
// Return Docker's container ID for a manifest's container. Returns an empty string if it doesn't exist.
|
||||||
func (kl *Kubelet) getContainerId(manifest *api.ContainerManifest, container *api.Container) (string, error) {
|
func (kl *Kubelet) getContainerId(manifest *api.ContainerManifest, container *api.Container) (DockerContainerId, error) {
|
||||||
dockerContainers, err := kl.getDockerContainers()
|
dockerContainers, err := kl.getDockerContainers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
for id, dockerContainer := range dockerContainers {
|
for id, dockerContainer := range dockerContainers {
|
||||||
manifestId, containerName := dockerNameToManifestAndContainer(dockerContainer.Names[0])
|
manifestId, containerName := parseDockerName(dockerContainer.Names[0])
|
||||||
if manifestId == manifest.Id && containerName == container.Name {
|
if manifestId == manifest.Id && containerName == container.Name {
|
||||||
return id, nil
|
return DockerContainerId(id), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
@@ -234,14 +237,14 @@ func unescapeDash(in string) (out string) {
|
|||||||
const containerNamePrefix = "k8s"
|
const containerNamePrefix = "k8s"
|
||||||
|
|
||||||
// Creates a name which can be reversed to identify both manifest id and container name.
|
// Creates a name which can be reversed to identify both manifest id and container name.
|
||||||
func manifestAndContainerToDockerName(manifest *api.ContainerManifest, container *api.Container) string {
|
func buildDockerName(manifest *api.ContainerManifest, container *api.Container) string {
|
||||||
// Note, manifest.Id could be blank.
|
// Note, manifest.Id could be blank.
|
||||||
return fmt.Sprintf("%s--%s--%s--%08x", containerNamePrefix, escapeDash(container.Name), escapeDash(manifest.Id), rand.Uint32())
|
return fmt.Sprintf("%s--%s--%s--%08x", containerNamePrefix, escapeDash(container.Name), escapeDash(manifest.Id), rand.Uint32())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upacks a container name, returning the manifest id and container name we would have used to
|
// Upacks a container name, returning the manifest id and container name we would have used to
|
||||||
// construct the docker name. If the docker name isn't one we created, we may return empty strings.
|
// construct the docker name. If the docker name isn't one we created, we may return empty strings.
|
||||||
func dockerNameToManifestAndContainer(name string) (manifestId, containerName string) {
|
func parseDockerName(name string) (manifestId, containerName string) {
|
||||||
// For some reason docker appears to be appending '/' to names.
|
// For some reason docker appears to be appending '/' to names.
|
||||||
// If its there, strip it.
|
// If its there, strip it.
|
||||||
if name[0] == '/' {
|
if name[0] == '/' {
|
||||||
@@ -320,13 +323,13 @@ func makePortsAndBindings(container *api.Container) (map[docker.Port]struct{}, m
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run a single container from a manifest. Returns the docker container ID
|
// Run a single container from a manifest. Returns the docker container ID
|
||||||
func (kl *Kubelet) runContainer(manifest *api.ContainerManifest, container *api.Container, netMode string) (id string, err error) {
|
func (kl *Kubelet) runContainer(manifest *api.ContainerManifest, container *api.Container, netMode string) (id DockerContainerId, err error) {
|
||||||
envVariables := makeEnvironmentVariables(container)
|
envVariables := makeEnvironmentVariables(container)
|
||||||
volumes, binds := makeVolumesAndBinds(container)
|
volumes, binds := makeVolumesAndBinds(container)
|
||||||
exposedPorts, portBindings := makePortsAndBindings(container)
|
exposedPorts, portBindings := makePortsAndBindings(container)
|
||||||
|
|
||||||
opts := docker.CreateContainerOptions{
|
opts := docker.CreateContainerOptions{
|
||||||
Name: manifestAndContainerToDockerName(manifest, container),
|
Name: buildDockerName(manifest, container),
|
||||||
Config: &docker.Config{
|
Config: &docker.Config{
|
||||||
Image: container.Image,
|
Image: container.Image,
|
||||||
ExposedPorts: exposedPorts,
|
ExposedPorts: exposedPorts,
|
||||||
@@ -345,13 +348,13 @@ func (kl *Kubelet) runContainer(manifest *api.ContainerManifest, container *api.
|
|||||||
Binds: binds,
|
Binds: binds,
|
||||||
NetworkMode: netMode,
|
NetworkMode: netMode,
|
||||||
})
|
})
|
||||||
return dockerContainer.ID, err
|
return DockerContainerId(dockerContainer.ID), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kill a docker container
|
// Kill a docker container
|
||||||
func (kl *Kubelet) killContainer(container docker.APIContainers) error {
|
func (kl *Kubelet) killContainer(container docker.APIContainers) error {
|
||||||
err := kl.DockerClient.StopContainer(container.ID, 10)
|
err := kl.DockerClient.StopContainer(container.ID, 10)
|
||||||
manifestId, containerName := dockerNameToManifestAndContainer(container.Names[0])
|
manifestId, containerName := parseDockerName(container.Names[0])
|
||||||
kl.LogEvent(&api.Event{
|
kl.LogEvent(&api.Event{
|
||||||
Event: "STOP",
|
Event: "STOP",
|
||||||
Manifest: &api.ContainerManifest{
|
Manifest: &api.ContainerManifest{
|
||||||
@@ -607,13 +610,13 @@ func (kl *Kubelet) WatchEtcd(watchChannel <-chan *etcd.Response, updateChannel c
|
|||||||
|
|
||||||
const networkContainerName = "net"
|
const networkContainerName = "net"
|
||||||
|
|
||||||
// Return the docker ID for a manifest's network container. Returns an empty string if it doesn't exist
|
// Return the docker ID for a manifest's network container. Returns an empty string if it doesn't exist.
|
||||||
func (kl *Kubelet) getNetworkContainerId(manifest *api.ContainerManifest) (string, error) {
|
func (kl *Kubelet) getNetworkContainerId(manifest *api.ContainerManifest) (DockerContainerId, error) {
|
||||||
return kl.getContainerId(manifest, &api.Container{Name: networkContainerName})
|
return kl.getContainerId(manifest, &api.Container{Name: networkContainerName})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a network container for a manifest. Returns the docker container ID of the newly created container
|
// Create a network container for a manifest. Returns the docker container ID of the newly created container.
|
||||||
func (kl *Kubelet) createNetworkContainer(manifest *api.ContainerManifest) (string, error) {
|
func (kl *Kubelet) createNetworkContainer(manifest *api.ContainerManifest) (DockerContainerId, error) {
|
||||||
var ports []api.Port
|
var ports []api.Port
|
||||||
// Docker only exports ports from the network container. Let's
|
// Docker only exports ports from the network container. Let's
|
||||||
// collect all of the relevant ports and export them.
|
// collect all of the relevant ports and export them.
|
||||||
@@ -634,7 +637,7 @@ func (kl *Kubelet) createNetworkContainer(manifest *api.ContainerManifest) (stri
|
|||||||
func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error {
|
func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error {
|
||||||
glog.Infof("Desired: %#v", config)
|
glog.Infof("Desired: %#v", config)
|
||||||
var err error
|
var err error
|
||||||
dockerIdsToKeep := map[string]bool{}
|
dockerIdsToKeep := map[DockerContainerId]bool{}
|
||||||
|
|
||||||
// Check for any containers that need starting
|
// Check for any containers that need starting
|
||||||
for _, manifest := range config {
|
for _, manifest := range config {
|
||||||
@@ -667,7 +670,7 @@ func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error {
|
|||||||
glog.Errorf("Error pulling container: %#v", err)
|
glog.Errorf("Error pulling container: %#v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
containerId, err = kl.runContainer(&manifest, &container, "container:"+netId)
|
containerId, err = kl.runContainer(&manifest, &container, "container:"+string(netId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO(bburns) : Perhaps blacklist a container after N failures?
|
// TODO(bburns) : Perhaps blacklist a container after N failures?
|
||||||
glog.Errorf("Error creating container: %#v", err)
|
glog.Errorf("Error creating container: %#v", err)
|
||||||
@@ -731,14 +734,14 @@ func (kl *Kubelet) RunSyncLoop(updateChannel <-chan manifestUpdate, handler Sync
|
|||||||
// it returns true if the container is found, false otherwise, and any error that occurs.
|
// it returns true if the container is found, false otherwise, and any error that occurs.
|
||||||
// TODO: This functions exists to support GetContainerInfo and GetContainerStats
|
// TODO: This functions exists to support GetContainerInfo and GetContainerStats
|
||||||
// It should be removed once those two functions start taking proper pod.IDs
|
// It should be removed once those two functions start taking proper pod.IDs
|
||||||
func (kl *Kubelet) getContainerIdFromName(name string) (string, bool, error) {
|
func (kl *Kubelet) getContainerIdFromName(name string) (DockerContainerId, bool, error) {
|
||||||
containerList, err := kl.DockerClient.ListContainers(docker.ListContainersOptions{})
|
containerList, err := kl.DockerClient.ListContainers(docker.ListContainersOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", false, err
|
return "", false, err
|
||||||
}
|
}
|
||||||
for _, value := range containerList {
|
for _, value := range containerList {
|
||||||
if strings.Contains(value.Names[0], name) {
|
if strings.Contains(value.Names[0], name) {
|
||||||
return value.ID, true, nil
|
return DockerContainerId(value.ID), true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", false, nil
|
return "", false, nil
|
||||||
@@ -750,7 +753,7 @@ func (kl *Kubelet) GetContainerInfo(name string) (string, error) {
|
|||||||
if err != nil || !found {
|
if err != nil || !found {
|
||||||
return "{}", err
|
return "{}", err
|
||||||
}
|
}
|
||||||
info, err := kl.DockerClient.InspectContainer(dockerId)
|
info, err := kl.DockerClient.InspectContainer(string(dockerId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "{}", err
|
return "{}", err
|
||||||
}
|
}
|
||||||
@@ -768,7 +771,7 @@ func (kl *Kubelet) GetContainerStats(name string) (*api.ContainerStats, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := kl.CadvisorClient.ContainerInfo(fmt.Sprintf("/docker/%v", dockerId))
|
info, err := kl.CadvisorClient.ContainerInfo(fmt.Sprintf("/docker/%s", string(dockerId)))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -109,11 +109,11 @@ func verifyStringArrayEquals(t *testing.T, actual, expected []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verifyPackUnpack(t *testing.T, manifestId, containerName string) {
|
func verifyPackUnpack(t *testing.T, manifestId, containerName string) {
|
||||||
name := manifestAndContainerToDockerName(
|
name := buildDockerName(
|
||||||
&api.ContainerManifest{Id: manifestId},
|
&api.ContainerManifest{Id: manifestId},
|
||||||
&api.Container{Name: containerName},
|
&api.Container{Name: containerName},
|
||||||
)
|
)
|
||||||
returnedManifestId, returnedContainerName := dockerNameToManifestAndContainer(name)
|
returnedManifestId, returnedContainerName := parseDockerName(name)
|
||||||
if manifestId != returnedManifestId || containerName != returnedContainerName {
|
if manifestId != returnedManifestId || containerName != returnedContainerName {
|
||||||
t.Errorf("For (%s, %s), unpacked (%s, %s)", manifestId, containerName, returnedManifestId, returnedContainerName)
|
t.Errorf("For (%s, %s), unpacked (%s, %s)", manifestId, containerName, returnedManifestId, returnedContainerName)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user