Cattle-operator is an example operator generated by operator-sdk.
https://github.com/operator-framework/operator-sdk/blob/master/doc/user/install-operator-sdk.md
$ go version
go version go1.12.4 linux/amd64
$ operator-sdk version
operator-sdk version: v0.8.0, commit: 78c472461e75e6c64589cfadf577a2004b8a26b3
https://github.com/operator-framework/getting-started
New operator
cd ${GOPATH}/src/github.com/chechiachang
export GO111MODULE=on
OPERATOR=cattle-operator
operator-sdk new ${OPERATOR}
$ tree -L 2
.
├── cattle-operator
│ ├── build
│ ├── cmd
│ ├── deploy
│ ├── go.mod
│ ├── go.sum
│ ├── pkg
│ ├── tools.go
│ ├── vendor
│ └── version
└── README.md
Add api and controller
API_VERSION=cattle.chechiachang.com/v1alpha1
CRD=Cattle
operator-sdk add api --api-version=${API_VERSION} --kind=${CRD}
operator-sdk add controller --api-version=${API_VERSION} --kind=${CRD}
Add Spec to Cattle
vim pkg/apis/cattle/v1alpha1/cattle_types.go
type CattleSpec struct {
Name string `json:"name"`
Size int32 `json:"size"`
BeefParts []string `json:"beefParts"`
}
make generate
Add angus-cattle resource
cp deploy/crds/cattle_v1apha1_cattle_cr.yaml deploy/crds/angus_cattle_cr.yaml
vim deploy/crds/angus_cattle_cr.yaml
apiVersion: cattle.chechiachang.com/v1alpha1
kind: Cattle
metadata:
name: angus-cattle
spec:
# Add fields here
name: angus
beatParts:
- chuck
- rib
- plate
size: 3
vim pkg/controller/cattle/cattle_controller.go
make generate
Test on a local cluster
make local
make build push
Update image tag
make tag
(Grant extra privilege for serviceaccount rulebinding)
kubectl apply -f deploy/crds/cattle_v1alpha1_cattle_crd.yaml
kubectl apply -f deploy
kubectl apply -f deploy/crds/angus_cattle_cr.yaml