kubernets code-generator
REF:
how-to-generate-client-codes-for-kubernetes-custom-resource-definitions-crd
Firstly we need to install golang.
1. the api spec looks like:
# Definition
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: hellotypes.foo.com
spec:
group: foo.com
version: v1
scope: Namespaced
names:
kind: HelloType
shortNames: ht
plural: hellotypes
singular: hellotype
# HelloType
---
apiVersion: foo.com/v1
kind: HelloType
metadata:
name: superman-hello
spec:
message: hello world
2. create the project workspace
mkdir -p $GOPATH/src/github.com/superman/demo/pkg/apis/foo/v1
cd $GOPATH/src/github.com/superman/demo/pkg/apis
3. create foo/v1/types.go
cat foo/v1/types.go
package v1 import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" // +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // HelloType is a top-level type
type HelloType struct {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"` // +optional
Status HelloTypeStatus `json:"status,omitempty"`
// This is where you can define
// your own custom spec
Spec HelloSpec `json:"spec,omitempty"`
} // custom spec
type HelloSpec struct {
Message string `json:"message,omitempty"`
} // custom status
type HelloTypeStatus struct {
Name string
} // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // no client needed for list as it's been created in above
type HelloTypeList struct {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ListMeta `son:"metadata,omitempty"` Items []HelloType `json:"items"`
}
4. create foo/v1/doc.go
cat foo/v1/doc.go
// +k8s:deepcopy-gen=package
// +k8s:defaulter-gen=TypeMeta
// +groupName=foo.com package v1
5. ceate foo/v1/register.go
cat foo/v1/register.go
package v1 import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
) // Define your schema name and the version
var SchemeGroupVersion = schema.GroupVersion{
Group: "foo.com",
Version: "v1",
} var (
SchemeBuilder runtime.SchemeBuilder
localSchemeBuilder = &SchemeBuilder
AddToScheme = localSchemeBuilder.AddToScheme
) func init() {
// We only register manually written functions here. The registration of the
// generated functions takes place in the generated files. The separation
// makes the code compile even when the generated files are missing.
localSchemeBuilder.Register(addKnownTypes)
} // Resource takes an unqualified resource and returns a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
} // Adds the list of known types to the given scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(
SchemeGroupVersion,
&HelloType{},
&HelloTypeList{},
) scheme.AddKnownTypes(
SchemeGroupVersion,
&metav1.Status{},
) metav1.AddToGroupVersion(
scheme,
SchemeGroupVersion,
) return nil
}
cd d $GOPATH/src/github.com/superman/demo/pkg/apis
.
├── apis
│ └── foo
│ └── v1
│ ├── doc.go
│ ├── register.go
│ ├── types.go
5. gen code
go get k8s.io/code-generator
go get k8s.io/apimachinery
cd $GOPATH/src/k8s.io/code-generator
./generate-groups.sh all \
"github.com/superman/demo/pkg/client" \
"github.com/superman/demo/pkg/apis" \
foo:v1
cd $GOPATH/src/github.com/superman/demo/pkg/
tree
.
├── apis
│ └── foo
│ └── v1
│ ├── doc.go
│ ├── register.go
│ ├── types.go
│ └── zz_generated.deepcopy.go
└── client
├── clientset
│ └── versioned
│ ├── clientset.go
│ ├── doc.go
│ ├── fake
│ │ ├── clientset_generated.go
│ │ ├── doc.go
│ │ └── register.go
│ ├── scheme
│ │ ├── doc.go
│ │ └── register.go
│ └── typed
│ └── foo
│ └── v1
│ ├── doc.go
│ ├── fake
│ │ ├── doc.go
│ │ ├── fake_foo_client.go
│ │ └── fake_hellotype.go
│ ├── foo_client.go
│ ├── generated_expansion.go
│ └── hellotype.go
├── informers
│ └── externalversions
│ ├── factory.go
│ ├── foo
│ │ ├── interface.go
│ │ └── v1
│ │ ├── hellotype.go
│ │ └── interface.go
│ ├── generic.go
│ └── internalinterfaces
│ └── factory_interfaces.go
└── listers
└── foo
└── v1
├── expansion_generated.go
└── hellotype.go
REF:
Kubernetes Deep Dive: Code Generation for CustomResources (翻译)
Extending Kubernetes: Create Controllers for Core and Custom Resources (total example)
Writing Kubernetes Custom Controllers
How to generate client codes for Kubernetes Custom Resource Definitions (CRD)
code-generator git hub(各种概念定义)
Accessing Kubernetes CRDs from the client-go package (存手工代码,没有自动生成)
创建和管理CRD的client库位于:github.com/kubernetes/…
writing-your-first-kubernetes-operator
search "kubernetes operator example" or "kubernetes operator tutorial" for operator auto gen
kubernets code-generator的更多相关文章
- ABP配套代码生成器(ABP Code Generator)帮助文档,实现快速开发
ABP代码生成器介绍 针对abp这个框架做了一个代码生成器,功能强大.分为两大功能点,一个是数据层,一个是视图层. 数据服务层:通过它,可以实现表设计.领域层初始化.多语言.automapper自动注 ...
- OData Client Code Generator
转发. [Tutorial & Sample] How to use OData Client Code Generator to generate client-side proxy cla ...
- Android项目实战(十九):Android Studio 优秀插件: Parcelable Code Generator
Android Studio 优秀插件系列: Android Studio 优秀插件(一):GsonFormat Android Studio 优秀插件(二): Parcelable Code Gen ...
- JAVA 调用Axis2 code generator 生成的webservice
以下代码为调用 JAVA 调用Axis2 code generator 生成的webservice的代码. package test; import java.rmi.RemoteException; ...
- DNN - Modules - QR Code Generator
Dotnetnuke 平台上的二维码模块.支持DNN 7.x平台的安装 QR码(快速响应码)是二维条形码.随着移动设备市场正以快速的步伐,QR码正在成为非常重要的营销工具.与移动电话或平板电脑的扫描, ...
- there was an error running the selected code generator unable to retrieve metadata for
there was an error running the selected code generator unable to retrieve metadata for PROBLEM: I ha ...
- Android开发的插件Code Generator与LayoutCreator的安装与使用,提升你的开发效率
前言 大家好,给大家带来Android开发的插件Code Generator与LayoutCreator的安装与使用,提升你的开发效率的概述,希望你们喜欢 学习目标 掌握两个插件的安装和使用,能够实现 ...
- 安卓自动生成代码插件-Android code Generator(转)
编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! 介绍 A ...
- 【2017年9月10日更新】ABP配套代码生成器(ABP Code Generator)帮助文档,实现快速开发
ABP代码生成器介绍 ABP Code Generator 针对abp这个框架做了一个代码生成器,功能强大.分为两大功能点,一个是数据层,一个是视图层. 数据服务层:通过它,可以实现表设计.领域层初始 ...
- FSM Code Generator
FSM Code Generator is a script code generator for Finite State Machine, it has a viaual designer bas ...
随机推荐
- spring boot 采坑
2019年2月19日19:25:42 版本 2.1.3.RELEASE 1,本地开发需要加依赖库,保存实时热更新 <dependency> <groupId>org.sprin ...
- 4.29python
题目: 代码:(?) list = input().split()list1 = []list2 = []for i in range(len(list)): if (i+1)%3 != 0 and ...
- 去除桌面SVN问号
由于误操作,桌面文件全部带着问号. 找到一种比较方便的方法解决. 1.显示受保护文件.文件夹win7系统 - 随便打开一个文件夹 - 在菜单栏里点 工具 - 文件夹选项 - 查看 以下如图中选择 2 ...
- webapi 利用webapiHelp和swagger生成接口文档
webapi 利用webapiHelp和swagger生成接口文档.均依赖xml(需允许项目生成注释xml) webapiHelp:微软技术自带,仅含有模块.方法.请求-相应参数的注释. swagge ...
- 42028: Assignment 1 – Autumn 2019
42028: Assignment 1 – Autumn 2019 Page 1 of 4Faculty of Engineering and Information TechnologySchool ...
- Python 笔记 v1 - json和字符串互转
字符串转为json对象并取值 >>> 使用了Json包中的loads()方法和dumps()方法 string =" { "status": " ...
- FPGA小例子
AND ---与门:OR --- 或门:INV --- 非门:NAND --- 与非门:NOR --- 或非门:XOR --- 异或门:XNOR ---同或门:MUX --- 数据选择器: 1.使用一 ...
- webpack4.0.1安装问题和webpack.config.js的配置变化
The CLI moved into a separate package: webpack-cli. Please install 'webpack-cli' in addition to webp ...
- Cron表达式范例
每隔5秒执行一次:*/5 * * * * ? 每隔1分钟执行一次:0 */1 * * * ? 每天23点执行一次:0 0 23 * * ? 每天凌晨1点执行一次:0 0 1 * * ? 每月1号凌晨1 ...
- Mysql事务隔离级别学习
这篇文章主要谈谈Mysql事务隔离级别的区别,以及自己的一些感受. 自己一直以来没搞懂“可重复读”和可提交读“两者之间的区别,通过此次的实践,清楚了两者之间的区别.废话不说,先上图看看这几个事务隔离级 ...