From 97b5964e5ab9aad49f08799aaaf1a82706d1a302 Mon Sep 17 00:00:00 2001 From: Krzysztof Pudlowski Date: Tue, 5 Jul 2022 23:27:06 +0200 Subject: [PATCH] adding configmap creation --- README.md | 2 +- charts/go-project-operator/Chart.yaml | 4 +-- charts/go-project-operator/values.yaml | 2 +- controllers/config.go | 4 ++- controllers/project_controller.go | 40 ++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a14b57a..e31fecb 100644 --- a/README.md +++ b/README.md @@ -426,7 +426,7 @@ make run bash prepare-setup.sh make docker-build docker-push IMG=$IMG -make docker-build docker-push IMG="docker.io/djkormo/go-project-operator:v0.0.6" +make docker-build docker-push IMG="docker.io/djkormo/go-project-operator:v0.0.8" ``` diff --git a/charts/go-project-operator/Chart.yaml b/charts/go-project-operator/Chart.yaml index 24678d4..8792d10 100644 --- a/charts/go-project-operator/Chart.yaml +++ b/charts/go-project-operator/Chart.yaml @@ -13,9 +13,9 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.0.7 +version: 0.0.8 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. # It is recommended to use it with quotes. -appVersion: "0.0.7" +appVersion: "0.0.8" diff --git a/charts/go-project-operator/values.yaml b/charts/go-project-operator/values.yaml index f8f48a7..7df08bc 100644 --- a/charts/go-project-operator/values.yaml +++ b/charts/go-project-operator/values.yaml @@ -13,7 +13,7 @@ controllerManager: manager: image: repository: docker.io/djkormo/go-project-operator - tag: v0.0.7 + tag: v0.0.8 resources: limits: cpu: 500m diff --git a/controllers/config.go b/controllers/config.go index d5c67e9..675c4d8 100644 --- a/controllers/config.go +++ b/controllers/config.go @@ -1,6 +1,8 @@ package controllers -// for pausing operator loop const ( + // for pausing operator loop pauseReconciliationLabel = "project-operator/pauseReconciliation" + // for creating configmap + configMapName = "project-operator-configmap-override" ) diff --git a/controllers/project_controller.go b/controllers/project_controller.go index 2b4b926..0ceed4f 100644 --- a/controllers/project_controller.go +++ b/controllers/project_controller.go @@ -89,10 +89,33 @@ func (r *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct return ctrl.Result{}, nil } + // creating missing override configmap + + configMapFound := &corev1.ConfigMap{} + err = r.Get(ctx, types.NamespacedName{Name: configMapName, Namespace: Project.Name}, configMapFound) + // Find if namespace exists namespaceFound := &corev1.Namespace{} err = r.Get(ctx, types.NamespacedName{Name: Project.Name}, namespaceFound) + if err != nil && errors.IsNotFound(err) { + // define a new configmap + cm := r.populateConfigOverrideConfigMap(Project) // returns a configmap + logger.Info("Creating a new Configmap", "ConfigMap.Name", configMapName) + err = r.Create(ctx, cm) + if err != nil { + logger.Error(err, "Failed to create new ConfigMap", "Configmap.Name", configMapName) + return ctrl.Result{}, err + } + // configmap created, return and requeue + logger.Info("ConfigMap created", "ConfiMap.Name", configMapName) + return ctrl.Result{Requeue: true}, nil + } else if err != nil { + logger.Error(err, "Failed to get ConfigMap") + // Reconcile failed due to error - requeue + return ctrl.Result{}, err + } + if err != nil && errors.IsNotFound(err) { // define a new namespace ns := r.namespaceForProjectApp(Project) // namespaceForProjectApp() returns a namespace @@ -362,3 +385,20 @@ func ignoreDeletionPredicate() predicate.Predicate { }, } } + +func (r *ProjectReconciler) populateConfigOverrideConfigMap(m *projectv1alpha1.Project) *corev1.ConfigMap { + + placeholderConfig := map[string]string{ + "pauseReconciliationLabel": pauseReconciliationLabel, + } + namespace := m.Namespace + configmap := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: configMapName, + Namespace: namespace, + }, + Data: placeholderConfig, + } + + return configmap +}