接上一篇当生成下面代码之后怎么去使用呢?
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.bigbird0101names:kind: FoolistKind: FooListplural: foossingular: fooscope: Namespacedversions:- name: v1schema:openAPIV3Schema:description: Foo is a specification for a Foo resourceproperties:apiVersion:description: |-APIVersion defines the versioned schema of this representation of an object.Servers should convert recognized schemas to the latest internal value, andmay reject unrecognized values.More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourcestype: stringkind: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-kindstype: stringmetadata:type: objectspec:description: FooSpec is the spec for a Foo resourceproperties:deploymentName:type: stringreplicas:format: int32type: integerrequired:- deploymentName- replicastype: objectstatus:description: FooStatus is the status for a Foo resourceproperties:availableReplicas:format: int32type: integerrequired:- availableReplicastype: objectrequired:- spec- statustype: objectserved: truestorage: true
在 k8s当中执行一下这个yaml,可以看到已经成功
apiVersion: core.bigbird0101/v1
kind: Foo
metadata:name: test-foo
spec:deploymentName: "test-foo"replicas: 1
status: availableReplicas: 1
再去执行 可以看到已经成功
2.写监听代码
package mainimport (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())
}
可以代码测试
删除资源和创建资源 可以看到都收到了通知