更改 ReplicaSet 和 StatefulSet 的 AGE(API 版本)的编辑类型有哪些?

huangapple go评论67阅读模式
英文:

What type of edits will change a ReplicaSet and StatefulSet AGE?

问题

更改 ReplicaSet 和 StatefulSet 的哪些编辑会改变 AGE(CreationTimeStamp)?

我之所以问这个问题,是因为我注意到:

1)如果我更改 Deployment 的镜像,将创建一个新的 ReplicaSet。
2)旧的 ReplicaSet 仍然存在,DESIRED 设置为 0。
3)如果我切换回先前的容器镜像,这两个 ReplicaSet 的年龄不会改变,也不会重新创建。

那么,验证 Deployment/ReplicaSet 和 StatefulSet 是否有最近的更新的最佳方法是什么?

到目前为止,我使用 client-go 检查这些资源的年龄:

func statefulsetCheck(namespace string, clientset *kubernetes.Clientset) bool {
	// 获取命名空间中的 StatefulSet
	statefulsets, err := clientset.AppsV1().StatefulSets(namespace).List(context.TODO(), metav1.ListOptions{})
	if errors.IsNotFound(err) {
		log.Fatal("\n命名空间中没有 StatefulSet", err)
	} else if err != nil {
		log.Fatal("\n获取命名空间中的 StatefulSet 失败:", err)
	}

	var stsNames []string
	for _, sts := range statefulsets.Items {
		stsNames = append(stsNames, sts.Name)
	}
	fmt.Printf("\n命名空间中的 StatefulSet:%v", stsNames)
	
	// 检查 StatefulSet 是否超过 9 天
	for _, sts := range statefulsets.Items {
		stsAge := time.Since(sts.CreationTimestamp.Time)
		fmt.Printf("\nStatefulSet %v 的年龄:%v", sts.Name, stsAge)
		if stsAge.Minutes() < 5 {
			fmt.Printf("\nStatefulSet %v 有最近的更新。跳过...", sts.Name)
			return true
		}
	}
	return false
}

func replicasetCheck(namespace string, clientset *kubernetes.Clientset) bool {
	// 获取命名空间中的 ReplicaSet
	replicasets, err := clientset.AppsV1().ReplicaSets(namespace).List(context.TODO(), metav1.ListOptions{})
	if errors.IsNotFound(err) {
		log.Fatal("\n命名空间中没有 ReplicaSet", err)
	} else if err != nil {
		log.Fatal("\n获取命名空间中的 ReplicaSet 失败", err)
	}
	
	var rpsNames []string
	for _, rps := range replicasets.Items {
		rpsNames = append(rpsNames, rps.Name)
	}
	fmt.Printf("\n命名空间中的 ReplicaSet:%v", rpsNames)
	
	// 检查 ReplicaSet 是否有最近的更新
	for _, rps := range replicasets.Items {
		rpsAge := time.Since(rps.CreationTimestamp.Time)
		fmt.Printf("\nReplicaSet %v 的年龄:%v", rps.Name, rpsAge)
		if rpsAge.Minutes() < 5 {
			fmt.Printf("\nReplicaSet %v 有最近的更新...", rps.Name)
			return true
		}
	}
	return false
}

以上是要翻译的内容。

英文:

What type of edits will change a ReplicaSet and StatefulSet AGE(CreationTimeStamp)?

I'm asking this because I noticed that

  1. If I change a Deployment image, a new ReplicaSet will be created.
  2. The old ReplicaSet continues to exist with DESIRED set to 0.
  3. If I change back to the previous container image, the 2 ReplicaSets don't change their age nor are recreated.

So, what is the best way to verify if there were recent updates to a Deployment/ReplicaSet and StatefulSet?

So far, I'm using client-go to check these resources ages:

func statefulsetCheck(namespace string, clientset *kubernetes.Clientset) bool {
// get the statefulsets in the namespace
statefulsets, err := clientset.AppsV1().StatefulSets(namespace).List(context.TODO(), metav1.ListOptions{})
if errors.IsNotFound(err) {
log.Fatal(&quot;\nNo statefulsets in the namespace&quot;, err)
} else if err != nil {
log.Fatal(&quot;\nFailed to fetch statefulsets in the namespace: &quot;, err)
}
var stsNames []string
for _, sts := range statefulsets.Items {
stsNames = append(stsNames, sts.Name)
}
fmt.Printf(&quot;\nStatefulsets in the namespace: %v&quot;, stsNames)
// check if the statefulsets are older than the 9 days
for _, sts := range statefulsets.Items {
stsAge := time.Since(sts.CreationTimestamp.Time)
fmt.Printf(&quot;\nStatefulset %v age: %v&quot;, sts.Name, stsAge)
if stsAge.Minutes() &lt; 5 {
fmt.Printf(&quot;\nStatefulset %v had recent updates. Skipping...&quot;, sts.Name)
return true
}
}
return false
}
func replicasetCheck(namespace string, clientset *kubernetes.Clientset) bool {
// get the replicasets in the namespace
replicasets, err := clientset.AppsV1().ReplicaSets(namespace).List(context.TODO(), metav1.ListOptions{})
if errors.IsNotFound(err) {
log.Fatal(&quot;\nNo replicasets in the namespace&quot;, err)
} else if err != nil {
log.Fatal(&quot;\nFailed to fetch replicasets in the namespace&quot;, err)
}
var rpsNames []string
for _, rps := range replicasets.Items {
rpsNames = append(rpsNames, rps.Name)
}
fmt.Printf(&quot;\nReplicasets in the namespace: %v&quot;, rpsNames)
// check if the replicasets have recent updates
for _, rps := range replicasets.Items {
rpsAge := time.Since(rps.CreationTimestamp.Time)
fmt.Printf(&quot;\nReplicaset %v age: %v&quot;, rps.Name, rpsAge)
if rpsAge.Minutes() &lt; 5 {
fmt.Printf(&quot;\nReplicaset %v had recent updates...&quot;, rps.Name)
return true
}
}
return false
}

答案1

得分: 1

"AGE(CreationTimeStamp)"

资源的CreationTimeStamp(即其年龄)在资源被创建时设置。例如,要更改它,您必须删除该资源并重新创建。

英文:

>AGE(CreationTimeStamp)

A resource's CreationTimeStamp (and thereby its age) is set when a resource is created. E.g. to change it, you must delete the resource and create it again.

huangapple
  • 本文由 发表于 2022年3月30日 22:16:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/71678918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定