Skip to content

Commit

Permalink
feat: add support for organization environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketeerbkw committed Dec 18, 2024
1 parent e7c023d commit c04f87f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
5 changes: 3 additions & 2 deletions api/lagoon/v1beta1/lagoonbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions api/lagoon/v1beta2/lagoonbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion internal/controllers/v1beta1/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,19 @@ 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
// if this value is true, then we want to use what is provided by Lagoon
// 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)
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions internal/controllers/v1beta1/task_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion internal/controllers/v1beta2/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,16 +588,19 @@ 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
// if this value is true, then we want to use what is provided by Lagoon
// 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)
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions internal/controllers/v1beta2/task_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c04f87f

Please sign in to comment.