Skip to content

Commit

Permalink
refactor: check for lagoon-env secrets first
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jan 17, 2025
1 parent f2c147f commit 449b0e2
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 159 deletions.
12 changes: 1 addition & 11 deletions internal/controllers/v1beta2/build_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,6 @@ func (r *LagoonBuildReconciler) createNamespaceBuild(ctx context.Context,
return ctrl.Result{}, err
}

// Get or create the lagoon-env configmap
lagoonEnvConfigMap := &corev1.ConfigMap{}
if r.EnableDebug {
opLog.Info("Checking `lagoon-env` configMap exists - creating if not")
}
err = r.getOrCreateConfigMap(ctx, "lagoon-env", lagoonEnvConfigMap, namespace.ObjectMeta.Name)
if err != nil {
return ctrl.Result{}, err
}

// copy the build resource into a new resource and set the status to pending
// create the new resource and the controller will handle it via queue
opLog.Info(fmt.Sprintf("Creating LagoonBuild in Pending status: %s", lagoonBuild.ObjectMeta.Name))
Expand Down Expand Up @@ -308,7 +298,7 @@ func (r *LagoonBuildReconciler) createNamespaceBuild(ctx context.Context,
continue
}
// send the status change to lagoon
r.updateDeploymentAndEnvironmentTask(opLog, runningBuild, nil, buildCondition, "cancelled")
r.updateDeploymentAndEnvironmentTask(ctx, opLog, runningBuild, false, buildCondition, "cancelled")
continue
}
// handle processing running but no pod/failed pod builds
Expand Down
51 changes: 14 additions & 37 deletions internal/controllers/v1beta2/build_deletionhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -195,25 +194,10 @@ Build cancelled
if err := r.Patch(ctx, &lagoonBuild, client.RawPatch(types.MergePatchType, mergePatch)); err != nil {
opLog.Error(err, "unable to update build status")
}
// get the configmap for lagoon-env so we can use it for updating the deployment in lagoon
var lagoonEnv corev1.ConfigMap
err := r.Get(ctx, types.NamespacedName{
Namespace: lagoonBuild.ObjectMeta.Namespace,
Name: "lagoon-env",
},
&lagoonEnv,
)
if err != nil {
// if there isn't a configmap, just info it and move on
// the updatedeployment function will see it as nil and not bother doing the bits that require the configmap
if r.EnableDebug {
opLog.Info(fmt.Sprintf("There is no configmap %s in namespace %s ", "lagoon-env", lagoonBuild.ObjectMeta.Namespace))
}
}
// send any messages to lagoon message queues
// update the deployment with the status of cancelled in lagoon
r.buildStatusLogsToLagoonLogs(opLog, &lagoonBuild, &lagoonEnv, lagooncrd.BuildStatusCancelled, "cancelled")
r.updateDeploymentAndEnvironmentTask(opLog, &lagoonBuild, &lagoonEnv, lagooncrd.BuildStatusCancelled, "cancelled")
r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, lagooncrd.BuildStatusCancelled, "cancelled")
r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, true, lagooncrd.BuildStatusCancelled, "cancelled")
r.buildLogsToLagoonLogs(opLog, &lagoonBuild, allContainerLogs, lagooncrd.BuildStatusCancelled)
}
return nil
Expand Down Expand Up @@ -267,9 +251,10 @@ func (r *LagoonBuildReconciler) buildLogsToLagoonLogs(
// updateDeploymentAndEnvironmentTask sends the status of the build and deployment to the controllerhandler message queue in lagoon,
// this is for the handler in lagoon to process.
func (r *LagoonBuildReconciler) updateDeploymentAndEnvironmentTask(
ctx context.Context,
opLog logr.Logger,
lagoonBuild *lagooncrd.LagoonBuild,
lagoonEnv *corev1.ConfigMap,
checkLagoonEnv bool,
buildCondition lagooncrd.BuildStatusType,
buildStep string,
) {
Expand Down Expand Up @@ -331,15 +316,12 @@ func (r *LagoonBuildReconciler) updateDeploymentAndEnvironmentTask(
msg.Meta.Services = serviceNames
msg.Meta.EnvironmentServices = services
}
// if we aren't being provided the lagoon config, we can skip adding the routes etc
if lagoonEnv != nil {
msg.Meta.Route = ""
if route, ok := lagoonEnv.Data["LAGOON_ROUTE"]; ok {
if checkLagoonEnv {
lagoonEnv, route, routes := helpers.GetLagoonEnvRoutes(ctx, opLog, r.Client, lagoonBuild.ObjectMeta.Namespace)
// if we aren't being provided the lagoon config, we can skip adding the routes etc
if lagoonEnv {
msg.Meta.Route = route
}
msg.Meta.Routes = []string{}
if routes, ok := lagoonEnv.Data["LAGOON_ROUTES"]; ok {
msg.Meta.Routes = strings.Split(routes, ",")
msg.Meta.Routes = routes
}
}
if buildCondition.ToLower() == "failed" || buildCondition.ToLower() == "complete" || buildCondition.ToLower() == "cancelled" {
Expand All @@ -361,9 +343,9 @@ func (r *LagoonBuildReconciler) updateDeploymentAndEnvironmentTask(

// buildStatusLogsToLagoonLogs sends the logs to lagoon-logs message queue, used for general messaging
func (r *LagoonBuildReconciler) buildStatusLogsToLagoonLogs(
ctx context.Context,
opLog logr.Logger,
lagoonBuild *lagooncrd.LagoonBuild,
lagoonEnv *corev1.ConfigMap,
buildCondition lagooncrd.BuildStatusType,
buildStep string,
) {
Expand All @@ -388,16 +370,11 @@ func (r *LagoonBuildReconciler) buildStatusLogsToLagoonLogs(
buildCondition.ToLower(),
),
}
lagoonEnv, route, routes := helpers.GetLagoonEnvRoutes(ctx, opLog, r.Client, lagoonBuild.ObjectMeta.Namespace)
// if we aren't being provided the lagoon config, we can skip adding the routes etc
if lagoonEnv != nil {
msg.Meta.Route = ""
if route, ok := lagoonEnv.Data["LAGOON_ROUTE"]; ok {
msg.Meta.Route = route
}
msg.Meta.Routes = []string{}
if routes, ok := lagoonEnv.Data["LAGOON_ROUTES"]; ok {
msg.Meta.Routes = strings.Split(routes, ",")
}
if lagoonEnv {
msg.Meta.Route = route
msg.Meta.Routes = routes
}
msgBytes, err := json.Marshal(msg)
if err != nil {
Expand Down
58 changes: 6 additions & 52 deletions internal/controllers/v1beta2/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,22 +279,6 @@ func (r *LagoonBuildReconciler) getCreateOrUpdateSSHKeySecret(ctx context.Contex
return nil
}

func (r *LagoonBuildReconciler) getOrCreateConfigMap(ctx context.Context, cmName string, configMap *corev1.ConfigMap, ns string) error {
err := r.Get(ctx, types.NamespacedName{
Namespace: ns,
Name: cmName,
}, configMap)
if err != nil {
configMap.SetNamespace(ns)
configMap.SetName(cmName)
//we create it
if err = r.Create(ctx, configMap); err != nil {
return fmt.Errorf("there was an error creating the configmap '%v'. Error was: %v", cmName, err)
}
}
return nil
}

// processBuild will actually process the build.
func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Logger, lagoonBuild lagooncrd.LagoonBuild) error {
// we run these steps again just to be sure that it gets updated/created if it hasn't already
Expand Down Expand Up @@ -905,30 +889,15 @@ func (r *LagoonBuildReconciler) updateQueuedBuild(
%s
========================================
`, fmt.Sprintf("This build is currently queued in position %v/%v", queuePosition, queueLength)))
// get the configmap for lagoon-env so we can use it for updating the deployment in lagoon
var lagoonEnv corev1.ConfigMap
err := r.Get(ctx, types.NamespacedName{
Namespace: lagoonBuild.ObjectMeta.Namespace,
Name: "lagoon-env",
},
&lagoonEnv,
)
if err != nil {
// if there isn't a configmap, just info it and move on
// the updatedeployment function will see it as nil and not bother doing the bits that require the configmap
if r.EnableDebug {
opLog.Info(fmt.Sprintf("There is no configmap %s in namespace %s ", "lagoon-env", lagoonBuild.ObjectMeta.Namespace))
}
}
// send any messages to lagoon message queues
// update the deployment with the status, lagoon v2.12.0 supports queued status, otherwise use pending
if lagooncrd.CheckLagoonVersion(&lagoonBuild, "2.12.0") {
r.buildStatusLogsToLagoonLogs(opLog, &lagoonBuild, &lagoonEnv, lagooncrd.BuildStatusQueued, fmt.Sprintf("queued %v/%v", queuePosition, queueLength))
r.updateDeploymentAndEnvironmentTask(opLog, &lagoonBuild, &lagoonEnv, lagooncrd.BuildStatusQueued, fmt.Sprintf("queued %v/%v", queuePosition, queueLength))
r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, lagooncrd.BuildStatusQueued, fmt.Sprintf("queued %v/%v", queuePosition, queueLength))
r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, true, lagooncrd.BuildStatusQueued, fmt.Sprintf("queued %v/%v", queuePosition, queueLength))
r.buildLogsToLagoonLogs(opLog, &lagoonBuild, allContainerLogs, lagooncrd.BuildStatusQueued)
} else {
r.buildStatusLogsToLagoonLogs(opLog, &lagoonBuild, &lagoonEnv, lagooncrd.BuildStatusPending, fmt.Sprintf("queued %v/%v", queuePosition, queueLength))
r.updateDeploymentAndEnvironmentTask(opLog, &lagoonBuild, &lagoonEnv, lagooncrd.BuildStatusPending, fmt.Sprintf("queued %v/%v", queuePosition, queueLength))
r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, lagooncrd.BuildStatusPending, fmt.Sprintf("queued %v/%v", queuePosition, queueLength))
r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, true, lagooncrd.BuildStatusPending, fmt.Sprintf("queued %v/%v", queuePosition, queueLength))
r.buildLogsToLagoonLogs(opLog, &lagoonBuild, allContainerLogs, lagooncrd.BuildStatusPending)
}
return nil
Expand Down Expand Up @@ -964,25 +933,10 @@ Build cancelled
opLog.Error(err, "Unable to update build status")
}
}
// get the configmap for lagoon-env so we can use it for updating the deployment in lagoon
var lagoonEnv corev1.ConfigMap
err := r.Get(ctx, types.NamespacedName{
Namespace: lagoonBuild.ObjectMeta.Namespace,
Name: "lagoon-env",
},
&lagoonEnv,
)
if err != nil {
// if there isn't a configmap, just info it and move on
// the updatedeployment function will see it as nil and not bother doing the bits that require the configmap
if r.EnableDebug {
opLog.Info(fmt.Sprintf("There is no configmap %s in namespace %s ", "lagoon-env", lagoonBuild.ObjectMeta.Namespace))
}
}
// send any messages to lagoon message queues
// update the deployment with the status of cancelled in lagoon
r.buildStatusLogsToLagoonLogs(opLog, &lagoonBuild, &lagoonEnv, lagooncrd.BuildStatusCancelled, "cancelled")
r.updateDeploymentAndEnvironmentTask(opLog, &lagoonBuild, &lagoonEnv, lagooncrd.BuildStatusCancelled, "cancelled")
r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, lagooncrd.BuildStatusCancelled, "cancelled")
r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, true, lagooncrd.BuildStatusCancelled, "cancelled")
if cancelled {
r.buildLogsToLagoonLogs(opLog, &lagoonBuild, allContainerLogs, lagooncrd.BuildStatusCancelled)
}
Expand Down
63 changes: 15 additions & 48 deletions internal/controllers/v1beta2/podmonitor_buildhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ func (r *LagoonMonitorReconciler) handleBuildMonitor(ctx context.Context,
}
jobPod.Status.ContainerStatuses[0] = state

// get the configmap for lagoon-env so we can use it for updating the deployment in lagoon
var lagoonEnv corev1.ConfigMap
err := r.Get(ctx, types.NamespacedName{Namespace: jobPod.ObjectMeta.Namespace, Name: "lagoon-env"}, &lagoonEnv)
if err != nil {
// if there isn't a configmap, just info it and move on
// the updatedeployment function will see it as nil and not bother doing the bits that require the configmap
if r.EnableDebug {
opLog.Info(fmt.Sprintf("There is no configmap %s in namespace %s ", "lagoon-env", jobPod.ObjectMeta.Namespace))
}
}
// send any messages to lagoon message queues
logMsg := fmt.Sprintf("%v: %v", container.State.Waiting.Reason, container.State.Waiting.Message)
return r.updateDeploymentWithLogs(ctx, req, lagoonBuild, jobPod, []byte(logMsg), false)
Expand Down Expand Up @@ -230,10 +220,10 @@ Logs on pod %s, assigned to cluster %s
// updateDeploymentAndEnvironmentTask sends the status of the build and deployment to the controllerhandler message queue in lagoon,
// this is for the handler in lagoon to process.
func (r *LagoonMonitorReconciler) updateDeploymentAndEnvironmentTask(
ctx context.Context,
opLog logr.Logger,
lagoonBuild *lagooncrd.LagoonBuild,
jobPod *corev1.Pod,
lagoonEnv *corev1.ConfigMap,
namespace *corev1.Namespace,
condition string,
) error {
Expand Down Expand Up @@ -323,16 +313,11 @@ func (r *LagoonMonitorReconciler) updateDeploymentAndEnvironmentTask(
msg.Meta.Services = serviceNames
msg.Meta.EnvironmentServices = services
}
lagoonEnv, route, routes := helpers.GetLagoonEnvRoutes(ctx, opLog, r.Client, lagoonBuild.ObjectMeta.Namespace)
// if we aren't being provided the lagoon config, we can skip adding the routes etc
if lagoonEnv != nil {
msg.Meta.Route = ""
if route, ok := lagoonEnv.Data["LAGOON_ROUTE"]; ok {
msg.Meta.Route = route
}
msg.Meta.Routes = []string{}
if routes, ok := lagoonEnv.Data["LAGOON_ROUTES"]; ok {
msg.Meta.Routes = strings.Split(routes, ",")
}
if lagoonEnv {
msg.Meta.Route = route
msg.Meta.Routes = routes
}
// we can add the build start time here
if jobPod.Status.StartTime != nil {
Expand Down Expand Up @@ -375,10 +360,10 @@ func (r *LagoonMonitorReconciler) updateDeploymentAndEnvironmentTask(

// buildStatusLogsToLagoonLogs sends the logs to lagoon-logs message queue, used for general messaging
func (r *LagoonMonitorReconciler) buildStatusLogsToLagoonLogs(
ctx context.Context,
opLog logr.Logger,
lagoonBuild *lagooncrd.LagoonBuild,
jobPod *corev1.Pod,
lagoonEnv *corev1.ConfigMap,
namespace *corev1.Namespace,
condition string,
) error {
Expand Down Expand Up @@ -422,17 +407,13 @@ func (r *LagoonMonitorReconciler) buildStatusLogsToLagoonLogs(
}
// if we aren't being provided the lagoon config, we can skip adding the routes etc
var addRoute, addRoutes string
if lagoonEnv != nil {
msg.Meta.Route = ""
if route, ok := lagoonEnv.Data["LAGOON_ROUTE"]; ok {
msg.Meta.Route = route
addRoute = fmt.Sprintf("\n%s", route)
}
msg.Meta.Routes = []string{}
if routes, ok := lagoonEnv.Data["LAGOON_ROUTES"]; ok {
msg.Meta.Routes = strings.Split(routes, ",")
addRoutes = fmt.Sprintf("\n%s", strings.Join(strings.Split(routes, ","), "\n"))
}
lagoonEnv, route, routes := helpers.GetLagoonEnvRoutes(ctx, opLog, r.Client, lagoonBuild.ObjectMeta.Namespace)
// if we aren't being provided the lagoon config, we can skip adding the routes etc
if lagoonEnv {
msg.Meta.Route = route
addRoute = fmt.Sprintf("\n%s", route)
msg.Meta.Routes = routes
addRoutes = fmt.Sprintf("\n%s", strings.Join(routes, "\n"))
}
msg.Message = fmt.Sprintf("*[%s]* `%s` Build `%s` %s <%s|Logs>%s%s",
projectName,
Expand Down Expand Up @@ -560,26 +541,12 @@ Build %s
"conditions": lagoonBuild.Status.Conditions,
"phase": buildCondition.String(),
}
// get the configmap for lagoon-env so we can use it for updating the deployment in lagoon
var lagoonEnv corev1.ConfigMap
if err := r.Get(ctx, types.NamespacedName{
Namespace: jobPod.ObjectMeta.Namespace,
Name: "lagoon-env",
},
&lagoonEnv,
); err != nil {
// if there isn't a configmap, just info it and move on
// the updatedeployment function will see it as nil and not bother doing the bits that require the configmap
if r.EnableDebug {
opLog.Info(fmt.Sprintf("There is no configmap %s in namespace %s ", "lagoon-env", jobPod.ObjectMeta.Namespace))
}
}

// do any message publishing here, and update any pending messages if needed
if err = r.buildStatusLogsToLagoonLogs(opLog, &lagoonBuild, &jobPod, &lagoonEnv, namespace, buildCondition.ToLower()); err != nil {
if err = r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, &jobPod, namespace, buildCondition.ToLower()); err != nil {
opLog.Error(err, "unable to publish build status logs")
}
if err = r.updateDeploymentAndEnvironmentTask(opLog, &lagoonBuild, &jobPod, &lagoonEnv, namespace, buildCondition.ToLower()); err != nil {
if err = r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, &jobPod, namespace, buildCondition.ToLower()); err != nil {
opLog.Error(err, "unable to publish build update")
}
// if the container logs can't be retrieved, we don't want to send any build logs back, as this will nuke
Expand Down
Loading

0 comments on commit 449b0e2

Please sign in to comment.