2014-07-15 15:34:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-09-09 04:33:17 +00:00
|
|
|
package dockertools
|
2014-07-15 15:34:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-07-18 14:54:43 +00:00
|
|
|
"errors"
|
2014-07-15 15:34:48 +00:00
|
|
|
"fmt"
|
2014-08-07 23:59:18 +00:00
|
|
|
"hash/adler32"
|
2014-09-24 21:27:10 +00:00
|
|
|
"io"
|
2014-07-15 15:34:48 +00:00
|
|
|
"math/rand"
|
2014-08-07 18:15:11 +00:00
|
|
|
"os/exec"
|
2014-09-06 01:13:19 +00:00
|
|
|
"sort"
|
2014-08-07 23:59:18 +00:00
|
|
|
"strconv"
|
2014-07-15 15:34:48 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/fsouza/go-dockerclient"
|
2014-08-07 23:59:18 +00:00
|
|
|
"github.com/golang/glog"
|
2014-07-15 15:34:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DockerContainerData is the structured representation of the JSON object returned by Docker inspect
|
|
|
|
type DockerContainerData struct {
|
|
|
|
state struct {
|
|
|
|
Running bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DockerInterface is an abstract interface for testability. It abstracts the interface of docker.Client.
|
|
|
|
type DockerInterface interface {
|
|
|
|
ListContainers(options docker.ListContainersOptions) ([]docker.APIContainers, error)
|
|
|
|
InspectContainer(id string) (*docker.Container, error)
|
|
|
|
CreateContainer(docker.CreateContainerOptions) (*docker.Container, error)
|
|
|
|
StartContainer(id string, hostConfig *docker.HostConfig) error
|
|
|
|
StopContainer(id string, timeout uint) error
|
|
|
|
PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error
|
2014-08-27 19:41:32 +00:00
|
|
|
Logs(opts docker.LogsOptions) error
|
2014-07-15 15:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DockerID is an ID of docker container. It is a type to make it clear when we're working with docker container Ids
|
|
|
|
type DockerID string
|
|
|
|
|
|
|
|
// DockerPuller is an abstract interface for testability. It abstracts image pull operations.
|
|
|
|
type DockerPuller interface {
|
|
|
|
Pull(image string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// dockerPuller is the default implementation of DockerPuller.
|
|
|
|
type dockerPuller struct {
|
2014-09-06 01:13:19 +00:00
|
|
|
client DockerInterface
|
|
|
|
keyring *dockerKeyring
|
2014-07-15 15:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerPuller creates a new instance of the default implementation of DockerPuller.
|
|
|
|
func NewDockerPuller(client DockerInterface) DockerPuller {
|
2014-09-06 01:13:19 +00:00
|
|
|
dp := dockerPuller{
|
|
|
|
client: client,
|
|
|
|
keyring: newDockerKeyring(),
|
2014-07-15 15:34:48 +00:00
|
|
|
}
|
2014-09-06 01:13:19 +00:00
|
|
|
|
|
|
|
cfg, err := readDockerConfigFile()
|
|
|
|
if err == nil {
|
|
|
|
cfg.addToKeyring(dp.keyring)
|
|
|
|
} else {
|
|
|
|
glog.Errorf("Unable to parse docker config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dp.keyring.count() == 0 {
|
|
|
|
glog.Infof("Continuing with empty docker keyring")
|
|
|
|
}
|
|
|
|
|
|
|
|
return dp
|
2014-07-15 15:34:48 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 18:15:11 +00:00
|
|
|
type dockerContainerCommandRunner struct{}
|
|
|
|
|
|
|
|
func (d *dockerContainerCommandRunner) getRunInContainerCommand(containerID string, cmd []string) (*exec.Cmd, error) {
|
|
|
|
args := append([]string{"exec"}, cmd...)
|
|
|
|
command := exec.Command("/usr/sbin/nsinit", args...)
|
|
|
|
command.Dir = fmt.Sprintf("/var/lib/docker/execdriver/native/%s", containerID)
|
|
|
|
return command, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunInContainer uses nsinit to run the command inside the container identified by containerID
|
|
|
|
func (d *dockerContainerCommandRunner) RunInContainer(containerID string, cmd []string) ([]byte, error) {
|
|
|
|
c, err := d.getRunInContainerCommand(containerID, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.CombinedOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerContainerCommandRunner creates a ContainerCommandRunner which uses nsinit to run a command
|
|
|
|
// inside a container.
|
|
|
|
func NewDockerContainerCommandRunner() ContainerCommandRunner {
|
|
|
|
return &dockerContainerCommandRunner{}
|
|
|
|
}
|
|
|
|
|
2014-07-15 15:34:48 +00:00
|
|
|
func (p dockerPuller) Pull(image string) error {
|
|
|
|
image, tag := parseImageName(image)
|
2014-07-20 17:31:26 +00:00
|
|
|
|
|
|
|
// If no tag was specified, use the default "latest".
|
|
|
|
if len(tag) == 0 {
|
|
|
|
tag = "latest"
|
|
|
|
}
|
|
|
|
|
2014-07-15 15:34:48 +00:00
|
|
|
opts := docker.PullImageOptions{
|
|
|
|
Repository: image,
|
|
|
|
Tag: tag,
|
|
|
|
}
|
2014-09-06 01:13:19 +00:00
|
|
|
|
|
|
|
creds, ok := p.keyring.lookup(image)
|
|
|
|
if !ok {
|
|
|
|
glog.V(1).Infof("Pulling image %s without credentials", image)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.client.PullImage(opts, creds)
|
2014-07-15 15:34:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 17:26:56 +00:00
|
|
|
// DockerContainers is a map of containers
|
|
|
|
type DockerContainers map[DockerID]*docker.APIContainers
|
|
|
|
|
2014-09-05 09:49:11 +00:00
|
|
|
func (c DockerContainers) FindPodContainer(podFullName, uuid, containerName string) (*docker.APIContainers, bool, uint64) {
|
2014-07-15 17:26:56 +00:00
|
|
|
for _, dockerContainer := range c {
|
2014-09-09 04:33:17 +00:00
|
|
|
dockerManifestID, dockerUUID, dockerContainerName, hash := ParseDockerName(dockerContainer.Names[0])
|
2014-09-05 09:49:11 +00:00
|
|
|
if dockerManifestID == podFullName &&
|
|
|
|
(uuid == "" || dockerUUID == uuid) &&
|
|
|
|
dockerContainerName == containerName {
|
2014-08-07 23:59:18 +00:00
|
|
|
return dockerContainer, true, hash
|
2014-07-15 17:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-07 23:59:18 +00:00
|
|
|
return nil, false, 0
|
2014-07-15 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 09:49:11 +00:00
|
|
|
// Note, this might return containers belong to a different Pod instance with the same name
|
2014-07-18 18:42:47 +00:00
|
|
|
func (c DockerContainers) FindContainersByPodFullName(podFullName string) map[string]*docker.APIContainers {
|
2014-07-15 17:26:56 +00:00
|
|
|
containers := make(map[string]*docker.APIContainers)
|
|
|
|
|
|
|
|
for _, dockerContainer := range c {
|
2014-09-09 04:33:17 +00:00
|
|
|
dockerManifestID, _, dockerContainerName, _ := ParseDockerName(dockerContainer.Names[0])
|
2014-07-18 18:42:47 +00:00
|
|
|
if dockerManifestID == podFullName {
|
2014-07-15 17:26:56 +00:00
|
|
|
containers[dockerContainerName] = dockerContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return containers
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKubeletDockerContainers returns a map of docker containers that we manage. The map key is the docker container ID
|
2014-09-09 04:33:17 +00:00
|
|
|
func GetKubeletDockerContainers(client DockerInterface) (DockerContainers, error) {
|
2014-07-15 17:26:56 +00:00
|
|
|
result := make(DockerContainers)
|
|
|
|
containers, err := client.ListContainers(docker.ListContainersOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i := range containers {
|
|
|
|
container := &containers[i]
|
|
|
|
// Skip containers that we didn't create to allow users to manually
|
|
|
|
// spin up their own containers if they want.
|
|
|
|
if !strings.HasPrefix(container.Names[0], "/"+containerNamePrefix+"--") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result[DockerID(container.ID)] = container
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-09-09 04:33:17 +00:00
|
|
|
// GetRecentDockerContainersWithNameAndUUID returns a list of dead docker containers which matches the name
|
2014-08-26 18:25:17 +00:00
|
|
|
// and uuid given.
|
2014-09-09 04:33:17 +00:00
|
|
|
func GetRecentDockerContainersWithNameAndUUID(client DockerInterface, podFullName, uuid, containerName string) ([]*docker.Container, error) {
|
2014-08-26 18:25:17 +00:00
|
|
|
var result []*docker.Container
|
|
|
|
containers, err := client.ListContainers(docker.ListContainersOptions{All: true})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, dockerContainer := range containers {
|
2014-09-09 04:33:17 +00:00
|
|
|
dockerPodName, dockerUUID, dockerContainerName, _ := ParseDockerName(dockerContainer.Names[0])
|
2014-08-26 18:25:17 +00:00
|
|
|
if dockerPodName != podFullName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if uuid != "" && dockerUUID != uuid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if dockerContainerName != containerName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
inspectResult, _ := client.InspectContainer(dockerContainer.ID)
|
|
|
|
if inspectResult != nil && !inspectResult.State.Running && !inspectResult.State.Paused {
|
|
|
|
result = append(result, inspectResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-08-27 19:41:32 +00:00
|
|
|
// GetKubeletDockerContainerLogs returns logs of specific container
|
2014-09-15 16:20:01 +00:00
|
|
|
// By default the function will return snapshot of the container log
|
|
|
|
// Log streaming is possible if 'follow' param is set to true
|
|
|
|
// Log tailing is possible when number of tailed lines are set and only if 'follow' is false
|
2014-09-22 20:14:23 +00:00
|
|
|
func GetKubeletDockerContainerLogs(client DockerInterface, containerID, tail string, follow bool, stdout, stderr io.Writer) (err error) {
|
2014-08-27 19:41:32 +00:00
|
|
|
opts := docker.LogsOptions{
|
|
|
|
Container: containerID,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
2014-09-22 20:14:23 +00:00
|
|
|
OutputStream: stdout,
|
|
|
|
ErrorStream: stderr,
|
2014-08-27 19:41:32 +00:00
|
|
|
Timestamps: true,
|
|
|
|
RawTerminal: true,
|
2014-09-15 16:20:01 +00:00
|
|
|
Follow: follow,
|
2014-08-27 19:41:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 16:20:01 +00:00
|
|
|
if !follow {
|
2014-08-27 19:41:32 +00:00
|
|
|
opts.Tail = tail
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Logs(opts)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-16 20:21:04 +00:00
|
|
|
func generateContainerStatus(inspectResult *docker.Container) api.ContainerStatus {
|
|
|
|
if inspectResult == nil {
|
|
|
|
// Why did we not get an error?
|
|
|
|
return api.ContainerStatus{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var containerStatus api.ContainerStatus
|
|
|
|
|
|
|
|
if inspectResult.State.Running {
|
|
|
|
containerStatus.State.Running = &api.ContainerStateRunning{}
|
|
|
|
} else {
|
|
|
|
containerStatus.State.Termination = &api.ContainerStateTerminated{
|
|
|
|
ExitCode: inspectResult.State.ExitCode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
containerStatus.DetailInfo = *inspectResult
|
|
|
|
return containerStatus
|
|
|
|
}
|
|
|
|
|
2014-09-10 20:20:29 +00:00
|
|
|
// ErrNoContainersInPod is returned when there are no containers for a given pod
|
2014-07-18 14:54:43 +00:00
|
|
|
var ErrNoContainersInPod = errors.New("no containers exist for this pod")
|
|
|
|
|
2014-07-15 17:26:56 +00:00
|
|
|
// GetDockerPodInfo returns docker info for all containers in the pod/manifest.
|
2014-09-09 04:33:17 +00:00
|
|
|
func GetDockerPodInfo(client DockerInterface, podFullName, uuid string) (api.PodInfo, error) {
|
2014-07-15 17:26:56 +00:00
|
|
|
info := api.PodInfo{}
|
|
|
|
|
2014-09-10 20:20:29 +00:00
|
|
|
containers, err := client.ListContainers(docker.ListContainersOptions{All: true})
|
2014-07-15 17:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range containers {
|
2014-09-09 04:33:17 +00:00
|
|
|
dockerManifestID, dockerUUID, dockerContainerName, _ := ParseDockerName(value.Names[0])
|
2014-07-18 18:42:47 +00:00
|
|
|
if dockerManifestID != podFullName {
|
2014-07-15 17:26:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-09-05 09:49:11 +00:00
|
|
|
if uuid != "" && dockerUUID != uuid {
|
|
|
|
continue
|
|
|
|
}
|
2014-09-10 20:20:29 +00:00
|
|
|
// We assume docker return us a list of containers in time order
|
2014-09-16 20:21:04 +00:00
|
|
|
if containerStatus, found := info[dockerContainerName]; found {
|
|
|
|
containerStatus.RestartCount += 1
|
|
|
|
info[dockerContainerName] = containerStatus
|
2014-09-10 20:20:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-15 17:26:56 +00:00
|
|
|
inspectResult, err := client.InspectContainer(value.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-16 20:21:04 +00:00
|
|
|
info[dockerContainerName] = generateContainerStatus(inspectResult)
|
2014-07-15 17:26:56 +00:00
|
|
|
}
|
2014-07-18 14:54:43 +00:00
|
|
|
if len(info) == 0 {
|
|
|
|
return nil, ErrNoContainersInPod
|
|
|
|
}
|
|
|
|
|
2014-07-15 17:26:56 +00:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2014-07-15 15:34:48 +00:00
|
|
|
// Converts "-" to "_-_" and "_" to "___" so that we can use "--" to meaningfully separate parts of a docker name.
|
|
|
|
func escapeDash(in string) (out string) {
|
|
|
|
out = strings.Replace(in, "_", "___", -1)
|
|
|
|
out = strings.Replace(out, "-", "_-_", -1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverses the transformation of escapeDash.
|
|
|
|
func unescapeDash(in string) (out string) {
|
|
|
|
out = strings.Replace(in, "_-_", "-", -1)
|
|
|
|
out = strings.Replace(out, "___", "_", -1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const containerNamePrefix = "k8s"
|
|
|
|
|
2014-09-09 04:33:17 +00:00
|
|
|
func HashContainer(container *api.Container) uint64 {
|
2014-08-07 23:59:18 +00:00
|
|
|
hash := adler32.New()
|
|
|
|
fmt.Fprintf(hash, "%#v", *container)
|
|
|
|
return uint64(hash.Sum32())
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:42:47 +00:00
|
|
|
// Creates a name which can be reversed to identify both full pod name and container name.
|
2014-09-09 04:33:17 +00:00
|
|
|
func BuildDockerName(manifestUUID, podFullName string, container *api.Container) string {
|
|
|
|
containerName := escapeDash(container.Name) + "." + strconv.FormatUint(HashContainer(container), 16)
|
2014-07-15 15:34:48 +00:00
|
|
|
// Note, manifest.ID could be blank.
|
2014-09-09 04:33:17 +00:00
|
|
|
if len(manifestUUID) == 0 {
|
2014-09-05 09:49:11 +00:00
|
|
|
return fmt.Sprintf("%s--%s--%s--%08x",
|
|
|
|
containerNamePrefix,
|
|
|
|
containerName,
|
2014-09-09 04:33:17 +00:00
|
|
|
escapeDash(podFullName),
|
2014-09-05 09:49:11 +00:00
|
|
|
rand.Uint32())
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("%s--%s--%s--%s--%08x",
|
|
|
|
containerNamePrefix,
|
|
|
|
containerName,
|
2014-09-09 04:33:17 +00:00
|
|
|
escapeDash(podFullName),
|
|
|
|
escapeDash(manifestUUID),
|
2014-09-05 09:49:11 +00:00
|
|
|
rand.Uint32())
|
|
|
|
}
|
2014-07-15 15:34:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 23:33:39 +00:00
|
|
|
// Unpacks a container name, returning the pod full name and container name we would have used to
|
|
|
|
// construct the docker name. If the docker name isn't the one we created, we may return empty strings.
|
2014-09-09 04:33:17 +00:00
|
|
|
func ParseDockerName(name string) (podFullName, uuid, containerName string, hash uint64) {
|
2014-07-15 15:34:48 +00:00
|
|
|
// For some reason docker appears to be appending '/' to names.
|
2014-07-28 13:56:03 +00:00
|
|
|
// If it's there, strip it.
|
2014-07-15 15:34:48 +00:00
|
|
|
if name[0] == '/' {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
|
|
|
parts := strings.Split(name, "--")
|
|
|
|
if len(parts) == 0 || parts[0] != containerNamePrefix {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(parts) > 1 {
|
2014-08-07 23:59:18 +00:00
|
|
|
pieces := strings.Split(parts[1], ".")
|
|
|
|
containerName = unescapeDash(pieces[0])
|
|
|
|
if len(pieces) > 1 {
|
|
|
|
var err error
|
|
|
|
hash, err = strconv.ParseUint(pieces[1], 16, 32)
|
|
|
|
if err != nil {
|
|
|
|
glog.Infof("invalid container hash: %s", pieces[1])
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 15:34:48 +00:00
|
|
|
}
|
|
|
|
if len(parts) > 2 {
|
2014-07-15 20:24:41 +00:00
|
|
|
podFullName = unescapeDash(parts[2])
|
2014-07-15 15:34:48 +00:00
|
|
|
}
|
2014-09-05 09:49:11 +00:00
|
|
|
if len(parts) > 4 {
|
|
|
|
uuid = unescapeDash(parts[3])
|
|
|
|
}
|
2014-07-15 15:34:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-20 17:31:26 +00:00
|
|
|
// Parses image name including a tag and returns image name and tag.
|
2014-09-05 09:49:11 +00:00
|
|
|
// TODO: Future Docker versions can parse the tag on daemon side, see
|
2014-07-15 15:34:48 +00:00
|
|
|
// https://github.com/dotcloud/docker/issues/6876
|
|
|
|
// So this can be deprecated at some point.
|
|
|
|
func parseImageName(image string) (string, string) {
|
|
|
|
tag := ""
|
|
|
|
parts := strings.SplitN(image, "/", 2)
|
|
|
|
repo := ""
|
|
|
|
if len(parts) == 2 {
|
|
|
|
repo = parts[0]
|
|
|
|
image = parts[1]
|
|
|
|
}
|
|
|
|
parts = strings.SplitN(image, ":", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
image = parts[0]
|
|
|
|
tag = parts[1]
|
|
|
|
}
|
|
|
|
if repo != "" {
|
|
|
|
image = fmt.Sprintf("%s/%s", repo, image)
|
|
|
|
}
|
|
|
|
return image, tag
|
|
|
|
}
|
2014-09-09 04:33:17 +00:00
|
|
|
|
|
|
|
type ContainerCommandRunner interface {
|
|
|
|
RunInContainer(containerID string, cmd []string) ([]byte, error)
|
|
|
|
}
|
2014-09-06 01:13:19 +00:00
|
|
|
|
|
|
|
// dockerKeyring tracks a set of docker registry credentials, maintaining a
|
|
|
|
// reverse index across the registry endpoints. A registry endpoint is made
|
|
|
|
// up of a host (e.g. registry.example.com), but it may also contain a path
|
|
|
|
// (e.g. registry.example.com/foo) This index is important for two reasons:
|
|
|
|
// - registry endpoints may overlap, and when this happens we must find the
|
|
|
|
// most specific match for a given image
|
|
|
|
// - iterating a map does not yield predictable results
|
|
|
|
type dockerKeyring struct {
|
|
|
|
index []string
|
|
|
|
creds map[string]docker.AuthConfiguration
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDockerKeyring() *dockerKeyring {
|
|
|
|
return &dockerKeyring{
|
|
|
|
index: make([]string, 0),
|
|
|
|
creds: make(map[string]docker.AuthConfiguration),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dk *dockerKeyring) add(registry string, creds docker.AuthConfiguration) {
|
|
|
|
dk.creds[registry] = creds
|
|
|
|
|
|
|
|
dk.index = append(dk.index, registry)
|
|
|
|
dk.reindex()
|
|
|
|
}
|
|
|
|
|
|
|
|
// reindex updates the index used to identify which credentials to use for
|
|
|
|
// a given image. The index is reverse-sorted so more specific paths are
|
|
|
|
// matched first. For example, if for the given image "quay.io/coreos/etcd",
|
|
|
|
// credentials for "quay.io/coreos" should match before "quay.io".
|
|
|
|
func (dk *dockerKeyring) reindex() {
|
|
|
|
sort.Sort(sort.Reverse(sort.StringSlice(dk.index)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dk *dockerKeyring) lookup(image string) (docker.AuthConfiguration, bool) {
|
|
|
|
// range over the index as iterating over a map does not provide
|
|
|
|
// a predictable ordering
|
|
|
|
for _, k := range dk.index {
|
|
|
|
if !strings.HasPrefix(image, k) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return dk.creds[k], true
|
|
|
|
}
|
|
|
|
|
|
|
|
return docker.AuthConfiguration{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dk dockerKeyring) count() int {
|
|
|
|
return len(dk.creds)
|
|
|
|
}
|