From f23703038432c5a3a0f205961d83711d3e605e6b Mon Sep 17 00:00:00 2001 From: Andi Skrgat Date: Fri, 26 Jul 2024 11:59:31 +0200 Subject: [PATCH] Basic version of the controller --- .gitignore | 2 ++ Dockerfile | 2 +- Makefile | 6 ++--- config/manager/kustomization.yaml | 6 +++++ config/manager/manager.yaml | 27 -------------------- internal/controller/memgraphha_controller.go | 24 ++++++++++------- 6 files changed, 27 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index 62fd3e3..6a96658 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ bin *.swp *.swo *~ + +./manager diff --git a/Dockerfile b/Dockerfile index b078298..a48973e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build the manager binary -FROM golang:1.20 AS builder +FROM golang:1.22 AS builder ARG TARGETOS ARG TARGETARCH diff --git a/Makefile b/Makefile index b304cb5..3244f81 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # To re-generate a bundle for another specific version without changing the standard setup, you can: # - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2) # - use environment variables to overwrite this value (e.g export VERSION=0.0.2) -VERSION ?= 0.0.1 +VERSION ?= 0.0.4 # CHANNELS define the bundle channels used in the bundle. # Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable") @@ -29,7 +29,7 @@ BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL) # # For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both # com/kubernetes-operator-bundle:$VERSION and com/kubernetes-operator-catalog:$VERSION. -IMAGE_TAG_BASE ?= com/kubernetes-operator +IMAGE_TAG_BASE ?= memgraph/kubernetes-operator # BUNDLE_IMG defines the image:tag used for the bundle. # You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=/:) @@ -51,7 +51,7 @@ endif OPERATOR_SDK_VERSION ?= v1.35.0 # Image URL to use all building/pushing image targets -IMG ?= controller:latest +IMG ?= $(IMAGE_TAG_BASE):$(VERSION) # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. ENVTEST_K8S_VERSION = 1.28.3 diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index ac75006..26247ab 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -1,3 +1,9 @@ resources: - manager.yaml - namespace.yaml +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +images: +- name: controller + newName: memgraph/kubernetes-operator + newTag: 0.0.4 diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index fa3b39c..ff8492b 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -23,35 +23,8 @@ spec: labels: control-plane: controller-manager spec: - # TODO(user): Uncomment the following code to configure the nodeAffinity expression - # according to the platforms which are supported by your solution. - # It is considered best practice to support multiple architectures. You can - # build your manager image using the makefile target docker-buildx. - # affinity: - # nodeAffinity: - # requiredDuringSchedulingIgnoredDuringExecution: - # nodeSelectorTerms: - # - matchExpressions: - # - key: kubernetes.io/arch - # operator: In - # values: - # - amd64 - # - arm64 - # - ppc64le - # - s390x - # - key: kubernetes.io/os - # operator: In - # values: - # - linux securityContext: runAsNonRoot: true - # TODO(user): For common cases that do not require escalating privileges - # it is recommended to ensure that all your Pods/Containers are restrictive. - # More info: https://kubernetes.io/docs/concepts/security/pod-security-standards/#restricted - # Please uncomment the following code if your project does NOT have to work on old Kubernetes - # versions < 1.19 or on vendors versions which do NOT support this field by default (i.e. Openshift < 4.11 ). - # seccompProfile: - # type: RuntimeDefault containers: - command: - /manager diff --git a/internal/controller/memgraphha_controller.go b/internal/controller/memgraphha_controller.go index 8915145..69c45e8 100644 --- a/internal/controller/memgraphha_controller.go +++ b/internal/controller/memgraphha_controller.go @@ -19,6 +19,7 @@ package controller import ( "context" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -37,20 +38,25 @@ type MemgraphHAReconciler struct { //+kubebuilder:rbac:groups=memgraph.com,resources=memgraphhas/status,verbs=get;update;patch //+kubebuilder:rbac:groups=memgraph.com,resources=memgraphhas/finalizers,verbs=update -// Reconcile is part of the main kubernetes reconciliation loop which aims to -// move the current state of the cluster closer to the desired state. -// TODO(user): Modify the Reconcile function to compare the state specified by -// the MemgraphHA object against the actual cluster state, and then -// perform operations to make the cluster state reflect the state specified by -// the user. -// // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.16.3/pkg/reconcile func (r *MemgraphHAReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - _ = log.FromContext(ctx) + logger := log.FromContext(ctx) - // TODO(user): your logic here + memgraphha := &memgraphv1.MemgraphHA{} + err := r.Get(ctx, req.NamespacedName, memgraphha) + if err != nil { + // Handle specifically not found error + if errors.IsNotFound(err) { + logger.Info("MemgraphHA resource not found. Ignoring since object must be deleted.") + return ctrl.Result{}, nil + } + logger.Error(err, "Failed to get MemgraphHA") + // Requeue + return ctrl.Result{}, err + } + // The resource doesn't need to be reconciled anymore return ctrl.Result{}, nil }