mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 01:06:27 +00:00
Use the first token of hostname for generating static pod names
This increases the readability of pods by avoiding long names. This fixes #5936
This commit is contained in:
parent
7386897d00
commit
793a3c0c63
@ -21,6 +21,7 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
||||||
@ -33,6 +34,16 @@ import (
|
|||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Generate a pod name that is unique among nodes by appending the hostname.
|
||||||
|
func generatePodName(name, hostname string) (string, error) {
|
||||||
|
// Hostname can be a fully-qualified domain name. To increase readability,
|
||||||
|
// only append the first chunk. Note that this assumes that no two nodes in
|
||||||
|
// the cluster should have the same host-specific label; this is true if
|
||||||
|
// all nodes have the same domain name.
|
||||||
|
chunks := strings.Split(hostname, ".")
|
||||||
|
return fmt.Sprintf("%s-%s", name, chunks[0]), nil
|
||||||
|
}
|
||||||
|
|
||||||
func applyDefaults(pod *api.Pod, source string, isFile bool, hostname string) error {
|
func applyDefaults(pod *api.Pod, source string, isFile bool, hostname string) error {
|
||||||
if len(pod.UID) == 0 {
|
if len(pod.UID) == 0 {
|
||||||
hasher := md5.New()
|
hasher := md5.New()
|
||||||
@ -53,7 +64,7 @@ func applyDefaults(pod *api.Pod, source string, isFile bool, hostname string) er
|
|||||||
if len(pod.Name) == 0 {
|
if len(pod.Name) == 0 {
|
||||||
pod.Name = string(pod.UID)
|
pod.Name = string(pod.UID)
|
||||||
}
|
}
|
||||||
if pod.Name, err = GeneratePodName(pod.Name, hostname); err != nil {
|
if pod.Name, err = generatePodName(pod.Name, hostname); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
glog.V(5).Infof("Generated Name %q for UID %q from URL %s", pod.Name, pod.UID, source)
|
glog.V(5).Infof("Generated Name %q for UID %q from URL %s", pod.Name, pod.UID, source)
|
||||||
|
@ -361,7 +361,3 @@ func bestPodIdentString(pod *api.Pod) string {
|
|||||||
}
|
}
|
||||||
return fmt.Sprintf("%s.%s", name, namespace)
|
return fmt.Sprintf("%s.%s", name, namespace)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GeneratePodName(name, hostname string) (string, error) {
|
|
||||||
return fmt.Sprintf("%s-%s", name, hostname), nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user