代码拉取完成,页面将自动刷新
package main
import (
"flag"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
"time"
)
import (
"context"
"flag"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
"time"
)
func main(){
fmt.Println(time.Now().Format("2006:01:02 15:04:05")) //格式化日期
var kubeconfig *string
var namespacelist = []string{"bcap","cesp","fuxing"}
if home := homedir.HomeDir();home != ""{
kubeconfig = flag.String("kubeconfig",filepath.Join(home,".kube","config"),"option absolute path")
}else {
kubeconfig = flag.String("kubeconfig","","absolute config file path")
}
flag.Parse()
config,err := clientcmd.BuildConfigFromFlags("",*kubeconfig)
if err != nil{
panic(err.Error())
}
clientset,err := kubernetes.NewForConfig(config)
if err!= nil{
panic(err.Error())
}
namespaces,err := clientset.CoreV1().Namespaces().List(context.TODO(),metav1.ListOptions{})
if err != nil{
panic(err.Error())
}
for _,ns := range namespaces.Items{
fmt.Println(ns.Name,ns.Namespace)
result := in(ns.Name,namespacelist)
if result{
deployments, err := clientset.AppsV1().Deployments(ns.Name).List(context.TODO(),metav1.ListOptions{})
if err != nil{
panic(err.Error())
}
for _,dep := range deployments.Items {
deployment := &dep
deployment.Spec.Template.ObjectMeta.Labels["restarts"] = fmt.Sprintf("%s", time.Now().Format("20060102150405"))
_,err := clientset.AppsV1().Deployments(ns.Name).Update(context.TODO(),deployment,metav1.UpdateOptions{})
if err != nil{
panic(err.Error())
}
}
}
}
}
func in(target string, str_arry []string) bool {
for _,element := range str_arry{
if target == element{
return true
}
}
return false
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。