首页 > 解决方案 > 我在哪里可以找到有关 kubernetes ID 规范的信息,即 pod 后缀 ID 长度或 replicaSet 后缀 ID 长度?

问题描述

我正在尝试查找上述信息,但似乎找不到任何具体的信息。

我已经尝试查看k8s 源代码,但在那里找到相关位有点困难,我不确定我是否可以依赖这些信息。

这样做的原因是我想在不调用 API 的情况下提取有关 pod 的某些数据。

所以有:

$ kubectl get pods
NAME                                                   READY   STATUS    RESTARTS   AGE
grafana-79dcc6469f-zzgmh                               2/2     Running   2          4d

$ kubectl get rs
NAME                                             DESIRED   CURRENT   READY   AGE
collection-grafana-79dcc6469f                    1         1         1       4d1h

$ kubectl get deployment
NAME                                  READY   UP-TO-DATE   AVAILABLE   AGE
collection-grafana                    1/1     1            1           4d1h

我想提取zzgmh为 pod ID 和79dcc6469f副本集 ID。

标签: kubernetes

解决方案


我在哪里可以找到有关 kubernetes ID 规范的信息,即 pod 后缀 ID 长度?

simpleNameGenerator函数在许多地方用于名称生成。

$ cat kubernetes/staging/src/k8s.io/apiserver/pkg/storage/names/generate.go

var SimpleNameGenerator NameGenerator = simpleNameGenerator{}

const (
        // TODO: make this flexible for non-core resources with alternate naming rules.
        maxNameLength          = 63
        randomLength           = 5
        maxGeneratedNameLength = maxNameLength - randomLength
)

func (simpleNameGenerator) GenerateName(base string) string {
        if len(base) > maxGeneratedNameLength {
                base = base[:maxGeneratedNameLength]
        }
        return fmt.Sprintf("%s%s", base, utilrand.String(randomLength))
}

replicaSet 后缀 ID 长度

从 1.8+开始,名称有 10 个符文长的后缀(辅音 + 数字)。这是通过文件控制func ControllerRevisionNamekubernetes/pkg/controller/history/controller_history.go

$ cat pkg/controller/history/controller_history.go
// ControllerRevisionName returns the Name for a ControllerRevision in the form prefix-hash. If the length
// of prefix is greater than 223 bytes, it is truncated to allow for a name that is no larger than 253 bytes.
func ControllerRevisionName(prefix string, hash string) string {
        if len(prefix) > 223 {
                prefix = prefix[:223]
        }

        return fmt.Sprintf("%s-%s", prefix, rand.SafeEncodeString(strconv.FormatInt(int64(hash), 10)))
}

此外,如果您要引入自己的更改ReplicaSetPod命名,您可能需要至少查看pkg/controller/deployment/sync.go描述podTemplateSpecHash.

此外,如果您只需要快速浏览 pod 或跟踪日志,您可能需要检查Sternkube-fzf工具。


推荐阅读