K8S命名机制学习

本文深入探讨了Kubernetes中的命名机制,包括kube-controller-manager、StatefulSet、ReplicaSet和kubevirt克隆的命名策略。详细介绍了源码位置,如stateful_set_utils.go、controller_utils.go和util.go,阐述了如何生成随机名、正则表达式以及通过hash加密来确定名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习来源,新增一个ReplicaSet和kubevirt对应的实现。

对应的代码处于的k8s源码的位置已标注。

1. kube-controller-manager等的名称(自实现rand)

staging/src/k8s.io/apimachinery/pkg/util/rand/rand.go

const (
	// We omit vowels from the set of available characters to reduce the chances
	// of "bad words" being formed.
	alphanums = "bcdfghjklmnpqrstvwxz2456789"
	// No. of bits required to index into alphanums string.
	alphanumsIdxBits = 5
	// Mask used to extract last alphanumsIdxBits of an int.
	alphanumsIdxMask = 1<<alphanumsIdxBits - 1
	// No. of random letters we can extract from a single int63.
	maxAlphanumsPerInt = 63 / alphanumsIdxBits
)

// String generates a random alphanumeric string, without vowels, which is n
// characters long.  This will panic if n is less than zero.
// How the random string is created:
// - we generate random int63's
// - from each int63, we are extracting multiple random letters by bit-shifting and masking
// - if some index is out of range of alphanums we neglect it (unlikely to happen multiple times in a row)
func String(n int) string {
   
   
	b := make([]byte, n)	
	rng.Lock()
	defer rng.Unlock()

	randomInt63 := rng.rand.Int63()
	remaining := maxAlphanumsPerInt
	for i := 0; i < n; {
   
   
		if remaining == 0 {
   
   
			randomInt63, remaining = rng.rand.Int63(), maxAlphanumsPerInt
		}
		if idx := int(randomInt63 & alphanumsIdxMask); idx < len(alphanums) {
   
   
			b[i] 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值