1 Star 0 Fork 0

wade.qu/k8s-cicd_client-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
restart_deployment.go 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
wade.qu 提交于 2023-07-11 15:16 . first commit config cmdb file to git
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wadequ/k8s-cicd_client-go.git
git@gitee.com:wadequ/k8s-cicd_client-go.git
wadequ
k8s-cicd_client-go
k8s-cicd_client-go
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385