Merge pull request #87461 from bboreham/fix-uid-gen

kubelet: ensure static pod UIDs are unique
This commit is contained in:
Kubernetes Prow Robot 2020-12-01 08:18:50 -08:00 committed by GitHub
commit 61dc69ac2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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