Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow environment provider jobs not bound to cluster #320

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 19 additions & 38 deletions internal/controller/environmentrequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,23 @@ func (r EnvironmentRequestReconciler) environmentProviderJob(environmentrequest
grace := int64(30)
backoff := int32(0)

jobRef := &batchv1.Job{
envFrom := func(er *etosv1alpha1.EnvironmentRequest) []corev1.EnvFromSource {
andmat900 marked this conversation as resolved.
Show resolved Hide resolved
result := []corev1.EnvFromSource{}
if er.Spec.SecretRefName == "" {
return result
}
item := corev1.EnvFromSource{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: er.Spec.SecretRefName,
},
},
}
result = append(result, item)
return result
}

return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"etos.eiffel-community.github.io/id": environmentrequest.Spec.Identifier, // TODO: omitempty
Expand All @@ -247,6 +263,7 @@ func (r EnvironmentRequestReconciler) environmentProviderJob(environmentrequest
Name: environmentrequest.Name,
},
Spec: corev1.PodSpec{
ServiceAccountName: environmentrequest.Spec.ServiceAccountName,
TerminationGracePeriodSeconds: &grace,
RestartPolicy: "Never",
Containers: []corev1.Container{
Expand All @@ -264,7 +281,7 @@ func (r EnvironmentRequestReconciler) environmentProviderJob(environmentrequest
corev1.ResourceCPU: resource.MustParse("100m"),
},
},
EnvFrom: []corev1.EnvFromSource{},
EnvFrom: envFrom(environmentrequest),
Env: []corev1.EnvVar{
{
Name: "REQUEST",
Expand All @@ -277,42 +294,6 @@ func (r EnvironmentRequestReconciler) environmentProviderJob(environmentrequest
},
},
}
clusterLabelName := "etos.eiffel-community.github.io/cluster"
if environmentrequest.Labels[clusterLabelName] != "" {
cluster := environmentrequest.Labels[clusterLabelName]
jobRef.ObjectMeta.Labels[clusterLabelName] = cluster
jobRef.Spec.Template.Spec.ServiceAccountName = fmt.Sprintf("%s-provider", cluster)

envFrom := corev1.EnvFromSource{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: fmt.Sprintf("%s-environment-provider-cfg", cluster),
},
},
}
container := &jobRef.Spec.Template.Spec.Containers[0]
container.EnvFrom = append(container.EnvFrom, envFrom)
} else {
// ServiceAccountName is optional in environment request. If not given it will be set to empty string/omitted in jobRef.
jobRef.Spec.Template.Spec.ServiceAccountName = environmentrequest.Spec.ServiceAccountName

// SecretRefName is optional in environment request. If not given there, it will be set to empty string/omitted in jobRef
srName := environmentrequest.Spec.SecretRefName

container := &jobRef.Spec.Template.Spec.Containers[0]
container.EnvFrom = []corev1.EnvFromSource{}
if srName != "" {
envFrom := corev1.EnvFromSource{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: srName,
},
},
}
container.EnvFrom = append(container.EnvFrom, envFrom)
}
}
return jobRef
}

// registerOwnerIndexForJob will set an index of the jobs that an environment request owns.
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/testrun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ func (r TestRunReconciler) environmentRequest(testrun *etosv1alpha1.TestRun, sui
Splitter: etosv1alpha1.Splitter{
Tests: suite.Tests,
},
Image: testrun.Spec.EnvironmentProvider.Image,
Image: testrun.Spec.EnvironmentProvider.Image,
ServiceAccountName: fmt.Sprintf("%s-provider", testrun.Spec.Cluster),
SecretRefName: fmt.Sprintf("%s-environment-provider-cfg", testrun.Spec.Cluster),
},
}
}
Expand Down