接上一篇当生成下面代码之后怎么去使用呢?
1.生成crd文件
这里我们通过kubebuilder的一个子项目 controller-gen 来生成crd文件
https://github.com/kubernetes-sigs/controller-tools
curl -L -o https://github.com/kubernetes-sigs/controller-tools;
go install controller-tools/cmd/controller-gen;
controller-gen crd paths=./pkg/apis/... output:crd:dir=config/crd
可以看到已经生成成功了。
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: (devel)
name: foos.core.bigbird0101
spec:
group: core.bigbird0101
names:
kind: Foo
listKind: FooList
plural: foos
singular: foo
scope: Namespaced
versions:
- name: v1
schema:
openAPIV3Schema:
description: Foo is a specification for a Foo resource
properties:
apiVersion:
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
spec:
description: FooSpec is the spec for a Foo resource
properties:
deploymentName:
type: string
replicas:
format: int32
type: integer
required:
- deploymentName
- replicas
type: object
status:
description: FooStatus is the status for a Foo resource
properties:
availableReplicas:
format: int32
type: integer
required:
- availableReplicas
type: object
required:
- spec
- status
type: object
served: true
storage: true
在 k8s当中执行一下这个yaml,可以看到已经成功
apiVersion: core.bigbird0101/v1
kind: Foo
metadata:
name: test-foo
spec:
deploymentName: "test-foo"
replicas: 1
status:
availableReplicas: 1
再去执行 可以看到已经成功
2.写监听代码
package main
import (
v1 "bigbird0101/crd-test/pkg/apis/core/v1"
"bigbird0101/crd-test/pkg/generated/clientset/versioned"
"bigbird0101/crd-test/pkg/generated/informers/externalversions"
"context"
"fmt"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
flags, _ := clientcmd.BuildConfigFromFlags("", clientcmd.RecommendedHomeFile)
client, _ := versioned.NewForConfig(flags)
factory := externalversions.NewSharedInformerFactoryWithOptions(client, 0,externalversions.WithNamespace("default"))
informer := factory.Core().V1().Foos().Informer()
_, err := informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
foo := obj.(*v1.Foo)
fmt.Printf("foo added: %v\n", foo)
},
UpdateFunc: func(oldObj, newObj interface{}) {
foo := oldObj.(*v1.Foo)
fmt.Printf("foo UpdateFunc: %v\n", foo)
foo2 := newObj.(*v1.Foo)
fmt.Printf("foo UpdateFunc: %v\n", foo2)
},
DeleteFunc: func(obj interface{}) {
foo := obj.(*v1.Foo)
fmt.Printf("foo Deleted: %v\n", foo)
},
})
if err != nil {
fmt.Println(err)
return
}
informer.Run(context.Background().Done())
}
可以代码测试
删除资源和创建资源 可以看到都收到了通知