mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-16 22:53:22 +00:00
Remove logic to handle if UID is blank
This should only have been triggered by tests, and those should now be fixed. I tested by calling panic() if UID was blank in BuildDockerName() or if number of fields was < 5 in ParseDockerName(). All errors were fixed.
This commit is contained in:
@@ -547,26 +547,17 @@ func HashContainer(container *api.Container) uint64 {
|
||||
// Creates a name which can be reversed to identify both full pod name and container name.
|
||||
func BuildDockerName(podUID, podFullName string, container *api.Container) string {
|
||||
containerName := container.Name + "." + strconv.FormatUint(HashContainer(container), 16)
|
||||
// Note, manifest.ID could be blank.
|
||||
if len(podUID) == 0 {
|
||||
return fmt.Sprintf("%s_%s_%s_%08x",
|
||||
containerNamePrefix,
|
||||
containerName,
|
||||
podFullName,
|
||||
rand.Uint32())
|
||||
} else {
|
||||
return fmt.Sprintf("%s_%s_%s_%s_%08x",
|
||||
containerNamePrefix,
|
||||
containerName,
|
||||
podFullName,
|
||||
podUID,
|
||||
rand.Uint32())
|
||||
}
|
||||
return fmt.Sprintf("%s_%s_%s_%s_%08x",
|
||||
containerNamePrefix,
|
||||
containerName,
|
||||
podFullName,
|
||||
podUID,
|
||||
rand.Uint32())
|
||||
}
|
||||
|
||||
// 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.
|
||||
func ParseDockerName(name string) (podFullName, uuid, containerName string, hash uint64) {
|
||||
func ParseDockerName(name string) (podFullName, podUID, containerName string, hash uint64) {
|
||||
// For some reason docker appears to be appending '/' to names.
|
||||
// If it's there, strip it.
|
||||
if name[0] == '/' {
|
||||
@@ -576,29 +567,31 @@ func ParseDockerName(name string) (podFullName, uuid, containerName string, hash
|
||||
if len(parts) == 0 || parts[0] != containerNamePrefix {
|
||||
return
|
||||
}
|
||||
if len(parts) > 1 {
|
||||
pieces := strings.Split(parts[1], ".")
|
||||
containerName = pieces[0]
|
||||
if len(pieces) > 1 {
|
||||
var err error
|
||||
hash, err = strconv.ParseUint(pieces[1], 16, 32)
|
||||
if err != nil {
|
||||
glog.Warningf("invalid container hash: %s", pieces[1])
|
||||
}
|
||||
if len(parts) < 5 {
|
||||
// We have at least 5 fields. We may have more in the future.
|
||||
// Anything with less fields than this is not something we can
|
||||
// manage.
|
||||
glog.Warningf("found a container with the %q prefix, but too few fields (%d): ", containerNamePrefix, len(parts), name)
|
||||
return
|
||||
}
|
||||
|
||||
// Container name.
|
||||
nameParts := strings.Split(parts[1], ".")
|
||||
containerName = nameParts[0]
|
||||
if len(nameParts) > 1 {
|
||||
var err error
|
||||
hash, err = strconv.ParseUint(nameParts[1], 16, 32)
|
||||
if err != nil {
|
||||
glog.Warningf("invalid container hash: %s", nameParts[1])
|
||||
}
|
||||
}
|
||||
if len(parts) > 2 {
|
||||
podFullName = parts[2]
|
||||
}
|
||||
// This is not an off-by-one. We check for > 4 here because (sadly) the
|
||||
// format generated by BuildDockerName() has an optional field in the
|
||||
// middle. If len(parts) > 3, parts[3] might be the optional field or
|
||||
// the (poorly documented) random suffix. If len(parts) > 4, then we
|
||||
// know [3] is the UUID and [4] is the suffix. Sort of pukey, should
|
||||
// be fixed by making UID non-optional.
|
||||
if len(parts) > 4 {
|
||||
uuid = parts[3]
|
||||
}
|
||||
|
||||
// Pod fullname.
|
||||
podFullName = parts[2]
|
||||
|
||||
// Pod UID.
|
||||
podUID = parts[3]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -53,11 +53,11 @@ func TestGetContainerID(t *testing.T) {
|
||||
fakeDocker.ContainerList = []docker.APIContainers{
|
||||
{
|
||||
ID: "foobar",
|
||||
Names: []string{"/k8s_foo_qux_1234"},
|
||||
Names: []string{"/k8s_foo_qux_1234_42"},
|
||||
},
|
||||
{
|
||||
ID: "barbar",
|
||||
Names: []string{"/k8s_bar_qux_2565"},
|
||||
Names: []string{"/k8s_bar_qux_2565_42"},
|
||||
},
|
||||
}
|
||||
fakeDocker.Container = &docker.Container{
|
||||
@@ -99,27 +99,23 @@ func verifyPackUnpack(t *testing.T, podNamespace, podUID, podName, containerName
|
||||
}
|
||||
|
||||
func TestContainerManifestNaming(t *testing.T) {
|
||||
podUID := "d1b925c9-444a-11e4-a576-42010af0a203"
|
||||
verifyPackUnpack(t, "file", podUID, "manifest1234", "container5678")
|
||||
verifyPackUnpack(t, "file", podUID, "mani-fest-1234", "container5678")
|
||||
podUID := "12345678"
|
||||
verifyPackUnpack(t, "file", podUID, "name", "container")
|
||||
verifyPackUnpack(t, "file", podUID, "name-with-dashes", "container")
|
||||
// UID is same as pod name
|
||||
verifyPackUnpack(t, "file", podUID, podUID, "container123")
|
||||
// empty namespace
|
||||
verifyPackUnpack(t, "", podUID, podUID, "container123")
|
||||
// No UID
|
||||
verifyPackUnpack(t, "other", "", podUID, "container456")
|
||||
verifyPackUnpack(t, "file", podUID, podUID, "container")
|
||||
// No Container name
|
||||
verifyPackUnpack(t, "other", "", podUID, "")
|
||||
verifyPackUnpack(t, "other", podUID, "name", "")
|
||||
|
||||
container := &api.Container{Name: "container"}
|
||||
podName := "foo"
|
||||
podNamespace := "test"
|
||||
name := fmt.Sprintf("k8s_%s_%s.%s_12345", container.Name, podName, podNamespace)
|
||||
|
||||
name := fmt.Sprintf("k8s_%s_%s.%s_%s_42", container.Name, podName, podNamespace, podUID)
|
||||
podFullName := fmt.Sprintf("%s.%s", podName, podNamespace)
|
||||
returnedPodFullName, _, returnedContainerName, hash := ParseDockerName(name)
|
||||
if returnedPodFullName != podFullName || returnedContainerName != container.Name || hash != 0 {
|
||||
t.Errorf("unexpected parse: %s %s %d", returnedPodFullName, returnedContainerName, hash)
|
||||
|
||||
returnedPodFullName, returnedPodUID, returnedContainerName, hash := ParseDockerName(name)
|
||||
if returnedPodFullName != podFullName || returnedPodUID != podUID || returnedContainerName != container.Name || hash != 0 {
|
||||
t.Errorf("unexpected parse: %s %s %s %d", returnedPodFullName, returnedPodUID, returnedContainerName, hash)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user