Skip to content

Commit

Permalink
feat: add organization labels to namespace and build/task pods
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Nov 15, 2023
1 parent 22597d2 commit 76b1e50
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 4 deletions.
6 changes: 6 additions & 0 deletions apis/lagoon/v1beta1/lagoonbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ type Project struct {
EnvironmentIdling *int `json:"environmentIdling,omitempty"`
ProjectIdling *int `json:"projectIdling,omitempty"`
StorageCalculator *int `json:"storageCalculator,omitempty"`
Organization *Organization `json:"organization,omitempty"`
}

type Organization struct {
ID *uint `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}

// Variables contains the project and environment variables from lagoon.
Expand Down
1 change: 1 addition & 0 deletions apis/lagoon/v1beta1/lagoontask_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type LagoonTaskProject struct {
Name string `json:"name"`
NamespacePattern string `json:"namespacePattern,omitempty"`
Variables LagoonVariables `json:"variables,omitempty"`
Organization *Organization `json:"organization,omitempty"`
}

// LagoonTaskEnvironment defines the lagoon environment information.
Expand Down
30 changes: 30 additions & 0 deletions apis/lagoon/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions config/crd/bases/crd.lagoon.sh_lagoonbuilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ spec:
type: string
namespacePattern:
type: string
organization:
properties:
id:
type: integer
name:
type: string
type: object
productionEnvironment:
type: string
projectIdling:
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/crd.lagoon.sh_lagoontasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ spec:
type: string
namespacePattern:
type: string
organization:
properties:
id:
type: integer
name:
type: string
type: object
variables:
description: Variables contains the project and environment variables
from lagoon.
Expand Down
32 changes: 29 additions & 3 deletions controller-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CHECK_TIMEOUT=20
NS=nginx-example-main
LBUILD=7m5zypx
LBUILD2=8m5zypx
LBUILD3=9m5zypx

HARBOR_VERSION=${HARBOR_VERSION:-1.6.4}

Expand Down Expand Up @@ -201,6 +202,31 @@ kubectl -n $CONTROLLER_NAMESPACE patch lagoonbuilds.crd.lagoon.sh lagoon-build-$
sleep 10
check_lagoon_build lagoon-build-${LBUILD}

echo "==> Trigger a lagoon build using kubectl apply and check organization labels exist"
kubectl -n $CONTROLLER_NAMESPACE apply -f test-resources/example-project2.yaml
# patch the resource with the controller namespace
kubectl -n $CONTROLLER_NAMESPACE patch lagoonbuilds.crd.lagoon.sh lagoon-build-${LBUILD2} --type=merge --patch '{"metadata":{"labels":{"lagoon.sh/controller":"'$CONTROLLER_NAMESPACE'"}}}'
# patch the resource with a random label to bump the controller event filter
kubectl -n $CONTROLLER_NAMESPACE patch lagoonbuilds.crd.lagoon.sh lagoon-build-${LBUILD2} --type=merge --patch '{"metadata":{"labels":{"bump":"bump"}}}'
sleep 10
check_lagoon_build lagoon-build-${LBUILD2}
if ! $(kubectl get namespace -l 'organization.lagoon.sh/name=test-org' --no-headers 2> /dev/null | grep -q ${NS}); then
echo "==> Build failed to set organization name label on namespace"
clean_task_test_resources
check_controller_log ${1}
tear_down
echo "============== FAILED ==============="
exit 1
fi
if ! $(kubectl get namespace -l 'organization.lagoon.sh/id=123' --no-headers 2> /dev/null | grep -q ${NS}); then
echo "==> Build failed to set organization id label on namespace"
clean_task_test_resources
check_controller_log ${1}
tear_down
echo "============== FAILED ==============="
exit 1
fi

echo "==> Trigger a Task using kubectl apply to test dynamic secret mounting"

kubectl -n $NS apply -f test-resources/dynamic-secret-in-task-project1-secret.yaml
Expand Down Expand Up @@ -235,7 +261,7 @@ echo '
"routing_key":"ci-local-controller-kubernetes:builddeploy",
"payload":"{
\"metadata\": {
\"name\": \"lagoon-build-8m5zypx\"
\"name\": \"lagoon-build-9m5zypx\"
},
\"spec\": {
\"build\": {
Expand All @@ -246,7 +272,7 @@ echo '
\"project\": {
\"name\": \"nginx-example\",
\"environment\": \"main\",
\"uiLink\": \"https:\/\/dashboard.amazeeio.cloud\/projects\/project\/project-environment\/deployments\/lagoon-build-8m5zypx\",
\"uiLink\": \"https:\/\/dashboard.amazeeio.cloud\/projects\/project\/project-environment\/deployments\/lagoon-build-9m5zypx\",
\"routerPattern\": \"main-nginx-example\",
\"environmentType\": \"production\",
\"productionEnvironment\": \"main\",
Expand Down Expand Up @@ -274,7 +300,7 @@ echo '
curl -s -u guest:guest -H "Accept: application/json" -H "Content-Type:application/json" -X POST -d @payload.json http://172.17.0.1:15672/api/exchanges/%2f/lagoon-tasks/publish
echo ""
sleep 10
check_lagoon_build lagoon-build-${LBUILD2}
check_lagoon_build lagoon-build-${LBUILD3}

echo "==> Check pod cleanup worked"
CHECK_COUNTER=1
Expand Down
16 changes: 16 additions & 0 deletions controllers/v1beta1/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ func (r *LagoonBuildReconciler) getOrCreateNamespace(ctx context.Context, namesp
"lagoon.sh/environmentType": lagoonBuild.Spec.Project.EnvironmentType,
"lagoon.sh/controller": r.ControllerNamespace,
}
if lagoonBuild.Spec.Project.Organization != nil {
if lagoonBuild.Spec.Project.Organization.ID != nil {
nsLabels["organization.lagoon.sh/id"] = fmt.Sprintf("%d", *lagoonBuild.Spec.Project.Organization.ID)
}
if lagoonBuild.Spec.Project.Organization.Name != "" {
nsLabels["organization.lagoon.sh/name"] = lagoonBuild.Spec.Project.Organization.Name
}
}
if lagoonBuild.Spec.Project.ID != nil {
nsLabels["lagoon.sh/projectId"] = fmt.Sprintf("%d", *lagoonBuild.Spec.Project.ID)
}
Expand Down Expand Up @@ -820,6 +828,14 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log
},
},
}
if lagoonBuild.Spec.Project.Organization != nil {
if lagoonBuild.Spec.Project.Organization.ID != nil {
newPod.ObjectMeta.Labels["organization.lagoon.sh/id"] = fmt.Sprintf("%d", *lagoonBuild.Spec.Project.Organization.ID)
}
if lagoonBuild.Spec.Project.Organization.Name != "" {
newPod.ObjectMeta.Labels["organization.lagoon.sh/name"] = lagoonBuild.Spec.Project.Organization.Name
}
}

// set the pod security context, if defined to a non-default value
if r.BuildPodRunAsUser != 0 || r.BuildPodRunAsGroup != 0 ||
Expand Down
16 changes: 16 additions & 0 deletions controllers/v1beta1/task_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ func (r *LagoonTaskReconciler) getTaskPodDeployment(ctx context.Context, lagoonT
},
Spec: dep.Spec.Template.Spec,
}
if lagoonTask.Spec.Project.Organization != nil {
if lagoonTask.Spec.Project.Organization.ID != nil {
taskPod.ObjectMeta.Labels["organization.lagoon.sh/id"] = fmt.Sprintf("%d", *lagoonTask.Spec.Project.Organization.ID)
}
if lagoonTask.Spec.Project.Organization.Name != "" {
taskPod.ObjectMeta.Labels["organization.lagoon.sh/name"] = lagoonTask.Spec.Project.Organization.Name
}
}
return taskPod, nil
}
}
Expand Down Expand Up @@ -564,6 +572,14 @@ func (r *LagoonTaskReconciler) createAdvancedTask(ctx context.Context, lagoonTas
},
},
}
if lagoonTask.Spec.Project.Organization != nil {
if lagoonTask.Spec.Project.Organization.ID != nil {
newPod.ObjectMeta.Labels["organization.lagoon.sh/id"] = fmt.Sprintf("%d", *lagoonTask.Spec.Project.Organization.ID)
}
if lagoonTask.Spec.Project.Organization.Name != "" {
newPod.ObjectMeta.Labels["organization.lagoon.sh/name"] = lagoonTask.Spec.Project.Organization.Name
}
}
if lagoonTask.Spec.AdvancedTask.DeployerToken {
// start this with the serviceaccount so that it gets the token mounted into it
newPod.Spec.ServiceAccountName = "lagoon-deployer"
Expand Down
5 changes: 4 additions & 1 deletion test-resources/example-project2.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
kind: LagoonBuild
apiVersion: crd.lagoon.sh/v1beta1
metadata:
name: lagoon-build-9m5zypx
name: lagoon-build-8m5zypx
spec:
build:
ci: 'true' #to make sure that readwritemany is changed to readwriteonce
type: branch
gitReference: origin/main
project:
name: nginx-example
organization:
id: 123
name: test-org
environment: main
uiLink: https://dashboard.amazeeio.cloud/projects/project/project-environment/deployments/lagoon-build-7m5zypx
routerPattern: 'main-nginx-example'
Expand Down

0 comments on commit 76b1e50

Please sign in to comment.