使用client-go自定义开发Kubernetes
参考链接:使用client-go自定义开发Kubernetes
1.本地运行 apiserver demo
[root@wangjq demo]# apiserver-boot run local
// :: 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
// :: CGO_ENABLED=
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 ::16.495764 authorization.go:] Authorization is disabled
W0926 ::16.495828 authentication.go:] Authentication is disabled
I0926 ::16.504351 logs.go:] Defaulting fields for Bird bird-example
I0926 ::16.540151 serve.go:] Serving securely on [::]:
I0926 ::17.502752 logs.go:] Defaulting fields for Bird bird-example
to test the server run `kubectl --kubeconfig kubeconfig api-versions`
bin/controller-manager --kubeconfig=kubeconfig
-- ::18.036499 I | Running reconcile Bird for bird-example
2.验证apiserver是否运行正常
[root@wangjq demo]# kubectl --kubeconfig kubeconfig api-versions
demo.qianxin.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.qianxin.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: ,
AwayTeam: "wangjq",
HomeTeam: "demo",
HomeTeamGoals: ,
},
}
_, 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+, "个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:
============================ List All ============================
podList:
第 个pod.................
{"metadata":{"name":"client-go-demo","namespace":"default","selfLink":"/apis/demo.qianxin.com/v1/namespaces/default/birds/client-go-demo","uid":"4264dd63-eb30-11e9-86cf-5098b81a3139","resourceVersion":"","generation":,"creationTimestamp":"2019-10-10T07:33:31Z"},"spec":{"datetime":"2018-06-14T18:00:00Z03:00","homeTeam":"demo","homeTeamGoals":,"awayTeam":"wangjq","awayTeamGoals":},"status":{}}
##################################################
AwayTeam: wangjq
AwayTeamGoals: %!s(int=)
Datetime: --14T18::00Z03:
HomeTeam: demo
HomeTeamGoals: %!s(int=)
##################################################
Name: client-go-demo, Status: {}, CreateTime: -- :: + HKT, Uid: 4264dd63-eb30-11e9-86cf-5098b81a3139
============================ List All ============================
podList:
第 个pod.................
{"metadata":{"name":"client-go-demo","namespace":"default","selfLink":"/apis/demo.qianxin.com/v1/namespaces/default/birds/client-go-demo","uid":"4264dd63-eb30-11e9-86cf-5098b81a3139","resourceVersion":"","generation":,"creationTimestamp":"2019-10-10T07:33:31Z"},"spec":{"datetime":"2222-12-22T22:22:00Z03:22","homeTeam":"zzzzz","homeTeamGoals":,"awayTeam":"zzzzz","awayTeamGoals":},"status":{}}
##################################################
AwayTeam: zzzzz
AwayTeamGoals: %!s(int=)
Datetime: --22T22::00Z03:
HomeTeam: zzzzz
HomeTeamGoals: %!s(int=)
##################################################
Name: client-go-demo, Status: {}, CreateTime: -- :: + HKT, Uid: 4264dd63-eb30-11e9-86cf-5098b81a3139
============================ List All ============================
podList:
使用client-go自定义开发Kubernetes的更多相关文章
- client-go客户端自定义开发Kubernetes及源码分析
介绍 client-go 是一种能够与 Kubernetes 集群通信的客户端,通过它可以对 Kubernetes 集群中各资源类型进行 CRUD 操作,它有三大 client 类,分别为:Clien ...
- TFS自定义开发中的反射应用
最近CM(Configuration Management) 的同事在自定义开发TFS的过程中遇到一个问题. 领导要求快速开发一个工具, 可以自动连接TFS,然后自动Check out一些word文件 ...
- odoo通过actions.client进行自定义页面
一.使用原因 由于odoo自带页面在项目开发过程中无法满足使用,需要使用到动作ir.actions.client进行自定义视图的开发,实现自定义的xml视图开发. 二.实现目标 三.开发过程 1.项目 ...
- VS自定义开发向导中的vsdir文件的简单说明
作者:朱金灿 来源:http://blog.csdn.net/clever101 VS自定义开发向导中有一个vsdir文件.这个文件指定了在VS中项目的标题.默认工程名等内容.下面对vsdir文件做一 ...
- 封装:简要介绍自定义开发基于WPF的MVC框架
原文:封装:简要介绍自定义开发基于WPF的MVC框架 一.目的:在使用Asp.net Core时,深感MVC框架作为页面跳转数据处理的方便,但WPF中似乎没有现成的MVC框架,由此自定义开发一套MVC ...
- Office 365 Connectors 的使用与自定义开发
前言 我相信很多人都看过<三国演义>,里面有很多引人入胜的故事和栩栩如生的人物,对我而言,曹操手下的一员猛将典韦实在让我印象深刻.例如,书中有一段描写典韦的作战经历: 时西面又急,韦进当之 ...
- 使用 C# 开发 Kubernetes 组件,获取集群资源信息
写什么呢 前段时间使用 C# 写了个项目,使用 Kubernetes API Server,获取信息以及监控 Kubernetes 资源,然后结合 Neting 做 API 网关. 体验地址 http ...
- BizTalk动手实验(十一)自定义开发管道组件
1 课程简介 通过本课程熟悉自定义开始管道组件的流程.各组件接口的功能作用以及自定义管道. 本场景为开发一个消息ZIP压缩的发送管道组件. 2 准备工作 1. 熟悉管道组件各阶段组成 2. 下载Ion ...
- openERP笔记,自定义开发模块
##目标 OpenERP模块基本结构 使用模块添加额外的字段(Date Required和Rush Order) 扩展视图, 让OpenERP能够显示新的字段 修改用于OpenERP工作流的可用状态 ...
随机推荐
- Android:自定义BaseActivity基类
使用BaseActivity可以封装一些重复代码例如设置标题栏颜色,封装一些工具类... 主要功能: 封装Toast 新建一个BaseActivity继承自Activity package com.o ...
- JAVA基础(jdk安装和环境变量的配置)
JAVA 1.何为JAVA Java的发展可以归纳如下的几个阶段. (1)第一阶段(完善期):JDK 1.0 ( 1995年推出)一JDK 1.2 (1998年推出,Java更名为Java 2): ( ...
- MacOS下JDK8的安装与配置
微信搜索"艺术行者",关注并回复关键词"jdk8"获取安装包和API文档资料! 一.安装环节 1.打开网页 https://www.oracle.com/jav ...
- IOFFSETOF ICONTAINEROF IQUEUE_ENTRY
#include <iostream> #define IOFFSETOF(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #de ...
- OpenCL Kernel设计优化
使用Intel® FPGA SDK for OpenCL™ 离线编译器,不需要调整kernel代码便可以将其最佳的适应于固定的硬件设备,而是离线编译器会根据kernel的要求自适应调整硬件的结构. 通 ...
- demo2动态加载显示商品详情页
/* 要求:实现 头像+昵称(多余7位用...) 商品图片(根据商品实际的图片的大小进行动态的展示.按照一定的比例进行展示.) 产品简介.产品简介在商品图片的下边.并跟随商品图片的大小进行动态的收缩或 ...
- python6.2类的封装
class Card(object): def __init__(self,num,pwd,ban): self.num=num#卡号 self.pwd=pwd#密码 self.__ban=ban#余 ...
- Python Tricks —— 使用 pywinrm 远程控制 Windows 主机
启用 WinRM 远程服务: winrm quickconfig 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不 ...
- 有哪些开源的 Python 库让你相见恨晚?
Arrow 我们知道 Python 已经内置了好几个处理时间相关的库,但是对于时间以及时区间的转换并不清晰,操作起来略繁琐,而 Arrow 可以弥补这个问题,它提供了更友好的方法,方便我们对时间,日期 ...
- Springboot常用的注解
1.@Controller主要用来修饰类,用来处理http请求 2.@ResponseBody主要用来修饰类和方法.返回字符串和json数据,不用来返回模板. 3.@RestController主要用 ...