Skip to content

Commit

Permalink
Merge pull request #146 from uselagoon/api-config-injection
Browse files Browse the repository at this point in the history
feat: inject lagoon api/ssh config into builds
  • Loading branch information
shreddedbacon authored Jul 13, 2022
2 parents c78e6de + 13137a5 commit e13d815
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
1 change: 1 addition & 0 deletions controllers/v1beta1/build_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type LagoonBuildReconciler struct {
NativeCronPodMinFrequency int
LagoonTargetName string
LagoonFeatureFlags map[string]string
LagoonAPIConfiguration LagoonAPIConfiguration
ProxyConfig ProxyConfig
}

Expand Down
14 changes: 14 additions & 0 deletions controllers/v1beta1/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,20 @@ func (r *LagoonBuildReconciler) processBuild(ctx context.Context, opLog logr.Log
Name: "NATIVE_CRON_POD_MINIMUM_FREQUENCY",
Value: strconv.Itoa(r.NativeCronPodMinFrequency),
},
// add the API and SSH endpoint configuration to environments
{
Name: "LAGOON_CONFIG_API_HOST",
Value: r.LagoonAPIConfiguration.APIHost,
},
{
Name: "LAGOON_CONFIG_SSH_HOST",
Value: r.LagoonAPIConfiguration.SSHHost,
},
{
Name: "LAGOON_CONFIG_SSH_PORT",
Value: r.LagoonAPIConfiguration.SSHPort,
},
// in the future, the SSH_HOST and SSH_PORT could also have regional variants
}
// add proxy variables to builds if they are defined
if r.ProxyConfig.HTTPProxy != "" {
Expand Down
59 changes: 45 additions & 14 deletions controllers/v1beta1/task_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ import (
// LagoonTaskReconciler reconciles a LagoonTask object
type LagoonTaskReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
ControllerNamespace string
NamespacePrefix string
RandomNamespacePrefix bool
TaskSettings LagoonTaskSettings
EnableDebug bool
LagoonTargetName string
ProxyConfig ProxyConfig
Log logr.Logger
Scheme *runtime.Scheme
ControllerNamespace string
NamespacePrefix string
RandomNamespacePrefix bool
LagoonAPIConfiguration LagoonAPIConfiguration
EnableDebug bool
LagoonTargetName string
ProxyConfig ProxyConfig
}

// LagoonTaskSettings is for the settings for task API/SSH host/ports
type LagoonTaskSettings struct {
// LagoonAPIConfiguration is for the settings for task API/SSH host/ports
type LagoonAPIConfiguration struct {
APIHost string
SSHHost string
SSHPort string
Expand Down Expand Up @@ -149,6 +149,7 @@ func (r *LagoonTaskReconciler) getTaskPodDeployment(ctx context.Context, lagoonT
hasService = true
// grab the container
for idx, depCon := range dep.Spec.Template.Spec.Containers {
// --- deprecate these at some point in favor of the `LAGOON_CONFIG_X` variants
dep.Spec.Template.Spec.Containers[idx].Env = append(dep.Spec.Template.Spec.Containers[idx].Env, corev1.EnvVar{
Name: "TASK_API_HOST",
Value: r.getTaskValue(lagoonTask, "TASK_API_HOST"),
Expand All @@ -161,6 +162,21 @@ func (r *LagoonTaskReconciler) getTaskPodDeployment(ctx context.Context, lagoonT
Name: "TASK_SSH_PORT",
Value: r.getTaskValue(lagoonTask, "TASK_SSH_PORT"),
})
// ^^ ---
// add the API and SSH endpoint configuration to environments
dep.Spec.Template.Spec.Containers[idx].Env = append(dep.Spec.Template.Spec.Containers[idx].Env, corev1.EnvVar{
Name: "LAGOON_CONFIG_API_HOST",
Value: r.getTaskValue(lagoonTask, "LAGOON_CONFIG_API_HOST"),
})
dep.Spec.Template.Spec.Containers[idx].Env = append(dep.Spec.Template.Spec.Containers[idx].Env, corev1.EnvVar{
Name: "LAGOON_CONFIG_SSH_HOST",
Value: r.getTaskValue(lagoonTask, "LAGOON_CONFIG_SSH_HOST"),
})
dep.Spec.Template.Spec.Containers[idx].Env = append(dep.Spec.Template.Spec.Containers[idx].Env, corev1.EnvVar{
Name: "LAGOON_CONFIG_SSH_PORT",
Value: r.getTaskValue(lagoonTask, "LAGOON_CONFIG_SSH_PORT"),
})
// in the future, the SSH_HOST and SSH_PORT could also have regional variants
dep.Spec.Template.Spec.Containers[idx].Env = append(dep.Spec.Template.Spec.Containers[idx].Env, corev1.EnvVar{
Name: "TASK_DATA_ID",
Value: lagoonTask.Spec.Task.ID,
Expand Down Expand Up @@ -495,17 +511,32 @@ func (r *LagoonTaskReconciler) getTaskValue(lagoonTask *lagoonv1beta1.LagoonTask
switch value {
case "TASK_API_HOST":
if lagoonTask.Spec.Task.APIHost == "" {
return r.TaskSettings.APIHost
return r.LagoonAPIConfiguration.APIHost
}
return lagoonTask.Spec.Task.APIHost
case "TASK_SSH_HOST":
if lagoonTask.Spec.Task.SSHHost == "" {
return r.TaskSettings.SSHHost
return r.LagoonAPIConfiguration.SSHHost
}
return lagoonTask.Spec.Task.SSHHost
case "TASK_SSH_PORT":
if lagoonTask.Spec.Task.SSHPort == "" {
return r.TaskSettings.SSHPort
return r.LagoonAPIConfiguration.SSHPort
}
return lagoonTask.Spec.Task.SSHPort
case "LAGOON_CONFIG_API_HOST":
if lagoonTask.Spec.Task.APIHost == "" {
return r.LagoonAPIConfiguration.APIHost
}
return lagoonTask.Spec.Task.APIHost
case "LAGOON_CONFIG_SSH_HOST":
if lagoonTask.Spec.Task.SSHHost == "" {
return r.LagoonAPIConfiguration.SSHHost
}
return lagoonTask.Spec.Task.SSHHost
case "LAGOON_CONFIG_SSH_PORT":
if lagoonTask.Spec.Task.SSHPort == "" {
return r.LagoonAPIConfiguration.SSHPort
}
return lagoonTask.Spec.Task.SSHPort
}
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,11 @@ func main() {
NativeCronPodMinFrequency: nativeCronPodMinFrequency,
LagoonTargetName: lagoonTargetName,
LagoonFeatureFlags: helpers.GetLagoonFeatureFlags(),
LagoonAPIConfiguration: lagoonv1beta1ctrl.LagoonAPIConfiguration{
APIHost: lagoonAPIHost,
SSHHost: lagoonSSHHost,
SSHPort: lagoonSSHPort,
},
ProxyConfig: lagoonv1beta1ctrl.ProxyConfig{
HTTPProxy: httpProxy,
HTTPSProxy: httpsProxy,
Expand Down Expand Up @@ -741,7 +746,7 @@ func main() {
ControllerNamespace: controllerNamespace,
NamespacePrefix: namespacePrefix,
RandomNamespacePrefix: randomPrefix,
TaskSettings: lagoonv1beta1ctrl.LagoonTaskSettings{
LagoonAPIConfiguration: lagoonv1beta1ctrl.LagoonAPIConfiguration{
APIHost: lagoonAPIHost,
SSHHost: lagoonSSHHost,
SSHPort: lagoonSSHPort,
Expand Down

0 comments on commit e13d815

Please sign in to comment.