使用client-go 进行k8s相关操作-restclient(三)

restclient 是dynamic client和clientset的基础,支持json与protobuf,可以访问所有资源,实现对自定义thirdpartresource资源的获取

示例代码:


package main

import (
	"flag"
	"k8s.io/client-go/pkg/api"
	"k8s.io/client-go/pkg/api/v1"
	"k8s.io/client-go/pkg/runtime"
	"k8s.io/client-go/pkg/runtime/schema"
	"k8s.io/client-go/pkg/runtime/serializer"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	"log"
)

func main() {
	log.SetFlags(log.Llongfile)
	kubeconfig := flag.String("kubeconfig", "./config", "Path to a kube config. Only required if out-of-cluster.")
	flag.Parse()
	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		log.Fatalln(err)
	}
	groupversion := schema.GroupVersion{
		Group:   "k8s.io",
		Version: "v1",
	}
	config.GroupVersion = &groupversion
	config.APIPath = "/apis"
	config.ContentType = runtime.ContentTypeJSON
	config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: api.Codecs}
	restclient, err := rest.RESTClientFor(config)
	if err != nil {
		log.Fatalln(err)
	}
	e := examples{}
	err = restclient.Get().
		Resource("examples").
		Namespace("default").
		Name("example1").
		Do().Into(&e)
	log.Println(e)
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yevvzi/article/details/54426775
文章标签: golang k8s k8s开发
个人分类: golang k8s
所属专栏: kubernetes 之client-go
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭