From 7227641fc2492379b4467c0876f61e0c45554121 Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Fri, 26 Aug 2016 16:23:10 -0700 Subject: [PATCH] dockershim: move naming helpers to a separate file --- pkg/kubelet/dockershim/helpers.go | 76 ---------------------- pkg/kubelet/dockershim/naming.go | 103 ++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 76 deletions(-) create mode 100644 pkg/kubelet/dockershim/naming.go diff --git a/pkg/kubelet/dockershim/helpers.go b/pkg/kubelet/dockershim/helpers.go index dd30318de2..a8aa51d3cb 100644 --- a/pkg/kubelet/dockershim/helpers.go +++ b/pkg/kubelet/dockershim/helpers.go @@ -18,7 +18,6 @@ package dockershim import ( "fmt" - "math/rand" "strconv" "strings" @@ -31,13 +30,6 @@ import ( runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" ) -const ( - // kubePrefix is used to identify the containers/sandboxes on the node managed by kubelet - kubePrefix = "k8s" - // kubeSandboxNamePrefix is used to keep sandbox name consistent with old podInfraContainer name - kubeSandboxNamePrefix = "POD" -) - // apiVersion implements kubecontainer.Version interface by implementing // Compare() and String(). It uses the compare function of engine-api to // compare docker apiversions. @@ -166,74 +158,6 @@ func getNetworkNamespace(c *dockertypes.ContainerJSON) string { return fmt.Sprintf(dockerNetNSFmt, c.State.Pid) } -// buildKubeGenericName creates a name which can be reversed to identify container/sandbox name. -// This function returns the unique name. -func buildKubeGenericName(sandboxConfig *runtimeApi.PodSandboxConfig, containerName string) string { - stableName := fmt.Sprintf("%s_%s_%s_%s_%s", - kubePrefix, - containerName, - sandboxConfig.Metadata.GetName(), - sandboxConfig.Metadata.GetNamespace(), - sandboxConfig.Metadata.GetUid(), - ) - UID := fmt.Sprintf("%08x", rand.Uint32()) - return fmt.Sprintf("%s_%s", stableName, UID) -} - -// buildSandboxName creates a name which can be reversed to identify sandbox full name. -func buildSandboxName(sandboxConfig *runtimeApi.PodSandboxConfig) string { - sandboxName := fmt.Sprintf("%s.%d", kubeSandboxNamePrefix, sandboxConfig.Metadata.GetAttempt()) - return buildKubeGenericName(sandboxConfig, sandboxName) -} - -// parseSandboxName unpacks a sandbox full name, returning the pod name, namespace, uid and attempt. -func parseSandboxName(name string) (string, string, string, uint32, error) { - podName, podNamespace, podUID, _, attempt, err := parseContainerName(name) - if err != nil { - return "", "", "", 0, err - } - - return podName, podNamespace, podUID, attempt, nil -} - -// buildContainerName creates a name which can be reversed to identify container name. -// This function returns stable name, unique name and a unique id. -func buildContainerName(sandboxConfig *runtimeApi.PodSandboxConfig, containerConfig *runtimeApi.ContainerConfig) string { - containerName := fmt.Sprintf("%s.%d", containerConfig.Metadata.GetName(), containerConfig.Metadata.GetAttempt()) - return buildKubeGenericName(sandboxConfig, containerName) -} - -// parseContainerName unpacks a container name, returning the pod name, namespace, UID, -// container name and attempt. -func parseContainerName(name string) (podName, podNamespace, podUID, containerName string, attempt uint32, err error) { - // Docker adds a "/" prefix to names. so trim it. - name = strings.TrimPrefix(name, "/") - - parts := strings.Split(name, "_") - if len(parts) == 0 || parts[0] != kubePrefix { - err = fmt.Errorf("failed to parse container name %q into parts", name) - return "", "", "", "", 0, err - } - if len(parts) < 6 { - glog.Warningf("Found a container with the %q prefix, but too few fields (%d): %q", kubePrefix, len(parts), name) - err = fmt.Errorf("container name %q has fewer parts than expected %v", name, parts) - return "", "", "", "", 0, err - } - - nameParts := strings.Split(parts[1], ".") - containerName = nameParts[0] - if len(nameParts) > 1 { - attemptNumber, err := strconv.ParseUint(nameParts[1], 10, 32) - if err != nil { - glog.Warningf("invalid container attempt %q in container %q", nameParts[1], name) - } - - attempt = uint32(attemptNumber) - } - - return parts[2], parts[3], parts[4], containerName, attempt, nil -} - // dockerFilter wraps around dockerfilters.Args and provides methods to modify // the filter easily. type dockerFilter struct { diff --git a/pkg/kubelet/dockershim/naming.go b/pkg/kubelet/dockershim/naming.go new file mode 100644 index 0000000000..5c9d67e0cd --- /dev/null +++ b/pkg/kubelet/dockershim/naming.go @@ -0,0 +1,103 @@ +/* +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" + "math/rand" + "strconv" + "strings" + + "github.com/golang/glog" + + runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" +) + +const ( + // kubePrefix is used to identify the containers/sandboxes on the node managed by kubelet + kubePrefix = "k8s" + // kubeSandboxNamePrefix is used to keep sandbox name consistent with old podInfraContainer name + kubeSandboxNamePrefix = "POD" +) + +// buildKubeGenericName creates a name which can be reversed to identify container/sandbox name. +// This function returns the unique name. +func buildKubeGenericName(sandboxConfig *runtimeApi.PodSandboxConfig, containerName string) string { + stableName := fmt.Sprintf("%s_%s_%s_%s_%s", + kubePrefix, + containerName, + sandboxConfig.Metadata.GetName(), + sandboxConfig.Metadata.GetNamespace(), + sandboxConfig.Metadata.GetUid(), + ) + UID := fmt.Sprintf("%08x", rand.Uint32()) + return fmt.Sprintf("%s_%s", stableName, UID) +} + +// buildSandboxName creates a name which can be reversed to identify sandbox full name. +func buildSandboxName(sandboxConfig *runtimeApi.PodSandboxConfig) string { + sandboxName := fmt.Sprintf("%s.%d", kubeSandboxNamePrefix, sandboxConfig.Metadata.GetAttempt()) + return buildKubeGenericName(sandboxConfig, sandboxName) +} + +// parseSandboxName unpacks a sandbox full name, returning the pod name, namespace, uid and attempt. +func parseSandboxName(name string) (string, string, string, uint32, error) { + podName, podNamespace, podUID, _, attempt, err := parseContainerName(name) + if err != nil { + return "", "", "", 0, err + } + + return podName, podNamespace, podUID, attempt, nil +} + +// buildContainerName creates a name which can be reversed to identify container name. +// This function returns stable name, unique name and an unique id. +func buildContainerName(sandboxConfig *runtimeApi.PodSandboxConfig, containerConfig *runtimeApi.ContainerConfig) string { + containerName := fmt.Sprintf("%s.%d", containerConfig.Metadata.GetName(), containerConfig.Metadata.GetAttempt()) + return buildKubeGenericName(sandboxConfig, containerName) +} + +// parseContainerName unpacks a container name, returning the pod name, namespace, UID, +// container name and attempt. +func parseContainerName(name string) (podName, podNamespace, podUID, containerName string, attempt uint32, err error) { + // Docker adds a "/" prefix to names. so trim it. + name = strings.TrimPrefix(name, "/") + + parts := strings.Split(name, "_") + if len(parts) == 0 || parts[0] != kubePrefix { + err = fmt.Errorf("failed to parse container name %q into parts", name) + return "", "", "", "", 0, err + } + if len(parts) < 6 { + glog.Warningf("Found a container with the %q prefix, but too few fields (%d): %q", kubePrefix, len(parts), name) + err = fmt.Errorf("container name %q has fewer parts than expected %v", name, parts) + return "", "", "", "", 0, err + } + + nameParts := strings.Split(parts[1], ".") + containerName = nameParts[0] + if len(nameParts) > 1 { + attemptNumber, err := strconv.ParseUint(nameParts[1], 10, 32) + if err != nil { + glog.Warningf("invalid container attempt %q in container %q", nameParts[1], name) + } + + attempt = uint32(attemptNumber) + } + + return parts[2], parts[3], parts[4], containerName, attempt, nil +}