vernemq / vmq-operator

VerneMQ Operator creates/configures/manages VerneMQ clusters atop Kubernetes
Apache License 2.0
30 stars 20 forks source link

Update to new operator SDK #34

Closed DominikBasnerSotec closed 2 years ago

DominikBasnerSotec commented 2 years ago

To keep this project alive and developable, here an update that allows working with the current operator SDK

Now uses

what has been done:

Migrate to new operator-sdk

Create a new operator project using the SDK Command Line Interface(CLI)

mkdir ../new-vmq-operator2
cd ../new-vmq-operator2
export GO111MODULE=on 
operator-sdk init --domain vernemq.com --repo github.com/vernemq/vmq-operator
# Add namespace to main.go  
## mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
##  Namespace:              "messaging",
go mod tidy

Define new resource APIs by adding Custom Resource Definitions(CRD)

operator-sdk create api --group vmq.k8s --version v1alpha1 --kind VerneMQ --resource --controller
go mod tidy
# copy vernemy_types from pkg/apis/vernemq/v1alpha1 to api/v1alpha1

Define Controllers to watch and reconcile resources

# copy pkg/controller/vernemq to controllers
# update import paths .../pkg/apis/vernemq/... to .../api/...
# replace 'package vernemq' with 'package controllers'
# Replace import sigs.k8s.io/controller-runtime/pkg/runtime/log with sigs.k8s.io/controller-runtime/pkg/log

Write the reconciling logic for your Controller using the SDK and controller-runtime APIs Use the SDK CLI to build and generate the operator deployment manifests

go get github.com/vernemq/vmq-operator/controllers
# adjust api usage in controllers  
## handler to probeHandler / lifecycleHandler
## default cluster name
## add context to reconcile methods
# update main.go 
##  (&controllers.VerneMQReconciler{
##      Client: mgr.GetClient(),
##      Scheme: mgr.GetScheme(),
##  }).SetupWithManager(mgr); 
##  to 
##    controllers.Add(mgr);
# add  to vernemq_controller.go
## //+kubebuilder:rbac:groups=cache.example.com,resources=memcacheds,verbs=get;list;watch;create;update;patch;delete
## //+kubebuilder:rbac:groups=cache.example.com,resources=memcacheds/status,verbs=get;update;patch
## //+kubebuilder:rbac:groups=cache.example.com,resources=memcacheds/finalizers,verbs=update
## //+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
## //+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;
## func (r *ReconcileVerneMQ) SetupWithManager(mgr ctrl.Manager) error {
##  return ctrl.NewControllerManagedBy(mgr).
##      For(&vernemqv1alpha1.VerneMQ{}).
##      Owns(&appsv1.Deployment{}).
##      Complete(r)
##}
## manually update role and roleBinding according to https://sdk.operatorframework.io/docs/building-operators/golang/operator-scope/#changing-the-permissions-to-namespaced
# update imageName in makefile from IMG ?= controller:latest to your docker name  
make docker-build
make docker-push
# update namespace to messaging in config/default/kustomization.yaml

Then, create the k8s files.

# add to makefile
##.PHONY: kmz
##kmz: manifests kustomize
##    cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
##    $(KUSTOMIZE) build config/default > default-deploy.yaml
# comment - role.yaml in config/rbac/kustomization.yaml
make kustomize
kubectl apply -f default-deploy.yaml
ioolkos commented 2 years ago

@DominikBasnerSotec Thanks a lot! :) if it's not too much to ask, can you add a couple points about what the main updates were? Should I look into anything specifically? Maybe we should also rebase the PR.

In any case, will review and get back if questions arise. :pray: :1st_place_medal: :tada:

DominikBasnerSotec commented 2 years ago

I updated my former message with the taken steps

DominikBasnerSotec commented 2 years ago

@ioolkos have you been able to look into it?

ioolkos commented 2 years ago

@DominikBasnerSotec thanks again. One quick question. I see references to "gcr.io/sotec-iot-core-dev/vmq-operator:latest". what can we do about this? (I wouldn't want to pin things to an external repo).

DominikBasnerSotec commented 2 years ago

@ioolkos Ah, I understand. I can change it to a public docker-hub ref and then it should be back to the current ref after it's merged

DominikBasnerSotec commented 2 years ago

Done!