为什么下这篇文章呢,在代码生成这一块跌了两次跟头,说白了只知道照抄,并不知道里面到底是干什么的,要做到知其然并知其所以然,也是为以后在自动生成代码的时候做一个参考。

生成的项目目录结构如下

.
├── go.mod
├── go.sum
├── hack
│   ├── boilerplate.go.txt
│   ├── update-codegen.sh
│   └── verify-codegen.sh
└── pkg├── apis│   └── mycontroller│       ├── register.go│       └── v1alpha1│           ├── 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│           └── mycontroller│               └── v1alpha1│                   ├── doc.go│                   ├── fake│                   │   ├── doc.go│                   │   ├── fake_mycontroller_client.go│                   │   └── fake_website.go│                   ├── generated_expansion.go│                   ├── mycontroller_client.go│                   └── website.go├── informers│   └── externalversions│       ├── factory.go│       ├── generic.go│       ├── internalinterfaces│       │   └── factory_interfaces.go│       └── mycontroller│           ├── interface.go│           └── v1alpha1│               ├── interface.go│               └── website.go└── listers└── mycontroller└── v1alpha1├── expansion_generated.go└── website.go

代码生成需要的文件

├── go.mod
├── go.sum
├── hack
│   ├── boilerplate.go.txt
│   ├── update-codegen.sh
│   └── verify-codegen.sh
└── pkg├── apis└── mycontroller├── register.go└── v1alpha1├── doc.go├── register.go├── types.go

hack目录下面的文件和代码生成有关
1、update-codegen.sh文件详解

#!/usr/bin/env bash# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.set -o errexit
set -o nounset
set -o pipefailSCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${SCRIPT_ROOT}; ls -d -1 .k8s.io/code-generator 2>/dev/null || echo ../code-generator)}# generate the code with:
# - --output-base because this script should also be able to run inside the vendor dir of
#   k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
#   instead of the $GOPATH directly. For normal projects this can be dropped.
# "$(dirname "${BASH_SOURCE[0]}")"/../generate-internal-groups.sh all \
#  k8s.io/code-generator/_examples/apiserver k8s.io/code-generator/_examples/apiserver/apis k8s.io/code-generator/_examples/apiserver/apis \
#  "example:v1 example2:v1 example3.io:v1" \
#  --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
#  --go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
# "$(dirname "${BASH_SOURCE[0]}")"/../generate-groups.sh all \
#  k8s.io/code-generator/_examples/crd k8s.io/code-generator/_examples/crd/apis \
#  "example:v1 example2:v1" \
#  --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
#  --go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
# "$(dirname "${BASH_SOURCE[0]}")"/../generate-groups.sh all \
#  k8s.io/code-generator/_examples/MixedCase k8s.io/code-generator/_examples/MixedCase/apis \
#  "example:v1" \
#  --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
#  --go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
# "$(dirname "${BASH_SOURCE[0]}")"/../generate-groups.sh all \
#  k8s.io/code-generator/_examples/HyphenGroup k8s.io/code-generator/_examples/HyphenGroup/apis \
#  "example:v1" \
#  --output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
#  --go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"
${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" \website/pkg/client website/pkg/apis \mycontroller:v1alpha1 \--output-base "$GOPATH/src" \--go-header-file ${SCRIPT_ROOT}/hack/boilerplate.go.txt

CODEGEN_PKG的值到该是在k8s.io/code-generator
website/pkg/client website/pkg/apis:表示个是你要生成代码的目录,目录的名称是client
mycontroller:v1alpha1:表示的是应用组名:版本
–output-base:表示生成文件的输出位置

2、mycontroller下面的register.go

/*
Copyright 2017 The Kubernetes Authors.Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mycontroller// GroupName is the group name used in this package// 上面那些注释啊,不能删啊,我搞了一下午,就是把上面的删除掉了
// 组名 mycontroller.xialee.io
const (GroupName = "mycontroller.xialee.io"
)

3、v1alpha1包下面的register.go文件

package v1alpha1import (metav1 "k8s.io/apimachinery/pkg/apis/meta/v1""k8s.io/apimachinery/pkg/runtime""k8s.io/apimachinery/pkg/runtime/schema""website/pkg/apis/mycontroller"
)var SchemeGroupVersion = schema.GroupVersion{Group:   mycontroller.GroupName,Version: "v1alpha1",
}// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {return SchemeGroupVersion.WithKind(kind).GroupKind()
}// Resource takes an unqualified resource and returns a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {return SchemeGroupVersion.WithResource(resource).GroupResource()
}var (// SchemeBuilder initializes a scheme builderSchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)// AddToScheme is a global function that registers this API group & version to a schemeAddToScheme = SchemeBuilder.AddToScheme
)// Adds the list of known types to Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {scheme.AddKnownTypes(SchemeGroupVersion,&Website{},&WebsiteList{},)metav1.AddToGroupVersion(scheme, SchemeGroupVersion)return nil
}

4、v1alpha1包下面的doc.go文件

// +k8s:deepcopy-gen=package
// +groupName=mycontroller.xialee.io// Package v1alpha1 is the v1alpha1 version of the API.
package v1alpha1

5、v1alpha1包下面的types.go文件

package v1alpha1import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Objecttype Website struct {metav1.TypeMeta   `json:",inline"`metav1.ObjectMeta `json:"metadata,omitempty"`Spec   WebsiteSpec   `json:"spec"`Status WebsiteStatus `json:"status"`
}type WebsiteSpec struct {GitRepo        string `json:"gitRepo"`DeploymentName string `json:"deploymentName"`Replicas       *int32 `json:"replicas"`// TargetDeployment string `json:"targetDeployment"`// MinReplicas      int    `json:"minReplicas"`// MaxReplicas      int    `json:"maxReplicas"`// MetricType       string `json:"metricType"`// Step             int    `json:"step"`// ScaleUp          int    `json:"scaleUp"`// ScaleDown        int    `json:"scaleDown"`
}type WebsiteStatus struct {AvailableReplicas int32 `json:"availableReplicas"`
}// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Objecttype WebsiteList struct {metav1.TypeMeta `json:",inline"`metav1.ListMeta `json:"metadata,omitempty"`Items []Website `json:"items"`
}

谨记:上面的注释不要随便删除

执行自动生成代码脚本

1、首先将自己的项目上传到gopath下面的src里面,(前提go环境已经安装好了),进入website目录里面,执行go mod init,会生成一个go.mod文件,在里面添加依赖的包require ( k8s.io/api v0.0.0-20191031065753-b19d8caf39be k8s.io/apimachinery v0.0.0-20191105185716-00d39968b57e k8s.io/client-go v0.0.0-20191101230044-e9766ae82012 k8s.io/code-generator v0.0.0-20191029223907-9f431a56fdbc k8s.io/klog v1.0.0 k8s.io/sample-controller v0.0.0-20191101231324-472018681a2b )然后执行go mod download下载相关的依赖包,然后执行sh hack/update-codegen.sh,正常输出,则证明自动生成代码成功

k8s的自定义资源代码生成详解相关推荐

  1. k8s kubectl run命令使用详解

    k8s kubectl run命令使用详解 在集群上运行指定镜像. 摘要 创建并运行一个指定的可复制的镜像. 创建一个deployment或者job来管理创建的容器. kubectl run NAME ...

  2. openstack ice自定义调度算法项目详解(horizon、novaclient、api、scheduler、db、自定义数据库)

    原文转自:openstack ice自定义调度算法项目详解(horizon.novaclient.api.scheduler.db.自定义数据库) 第一部分:页面层即horizon与novaclien ...

  3. SpringSecurity权限管理框架系列(六)-Spring Security框架自定义配置类详解(二)之authorizeRequests配置详解

    1.预置演示环境 这个演示环境继续沿用 SpringSecurit权限管理框架系列(五)-Spring Security框架自定义配置类详解(一)之formLogin配置详解的环境. 2.自定义配置类 ...

  4. [Python图像识别] 五十.Keras构建AlexNet和CNN实现自定义数据集分类详解

    该系列文章是讲解Python OpenCV图像处理知识,前期主要讲解图像入门.OpenCV基础用法,中期讲解图像处理的各种算法,包括图像锐化算子.图像增强技术.图像分割等,后期结合深度学习研究图像识别 ...

  5. 【k8s】八、Pod详解(二)

    目录 前言 Pod网络通讯方式 不同情况下的网络通讯方式 同一个Pod内部通讯 Pod间的通讯 Pod 与 Service之间的通讯 Pod与外网通讯 Pod到外网 外网到Pod CNI 什么是CNI ...

  6. object类中的equals与自定义equals方法详解

    object类中的equals与自定义equal方法详解 1.this怎么理解?this == obj表示什么? this就是当前你new出来的对象,这里指谁调用equal方法this指的就是谁,ob ...

  7. android标尺自定义view,android尺子的自定义view——RulerView详解

    项目中用到自定义尺子的样式: 原效果为 因为跟自己要使用的view稍有不同 所以做了一些修改,修改的注释都放在代码中了,特此记录一下. 首先是一个自定义View: public class RuleV ...

  8. k8s kubectl create命令使用详解

    k8s kubectl create命令使用详解 Create a resource by filename or stdin 摘要 Create a resource by filename or ...

  9. mysql: union / union all / 自定义函数用法详解

    mysql: union / union all http://www.cnblogs.com/wangyayun/p/6133540.html mysql:自定义函数用法详解 http://www. ...

最新文章

  1. 国防科大计算机考研大纲,2022年国防科技大学F1003计算机操作系统考研大纲及参考书目...
  2. python模块学习(四)
  3. 取KindEditor中的textarea的值区不到的解决方案,固定kindEditor的高度
  4. 4.有关日期格式属性修改常识,v$nls_parameters,between and,查询指定部门的员工信息,in和null,like模糊查询,order by后面可以跟:列名、表达式、别名、序号
  5. 【摄影测量原理】第三章:双像立体测图
  6. NOIP2017洛谷P3953:逛公园(分层图最短路、dp、拓扑)
  7. MongoDB学习笔记-06 数据库命令、固定集合、GridFS、javascript脚本
  8. 国内2大Git代码托管网站
  9. Pytorch:保存图片
  10. 基于arduino的光控窗帘_分别基于STM32和Arduino的智能窗帘硬件分析与程序设计
  11. JS实现监控微信小程序
  12. vue 中引入使用jquery
  13. 《钢构CAD》不断致力于帮助用户取得更多成就
  14. 山东理工大学2021年下半年实验室安全考试
  15. no serializer
  16. 肖文吉mysql_疯狂软件教育中心肖文吉老师_MYSQL视频教程
  17. P.W.N. CTF - MISC - Canadian FOI
  18. ROS笔记(一)xxx.launch文件详解
  19. 中小卖家电商节恐惧症:你们剁手,我们割肉 2017-10-27 09:00 稿源:懂懂笔记 0条评论 撤稿纠错 “其实对一部分我们这样的中小卖家来说,造节就是煎熬。” 在某大型电商平台上拥有两家
  20. 操作系统:Win10有哪些版本,看完你就知道了

热门文章

  1. Java线程池的使用方法
  2. android 空间分享到朋友圈,Android开发之微信分享到好友,朋友圈
  3. 20210429 蜗蜗安华Openmp 高精度PI计算
  4. 名帖175 苏轼 行书《寒食帖》
  5. 初三计算机模拟考试在房考吗,中考临近,初三学生想知道中考难还是模拟考试难,你知道哪个难吗...
  6. 写给初三备考职高的同学
  7. 解决报错TypeError: people() takes no arguments
  8. MySQL 基础 -- 约束(非空约束、唯一约束、主键约束、默认约束、检查约束、外键约束)
  9. 转行做程序员之前你应该考虑的三件事
  10. 梯度下降算法原理及其计算过程