packagev1alpha1import(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
typeFoostruct{metav1.TypeMeta`json:",inline"`metav1.ObjectMeta`json:"metadata,omitempty"`SpecFooSpec`json:"spec"`StatusFooStatus`json:"status"`}// FooSpec is the spec for a Foo resource
// FooSpec 定义
typeFooSpecstruct{DeploymentNamestring`json:"deploymentName"`Replicas*int32`json:"replicas"`}// FooStatus is the status for a Foo resource
typeFooStatusstruct{AvailableReplicasint32`json:"availableReplicas"`}// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// FooList is a list of Foo resources
typeFooListstruct{metav1.TypeMeta`json:",inline"`metav1.ListMeta`json:"metadata"`Items[]Foo`json:"items"`}
// 为包中任何类型生成深拷贝方法,可以在局部tag覆盖此默认行为
// +groupName=example.com
// +k8s:deepcopy-gen=package
// +groupName=samplecontroller.k8s.io // groupName指定API组的全限定名
// Package v1alpha1 is the v1alpha1 version of the API.
packagev1alpha1// import "k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1"
Usage: generate-groups.sh <generators> <output-package> <apis-package> <groups-versions> ...
<generators> the generators comma separated to run (deepcopy,defaulter,client,lister,informer) or "all".
<output-package> the output package name (e.g. github.com/example/project/pkg/generated).
<apis-package> the external types dir (e.g. github.com/example/api or github.com/example/project/pkg/apis).
<groups-versions> the groups and their versions in the format "groupA:v1,v2 groupB:v1 groupC:v2", relative
to <api-package>.
... arbitrary flags passed to all generator binaries.
Examples:
generate-groups.sh all github.com/example/project/pkg/client github.com/example/project/pkg/apis "foo:v1 bar:v1alpha1,v1beta1"
generate-groups.sh deepcopy,client github.com/example/project/pkg/client github.com/example/project/pkg/apis "foo:v1 bar:v1alpha1,v1beta1"
// FooSpec is the spec for a Foo resourcetype FooSpec struct {Fields map[string]interface{} `json:"fields"`}
当在代码生成的时候,发现会报错:
1
2
3
4
5
6
Generating deepcopy funcsF0518 17:53:33.568567 39986 deepcopy.go:750] DeepCopy of "interface{}" is unsupported. Instead, use named interfaces with DeepCopy<named-interface> as one of the methods.goroutine 1 [running]:k8s.io/klog/v2.stacks(0xc000132001, 0xc0002e9000, 0xad, 0xfd)/Users/silenceper/workspace/golang/pkg/mod/k8s.io/klog/v2@v2.0.0/klog.go:972 +0xb8......
funcNewController(kubeclientsetkubernetes.Interface,sampleclientsetclientset.Interface,deploymentInformerappsinformers.DeploymentInformer,fooInformerinformers.FooInformer)*Controller{....fooInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{AddFunc:controller.enqueueFoo,UpdateFunc:func(old,newinterface{}){controller.enqueueFoo(new)},})....}func(c*Controller)Run(threadinessint,stopCh<-chanstruct{})error{// Launch two workers to process Foo resources
fori:=0;i<threadiness;i++{gowait.Until(c.runWorker,time.Second,stopCh)}}func(c*Controller)runWorker(){forc.processNextWorkItem(){}}func(c*Controller)processNextWorkItem()bool{obj,shutdown:=c.workqueue.Get()...err:=func(objinterface{})error{...// Run the syncHandler, passing it the namespace/name string of the
// Foo resource to be synced.
iferr:=c.syncHandler(key);err!=nil{// Put the item back on the workqueue to handle any transient errors.
c.workqueue.AddRateLimited(key)returnfmt.Errorf("error syncing '%s': %s, requeuing",key,err.Error())}...}(obj)...}func(c*Controller)syncHandler(keystring)error{// Convert the namespace/name string into a distinct namespace and name
namespace,name,err:=cache.SplitMetaNamespaceKey(key)iferr!=nil{utilruntime.HandleError(fmt.Errorf("invalid resource key: %s",key))returnnil}//TODO 处理逻辑
}func(c*Controller)enqueueFoo(objinterface{}){...c.workqueue.Add(key)}
exampleClient,err:=clientset.NewForConfig(cfg)iferr!=nil{klog.Fatalf("Error building example clientset: %s",err.Error())}kubeInformerFactory:=kubeinformers.NewSharedInformerFactory(kubeClient,time.Second*30)exampleInformerFactory:=informers.NewSharedInformerFactory(exampleClient,time.Second*30)controller:=NewController(kubeClient,exampleClient,kubeInformerFactory.Apps().V1().Deployments(),exampleInformerFactory.Samplecontroller().V1alpha1().Foos())// notice that there is no need to run Start methods in a separate goroutine. (i.e. go kubeInformerFactory.Start(stopCh)
// Start method is non-blocking and runs all registered informers in a dedicated goroutine.
kubeInformerFactory.Start(stopCh)exampleInformerFactory.Start(stopCh)//启动informer