summaryrefslogtreecommitdiff
path: root/server/naming.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2017-11-01 11:24:59 -0400
committerMatthew Heon <matthew.heon@gmail.com>2017-11-01 11:24:59 -0400
commita031b83a09a8628435317a03f199cdc18b78262f (patch)
treebc017a96769ce6de33745b8b0b1304ccf38e9df0 /server/naming.go
parent2b74391cd5281f6fdf391ff8ad50fd1490f6bf89 (diff)
downloadpodman-a031b83a09a8628435317a03f199cdc18b78262f.tar.gz
podman-a031b83a09a8628435317a03f199cdc18b78262f.tar.bz2
podman-a031b83a09a8628435317a03f199cdc18b78262f.zip
Initial checkin from CRI-O repo
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Diffstat (limited to 'server/naming.go')
-rw-r--r--server/naming.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/server/naming.go b/server/naming.go
new file mode 100644
index 000000000..cc18ba409
--- /dev/null
+++ b/server/naming.go
@@ -0,0 +1,86 @@
+package server
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/docker/docker/pkg/stringid"
+ pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
+)
+
+const (
+ kubePrefix = "k8s"
+ infraName = "POD"
+ nameDelimiter = "_"
+)
+
+func makeSandboxName(sandboxConfig *pb.PodSandboxConfig) string {
+ return strings.Join([]string{
+ kubePrefix,
+ sandboxConfig.Metadata.Name,
+ sandboxConfig.Metadata.Namespace,
+ sandboxConfig.Metadata.Uid,
+ fmt.Sprintf("%d", sandboxConfig.Metadata.Attempt),
+ }, nameDelimiter)
+}
+
+func makeSandboxContainerName(sandboxConfig *pb.PodSandboxConfig) string {
+ return strings.Join([]string{
+ kubePrefix,
+ infraName,
+ sandboxConfig.Metadata.Name,
+ sandboxConfig.Metadata.Namespace,
+ sandboxConfig.Metadata.Uid,
+ fmt.Sprintf("%d", sandboxConfig.Metadata.Attempt),
+ }, nameDelimiter)
+}
+
+func makeContainerName(sandboxMetadata *pb.PodSandboxMetadata, containerConfig *pb.ContainerConfig) string {
+ return strings.Join([]string{
+ kubePrefix,
+ containerConfig.Metadata.Name,
+ sandboxMetadata.Name,
+ sandboxMetadata.Namespace,
+ sandboxMetadata.Uid,
+ fmt.Sprintf("%d", containerConfig.Metadata.Attempt),
+ }, nameDelimiter)
+}
+
+func (s *Server) generatePodIDandName(sandboxConfig *pb.PodSandboxConfig) (string, string, error) {
+ var (
+ err error
+ id = stringid.GenerateNonCryptoID()
+ )
+ if sandboxConfig.Metadata.Namespace == "" {
+ return "", "", fmt.Errorf("cannot generate pod ID without namespace")
+ }
+ name, err := s.ReservePodName(id, makeSandboxName(sandboxConfig))
+ if err != nil {
+ return "", "", err
+ }
+ return id, name, err
+}
+
+func (s *Server) generateContainerIDandNameForSandbox(sandboxConfig *pb.PodSandboxConfig) (string, string, error) {
+ var (
+ err error
+ id = stringid.GenerateNonCryptoID()
+ )
+ name, err := s.ReserveContainerName(id, makeSandboxContainerName(sandboxConfig))
+ if err != nil {
+ return "", "", err
+ }
+ return id, name, err
+}
+
+func (s *Server) generateContainerIDandName(sandboxMetadata *pb.PodSandboxMetadata, containerConfig *pb.ContainerConfig) (string, string, error) {
+ var (
+ err error
+ id = stringid.GenerateNonCryptoID()
+ )
+ name, err := s.ReserveContainerName(id, makeContainerName(sandboxMetadata, containerConfig))
+ if err != nil {
+ return "", "", err
+ }
+ return id, name, err
+}