[英]How to get the latest change time of StatefulSet in k8s
例如,我知道您可以使用kubectl獲取 Deployment 的 lastUpdateTime :
kubectl get deploy <deployment-name> -o jsonpath={.status.conditions[1].lastUpdateTime}
或通過client-go :
func deploymentCheck(namespace string, clientset *kubernetes.Clientset) bool {
// get the deployments in the namespace
deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
if errors.IsNotFound(err) {
log.Fatal("\nNo deployments in the namespace", err)
} else if err != nil {
log.Fatal("\nFailed to fetch deployments in the namespace", err)
}
var dptNames []string
for _, dpt := range deployments.Items {
dptNames = append(dptNames, dpt.Name)
}
// check the last update time of the deployments
for _, dpt := range deployments.Items {
lastUpdateTime := dpt.Status.Conditions[1].LastUpdateTime
dptAge := time.Since(lastUpdateTime.Time)
fmt.Printf("\nDeployment %v age: %v", dpt.Name, dptAge)
}
}
StatefulSet 的lastUpdateTime:= dpt.Status.Conditions[1].LastUpdateTime
的等效項似乎不存在。
那么,如何獲取 StatefulSet 的 lastUpdateTime 呢?
我注意到,在某人編輯給定資源后,唯一發生變化的是資源的 lastAppliedConfiguration、Generation 和 ObservedGeneration。 所以,我將它們存儲在列表中:
for _, deployment := range deployments.Items {
deploymentNames = append(deploymentNames, deployment.Name)
lastAppliedConfig := deployment.GetAnnotations()["kubectl.kubernetes.io/last-applied-configuration"]
lastAppliedConfigs = append(lastAppliedConfigs, lastAppliedConfig)
generations = append(generations, deployment.Generation)
observedGenerations = append(observedGenerations, deployment.Status.ObservedGeneration)
}
這是完整的 function:
func DeploymentCheck(namespace string, clientset *kubernetes.Clientset) ([]string, []string, []int64, []int64) {
var deploymentNames []string
var lastAppliedConfigs []string
var generations []int64
var observedGenerations []int64
deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
if errors.IsNotFound(err) {
log.Print("No deployments in the namespace", err)
} else if err != nil {
log.Print("Failed to fetch deployments in the namespace", err)
}
for _, deployment := range deployments.Items {
deploymentNames = append(deploymentNames, deployment.Name)
lastAppliedConfig := deployment.GetAnnotations()["kubectl.kubernetes.io/last-applied-configuration"]
lastAppliedConfigs = append(lastAppliedConfigs, lastAppliedConfig)
generations = append(generations, deployment.Generation)
observedGenerations = append(observedGenerations, deployment.Status.ObservedGeneration)
}
return deploymentNames, lastAppliedConfigs, generations, observedGenerations
}
我使用所有這些信息來實例化一個名為 Namespace 的結構,其中包含 k8s 命名空間可以擁有的所有主要資源。
然后,在給定時間后,我再次檢查相同的命名空間並檢查其資源是否有任何更改:
if !reflect.DeepEqual(namespace.stsLastAppliedConfig, namespaceCopy.stsLastAppliedConfig) {
...
}
else if !reflect.DeepEqual(namespace.stsGeneration, namespaceCopy.stsGeneration) {
...
}
else if !reflect.DeepEqual(namespace.stsObservedGeneration, namespaceCopy.stsObservedGeneration) {
...
}
因此,我發現的唯一解決方法是在給定時間后比較資源的配置,包括 StatefulSets'。 顯然,對於某些資源,您無法獲得有關其 lastUpdateTime 的任何信息。
我還發現 lastUpdateTime 實際上並不可靠,因為它將較小的集群更改理解為資源的更改。 例如,如果一個集群旋轉並殺死所有 pod,則 Deployment 的 lastUpdateTime 將更新其時間。 那不是我想要的。 我想檢測用戶對資源的更改,例如當有人應用編輯的 yaml 文件或運行kubectl edit
時。
如果您想通過每天自動運行此腳本來清理集群中未使用的資源,這將特別有用。 一些測試命名空間可以通過正則表達式作為目標,並且您可以設置腳本以刪除測試命名空間,例如,如果其中的資源在過去 7 天內沒有更改。 這可能意味着沒有人再致力於它了。
@hypperster,我希望它有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.