Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraining incoming CM types #20

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 54 additions & 23 deletions controllers/configmap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"errors"
"fmt"
"k8s.io/apimachinery/pkg/types"
"strconv"
Expand Down Expand Up @@ -45,6 +46,7 @@ type LagoonInsightsMessage struct {
Labels map[string]string `json:"labels"`
Environment string `json:"environment"`
Project string `json:"project"`
Type string `json:"type"`
}

// ConfigMapReconciler reconciles a ConfigMap object
Expand Down Expand Up @@ -93,40 +95,69 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
projectName = labels["lagoon.sh/project"]
}

var sendData = LagoonInsightsMessage{
Payload: configMap.Data,
BinaryPayload: configMap.BinaryData,
Annotations: configMap.Annotations,
Labels: configMap.Labels,
Environment: environmentName,
Project: projectName,
// insightsType is a way for us to classify incoming insights data, passing
insightsType := "unclassified"
if _, ok := configMap.Labels["insights.lagoon.sh/type"]; ok {
insightsType = configMap.Labels["insights.lagoon.sh/type"]
log.Info(fmt.Sprintf("Found insights.lagoon.sh/type:%v", insightsType))
} else {
// insightsType can be determined by the incoming data
if _, ok := configMap.Labels["lagoon.sh/insightsType"]; ok {
switch configMap.Labels["lagoon.sh/insightsType"] {
case ("sbom-gz"):
log.Info("Inferring insights type of sbom")
insightsType = "sbom"
case ("image-gz"):
log.Info("Inferring insights type of inspect")
insightsType = "inspect"
}
}
}

marshalledData, err := json.Marshal(sendData)
if err != nil {
log.Error(err, "Unable to marshall config data")
return ctrl.Result{}, err
}
// We reject any type that isn't either sbom or inspect to restrict outgoing types
if insightsType != "sbom" && insightsType != "inspect" {
//// we mark this configMap as bad, and log an error
err := errors.New(fmt.Sprintf("insightsType '%v' unrecognized - rejecting configMap", insightsType))
log.Error(err, err.Error())
} else {
// Here we attempt to process types using the new name structure

var sendData = LagoonInsightsMessage{
Payload: configMap.Data,
BinaryPayload: configMap.BinaryData,
Annotations: configMap.Annotations,
Labels: configMap.Labels,
Environment: environmentName,
Project: projectName,
Type: insightsType,
}

err = r.MessageQWriter(marshalledData)
marshalledData, err := json.Marshal(sendData)
if err != nil {
log.Error(err, "Unable to marshall config data")
return ctrl.Result{}, err
}
err = r.MessageQWriter(marshalledData)

if err != nil {
log.Error(err, "Unable to write to message broker")
if err != nil {
log.Error(err, "Unable to write to message broker")

//In this case what we want to do is defer the processing to a couple minutes from now
future := time.Minute * 5
futureTime := time.Now().Add(future).Unix()
err = cmlib.LabelCM(ctx, r.Client, configMap, InsightsWriteDeferred, strconv.FormatInt(futureTime, 10))
//In this case what we want to do is defer the processing to a couple minutes from now
future := time.Minute * 5
futureTime := time.Now().Add(future).Unix()
err = cmlib.LabelCM(ctx, r.Client, configMap, InsightsWriteDeferred, strconv.FormatInt(futureTime, 10))

if err != nil {
log.Error(err, "Unable to update configmap")
return ctrl.Result{}, err
}

if err != nil {
log.Error(err, "Unable to update configmap")
return ctrl.Result{}, err
}

return ctrl.Result{}, err
}

err = cmlib.AnnotateCM(ctx, r.Client, configMap, InsightsUpdatedAnnotationLabel, time.Now().UTC().Format(time.RFC3339))
err := cmlib.AnnotateCM(ctx, r.Client, configMap, InsightsUpdatedAnnotationLabel, time.Now().UTC().Format(time.RFC3339))

if err != nil {
log.Error(err, "Unable to update configmap")
Expand Down
Loading