This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.4.9. Operator SDK CLI reference
This guide documents the Operator SDK CLI commands and their syntax:
operator-sdk <command> [<subcommand>] [<argument>] [<flags>]
$ operator-sdk <command> [<subcommand>] [<argument>] [<flags>]
4.9.1. build 复制链接链接已复制到粘贴板!
The operator-sdk build
command compiles the code and builds the executables. After build
completes, the image is built using a local container engine. It must then be pushed to a remote registry.
Argument | Description |
---|---|
|
The container image to be built, for example |
Flag | Description |
---|---|
| Enable in-cluster testing by adding test binary to the image. |
|
Path of namespaced resources manifest for tests. Default: |
|
Location of tests. Default: |
| Usage help output. |
If --enable-tests
is set, the build
command also builds the testing binary, adds it to the container image, and generates a deploy/test-pod.yaml
file that allows a user to run the tests as a pod on a cluster.
For example:
operator-sdk build quay.io/example/operator:v0.0.1
$ operator-sdk build quay.io/example/operator:v0.0.1
Example output
4.9.2. completion 复制链接链接已复制到粘贴板!
The operator-sdk completion
command generates shell completions to make issuing CLI commands quicker and easier.
Subcommand | Description |
---|---|
| Generate bash completions. |
| Generate zsh completions. |
Flag | Description |
---|---|
| Usage help output. |
For example:
operator-sdk completion bash
$ operator-sdk completion bash
Example output
bash completion for operator-sdk -*- shell-script -*- ex: ts=4 sw=4 et filetype=sh
# bash completion for operator-sdk -*- shell-script -*-
...
# ex: ts=4 sw=4 et filetype=sh
4.9.3. print-deps 复制链接链接已复制到粘贴板!
The operator-sdk print-deps
command prints the most recent Golang packages and versions required by Operators. It prints in columnar format by default.
Flag | Description |
---|---|
|
Print packages and versions in |
For example:
operator-sdk print-deps --as-file
$ operator-sdk print-deps --as-file
Example output
4.9.4. generate 复制链接链接已复制到粘贴板!
The operator-sdk generate
command invokes a specific generator to generate code as needed.
4.9.4.1. crds 复制链接链接已复制到粘贴板!
The generate crds
subcommand generates custom resource definitions (CRDs) or updates them if they exist, under deploy/crds/__crd.yaml
. OpenAPI V3 validation YAML is generated as a validation
object.
Flag | Description |
---|---|
|
CRD version to generate. Default: |
|
Help for |
For example:
operator-sdk generate crds
$ operator-sdk generate crds
tree deploy/crds
$ tree deploy/crds
Example output
├── deploy/crds/app.example.com_v1alpha1_appservice_cr.yaml └── deploy/crds/app.example.com_appservices_crd.yaml
├── deploy/crds/app.example.com_v1alpha1_appservice_cr.yaml
└── deploy/crds/app.example.com_appservices_crd.yaml
4.9.4.2. csv 复制链接链接已复制到粘贴板!
The csv
subcommand writes a cluster service version (CSV) manifest for use with Operator Lifecycle Manager (OLM). It also optionally writes CRD files to deploy/olm-catalog/<operator_name>/<csv_version>
.
Flag | Description |
---|---|
| The channel the CSV should be registered under in the package manifest. |
|
The path to the CSV configuration file. Default: |
| The semantic version of the CSV manifest. Required. |
|
Use the channel passed to |
| The semantic version of CSV manifest to use as a base for a new version. |
| The Operator name to use while generating the CSV. |
|
Updates CRD manifests in |
For example:
operator-sdk generate csv \ --csv-version 0.1.0 \ --update-crds
$ operator-sdk generate csv \
--csv-version 0.1.0 \
--update-crds
Example output
4.9.4.3. k8s 复制链接链接已复制到粘贴板!
The k8s
subcommand runs the Kubernetes code-generators for all CRD APIs under pkg/apis/
. Currently, k8s
only runs deepcopy-gen
to generate the required DeepCopy()
functions for all custom resource (CR) types.
This command must be run every time the API (spec
and status
) for a custom resource type is updated.
For example:
tree pkg/apis/app/v1alpha1/
$ tree pkg/apis/app/v1alpha1/
Example output
pkg/apis/app/v1alpha1/ ├── appservice_types.go ├── doc.go └── register.go
pkg/apis/app/v1alpha1/
├── appservice_types.go
├── doc.go
└── register.go
operator-sdk generate k8s
$ operator-sdk generate k8s
Example output
Running code-generation for Custom Resource (CR) group versions: [app:v1alpha1] Generating deepcopy funcs
Running code-generation for Custom Resource (CR) group versions: [app:v1alpha1]
Generating deepcopy funcs
tree pkg/apis/app/v1alpha1/
$ tree pkg/apis/app/v1alpha1/
Example output
pkg/apis/app/v1alpha1/ ├── appservice_types.go ├── doc.go ├── register.go └── zz_generated.deepcopy.go
pkg/apis/app/v1alpha1/
├── appservice_types.go
├── doc.go
├── register.go
└── zz_generated.deepcopy.go
4.9.5. new 复制链接链接已复制到粘贴板!
The operator-sdk new
command creates a new Operator application and generates (or scaffolds) a default project directory layout based on the input <project_name>
.
Argument | Description |
---|---|
| Name of the new project. |
Flag | Description |
---|---|
|
CRD API version in the format |
|
Generate an Ansible playbook skeleton. Used with |
|
Path to file containing headers for generated Go files. Copied to |
|
Initialize Helm Operator with existing Helm chart: |
| Chart repository URL for the requested Helm chart. |
| Specific version of the Helm chart. Default: latest version. |
| Usage and help output. |
|
CRD kind, for example |
| Do not initialize the directory as a Git repository. |
|
Type of Operator to initialize: |
Starting with Operator SDK v0.12.0, the --dep-manager
flag and support for dep
-based projects have been removed. Go projects are now scaffolded to use Go modules.
Example usage for Go project
mkdir $GOPATH/src/github.com/example.com/
$ mkdir $GOPATH/src/github.com/example.com/
cd $GOPATH/src/github.com/example.com/
$ cd $GOPATH/src/github.com/example.com/
operator-sdk new app-operator
$ operator-sdk new app-operator
Example usage for Ansible project
operator-sdk new app-operator \ --type=ansible \ --api-version=app.example.com/v1alpha1 \ --kind=AppService
$ operator-sdk new app-operator \
--type=ansible \
--api-version=app.example.com/v1alpha1 \
--kind=AppService
4.9.6. add 复制链接链接已复制到粘贴板!
The operator-sdk add
command adds a controller or resource to the project. The command must be run from the Operator project root directory.
Subcommand | Description |
---|---|
|
Adds a new API definition for a new custom resource (CR) under |
|
Adds a new controller under |
|
Adds a CRD and the CR files. The
|
Flag | Description |
---|---|
|
CRD API version in the format |
|
CRD |
For example:
operator-sdk add api \ --api-version app.example.com/v1alpha1 \ --kind AppService
$ operator-sdk add api \
--api-version app.example.com/v1alpha1 \
--kind AppService
Example output
tree pkg/apis
$ tree pkg/apis
Example output
operator-sdk add controller \ --api-version app.example.com/v1alpha1 \ --kind AppService
$ operator-sdk add controller \
--api-version app.example.com/v1alpha1 \
--kind AppService
Example output
Create pkg/controller/appservice/appservice_controller.go Create pkg/controller/add_appservice.go
Create pkg/controller/appservice/appservice_controller.go
Create pkg/controller/add_appservice.go
tree pkg/controller
$ tree pkg/controller
Example output
pkg/controller/ ├── add_appservice.go ├── appservice │ └── appservice_controller.go └── controller.go
pkg/controller/
├── add_appservice.go
├── appservice
│ └── appservice_controller.go
└── controller.go
operator-sdk add crd \ --api-version app.example.com/v1alpha1 \ --kind AppService
$ operator-sdk add crd \
--api-version app.example.com/v1alpha1 \
--kind AppService
Example output
Generating Custom Resource Definition (CRD) files Create deploy/crds/app_v1alpha1_appservice_crd.yaml Create deploy/crds/app_v1alpha1_appservice_cr.yaml
Generating Custom Resource Definition (CRD) files
Create deploy/crds/app_v1alpha1_appservice_crd.yaml
Create deploy/crds/app_v1alpha1_appservice_cr.yaml
4.9.7. test 复制链接链接已复制到粘贴板!
The operator-sdk test
command can test the Operator locally.
4.9.7.1. local 复制链接链接已复制到粘贴板!
The local
subcommand runs Go tests built using the test framework of the Operator SDK locally.
Arguments | Description |
---|---|
|
Location of end-to-end (e2e) test files, for example |
Flags | Description |
---|---|
|
Location of |
|
Path to manifest for global resources. Default: |
|
Path to manifest for per-test, namespaced resources. Default: combines |
|
If non-empty, a single namespace to run tests in, for example |
|
Extra arguments to pass to |
|
Enable running the Operator locally with |
| Disable test resource creation. |
| Use a different Operator image from the one specified in the namespaced manifest. |
| Usage help output. |
For example:
operator-sdk test local ./test/e2e/
$ operator-sdk test local ./test/e2e/
Example output
ok github.com/operator-framework/operator-sdk-samples/memcached-operator/test/e2e 20.410s
ok github.com/operator-framework/operator-sdk-samples/memcached-operator/test/e2e 20.410s
4.9.8. run 复制链接链接已复制到粘贴板!
The operator-sdk run
command provides options that can launch the Operator in various environments.
Arguments | Description |
---|---|
|
The file path to a Kubernetes configuration file. Default: |
|
The Operator is run locally by building the Operator binary with the ability to access a Kubernetes cluster using a |
|
The namespace where the Operator watches for changes. Default: |
|
Flags that the local Operator might require. Example: |
| Usage help output. |
4.9.8.1. --local 复制链接链接已复制到粘贴板!
The --local
flag launches the Operator on the local machine by building the Operator binary with the ability to access a Kubernetes cluster using a kubeconfig
file.
For example:
operator-sdk run --local \ --kubeconfig "mycluster.kubecfg" \ --namespace "default" \ --operator-flags "--flag1 value1 --flag2=value2"
$ operator-sdk run --local \
--kubeconfig "mycluster.kubecfg" \
--namespace "default" \
--operator-flags "--flag1 value1 --flag2=value2"
The following example uses the default kubeconfig
, the default namespace environment variable, and passes in flags for the Operator. To use the Operator flags, your Operator must know how to handle the option. For example, for an Operator that understands the resync-interval
flag:
operator-sdk run --local --operator-flags "--resync-interval 10"
$ operator-sdk run --local --operator-flags "--resync-interval 10"
If you are planning on using a different namespace than the default, use the --namespace
flag to change where the Operator is watching for custom resources (CRs) to be created:
operator-sdk run --local --namespace "testing"
$ operator-sdk run --local --namespace "testing"
For this to work, your Operator must handle the WATCH_NAMESPACE
environment variable. This can be accomplished using the utility function k8sutil.GetWatchNamespace
in your Operator.