kubelet: ensure pod UIDs are unique

Ensure node name and file path are included in the hash which produces
the pod UID, otherwise all pods created from the same manifest have
the same UID across the cluster.

The real author of this code is Yu-Ju Hong <yjhong@google.com>.
I am resurrecting an abandoned PR, and changed the git author to pass
CLA check.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2017-03-20 17:24:40 -07:00 committed by Bryan Boreham
parent 4628c605aa
commit 19de43137b

View File

@ -58,13 +58,15 @@ func generatePodName(name string, nodeName types.NodeName) string {
func applyDefaults(pod *api.Pod, source string, isFile bool, nodeName types.NodeName) error {
if len(pod.UID) == 0 {
hasher := md5.New()
hash.DeepHashObject(hasher, pod)
// DeepHashObject resets the hash, so we should write the pod source
// information AFTER it.
if isFile {
fmt.Fprintf(hasher, "host:%s", nodeName)
fmt.Fprintf(hasher, "file:%s", source)
} else {
fmt.Fprintf(hasher, "url:%s", source)
}
hash.DeepHashObject(hasher, pod)
pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:]))
klog.V(5).Infof("Generated UID %q pod %q from %s", pod.UID, pod.Name, source)
}