登录
首页 >  Golang >  Go问答

会修改 ReplicaSet 和 StatefulSet 的编辑类型是哪种?

来源:stackoverflow

时间:2024-02-22 08:51:22 373浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《会修改 ReplicaSet 和 StatefulSet 的编辑类型是哪种?》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

什么类型的编辑会更改 replicaset 和 statefulset age(creationtimestamp)?

我问这个是因为我注意到

  1. 如果我更改部署映像,将会创建一个新的副本集。
  2. 旧的 replicaset 继续存在,且 desired 设置为 0。
  3. 如果我改回之前的容器映像,2 个 replicaset 不会更改其年龄,也不会重新创建。

那么,验证 deployment/replicaset 和 statefulset 是否有最新更新的最佳方法是什么?

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

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("\nNo statefulsets in the namespace", err)
    } else if err != nil {
        log.Fatal("\nFailed to fetch statefulsets in the namespace: ", err)
    }

    var stsNames []string
    for _, sts := range statefulsets.Items {
        stsNames = append(stsNames, sts.Name)
    }
    fmt.Printf("\nStatefulsets in the namespace: %v", stsNames)
    
    // check if the statefulsets are older than the 9 days
    for _, sts := range statefulsets.Items {
        stsAge := time.Since(sts.CreationTimestamp.Time)
        fmt.Printf("\nStatefulset %v age: %v", sts.Name, stsAge)
        if stsAge.Minutes() < 5 {
            fmt.Printf("\nStatefulset %v had recent updates. Skipping...", 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("\nNo replicasets in the namespace", err)
    } else if err != nil {
        log.Fatal("\nFailed to fetch replicasets in the namespace", err)
    }
    
    var rpsNames []string
    for _, rps := range replicasets.Items {
        rpsNames = append(rpsNames, rps.Name)
    }
    fmt.Printf("\nReplicasets in the namespace: %v", rpsNames)
    
    // check if the replicasets have recent updates
    for _, rps := range replicasets.Items {
        rpsAge := time.Since(rps.CreationTimestamp.Time)
        fmt.Printf("\nReplicaset %v age: %v", rps.Name, rpsAge)
        if rpsAge.Minutes() < 5 {
            fmt.Printf("\nReplicaset %v had recent updates...", rps.Name)
            return true
        }
    }
    return false
}

正确答案


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

到这里,我们也就讲完了《会修改 ReplicaSet 和 StatefulSet 的编辑类型是哪种?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>