job.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2026 LocoStack.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package reconciler
  14. import (
  15. "context"
  16. "fmt"
  17. "maps"
  18. "strings"
  19. "github.com/LocoStack/loco-operator/api/v1alpha1"
  20. batchv1 "k8s.io/api/batch/v1"
  21. v1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "sigs.k8s.io/controller-runtime/pkg/client"
  25. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  26. )
  27. type JobReconciler struct {
  28. client client.Client
  29. scheme *runtime.Scheme
  30. stack *v1alpha1.Stack
  31. component string
  32. name string
  33. owner metav1.Object
  34. }
  35. func NewJobReconciler(client client.Client, scheme *runtime.Scheme, stack *v1alpha1.Stack, component, name string, owner metav1.Object) *JobReconciler {
  36. return &JobReconciler{
  37. client: client,
  38. scheme: scheme,
  39. stack: stack,
  40. component: component,
  41. name: fmt.Sprintf("%s-%s", strings.ToLower(component), name),
  42. owner: owner,
  43. }
  44. }
  45. func (r *JobReconciler) ReconcileJob(ctx context.Context, tmpl *v1alpha1.Template, variables map[string]string) (bool, bool, error) {
  46. jobSpec, err := buildPodSpec(variables, tmpl)
  47. if err != nil {
  48. return false, true, fmt.Errorf("Failed to build job spec: %w", err)
  49. }
  50. jobSpec.RestartPolicy = v1.RestartPolicyOnFailure
  51. stackName := ""
  52. if r.stack != nil {
  53. stackName = r.stack.Name
  54. }
  55. labels := ResourceLabels(stackName, r.component, r.name, "")
  56. maps.Copy(labels, tmpl.Metadata.Labels)
  57. job := &batchv1.Job{ObjectMeta: metav1.ObjectMeta{Name: r.name, Namespace: r.owner.GetNamespace()}}
  58. jobCompleted := false
  59. jobFailed := false
  60. _, err = controllerutil.CreateOrUpdate(ctx, r.client, job, func() error {
  61. if job.CreationTimestamp.IsZero() {
  62. job.Labels = labels
  63. job.Spec = batchv1.JobSpec{
  64. Template: v1.PodTemplateSpec{
  65. ObjectMeta: metav1.ObjectMeta{
  66. Labels: labels,
  67. },
  68. Spec: jobSpec,
  69. },
  70. }
  71. }
  72. jobCompleted = job.Status.Succeeded >= 1
  73. jobFailed = job.Status.Failed >= 1
  74. return controllerutil.SetControllerReference(r.owner, job, r.scheme)
  75. })
  76. return jobCompleted, jobFailed, err
  77. }