func applyDefaults(pod *api.Pod, url string) error { if len(pod.UID) == 0 { hasher := md5.New() fmt.Fprintf(hasher, "url:%s", url) util.DeepHashObject(hasher, pod) pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:])) glog.V(5).Infof("Generated UID %q for pod %q from URL %s", pod.UID, pod.Name, url) } // This is required for backward compatibility, and should be removed once we // completely deprecate ContainerManifest. var err error if len(pod.Name) == 0 { pod.Name = string(pod.UID) } pod.Name, err = GeneratePodName(pod.Name) if err != nil { return err } glog.V(5).Infof("Generated Name %q for UID %q from URL %s", pod.Name, pod.UID, url) // Always overrides the namespace. pod.Namespace = kubelet.NamespaceDefault glog.V(5).Infof("Using namespace %q for pod %q from URL %s", pod.Namespace, pod.Name, url) return nil }
func applyDefaults(pod *api.Pod, source string, isFile bool, nodeName string) error { if len(pod.UID) == 0 { hasher := md5.New() if isFile { fmt.Fprintf(hasher, "host:%s", nodeName) fmt.Fprintf(hasher, "file:%s", source) } else { fmt.Fprintf(hasher, "url:%s", source) } util.DeepHashObject(hasher, pod) pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:])) glog.V(5).Infof("Generated UID %q pod %q from %s", pod.UID, pod.Name, source) } // This is required for backward compatibility, and should be removed once we // completely deprecate ContainerManifest. if len(pod.Name) == 0 { pod.Name = string(pod.UID) } pod.Name = generatePodName(pod.Name, nodeName) glog.V(5).Infof("Generated Name %q for UID %q from URL %s", pod.Name, pod.UID, source) if pod.Namespace == "" { pod.Namespace = kubelet.NamespaceDefault } glog.V(5).Infof("Using namespace %q for pod %q from %s", pod.Namespace, pod.Name, source) // Set the Host field to indicate this pod is scheduled on the current node. pod.Spec.NodeName = nodeName pod.ObjectMeta.SelfLink = getSelfLink(pod.Name, pod.Namespace) return nil }
func extractFromFile(filename string) (api.BoundPod, error) { var pod api.BoundPod glog.V(3).Infof("Reading config file %q", filename) file, err := os.Open(filename) if err != nil { return pod, err } defer file.Close() data, err := ioutil.ReadAll(file) if err != nil { return pod, err } manifest := &api.ContainerManifest{} // TODO: use api.Scheme.DecodeInto if err := yaml.Unmarshal(data, manifest); err != nil { return pod, fmt.Errorf("can't unmarshal file %q: %v", filename, err) } if err := api.Scheme.Convert(manifest, &pod); err != nil { return pod, fmt.Errorf("can't convert pod from file %q: %v", filename, err) } hostname, err := os.Hostname() //TODO: kubelet name would be better if err != nil { return pod, err } if len(pod.UID) == 0 { hasher := md5.New() fmt.Fprintf(hasher, "host:%s", hostname) fmt.Fprintf(hasher, "file:%s", filename) util.DeepHashObject(hasher, pod) pod.UID = hex.EncodeToString(hasher.Sum(nil)[0:]) glog.V(5).Infof("Generated UID %q for pod %q from file %s", pod.UID, pod.Name, filename) } if len(pod.Namespace) == 0 { hasher := adler32.New() fmt.Fprint(hasher, filename) // TODO: file-<sum>.hostname would be better, if DNS subdomains // are allowed for namespace (some places only allow DNS // labels). pod.Namespace = fmt.Sprintf("file-%08x-%s", hasher.Sum32(), hostname) glog.V(5).Infof("Generated namespace %q for pod %q from file %s", pod.Namespace, pod.Name, filename) } // TODO(dchen1107): BoundPod is not type of runtime.Object. Once we allow kubelet talks // about Pod directly, we can use SelfLinker defined in package: latest // Currently just simply follow the same format in resthandler.go pod.ObjectMeta.SelfLink = fmt.Sprintf("/api/v1beta2/pods/%s?namespace=%s", pod.Name, pod.Namespace) if glog.V(4) { glog.Infof("Got pod from file %q: %#v", filename, pod) } else { glog.V(1).Infof("Got pod from file %q: %s.%s (%s)", filename, pod.Namespace, pod.Name, pod.UID) } return pod, nil }
func verifyPackUnpack(t *testing.T, podNamespace, podUID, podName, containerName string) { container := &api.Container{Name: containerName} hasher := adler32.New() util.DeepHashObject(hasher, *container) computedHash := uint64(hasher.Sum32()) podFullName := fmt.Sprintf("%s.%s", podName, podNamespace) name := BuildDockerName(podUID, podFullName, container) returnedPodFullName, returnedUID, returnedContainerName, hash := ParseDockerName(name) if podFullName != returnedPodFullName || podUID != returnedUID || containerName != returnedContainerName || computedHash != hash { t.Errorf("For (%s, %s, %s, %d), unpacked (%s, %s, %s, %d)", podFullName, podUID, containerName, computedHash, returnedPodFullName, returnedUID, returnedContainerName, hash) } }
func hashAddresses(addrs addressSet) string { // Flatten the list of addresses into a string so it can be used as a // map key. Unfortunately, DeepHashObject is implemented in terms of // spew, and spew does not handle non-primitive map keys well. So // first we collapse it into a slice, sort the slice, then hash that. slice := []*api.EndpointAddress{} for k := range addrs { slice = append(slice, k) } sort.Sort(addrPtrsByIP(slice)) hasher := md5.New() util.DeepHashObject(hasher, slice) return hex.EncodeToString(hasher.Sum(nil)[0:]) }
func applyDefaults(pod *api.BoundPod, url string) { if len(pod.UID) == 0 { hasher := md5.New() fmt.Fprintf(hasher, "url:%s", url) util.DeepHashObject(hasher, pod) pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:])) glog.V(5).Infof("Generated UID %q for pod %q from URL %s", pod.UID, pod.Name, url) } if len(pod.Namespace) == 0 { hasher := adler32.New() fmt.Fprint(hasher, url) pod.Namespace = fmt.Sprintf("url-%08x", hasher.Sum32()) glog.V(5).Infof("Generated namespace %q for pod %q from URL %s", pod.Namespace, pod.Name, url) } }
func verifyPackUnpack(t *testing.T, podNamespace, podUID, podName, containerName string) { container := &api.Container{Name: containerName} hasher := adler32.New() util.DeepHashObject(hasher, *container) computedHash := uint64(hasher.Sum32()) podFullName := fmt.Sprintf("%s_%s", podName, podNamespace) name := BuildDockerName(KubeletContainerName{podFullName, types.UID(podUID), container.Name}, container) returned, hash, err := ParseDockerName(name) if err != nil { t.Errorf("Failed to parse Docker container name %q: %v", name, err) } if podFullName != returned.PodFullName || podUID != string(returned.PodUID) || containerName != returned.ContainerName || computedHash != hash { t.Errorf("For (%s, %s, %s, %d), unpacked (%s, %s, %s, %d)", podFullName, podUID, containerName, computedHash, returned.PodFullName, returned.PodUID, returned.ContainerName, hash) } }
func applyDefaults(pod *api.BoundPod, url string) { if len(pod.UID) == 0 { hasher := md5.New() fmt.Fprintf(hasher, "url:%s", url) util.DeepHashObject(hasher, pod) pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:])) glog.V(5).Infof("Generated UID %q for pod %q from URL %s", pod.UID, pod.Name, url) } // This is required for backward compatibility, and should be removed once we // completely deprecate ContainerManifest. if len(pod.Name) == 0 { pod.Name = string(pod.UID) glog.V(5).Infof("Generate Name %q from UID %q from URL %s", pod.Name, pod.UID, url) } if len(pod.Namespace) == 0 { hasher := adler32.New() fmt.Fprint(hasher, url) pod.Namespace = fmt.Sprintf("url-%08x", hasher.Sum32()) glog.V(5).Infof("Generated namespace %q for pod %q from URL %s", pod.Namespace, pod.Name, url) } }
func HashContainer(container *api.Container) uint64 { hash := adler32.New() util.DeepHashObject(hash, *container) return uint64(hash.Sum32()) }
func extractFromFile(filename string) (api.BoundPod, error) { var pod api.BoundPod glog.V(3).Infof("Reading config file %q", filename) file, err := os.Open(filename) if err != nil { return pod, err } defer file.Close() data, err := ioutil.ReadAll(file) if err != nil { return pod, err } // TODO: use api.Scheme.DecodeInto // This is awful. DecodeInto() expects to find an APIObject, which // Manifest is not. We keep reading manifest for now for compat, but // we will eventually change it to read Pod (at which point this all // becomes nicer). Until then, we assert that the ContainerManifest // structure on disk is always v1beta1. Read that, convert it to a // "current" ContainerManifest (should be ~identical), then convert // that to a BoundPod (which is a well-understood conversion). This // avoids writing a v1beta1.ContainerManifest -> api.BoundPod // conversion which would be identical to the api.ContainerManifest -> // api.BoundPod conversion. oldManifest := &v1beta1.ContainerManifest{} if err := yaml.Unmarshal(data, oldManifest); err != nil { return pod, fmt.Errorf("can't unmarshal file %q: %v", filename, err) } newManifest := &api.ContainerManifest{} if err := api.Scheme.Convert(oldManifest, newManifest); err != nil { return pod, fmt.Errorf("can't convert pod from file %q: %v", filename, err) } if err := api.Scheme.Convert(newManifest, &pod); err != nil { return pod, fmt.Errorf("can't convert pod from file %q: %v", filename, err) } hostname, err := os.Hostname() //TODO: kubelet name would be better if err != nil { return pod, err } if len(pod.UID) == 0 { hasher := md5.New() fmt.Fprintf(hasher, "host:%s", hostname) fmt.Fprintf(hasher, "file:%s", filename) util.DeepHashObject(hasher, pod) pod.UID = types.UID(hex.EncodeToString(hasher.Sum(nil)[0:])) glog.V(5).Infof("Generated UID %q for pod %q from file %s", pod.UID, pod.Name, filename) } // This is required for backward compatibility, and should be removed once we // completely deprecate ContainerManifest. if len(pod.Name) == 0 { pod.Name = string(pod.UID) glog.V(5).Infof("Generated Name %q for UID %q from file %s", pod.Name, pod.UID, filename) } if len(pod.Namespace) == 0 { hasher := adler32.New() fmt.Fprint(hasher, filename) // TODO: file-<sum>.hostname would be better, if DNS subdomains // are allowed for namespace (some places only allow DNS // labels). pod.Namespace = fmt.Sprintf("file-%08x-%s", hasher.Sum32(), hostname) glog.V(5).Infof("Generated namespace %q for pod %q from file %s", pod.Namespace, pod.Name, filename) } // TODO(dchen1107): BoundPod is not type of runtime.Object. Once we allow kubelet talks // about Pod directly, we can use SelfLinker defined in package: latest // Currently just simply follow the same format in resthandler.go pod.ObjectMeta.SelfLink = fmt.Sprintf("/api/v1beta2/pods/%s?namespace=%s", pod.Name, pod.Namespace) if glog.V(4) { glog.Infof("Got pod from file %q: %#v", filename, pod) } else { glog.V(1).Infof("Got pod from file %q: %s.%s (%s)", filename, pod.Namespace, pod.Name, pod.UID) } return pod, nil }
func hashObject(hasher hash.Hash, obj interface{}) []byte { util.DeepHashObject(hasher, obj) return hasher.Sum(nil) }