What is CRD
The CustomResourceDefinition API resource allows you to define custom resources. Defining a CRD object creates a new custom resource with a name and schema that you specify. The Kubernetes API serves and handles the storage of your custom resource. The name of a CRD object must be a valid DNS subdomain name. more detailed info please refer to CRD definition
What is operator-sdk
The SDK provides the tools to build, test and package Operators. Initially, the SDK facilitates the marriage of an application’s business logic (for example, how to scale, upgrade, or backup) with the Kubernetes API to execute those operations. Over time, the SDK can allow engineers to make applications smarter and have the user experience of cloud services. Leading practices and code patterns that are shared across Operators are included in the SDK to help prevent reinventing the wheel.
User guide
This is an official user guide. please reference the user guide.
Installation guide
1. Download the release binary
Install CLI in Linux env.
# Set the release version variable
$ RELEASE_VERSION=v0.16.0
# Linux
$ curl -LO https://github.com/operator-framework/operator-sdk/releases/download/${RELEASE_VERSION}/operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu
2. Verify the downloaded release binary
# Linux
$ curl -LO https://github.com/operator-framework/operator-sdk/releases/download/${RELEASE_VERSION}/operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu.asc
3. To verify a release binary using the provided asc files, place the binary and corresponding asc file into the same directory and use the corresponding command:
# Linux
$ gpg --verify operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu.asc
If you do not have the maintainers public key on your machine, you will get an error message similar to this:
$ gpg --verify operator-sdk-${RELEASE_VERSION}-x86_64-apple-darwin.asc
$ gpg: assuming signed data in 'operator-sdk-${RELEASE_VERSION}-x86_64-apple-darwin'
$ gpg: Signature made Fri Apr 5 20:03:22 2019 CEST
$ gpg: using RSA key <KEY_ID>
$ gpg: Can't check signature: No public key
To download the key, use the following command, replacing $KEY_ID with the RSA key string provided in the output of the previous command:
$ gpg --recv-key "$KEY_ID"
You'll need to specify a key server if one hasn't been configured. For example:
$ gpg --keyserver keyserver.ubuntu.com --recv-key "$KEY_ID"
Now you should be able to verify the binary.
4. Install the release binary in your PATH
# Linux
$ chmod +x operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu && sudo mkdir -p /usr/local/bin/ && sudo cp operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu /usr/local/bin/operator-sdk && rm operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu
This is an official operator-sdk installation guide. more information please refer to installation guide.
Example
-
Installation operator-sdk CLI.
please reference installation guide section.
-
Using operator-sdk to generate a CRD.
-
Generate a common framwork
operator-sdk new repo_name --repo=repo_name --vendor=true
-
New a API
-
Generate a API
operator-sdk add api --api-version=xxxx/v1alpha1 --kind=$kind
-
update the API contents then using “operator-sdk generate k8s” and “operator-sdk generate crds” commands to update your code and deploy.
-
-
New a controller
operator-sdk add controller --api-version=xxxx/v1alpha1 --kind=$kind
-
-
Generate a clientset
-
Get code-generator
go get k8s.io/code-generator
-
Add following annotations in your xxx_types.go file
// +genclient // +genclient:noStatus // +genclient:onlyVerbs=create,delete,update,get // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // Xxxx is the Schema for the xxxxs API // +kubebuilder:subresource:status // +kubebuilder:resource:path=xxxxs,scope=Namespaced type Xxxx struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` Spec XxxxSpec `json:"spec,omitempty"` Status XxxxStatus `json:"status,omitempty"` }
-
Generate a clientset
$GOPATH/src/k8s.io/code-generator/generate-groups.sh client repo/pkg/client repo/pkg/apis xxxx:v1alpha1
-
deploy and test it.
-