From 5b087c2f2c3fed8cd0cc1e7a0de3a26e3aca33c9 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Fri, 16 Dec 2022 10:14:02 +1100 Subject: [PATCH 1/3] chore: use queued status --- controllers/v1beta1/build_helpers.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/controllers/v1beta1/build_helpers.go b/controllers/v1beta1/build_helpers.go index 50561510..e10359c5 100644 --- a/controllers/v1beta1/build_helpers.go +++ b/controllers/v1beta1/build_helpers.go @@ -973,10 +973,9 @@ func (r *LagoonBuildReconciler) updateQueuedBuild( } // send any messages to lagoon message queues // update the deployment with the status - //@TODO: change BuildStatusPending to BuildStatusQueued when lagoon supports queued - r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusPending) - r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusPending) - r.buildLogsToLagoonLogs(ctx, opLog, &lagoonBuild, allContainerLogs, lagoonv1beta1.BuildStatusPending) + r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusQueued) + r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusQueued) + r.buildLogsToLagoonLogs(ctx, opLog, &lagoonBuild, allContainerLogs, lagoonv1beta1.BuildStatusQueued) return nil } From 8805b35750717a55d77f98a85cb085165815066e Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Mon, 20 Feb 2023 15:05:59 +1100 Subject: [PATCH 2/3] refactor: check lagoon version supports queued status --- .github/workflows/remote-controller.yaml | 4 + config/default/kustomization.yaml | 49 +++-------- controller-test.sh | 5 ++ controllers/v1beta1/build_helpers.go | 15 +++- go.mod | 1 + go.sum | 2 + internal/helpers/helpers.go | 49 +++++++++++ internal/helpers/helpers_test.go | 102 +++++++++++++++++++++++ 8 files changed, 186 insertions(+), 41 deletions(-) diff --git a/.github/workflows/remote-controller.yaml b/.github/workflows/remote-controller.yaml index 4032cf13..2f99b7dd 100644 --- a/.github/workflows/remote-controller.yaml +++ b/.github/workflows/remote-controller.yaml @@ -30,6 +30,10 @@ jobs: lagoon_build_image: "uselagoon/build-deploy-image:main" experimental: true steps: + - name: Setup tmate session + uses: mxschmitt/action-tmate@v3 + timeout-minutes: 1 + continue-on-error: true - name: Checkout uses: actions/checkout@v2 with: diff --git a/config/default/kustomization.yaml b/config/default/kustomization.yaml index 3ef65238..e639c738 100644 --- a/config/default/kustomization.yaml +++ b/config/default/kustomization.yaml @@ -12,10 +12,6 @@ namePrefix: remote-controller- #commonLabels: # someName: someValue -bases: -- ../crd -- ../rbac -- ../manager # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml #- ../webhook # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required. @@ -24,15 +20,13 @@ bases: #- ../prometheus configMapGenerator: - - name: overrides - env: config.properties +- envs: + - config.properties + name: overrides -patchesStrategicMerge: -- envs.yaml # Protect the /metrics endpoint by putting it behind auth. # Only one of manager_auth_proxy_patch.yaml and # manager_prometheus_metrics_patch.yaml should be enabled. -- manager_auth_proxy_patch.yaml # If you want your controller-manager to expose the /metrics # endpoint w/o any authn/z, uncomment the following line and # comment manager_auth_proxy_patch.yaml. @@ -49,31 +43,12 @@ patchesStrategicMerge: #- webhookcainjection_patch.yaml # the following config is for teaching kustomize how to do var substitution -vars: -# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix. -#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR -# objref: -# kind: Certificate -# group: cert-manager.io -# version: v1alpha2 -# name: serving-cert # this name should match the one in certificate.yaml -# fieldref: -# fieldpath: metadata.namespace -#- name: CERTIFICATE_NAME -# objref: -# kind: Certificate -# group: cert-manager.io -# version: v1alpha2 -# name: serving-cert # this name should match the one in certificate.yaml -#- name: SERVICE_NAMESPACE # namespace of the service -# objref: -# kind: Service -# version: v1 -# name: webhook-service -# fieldref: -# fieldpath: metadata.namespace -#- name: SERVICE_NAME -# objref: -# kind: Service -# version: v1 -# name: webhook-service +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: +- ../crd +- ../rbac +- ../manager +patches: +- path: envs.yaml +- path: manager_auth_proxy_patch.yaml diff --git a/controller-test.sh b/controller-test.sh index 70575bd9..281ffc01 100755 --- a/controller-test.sh +++ b/controller-test.sh @@ -83,6 +83,11 @@ build_deploy_controller () { echo "==> Install CRDs and deploy controller" make install + # set the images into config.properties so that kustomize can read them + echo "OVERRIDE_BUILD_DEPLOY_DIND_IMAGE=$OVERRIDE_BUILD_DEPLOY_DIND_IMAGE" > config/default/config.properties + echo "HARBOR_URL=$HARBOR_URL" >> config/default/config.properties + echo "HARBOR_API=$HARBOR_API" >> config/default/config.properties + kind load docker-image ${CONTROLLER_IMAGE} --name ${KIND_NAME} make deploy IMG=${CONTROLLER_IMAGE} diff --git a/controllers/v1beta1/build_helpers.go b/controllers/v1beta1/build_helpers.go index a362783d..e921e8b6 100644 --- a/controllers/v1beta1/build_helpers.go +++ b/controllers/v1beta1/build_helpers.go @@ -888,10 +888,17 @@ func (r *LagoonBuildReconciler) updateQueuedBuild( } } // send any messages to lagoon message queues - // update the deployment with the status - r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusQueued) - r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusQueued) - r.buildLogsToLagoonLogs(ctx, opLog, &lagoonBuild, allContainerLogs, lagoonv1beta1.BuildStatusQueued) + // update the deployment with the status, lagoon v2.12.0 supports queued status, otherwise use pending + if helpers.CheckLagoonVersion(&lagoonBuild, "2.12.0") { + r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusQueued) + r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusQueued) + r.buildLogsToLagoonLogs(ctx, opLog, &lagoonBuild, allContainerLogs, lagoonv1beta1.BuildStatusQueued) + } else { + r.buildStatusLogsToLagoonLogs(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusPending) + r.updateDeploymentAndEnvironmentTask(ctx, opLog, &lagoonBuild, &lagoonEnv, lagoonv1beta1.BuildStatusPending) + r.buildLogsToLagoonLogs(ctx, opLog, &lagoonBuild, allContainerLogs, lagoonv1beta1.BuildStatusPending) + + } return nil } diff --git a/go.mod b/go.mod index 25fc6777..3b85d1e6 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/cheshir/go-mq v1.0.2 github.com/coreos/go-semver v0.3.0 github.com/go-logr/logr v1.2.3 + github.com/hashicorp/go-version v1.6.0 github.com/k8up-io/k8up/v2 v2.5.2 github.com/mittwald/goharbor-client/v3 v3.3.0 github.com/mittwald/goharbor-client/v5 v5.1.0 diff --git a/go.sum b/go.sum index 99d03200..183f0d4d 100644 --- a/go.sum +++ b/go.sum @@ -553,6 +553,8 @@ github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdv github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= +github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= diff --git a/internal/helpers/helpers.go b/internal/helpers/helpers.go index 0ffce401..6c515a0f 100644 --- a/internal/helpers/helpers.go +++ b/internal/helpers/helpers.go @@ -5,6 +5,7 @@ import ( "crypto/sha1" "crypto/sha256" "encoding/base32" + "encoding/json" "fmt" "math/rand" "os" @@ -15,6 +16,7 @@ import ( "time" "github.com/go-logr/logr" + "github.com/hashicorp/go-version" lagoonv1beta1 "github.com/uselagoon/remote-controller/apis/lagoon/v1beta1" apierrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" @@ -219,6 +221,53 @@ func VariableExists(vars *[]LagoonEnvironmentVariable, name, value string) bool return exists } +// GetLagoonVariable returns a given environment variable +func GetLagoonVariable(name string, scope []string, variables []LagoonEnvironmentVariable) (*LagoonEnvironmentVariable, error) { + for _, v := range variables { + scoped := true + if scope != nil { + scoped = false + if Contains(scope, v.Scope) { + scoped = true + } + } + if v.Name == name && scoped { + return &v, nil + } + } + return nil, fmt.Errorf("variable %s not found", name) +} + +// Contains checks if a string slice contains a specific string. +func Contains(s []string, str string) bool { + for _, v := range s { + if v == str { + return true + } + } + + return false +} + +// Check if the version of lagoon provided in the internal_system scope variable is greater than or equal to the checked version +func CheckLagoonVersion(build *lagoonv1beta1.LagoonBuild, checkVersion string) bool { + lagoonProjectVariables := &[]LagoonEnvironmentVariable{} + json.Unmarshal(build.Spec.Project.Variables.Project, lagoonProjectVariables) + lagoonVersion, err := GetLagoonVariable("LAGOON_SYSTEM_CORE_VERSION", []string{"internal_system"}, *lagoonProjectVariables) + if err != nil { + return false + } + aVer, err := version.NewSemver(lagoonVersion.Value) + if err != nil { + return false + } + bVer, err := version.NewSemver(checkVersion) + if err != nil { + return false + } + return aVer.GreaterThanOrEqual(bVer) +} + // CancelExtraBuilds cancels extra builds. func CancelExtraBuilds(ctx context.Context, r client.Client, opLog logr.Logger, pendingBuilds *lagoonv1beta1.LagoonBuildList, ns string, status string) error { listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{ diff --git a/internal/helpers/helpers_test.go b/internal/helpers/helpers_test.go index e1181b7d..c2191e96 100644 --- a/internal/helpers/helpers_test.go +++ b/internal/helpers/helpers_test.go @@ -4,6 +4,8 @@ import ( "os" "reflect" "testing" + + lagoonv1beta1 "github.com/uselagoon/remote-controller/apis/lagoon/v1beta1" ) func TestShortName(t *testing.T) { @@ -256,3 +258,103 @@ func TestGetLagoonFeatureFlags(t *testing.T) { }) } } + +func TestCheckLagoonVersion(t *testing.T) { + type args struct { + build *lagoonv1beta1.LagoonBuild + checkVersion string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "test1", + args: args{ + build: &lagoonv1beta1.LagoonBuild{ + Spec: lagoonv1beta1.LagoonBuildSpec{ + Project: lagoonv1beta1.Project{ + Variables: lagoonv1beta1.Variables{ + Project: []byte(`[{"name":"LAGOON_SYSTEM_CORE_VERSION","value":"v2.12.0","scope":"internal_system"}]`), + }, + }, + }, + }, + checkVersion: "2.12.0", + }, + want: true, + }, + { + name: "test2", + args: args{ + build: &lagoonv1beta1.LagoonBuild{ + Spec: lagoonv1beta1.LagoonBuildSpec{ + Project: lagoonv1beta1.Project{ + Variables: lagoonv1beta1.Variables{ + Project: []byte(`[{"name":"LAGOON_SYSTEM_CORE_VERSION","value":"v2.11.0","scope":"internal_system"}]`), + }, + }, + }, + }, + checkVersion: "2.12.0", + }, + want: false, + }, + { + name: "test3", + args: args{ + build: &lagoonv1beta1.LagoonBuild{ + Spec: lagoonv1beta1.LagoonBuildSpec{ + Project: lagoonv1beta1.Project{ + Variables: lagoonv1beta1.Variables{ + Project: []byte(`[]`), + }, + }, + }, + }, + checkVersion: "2.12.0", + }, + want: false, + }, + { + name: "test4", + args: args{ + build: &lagoonv1beta1.LagoonBuild{ + Spec: lagoonv1beta1.LagoonBuildSpec{ + Project: lagoonv1beta1.Project{ + Variables: lagoonv1beta1.Variables{ + Project: []byte(`[{"name":"LAGOON_SYSTEM_CORE_VERSION","value":"v2.12.0","scope":"internal_system"}]`), + }, + }, + }, + }, + checkVersion: "v2.12.0", + }, + want: true, + }, + { + name: "test5", + args: args{ + build: &lagoonv1beta1.LagoonBuild{ + Spec: lagoonv1beta1.LagoonBuildSpec{ + Project: lagoonv1beta1.Project{ + Variables: lagoonv1beta1.Variables{ + Project: []byte(`[{"name":"LAGOON_SYSTEM_CORE_VERSION","value":"v2.11.0","scope":"internal_system"}]`), + }, + }, + }, + }, + checkVersion: "v2.12.0", + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := CheckLagoonVersion(tt.args.build, tt.args.checkVersion); got != tt.want { + t.Errorf("CheckLagoonVersion() = %v, want %v", got, tt.want) + } + }) + } +} From 073bf9b4b52ae342acfa78b845143bbc0ed69134 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Tue, 7 Mar 2023 11:22:30 +1100 Subject: [PATCH 3/3] chore: rename function --- internal/helpers/helpers.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/internal/helpers/helpers.go b/internal/helpers/helpers.go index 6c515a0f..6d9571e2 100644 --- a/internal/helpers/helpers.go +++ b/internal/helpers/helpers.go @@ -226,10 +226,7 @@ func GetLagoonVariable(name string, scope []string, variables []LagoonEnvironmen for _, v := range variables { scoped := true if scope != nil { - scoped = false - if Contains(scope, v.Scope) { - scoped = true - } + scoped = containsStr(scope, v.Scope) } if v.Name == name && scoped { return &v, nil @@ -238,14 +235,13 @@ func GetLagoonVariable(name string, scope []string, variables []LagoonEnvironmen return nil, fmt.Errorf("variable %s not found", name) } -// Contains checks if a string slice contains a specific string. -func Contains(s []string, str string) bool { +// containsStr checks if a string slice contains a specific string. +func containsStr(s []string, str string) bool { for _, v := range s { if v == str { return true } } - return false }