k8s怎么监听自定义资源的变更?(1)

这里我们通过 k8s的 code-generate来生成操作自定义资源的代码来监听变更

第一步下载工具

下载安装 k8s code-generate

  • 查看我们的k8s版本
shell 复制代码
kubectl get node 

输出结果为

可以看到我们的k8s版本为 v1.22.0

所以此时我们要下载与之对应的版本的code-generate

shell 复制代码
git clone https://github.com/kubernetes/code-generator.git -b v0.22.0

第二步编译程序

shell 复制代码
cd code-generate;
go install ./cmd/{client-gen,lister-gen,informer-gen,deepcopy-gen}

第三步 下载对应例子查看

shell 复制代码
git clone https://github.com/kubernetes/sample-controller.git -b v0.22.0

查看文档和目录发现结构如下,重要文件为三个 doc.go register.go types.go

第四步

参照例子 也创建 pkg 目录 apis目录 和 组目录(core)(这个组目录非常重要 下面需要用到) 和 版本目录 (v1) 和三个文件

文件内容如下

core.bigbird0101 为组目录core和模块的前缀 bigbird0101

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 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.
*/

// +k8s:deepcopy-gen=package
// +groupName=core.bigbird0101

// Package v1 is the v1 version of the API.

package v1 // import "bigbird0101/crd-test/pkg/core/v1"
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 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.
*/

package v1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
)

// SchemeGroupVersion is group version used to register these objects
// core为组名称
var SchemeGroupVersion = schema.GroupVersion{Group: "core", Version: "v1"}

// 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 builder
	SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
	// AddToScheme is a global function that registers this API group & version to a scheme
	AddToScheme = SchemeBuilder.AddToScheme
)

// Adds the list of known types to Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
	scheme.AddKnownTypes(SchemeGroupVersion,
		&Foo{},
		&FooList{},
	)
	metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
	return nil
}

根据自己的需要添加types

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 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.
*/

package v1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Foo is a specification for a Foo resource
type Foo struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   FooSpec   `json:"spec"`
	Status FooStatus `json:"status"`
}

// FooSpec is the spec for a Foo resource
type FooSpec struct {
	DeploymentName string `json:"deploymentName"`
	Replicas       *int32 `json:"replicas"`
}

// FooStatus is the status for a Foo resource
type FooStatus struct {
	AvailableReplicas int32 `json:"availableReplicas"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// FooList is a list of Foo resources
type FooList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata"`

	Items []Foo `json:"items"`
}

第五步 执行code-generate 生成命令

shell 复制代码
cd code-generate;
bash generate-groups.sh all bigbird0101/crd-test/pkg/generated bigbird0101/crd-test/pkg/apis core:v1 \
--go-header-file=/code/code-generate/hack/boilerplate.go.txt \
--output-base /code/crd-test

可以看到生成了这个目录 这个目录有我们需要的代码

生成的deepcopy

生成了 clientset 和 informets 和 listers

遇到的坑

  1. doc.go当中的 groupName 和 register.go 中的 group是不一样的,一定要注意 ,
  2. generate-groups 中 的 GroupVersion要与 register.go中的 GroupVersion要保持一致
  3. 目录一样向如下一样 pkg/apis/组名/组版本
相关推荐
hnlucky1 小时前
《基于 Kubernetes 的 WordPress 高可用部署实践:从 MariaDB 到 Nginx 反向代理》
运维·数据库·nginx·云原生·容器·kubernetes·mariadb
_板栗_1 小时前
livenessProbe 和 readinessProbe 最佳实践
云原生·容器·kubernetes
luck_me54 小时前
K8s 图形界面管理kubesphere
云原生·容器·kubernetes
一丝晨光6 小时前
数值溢出保护?数值溢出应该是多少?Swift如何让整数计算溢出不抛出异常?类型最大值和最小值?
java·javascript·c++·rust·go·c·swift
alden_ygq11 小时前
Kubernetes容器运行时:Containerd vs Docker
docker·容器·kubernetes
张青贤14 小时前
K8s中的containerPort与port、targetPort、nodePort的关系:
云原生·容器·kubernetes
小马爱打代码20 小时前
K8S - GitLab CI 自动化构建镜像入门
ci/cd·kubernetes·gitlab
hi,编程哥1 天前
Docker、ECS 与 K8s 网段冲突:解决跨服务通信中的路由问题
docker·容器·kubernetes
Cloud Traveler1 天前
Kubernetes vs. OpenShift:深入比较与架构解析
架构·kubernetes·openshift
杰克逊的日记1 天前
大规模k8s集群怎么规划
云原生·容器·kubernetes