Kubernetes operators
Introduction
Purpose
Vocabulary
References
Overview
Installation of operator-sdk
Install the Operator SDK CLI
export GOPROXY="https://proxy.golang.org|direct"
- export GOBIN=/usr/local/opsdk/bin
- sudo mkdir ${GOBIN}
- sudo chown hck ${GOBIN}
- make install
Rolling your own Operator
- Quickstart for Go-based Operators
- export PATH=${PATH}:/usr/local/opsdk
- mkdir memcached-operator
- cd memcached-operator
- operator-sdk init --domain example.com --repo github.com/example/memcached-operator
- operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller
- make docker-build
- operator-sdk olm install
Troubleshooting
Troubleshooting api creation errors
FATA[0019] failed to create API: unable to run post-scaffold tasks of "base.go.kubebuilder.io/v4": exit status 2
/home/hup/Sources/operator-sdk-examples/memcached-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xa0df0f]
goroutine 178 [running]:
go/types.(*Checker).handleBailout(0xc000b4a000, 0xc0007ddd40)
/usr/local/go/src/go/types/check.go:367 +0x88
panic({0xbc7860?, 0x12b3880?})
/usr/local/go/src/runtime/panic.go:770 +0x132
go/types.(*StdSizes).Sizeof(0x0, {0xdc3678, 0x12bc060})
/usr/local/go/src/go/types/sizes.go:228 +0x30f
go/types.(*Config).sizeof(...)
/usr/local/go/src/go/types/sizes.go:333
go/types.representableConst.func1({0xdc3678?, 0x12bc060?})
/usr/local/go/src/go/types/const.go:76 +0x9e
go/types.representableConst({0xdc9a50, 0x1286f60}, 0xc000b4a000, 0x12bc060, 0x0)
/usr/local/go/src/go/types/const.go:92 +0x192
go/types.(*Checker).arrayLength(0xc000b4a000, {0xdc7d00, 0xc0012f2b60?})
/usr/local/go/src/go/types/typexpr.go:510 +0x2d3
go/types.(*Checker).typInternal(0xc000b4a000, {0xdc6320, 0xc0012f4ab0}, 0x0)
/usr/local/go/src/go/types/typexpr.go:299 +0x49d
go/types.(*Checker).definedType(0xc000b4a000, {0xdc6320, 0xc0012f4ab0}, 0xc0007dd328?)
/usr/local/go/src/go/types/typexpr.go:180 +0x37
go/types.(*Checker).varType(0xc000b4a000, {0xdc6320, 0xc0012f4ab0})
/usr/local/go/src/go/types/typexpr.go:145 +0x25
go/types.(*Checker).structType(0xc000b4a000, 0xc00135acc0, 0xc00135acc0?)
/usr/local/go/src/go/types/struct.go:113 +0x19f
go/types.(*Checker).typInternal(0xc000b4a000, {0xdc6290, 0xc000e577d0}, 0xc00135c780)
/usr/local/go/src/go/types/typexpr.go:316 +0x1345
go/types.(*Checker).definedType(0xc000b4a000, {0xdc6290, 0xc000e577d0}, 0xc91787?)
/usr/local/go/src/go/types/typexpr.go:180 +0x37
go/types.(*Checker).typeDecl(0xc000b4a000, 0xc00135c780, 0xc0012be840, 0x0)
/usr/local/go/src/go/types/decl.go:615 +0x44d
go/types.(*Checker).objDecl(0xc000b4a000, {0xdcf000, 0xc00135c780}, 0x0)
/usr/local/go/src/go/types/decl.go:197 +0xa7f
go/types.(*Checker).packageObjects(0xc000b4a000)
/usr/local/go/src/go/types/resolver.go:681 +0x425
go/types.(*Checker).checkFiles(0xc000b4a000, {0xc0014c5020, 0x3, 0x3})
/usr/local/go/src/go/types/check.go:408 +0x1a5
go/types.(*Checker).Files(...)
/usr/local/go/src/go/types/check.go:372
sigs.k8s.io/controller-tools/pkg/loader.(*loader).typeCheck(0xc000239380, 0xc0001e03e0)
/home/hup/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/loader/loader.go:286 +0x36a
sigs.k8s.io/controller-tools/pkg/loader.(*Package).NeedTypesInfo(0xc0001e03e0)
/home/hup/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/loader/loader.go:99 +0x39
sigs.k8s.io/controller-tools/pkg/loader.(*TypeChecker).check(0xc0001a1ec0, 0xc0001e03e0)
/home/hup/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/loader/refs.go:268 +0x2b7
sigs.k8s.io/controller-tools/pkg/loader.(*TypeChecker).check.func1(0x1e?)
/home/hup/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/loader/refs.go:262 +0x53
created by sigs.k8s.io/controller-tools/pkg/loader.(*TypeChecker).check in goroutine 162
/home/hup/go/pkg/mod/sigs.k8s.io/[email protected]/pkg/loader/refs.go:260 +0x1c5
make: *** [Makefile:104: generate] Error 2
Error: failed to create API: unable to run post-scaffold tasks of "base.go.kubebuilder.io/v4": exit status 2
Usage:
operator-sdk create api [flags]
Examples:
# Create a frigates API with Group: ship, Version: v1beta1 and Kind: Frigate
operator-sdk create api --group ship --version v1beta1 --kind Frigate
# Edit the API Scheme
nano api/v1beta1/frigate_types.go
# Edit the Controller
nano internal/controller/frigate/frigate_controller.go
# Edit the Controller Test
nano internal/controller/frigate/frigate_controller_test.go
# Generate the manifests
make manifests
# Install CRDs into the Kubernetes cluster using kubectl apply
make install
# Regenerate code and run against the Kubernetes cluster configured by ~/.kube/config
make run
Flags:
--controller if set, generate the controller without prompting the user (default true)
--force attempt to create resource even if it already exists
--group string resource Group
-h, --help help for api
--kind string resource Kind
--make make generate if true, run make generate after generating files (default true)
--namespaced resource is namespaced (default true)
--plural string resource irregular plural form
--resource if set, generate the resource without prompting the user (default true)
--version string resource Version
Global Flags:
--plugins strings plugin keys to be used for this subcommand execution
--verbose Enable verbose logging
FATA[0019] failed to create API: unable to run post-scaffold tasks of "base.go.kubebuilder.io/v4": exit status 2
ERROR [builder 9/9] RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager cmd/main
=> [builder 2/9] WORKDIR /workspace 16.2s
=> [builder 3/9] COPY go.mod go.mod 0.7s
=> [builder 4/9] COPY go.sum go.sum 1.2s
=> [builder 5/9] RUN go mod download 15.8s
=> [builder 6/9] COPY cmd/main.go cmd/main.go 0.5s
=> [builder 7/9] COPY api/ api/ 1.0s
=> [builder 8/9] COPY internal/controller/ internal/controller/ 1.0s
=> ERROR [builder 9/9] RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager cmd/main.go 34.4s
------
> [builder 9/9] RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager cmd/main.go:
13.22 # github.com/example/memcached-operator/api/v1alpha1
13.22 api/v1alpha1/memcached_types.go:63:25: cannot use &Memcached{} (value of type *Memcached) as "k8s.io/apimachinery/pkg/runtime".Object value in argument to SchemeBuilder.Register: *Memcached does not implement "k8s.io/apimachinery/pkg/runtime".Object (missing method DeepCopyObject)
13.22 api/v1alpha1/memcached_types.go:63:39: cannot use &MemcachedList{} (value of type *MemcachedList) as "k8s.io/apimachinery/pkg/runtime".Object value in argument to SchemeBuilder.Register: *MemcachedList does not implement "k8s.io/apimachinery/pkg/runtime".Object (missing method DeepCopyObject)
------
Dockerfile:24
--------------------
22 | # the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
23 | # by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24 | >>> RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
25 |
26 | # Use distroless as minimal base image to package the manager binary
--------------------
ERROR: failed to solve: process "/bin/sh -c CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go" did not complete successfully: exit code: 1
make: *** [Makefile:154: docker-build] Error 1