summaryrefslogtreecommitdiff
path: root/server/naming.go
blob: cc18ba40967a416f6e9c4faa49881b3d114875b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
}