Skip to content

Commit

Permalink
Merge pull request #196 from uselagoon/queued-status
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Mar 7, 2023
2 parents 3dbe104 + 073bf9b commit 2919437
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 42 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/remote-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
49 changes: 12 additions & 37 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
5 changes: 5 additions & 0 deletions controller-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
16 changes: 11 additions & 5 deletions controllers/v1beta1/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,11 +888,17 @@ 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)
// 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
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
45 changes: 45 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha1"
"crypto/sha256"
"encoding/base32"
"encoding/json"
"fmt"
"math/rand"
"os"
Expand All @@ -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"
Expand Down Expand Up @@ -219,6 +221,49 @@ 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 = containsStr(scope, v.Scope)
}
if v.Name == name && scoped {
return &v, nil
}
}
return nil, fmt.Errorf("variable %s not found", name)
}

// 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
}

// 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{
Expand Down
102 changes: 102 additions & 0 deletions internal/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"reflect"
"testing"

lagoonv1beta1 "github.com/uselagoon/remote-controller/apis/lagoon/v1beta1"
)

func TestShortName(t *testing.T) {
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 2919437

Please sign in to comment.