From 9176d9d634303c84144564923b5a63ead0cdaa7e Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 22 Jun 2023 11:20:47 +1000 Subject: [PATCH] Fixed logic in configmap controller --- controllers/configmap_controller.go | 33 ++++++++++++++++------------- internal/service/service.go | 3 +-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/controllers/configmap_controller.go b/controllers/configmap_controller.go index 3451fa7..60da1a1 100644 --- a/controllers/configmap_controller.go +++ b/controllers/configmap_controller.go @@ -19,7 +19,7 @@ package controllers import ( "context" "fmt" - "regexp" + "k8s.io/apimachinery/pkg/types" "strconv" "time" @@ -78,13 +78,28 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( return ctrl.Result{}, client.IgnoreNotFound(err) } + var nameSpace corev1.Namespace + if err := r.Get(ctx, types.NamespacedName{Name: req.Namespace}, &nameSpace); err != nil { + log.Error(err, "Unable to load Namespace") + return ctrl.Result{}, client.IgnoreNotFound(err) + } + + var environmentName string + var projectName string + labels := nameSpace.Labels + if _, ok := labels["lagoon.sh/environment"]; ok { + environmentName = labels["lagoon.sh/environment"] + } else if _, ok := labels["lagoon.sh/project"]; ok { + projectName = labels["lagoon.sh/project"] + } + var sendData = LagoonInsightsMessage{ Payload: configMap.Data, BinaryPayload: configMap.BinaryData, Annotations: configMap.Annotations, Labels: configMap.Labels, - Environment: getEnvironmentFromName(configMap.Name), - Project: getProjectFromName(configMap.Name), + Environment: environmentName, + Project: projectName, } marshalledData, err := json.Marshal(sendData) @@ -173,15 +188,3 @@ func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error { WithEventFilter(insightLabelsOnlyPredicate()). Complete(r) } - -func getProjectFromName(project string) string { - regex := regexp.MustCompile(`/([^/]+)`) - match := regex.FindStringSubmatch(project) - return match[1] -} - -func getEnvironmentFromName(environment string) string { - regex := regexp.MustCompile(`/[^/]+/([^/]+)`) - match := regex.FindStringSubmatch(environment) - return match[1] -} diff --git a/internal/service/service.go b/internal/service/service.go index 817ded6..dc000eb 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -51,8 +51,7 @@ func (r *routerInstance) writeFacts(c *gin.Context) { fmt.Println("Going to write to namespace ", namespace) //TODO: drop "InsightsType" for Type of the form "direct.fact"/"direct.problem" - details := &internal.Facts{Type: "direct.Fact"} - // Do we want to have 'Type' defined at this level, or for each individual Fact? + details := &internal.Facts{Type: "direct.facts"} if err = c.ShouldBindJSON(details); err != nil { c.JSON(http.StatusBadRequest, gin.H{