使用client-go自定义开发Kubernetes
参考链接:使用client-go自定义开发Kubernetes
1.本地运行 apiserver demo
[root@wangjq demo]# apiserver-boot run local 2019/09/26 09:18:35 regenerating generated code. To disable regeneration, run with --generate=false. /var/wangjq/golang/bin/bin/apiregister-gen --input-dirs demo/pkg/apis/... --input-dirs demo/pkg/controller/... /var/wangjq/golang/bin/bin/conversion-gen --input-dirs demo/pkg/apis/demo/v1 --input-dirs demo/pkg/apis/demo -o /var/wangjq/golang/src --go-header-file boilerplate.go.txt -O zz_generated.conversion --extra-peer-dirs k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/apimachinery/pkg/conversion,k8s.io/apimachinery/pkg/runtime /var/wangjq/golang/bin/bin/deepcopy-gen --input-dirs demo/pkg/apis/demo/v1 --input-dirs demo/pkg/apis/demo -o /var/wangjq/golang/src --go-header-file boilerplate.go.txt -O zz_generated.deepcopy /var/wangjq/golang/bin/bin/openapi-gen --input-dirs demo/pkg/apis/demo/v1 --input-dirs demo/pkg/apis/demo -o /var/wangjq/golang/src --go-header-file boilerplate.go.txt -i k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/apimachinery/pkg/api/resource,k8s.io/apimachinery/pkg/version,k8s.io/apimachinery/pkg/runtime,k8s.io/apimachinery/pkg/util/intstr,k8s.io/api/admission/v1beta1,k8s.io/api/admissionregistration/v1alpha1,k8s.io/api/admissionregistration/v1beta1,k8s.io/api/apps/v1,k8s.io/api/apps/v1beta1,k8s.io/api/apps/v1beta2,k8s.io/api/authentication/v1,k8s.io/api/authentication/v1beta1,k8s.io/api/authorization/v1,k8s.io/api/authorization/v1beta1,k8s.io/api/autoscaling/v1,k8s.io/api/autoscaling/v2beta1,k8s.io/api/batch/v1,k8s.io/api/batch/v1beta1,k8s.io/api/batch/v2alpha1,k8s.io/api/certificates/v1beta1,k8s.io/api/core/v1,k8s.io/api/events/v1beta1,k8s.io/api/extensions/v1beta1,k8s.io/api/imagepolicy/v1alpha1,k8s.io/api/networking/v1,k8s.io/api/policy/v1beta1,k8s.io/api/rbac/v1,k8s.io/api/rbac/v1alpha1,k8s.io/api/rbac/v1beta1,k8s.io/api/scheduling/v1alpha1,k8s.io/api/settings/v1alpha1,k8s.io/api/storage/v1,k8s.io/api/storage/v1alpha1,k8s.io/api/storage/v1beta1,k8s.io/api/core/v1 --output-package demo/pkg/openapi /var/wangjq/golang/bin/bin/defaulter-gen --input-dirs demo/pkg/apis/demo/v1 --input-dirs demo/pkg/apis/demo -o /var/wangjq/golang/src --go-header-file boilerplate.go.txt -O zz_generated.defaults --extra-peer-dirs= k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/apimachinery/pkg/conversion,k8s.io/apimachinery/pkg/runtime /var/wangjq/golang/bin/bin/client-gen -o /var/wangjq/golang/src --go-header-file boilerplate.go.txt --input-base demo/pkg/apis --input demo/v1 --clientset-path demo/pkg/client/clientset_generated --clientset-name clientset /var/wangjq/golang/bin/bin/client-gen -o /var/wangjq/golang/src --go-header-file boilerplate.go.txt --input-base demo/pkg/apis --input demo --clientset-path demo/pkg/client/clientset_generated --clientset-name internalclientset /var/wangjq/golang/bin/bin/lister-gen --input-dirs demo/pkg/apis/demo/v1 --input-dirs demo/pkg/apis/demo -o /var/wangjq/golang/src --go-header-file boilerplate.go.txt --output-package demo/pkg/client/listers_generated /var/wangjq/golang/bin/bin/informer-gen --input-dirs demo/pkg/apis/demo/v1 --input-dirs demo/pkg/apis/demo -o /var/wangjq/golang/src --go-header-file boilerplate.go.txt --output-package demo/pkg/client/informers_generated --listers-package demo/pkg/client/listers_generated --versioned-clientset-package demo/pkg/client/clientset_generated/clientset --internal-clientset-package demo/pkg/client/clientset_generated/internalclientset 2019/09/26 09:19:04 CGO_ENABLED=0 go build -o bin/apiserver cmd/apiserver/main.go go build -o bin/controller-manager cmd/controller-manager/main.go etcd bin/apiserver --etcd-servers=http://localhost:2379 --secure-port=9443 --delegated-auth=false W0926 09:19:16.495764 2406 authorization.go:34] Authorization is disabled W0926 09:19:16.495828 2406 authentication.go:56] Authentication is disabled I0926 09:19:16.504351 2406 logs.go:41] Defaulting fields for Bird bird-example I0926 09:19:16.540151 2406 serve.go:89] Serving securely on [::]:9443 I0926 09:19:17.502752 2406 logs.go:41] Defaulting fields for Bird bird-example to test the server run `kubectl --kubeconfig kubeconfig api-versions` bin/controller-manager --kubeconfig=kubeconfig 2019-09-26 09:19:18.036499 I | Running reconcile Bird for bird-example
2.验证apiserver是否运行正常
[root@wangjq demo]# kubectl --kubeconfig kubeconfig api-versions
demo.xxx.com/v1
3.client-go源代码(创建,删除,更新,获取)
package main import ( birdApiv1 "demo/pkg/apis/demo/v1" clientset "demo/pkg/client/clientset_generated/clientset" "encoding/json" "fmt" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" schema "k8s.io/apimachinery/pkg/runtime/schema" restclient "k8s.io/client-go/rest" ) var url = "127.0.0.1:9443" var newName string = "client-go-demo" type ResourceHandler struct { client *DemoClient } type DemoClient struct { Client *clientset.Clientset } func newResourceHandler() *ResourceHandler { return &ResourceHandler{client: NewDemoClient()} } func NewDemoClient() *DemoClient { c := &DemoClient{} c.Client = clientset.NewForConfigOrDie(&restclient.Config{ Host: url, TLSClientConfig: restclient.TLSClientConfig{ Insecure: true, }, APIPath: "/apis/demo.xxx.com", ContentConfig: restclient.ContentConfig{ GroupVersion: &schema.GroupVersion{ Group: "demo", Version: "v1", }}}) fmt.Println(c.Client) return c } func (c *ResourceHandler) CreateBird() { namespace := &birdApiv1.Bird{ ObjectMeta: metav1.ObjectMeta{ Name: newName, UID: "aead7663-a7b6-471a-a222-a94c41a99999", }, Status: birdApiv1.BirdStatus{}, Spec: birdApiv1.BirdSpec{ Datetime: "2018-06-14T18:00:00Z03:00", AwayTeamGoals: 9, AwayTeam: "wangjq", HomeTeam: "demo", HomeTeamGoals: 10, }, } _, err := c.client.Client.DemoV1().Birds("default").Create(namespace) if err != nil { panic(err) } } func (c *ResourceHandler) DeleteBird(name string) { if err := c.client.Client.DemoV1().Birds("default").Delete(name, &metav1.DeleteOptions{}); err != nil { panic(err) } } func (c *ResourceHandler) UpdateBird(name string) { result := c.GetBird(name) result.Spec.AwayTeam = "zzzzz" result.Spec.Datetime = "2222-12-22T22:22:00Z03:22" result.Spec.HomeTeam = "zzzzz" c.client.Client.DemoV1().Birds("default").Update(result) } func (c *ResourceHandler) ListBirds() { fmt.Println("============================ List All ============================") podList, _ := c.client.Client.DemoV1().Birds("default").List(metav1.ListOptions{}) fmt.Println("podList: ", len(podList.Items)) for key, value := range podList.Items { fmt.Println("第", key+1, "个pod.................") bytes, err := json.Marshal(value) if err != nil { return } fmt.Println(string(bytes)) fmt.Println("##################################################") fmt.Printf("AwayTeam: %s\n", value.Spec.AwayTeam) fmt.Printf("AwayTeamGoals: %s\n", value.Spec.AwayTeamGoals) fmt.Printf("Datetime: %s\n", value.Spec.Datetime) fmt.Printf("HomeTeam: %s\n", value.Spec.HomeTeam) fmt.Printf("HomeTeamGoals: %s\n", value.Spec.HomeTeamGoals) fmt.Println("##################################################") } for _, pod := range podList.Items { fmt.Printf("Name: %s, Status: %s, CreateTime: %s, Uid: %s\n", pod.ObjectMeta.Name, pod.Status, pod.ObjectMeta.CreationTimestamp, pod.GetUID()) } } func (c *ResourceHandler) GetBird(name string) *birdApiv1.Bird { result, err := c.client.Client.DemoV1().Birds("default").Get(name, metav1.GetOptions{}) if err != nil { panic(err) } return result } func main() { resourceHandler := newResourceHandler() resourceHandler.ListBirds() resourceHandler.CreateBird() resourceHandler.ListBirds() resourceHandler.UpdateBird(newName) resourceHandler.ListBirds() resourceHandler.DeleteBird(newName) resourceHandler.ListBirds() }
4.运行测试
[root@wangjq demo]# go run client-demo.go &{0xc000292d40 0xc0002a3970} ============================ List All ============================ podList: 0 ============================ List All ============================ podList: 1 第 1 个pod................. {"metadata":{"name":"client-go-demo","namespace":"default","selfLink":"/apis/demo.xxx.com/v1/namespaces/default/birds/client-go-demo","uid":"4264dd63-eb30-11e9-86cf-5098b81a3139","resourceVersion":"2013","generation":1,"creationTimestamp":"2019-10-10T07:33:31Z"},"spec":{"datetime":"2018-06-14T18:00:00Z03:00","homeTeam":"demo","homeTeamGoals":10,"awayTeam":"wangjq","awayTeamGoals":9},"status":{}} ################################################## AwayTeam: wangjq AwayTeamGoals: %!s(int=9) Datetime: 2018-06-14T18:00:00Z03:00 HomeTeam: demo HomeTeamGoals: %!s(int=10) ################################################## Name: client-go-demo, Status: {}, CreateTime: 2019-10-10 15:33:31 +0800 HKT, Uid: 4264dd63-eb30-11e9-86cf-5098b81a3139 ============================ List All ============================ podList: 1 第 1 个pod................. {"metadata":{"name":"client-go-demo","namespace":"default","selfLink":"/apis/demo.xxx.com/v1/namespaces/default/birds/client-go-demo","uid":"4264dd63-eb30-11e9-86cf-5098b81a3139","resourceVersion":"2014","generation":2,"creationTimestamp":"2019-10-10T07:33:31Z"},"spec":{"datetime":"2222-12-22T22:22:00Z03:22","homeTeam":"zzzzz","homeTeamGoals":10,"awayTeam":"zzzzz","awayTeamGoals":9},"status":{}} ################################################## AwayTeam: zzzzz AwayTeamGoals: %!s(int=9) Datetime: 2222-12-22T22:22:00Z03:22 HomeTeam: zzzzz HomeTeamGoals: %!s(int=10) ################################################## Name: client-go-demo, Status: {}, CreateTime: 2019-10-10 15:33:31 +0800 HKT, Uid: 4264dd63-eb30-11e9-86cf-5098b81a3139 ============================ List All ============================ podList: 0