Development kit for building Kubernetes extensions and tools.
Provides libraries and tools to create new projects, APIs and controllers. Includes tools for packaging artifacts into an installer container.
Typical project lifecycle:
- initialize a project:
kubebuilder init --domain example.com --license apache2 --owner "The Kubernetes authors"
- create one or more a new resource APIs and add your code to them:
kubebuilder create api --group <group> --version <version> --kind <Kind>
Create resource will prompt the user forif it should scaffold the Resource and / or Controller. To only scaffold a Controller for an existing Resource, select "n"for Resource. To only define the schema for a Resource without writing a Controller, select "n"for Controller.
After the scaffold is written, api will run make on the project.
Usage: kubebuilder [command]
Available Commands: create Scaffold a Kubernetes API or webhook. edit This command will edit the project configuration help Help about any command init Initialize a new project version Print the kubebuilder version
Flags: -h, --helphelpfor kubebuilder
Use "kubebuilder [command] --help"for more information about a command.
go mod init github.com/huweihuang/operator-example
2.2. 创建项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# kubebuilder init --domain github.com --license apache2 --owner "Hu Weihuang" Writing scaffold for you to edit... Get controller runtime: $ go get sigs.k8s.io/controller-runtime@v0.5.0 Update go.mod: $ go mod tidy Running make: $ make go: creating new go.mod: module tmp go: finding sigs.k8s.io v0.2.5 go: finding sigs.k8s.io/controller-tools/cmd v0.2.5 go: finding sigs.k8s.io/controller-tools/cmd/controller-gen v0.2.5 /Users/weihuanghu/go/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..." go fmt ./... go vet ./... go build -o bin/manager main.go Next: define a resource with: $ kubebuilder create api
// GuestbookSpec defines the desired state of Guestbook type GuestbookSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file
// Quantity of instances // +kubebuilder:validation:Minimum=1 // +kubebuilder:validation:Maximum=10 Size int32`json:"size"`
// Name of the ConfigMap for GuestbookSpec's configuration // +kubebuilder:validation:MaxLength=15 // +kubebuilder:validation:MinLength=1 ConfigMapName string`json:"configMapName"`
// +kubebuilder:validation:Enum=Phone;Address;Name Type string`json:"alias,omitempty"` }
// GuestbookStatus defines the observed state of Guestbook type GuestbookStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file
// PodName of the active Guestbook node. Active string`json:"active"`
// PodNames of the standby Guestbook nodes. Standby []string`json:"standby"` }
// Guestbook is the Schema for the guestbooks API type Guestbook struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"`
Spec GuestbookSpec `json:"spec,omitempty"` Status GuestbookStatus `json:"status,omitempty"` }
3. troubleshooting
3.1. controller-gen: No such file or directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
➜ operator-example kubebuilder init --domain github.com --license apache2 --owner "Hu Weihuang" Writing scaffold for you to edit... Get controller runtime: $ go get sigs.k8s.io/controller-runtime@v0.5.0 Update go.mod: $ go mod tidy Running make: $ make go: creating new go.mod: module tmp go: finding sigs.k8s.io v0.2.5 go: finding sigs.k8s.io/controller-tools/cmd v0.2.5 go: finding sigs.k8s.io/controller-tools/cmd/controller-gen v0.2.5 /Users/weihuanghu/go:/Users/weihuanghu/k8spath/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..." /bin/sh: /Users/weihuanghu/go:/Users/weihuanghu/k8spath/bin/controller-gen: No such file or directory make: *** [generate] Error 127 2020/04/13 14:34:47 failed to initialize project: exit status 2