Skip to content

Commit

Permalink
Fixed logic in configmap controller
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Jun 22, 2023
1 parent 24dc9fb commit 9176d9d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
33 changes: 18 additions & 15 deletions controllers/configmap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package controllers
import (
"context"
"fmt"
"regexp"
"k8s.io/apimachinery/pkg/types"
"strconv"
"time"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
}
3 changes: 1 addition & 2 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 9176d9d

Please sign in to comment.