mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
Kubelet: pass pod name/namespace/uid to runtimes
This commit is contained in:
@@ -18,9 +18,6 @@ package kuberuntime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
@@ -29,11 +26,6 @@ import (
|
||||
)
|
||||
|
||||
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"
|
||||
|
||||
// Taken from lmctfy https://github.com/google/lmctfy/blob/master/lmctfy/controllers/cpu_controller.cc
|
||||
minShares = 2
|
||||
sharesPerCPU = 1024
|
||||
@@ -56,69 +48,6 @@ func (b containersByID) Len() int { return len(b) }
|
||||
func (b containersByID) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
||||
func (b containersByID) Less(i, j int) bool { return b[i].ID.ID < b[j].ID.ID }
|
||||
|
||||
// buildSandboxName creates a name which can be reversed to identify sandbox full name
|
||||
func buildSandboxName(pod *api.Pod) string {
|
||||
_, sandboxName, _ := buildKubeGenericName(pod, kubeSandboxNamePrefix)
|
||||
return sandboxName
|
||||
}
|
||||
|
||||
// parseSandboxName unpacks a sandbox full name, returning the pod name, namespace and uid
|
||||
func parseSandboxName(name string) (string, string, string, error) {
|
||||
podName, podNamespace, podUID, _, _, err := parseContainerName(name)
|
||||
if err != nil {
|
||||
return "", "", "", err
|
||||
}
|
||||
|
||||
return podName, podNamespace, podUID, 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(pod *api.Pod, container *api.Container) (string, string, string) {
|
||||
// kubelet uses hash to determine whether an existing container matches the desired spec.
|
||||
containerName := container.Name + "." + strconv.FormatUint(kubecontainer.HashContainer(container), 16)
|
||||
return buildKubeGenericName(pod, containerName)
|
||||
}
|
||||
|
||||
// buildKubeGenericName creates a name which can be reversed to identify container/sandbox name.
|
||||
// This function returns stable name, unique name and an unique id.
|
||||
func buildKubeGenericName(pod *api.Pod, containerName string) (string, string, string) {
|
||||
stableName := fmt.Sprintf("%s_%s_%s_%s_%s",
|
||||
kubePrefix,
|
||||
containerName,
|
||||
pod.Name,
|
||||
pod.Namespace,
|
||||
string(pod.UID),
|
||||
)
|
||||
UID := fmt.Sprintf("%08x", rand.Uint32())
|
||||
return stableName, fmt.Sprintf("%s_%s", stableName, UID), UID
|
||||
}
|
||||
|
||||
// parseContainerName unpacks a container name, returning the pod name, namespace, UID and container name
|
||||
func parseContainerName(name string) (podName, podNamespace, podUID, containerName string, hash uint64, err error) {
|
||||
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 {
|
||||
hash, err = strconv.ParseUint(nameParts[1], 16, 32)
|
||||
if err != nil {
|
||||
glog.Warningf("Invalid container hash %q in container %q", nameParts[1], name)
|
||||
}
|
||||
}
|
||||
|
||||
return parts[2], parts[3], parts[4], containerName, hash, nil
|
||||
}
|
||||
|
||||
// toKubeContainerState converts runtimeApi.ContainerState to kubecontainer.ContainerState.
|
||||
func toKubeContainerState(state runtimeApi.ContainerState) kubecontainer.ContainerState {
|
||||
switch state {
|
||||
|
||||
Reference in New Issue
Block a user