From c04f87fd5b0943cb12cc9fef853c6e6196baf274 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 18 Dec 2024 14:09:47 -0600 Subject: [PATCH] feat: add support for organization environment variables --- api/lagoon/v1beta1/lagoonbuild_types.go | 5 +++-- api/lagoon/v1beta2/lagoonbuild_types.go | 5 +++-- internal/controllers/v1beta1/build_helpers.go | 15 +++++++++++++- .../controllers/v1beta1/task_controller.go | 20 +++++++++++++++++++ internal/controllers/v1beta2/build_helpers.go | 15 +++++++++++++- .../controllers/v1beta2/task_controller.go | 20 +++++++++++++++++++ 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/api/lagoon/v1beta1/lagoonbuild_types.go b/api/lagoon/v1beta1/lagoonbuild_types.go index 755c81a1..e3155d0e 100644 --- a/api/lagoon/v1beta1/lagoonbuild_types.go +++ b/api/lagoon/v1beta1/lagoonbuild_types.go @@ -147,8 +147,9 @@ type Organization struct { // Variables contains the project and environment variables from lagoon. type LagoonVariables struct { - Project []byte `json:"project,omitempty"` - Environment []byte `json:"environment,omitempty"` + Organization []byte `json:"organization,omitempty"` + Project []byte `json:"project,omitempty"` + Environment []byte `json:"environment,omitempty"` } // Branch contains the branch name used for a branch deployment. diff --git a/api/lagoon/v1beta2/lagoonbuild_types.go b/api/lagoon/v1beta2/lagoonbuild_types.go index 01a5b4e0..4f15c972 100644 --- a/api/lagoon/v1beta2/lagoonbuild_types.go +++ b/api/lagoon/v1beta2/lagoonbuild_types.go @@ -143,8 +143,9 @@ type Organization struct { // Variables contains the project and environment variables from lagoon. type LagoonVariables struct { - Project []byte `json:"project,omitempty"` - Environment []byte `json:"environment,omitempty"` + Organization []byte `json:"organization,omitempty"` + Project []byte `json:"project,omitempty"` + Environment []byte `json:"environment,omitempty"` } // Branch contains the branch name used for a branch deployment. diff --git a/internal/controllers/v1beta1/build_helpers.go b/internal/controllers/v1beta1/build_helpers.go index 56ed05f2..52441884 100644 --- a/internal/controllers/v1beta1/build_helpers.go +++ b/internal/controllers/v1beta1/build_helpers.go @@ -571,8 +571,10 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log // if local/regional harbor is enabled if r.LFFHarborEnabled { // unmarshal the project variables + lagoonOrganizationVariables := &[]helpers.LagoonEnvironmentVariable{} lagoonProjectVariables := &[]helpers.LagoonEnvironmentVariable{} lagoonEnvironmentVariables := &[]helpers.LagoonEnvironmentVariable{} + json.Unmarshal(lagoonBuild.Spec.Project.Variables.Organization, lagoonOrganizationVariables) json.Unmarshal(lagoonBuild.Spec.Project.Variables.Project, lagoonProjectVariables) json.Unmarshal(lagoonBuild.Spec.Project.Variables.Environment, lagoonEnvironmentVariables) // check if INTERNAL_REGISTRY_SOURCE_LAGOON is defined, and if it isn't true @@ -580,7 +582,8 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log // if it is false, or not set, then we use what is provided by this controller // this allows us to make it so a specific environment or the project entirely // can still use whats provided by lagoon - if !helpers.VariableExists(lagoonProjectVariables, "INTERNAL_REGISTRY_SOURCE_LAGOON", "true") || + if !helpers.VariableExists(lagoonOrganizationVariables, "INTERNAL_REGISTRY_SOURCE_LAGOON", "true") || + !helpers.VariableExists(lagoonProjectVariables, "INTERNAL_REGISTRY_SOURCE_LAGOON", "true") || !helpers.VariableExists(lagoonEnvironmentVariables, "INTERNAL_REGISTRY_SOURCE_LAGOON", "true") { // source the robot credential, and inject it into the lagoon project variables // this will overwrite what is provided by lagoon (if lagoon has provided them) @@ -617,6 +620,16 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log lagoonBuild.Spec.Project.Variables.Project, _ = json.Marshal(lagoonProjectVariables) } } + if lagoonBuild.Spec.Project.Variables.Organization != nil { + // if this is 2 bytes long, then it means its just an empty json array + // we only want to add it if it is more than 2 bytes + if len(lagoonBuild.Spec.Project.Variables.Organization) > 2 { + podEnvs = append(podEnvs, corev1.EnvVar{ + Name: "LAGOON_ORGANIZATION_VARIABLES", + Value: string(lagoonBuild.Spec.Project.Variables.Organization), + }) + } + } if lagoonBuild.Spec.Project.Variables.Project != nil { // if this is 2 bytes long, then it means its just an empty json array // we only want to add it if it is more than 2 bytes diff --git a/internal/controllers/v1beta1/task_controller.go b/internal/controllers/v1beta1/task_controller.go index b9572290..cd62baf1 100644 --- a/internal/controllers/v1beta1/task_controller.go +++ b/internal/controllers/v1beta1/task_controller.go @@ -228,6 +228,16 @@ func (r *LagoonTaskReconciler) getTaskPodDeployment(ctx context.Context, lagoonT Value: r.ProxyConfig.NoProxy, }) } + if lagoonTask.Spec.Project.Variables.Organization != nil { + // if this is 2 bytes long, then it means its just an empty json array + // we only want to add it if it is more than 2 bytes + if len(lagoonTask.Spec.Project.Variables.Organization) > 2 { + dep.Spec.Template.Spec.Containers[idx].Env = append(dep.Spec.Template.Spec.Containers[idx].Env, corev1.EnvVar{ + Name: "LAGOON_ORGANIZATION_VARIABLES", + Value: string(lagoonTask.Spec.Project.Variables.Organization), + }) + } + } if lagoonTask.Spec.Project.Variables.Project != nil { // if this is 2 bytes long, then it means its just an empty json array // we only want to add it if it is more than 2 bytes @@ -512,6 +522,16 @@ func (r *LagoonTaskReconciler) createAdvancedTask(ctx context.Context, lagoonTas Value: lagoonTask.Spec.Task.ID, }, } + if lagoonTask.Spec.Project.Variables.Organization != nil { + // if this is 2 bytes long, then it means its just an empty json array + // we only want to add it if it is more than 2 bytes + if len(lagoonTask.Spec.Project.Variables.Organization) > 2 { + podEnvs = append(podEnvs, corev1.EnvVar{ + Name: "LAGOON_ORGANIZATION_VARIABLES", + Value: string(lagoonTask.Spec.Project.Variables.Organization), + }) + } + } if lagoonTask.Spec.Project.Variables.Project != nil { // if this is 2 bytes long, then it means its just an empty json array // we only want to add it if it is more than 2 bytes diff --git a/internal/controllers/v1beta2/build_helpers.go b/internal/controllers/v1beta2/build_helpers.go index a72d03e7..91c8f0db 100644 --- a/internal/controllers/v1beta2/build_helpers.go +++ b/internal/controllers/v1beta2/build_helpers.go @@ -588,8 +588,10 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log // if local/regional harbor is enabled if r.LFFHarborEnabled { // unmarshal the project variables + lagoonOrganizationVariables := &[]helpers.LagoonEnvironmentVariable{} lagoonProjectVariables := &[]helpers.LagoonEnvironmentVariable{} lagoonEnvironmentVariables := &[]helpers.LagoonEnvironmentVariable{} + json.Unmarshal(lagoonBuild.Spec.Project.Variables.Organization, lagoonOrganizationVariables) json.Unmarshal(lagoonBuild.Spec.Project.Variables.Project, lagoonProjectVariables) json.Unmarshal(lagoonBuild.Spec.Project.Variables.Environment, lagoonEnvironmentVariables) // check if INTERNAL_REGISTRY_SOURCE_LAGOON is defined, and if it isn't true @@ -597,7 +599,8 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log // if it is false, or not set, then we use what is provided by this controller // this allows us to make it so a specific environment or the project entirely // can still use whats provided by lagoon - if !helpers.VariableExists(lagoonProjectVariables, "INTERNAL_REGISTRY_SOURCE_LAGOON", "true") || + if !helpers.VariableExists(lagoonOrganizationVariables, "INTERNAL_REGISTRY_SOURCE_LAGOON", "true") || + !helpers.VariableExists(lagoonProjectVariables, "INTERNAL_REGISTRY_SOURCE_LAGOON", "true") || !helpers.VariableExists(lagoonEnvironmentVariables, "INTERNAL_REGISTRY_SOURCE_LAGOON", "true") { // source the robot credential, and inject it into the lagoon project variables // this will overwrite what is provided by lagoon (if lagoon has provided them) @@ -634,6 +637,16 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log lagoonBuild.Spec.Project.Variables.Project, _ = json.Marshal(lagoonProjectVariables) } } + if lagoonBuild.Spec.Project.Variables.Organization != nil { + // if this is 2 bytes long, then it means its just an empty json array + // we only want to add it if it is more than 2 bytes + if len(lagoonBuild.Spec.Project.Variables.Organization) > 2 { + podEnvs = append(podEnvs, corev1.EnvVar{ + Name: "LAGOON_ORGANIZATION_VARIABLES", + Value: string(lagoonBuild.Spec.Project.Variables.Organization), + }) + } + } if lagoonBuild.Spec.Project.Variables.Project != nil { // if this is 2 bytes long, then it means its just an empty json array // we only want to add it if it is more than 2 bytes diff --git a/internal/controllers/v1beta2/task_controller.go b/internal/controllers/v1beta2/task_controller.go index 6eea9829..02f1067f 100644 --- a/internal/controllers/v1beta2/task_controller.go +++ b/internal/controllers/v1beta2/task_controller.go @@ -225,6 +225,16 @@ func (r *LagoonTaskReconciler) getTaskPodDeployment(ctx context.Context, lagoonT Value: r.ProxyConfig.NoProxy, }) } + if lagoonTask.Spec.Project.Variables.Organization != nil { + // if this is 2 bytes long, then it means its just an empty json array + // we only want to add it if it is more than 2 bytes + if len(lagoonTask.Spec.Project.Variables.Organization) > 2 { + dep.Spec.Template.Spec.Containers[idx].Env = append(dep.Spec.Template.Spec.Containers[idx].Env, corev1.EnvVar{ + Name: "LAGOON_ORGANIZATION_VARIABLES", + Value: string(lagoonTask.Spec.Project.Variables.Organization), + }) + } + } if lagoonTask.Spec.Project.Variables.Project != nil { // if this is 2 bytes long, then it means its just an empty json array // we only want to add it if it is more than 2 bytes @@ -515,6 +525,16 @@ func (r *LagoonTaskReconciler) createAdvancedTask(ctx context.Context, lagoonTas Value: lagoonTask.Spec.Task.ID, }, } + if lagoonTask.Spec.Project.Variables.Organization != nil { + // if this is 2 bytes long, then it means its just an empty json array + // we only want to add it if it is more than 2 bytes + if len(lagoonTask.Spec.Project.Variables.Organization) > 2 { + podEnvs = append(podEnvs, corev1.EnvVar{ + Name: "LAGOON_ORGANIZATION_VARIABLES", + Value: string(lagoonTask.Spec.Project.Variables.Organization), + }) + } + } if lagoonTask.Spec.Project.Variables.Project != nil { // if this is 2 bytes long, then it means its just an empty json array // we only want to add it if it is more than 2 bytes