Using Client-go to implement K8S operations

Source: Internet
Author: User
Tags k8s
This is a creation in Article, where the information may have evolved or changed. Package Main


Import (
"Flag"
"FMT"
"K8s.io/client-go/1.4/kubernetes"
"K8s.io/client-go/1.4/pkg/api"
"K8s.io/client-go/1.4/pkg/api/unversioned"
"K8s.io/client-go/1.4/pkg/api/v1"
"K8s.io/client-go/1.4/tools/clientcmd"
"Log"
)


VAR (
Kubeconfig = flag. String ("Kubeconfig", "./config", "absolute path to the Kubeconfig file")
)


Func Main () {
Flag. Parse ()
Uses the current context in Kubeconfig
Config, err: = Clientcmd. Buildconfigfromflags ("116.213.205.180:8080", *kubeconfig)
If err! = Nil {
Panic (err. Error ())
}
Creates the Clientset
Clientset, err: = Kubernetes. Newforconfig (config)
If err! = Nil {
Panic (err. Error ())
}
Create pod
Pod: = new (v1. POD)
Pod. Typemeta = unversioned. Typemeta{kind: "Pod", Apiversion: "V1"}
Pod. Objectmeta = v1. Objectmeta{name: "Testapi", Namespace: "Default", labels:map[string]string{"Name": "Testapi"}}
Pod. Spec = v1. podspec{
Restartpolicy:v1. Restartpolicyalways,
Containers: []v1. container{
V1. container{
Name: "Testapi",
Image: "Nginx",
Ports: []v1. containerport{
V1. containerport{
CONTAINERPORT:80,
Protocol:v1. PROTOCOLTCP,
},
},
},
},
}
_, Err = Clientset. Core (). Pods ("Default"). Create (POD)
If err! = Nil {
Panic (err. Error ())
}
Get the number of existing pods
Pods, err: = Clientset. Core (). Pods (""). List (API. listoptions{})
If err! = Nil {
Panic (err. Error ())
}
Fmt. Printf ("There is%d pods in the cluster\n", Len (pods. Items))


Create namespace
NC: = new (v1. Namespace)
nc. Typemeta = unversioned. Typemeta{kind: "NameSpace", Apiversion: "V1"}
nc. Objectmeta = v1. objectmeta{
Name: "K8s-test",
}
nc. Spec = v1. namespacespec{}
_, Err = Clientset. Core (). Namespaces (). Create (NC)
If err! = Nil {
Log. PRINTLN (ERR)
}


Get namespace
namespaces, Err: = Clientset. Core (). Namespaces (). List (API. listoptions{})
If err! = Nil {
Panic (err. Error ())
}
Fmt. Printf ("There is%d namespaces in the cluster\n", Len (namespaces. Items))
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.