kubernetes dev client-go管理namespace
1. 操作namespace创建查看删除package mainimport ("flag""fmt"apiv1 "k8s.io/api/core/v1"metav1 "k8s.io/apimachinery/pkg/apis/meta/v1""k8s.io/client-go/kubernetes""k8s.io/client-go/tools/clientcmd""k8s.io/client-
·
1. 操作namespace创建查看删除
package main
import (
"flag"
"fmt"
apiv1 "k8s.io/api/core/v1"
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"
)
func main() {
// 配置 k8s 集群外 kubeconfig 配置文件,默认位置 $HOME/.kube/config
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
//在 kubeconfig 中使用当前上下文环境,config 获取支持 url 和 path 方式
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
// 根据指定的 config 创建一个新的 clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
// 通过实现 clientset 的 CoreV1Interface 接口列表中的 NamespacesGetter 接口方法 Namespaces 返回 NamespaceInterface
// NamespaceInterface 接口拥有操作 Namespace 资源的方法,例如 Create、Update、Get、List 等方法
name := "client-go-test"
namespacesClient := clientset.CoreV1().Namespaces()
namespace := &apiv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Status: apiv1.NamespaceStatus{
Phase: apiv1.NamespaceActive,
},
}
// 创建一个新的 Namespaces
fmt.Println("Creating Namespaces...")
result, err := namespacesClient.Create(namespace)
if err != nil {
panic(err)
}
fmt.Printf("Created Namespaces %s on %s\n", result.ObjectMeta.Name, result.ObjectMeta.CreationTimestamp)
// 获取指定名称的 Namespaces 信息
fmt.Println("Getting Namespaces...")
result, err = namespacesClient.Get(name, metav1.GetOptions{})
if err != nil {
panic(err)
}
fmt.Printf("Name: %s, Status: %s, selfLink: %s, uid: %s\n",
result.ObjectMeta.Name, result.Status.Phase, result.ObjectMeta.SelfLink, result.ObjectMeta.UID)
// 删除指定名称的 Namespaces 信息
fmt.Println("Deleting Namespaces...")
deletePolicy := metav1.DeletePropagationForeground
if err := namespacesClient.Delete(name, &metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
}); err != nil {
panic(err)
}
fmt.Printf("Deleted Namespaces %s\n", name)
}
执行:
root@master:~/k8s_dev/namespace# go run client.go
Creating Namespaces...
Created Namespaces client-go-test on 2020-12-01 02:18:18 -0800 PST
Getting Namespaces...
Name: client-go-test, Status: Active, selfLink: /api/v1/namespaces/client-go-test, uid: d6b24e5a-1d17-40b9-848e-5599f6fddcd6
Deleting Namespaces...
Deleted Namespaces client-go-test
更多推荐
已为社区贡献43条内容
所有评论(0)