/* 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. */
// MyConfig is a specification for a MyConfig resource type MyConfig struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"`
Spec MyConfigSpec `json:"spec"` }
// MyConfigSpec is the spec for a MyConfig resource type MyConfigSpec struct { ConfigName string`json:"configName"` ConfigValue string`json:"configValue"` }
// MyConfigList is a list of MyConfig resources type MyConfigList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata"` Items []MyConfig `json:"items"` }
/* 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. */
/* 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. */
// SchemeGroupVersion is group version used to register these objects var SchemeGroupVersion = schema.GroupVersion{Group: "democontroller.github.com.liuyehcf", Version: "v1"}
// Kind takes an unqualified kind and returns back a Group qualified GroupKind funcKind(kind string) schema.GroupKind { return SchemeGroupVersion.WithKind(kind).GroupKind() }
// Resource takes an unqualified resource and returns a Group qualified GroupResource funcResource(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. funcaddKnownTypes(scheme *runtime.Scheme)error { scheme.AddKnownTypes(SchemeGroupVersion, &MyConfig{}, &MyConfigList{}, ) metav1.AddToGroupVersion(scheme, SchemeGroupVersion) returnnil }
cd$GOPATH mkdir -p src/k8s.io cd src/k8s.io git clone https://github.com/kubernetes/code-generator.git
然后执行脚本
1 2
cd <工程目录> bash hack/update-codegen.sh
输出如下
1 2 3 4
Generating deepcopy funcs Generating clientset for democontroller:v1 at github.com/liuyehcf/demo-controller/pkg/generated/clientset Generating listers for democontroller:v1 at github.com/liuyehcf/demo-controller/pkg/generated/listers Generating informers for democontroller:v1 at github.com/liuyehcf/demo-controller/pkg/generated/informers
import ( "flag" "fmt" clientset "github.com/liuyehcf/demo-controller/pkg/generated/clientset/versioned" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/clientcmd" "k8s.io/klog" // Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters). // _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
var ( masterURL string kubeconfig string )
funcmain() { //klog.InitFlags(nil) //flag.Parse() // //// set up signals so we handle the first shutdown signal gracefully //stopCh := signals.SetupSignalHandler() // //cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig) //if err != nil { // klog.Fatalf("Error building kubeconfig: %s", err.Error()) //} // //kubeClient, err := kubernetes.NewForConfig(cfg) //if err != nil { // klog.Fatalf("Error building kubernetes clientset: %s", err.Error()) //} // //exampleClient, err := clientset.NewForConfig(cfg) //if err != 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 := controllers.NewController(kubeClient, exampleClient, // exampleInformerFactory.Democontroller().V1().MyConfigs()) // //// 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) // //if err = controller.Run(2, stopCh); err != nil { // klog.Fatalf("Error running controller: %s", err.Error()) //}
klog.InitFlags(nil) flag.Parse()
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig) if err != nil { klog.Fatalf("Error building kubeconfig: %s", err.Error()) return }
exampleClient, err := clientset.NewForConfig(cfg) if err != nil { klog.Fatalf("Error building example clientset: %s", err.Error()) return }
funcinit() { flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") }