Add ability to provide annotations to the jobs.

This commit is contained in:
2024-09-11 13:09:54 +01:00
parent bbb41681df
commit fffbae11d8
33 changed files with 143 additions and 85 deletions
@@ -3,6 +3,7 @@ package raczylocom
import (
"context"
"fmt"
"os"
"strings"
"time"
@@ -34,6 +35,7 @@ type ClusterImageReconciler struct {
// +kubebuilder:rbac:groups=raczylo.com,resources=*/finalizers,verbs=update
// # additional RBAC rules - create and manage jobs
// +kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch;create;update;patch;delete
func (r *ClusterImageReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := log.FromContext(ctx)
@@ -227,10 +229,12 @@ func (r *ClusterImageReconciler) createBackupJob(ctx context.Context, clusterIma
defaultCommands = append(defaultCommands, "rm -f /tmp/"+normalisedImageName+".tar")
jobParams := shared.JobParams{
Name: fmt.Sprintf("img-export-%s", clusterImage.Name),
Namespace: clusterImage.Namespace,
Image: shared.BACKUP_JOB_IMAGE,
Commands: defaultCommands,
Name: fmt.Sprintf("img-export-%s", clusterImage.Name),
Namespace: clusterImage.Namespace,
Image: shared.BACKUP_JOB_IMAGE,
Annotations: clusterImage.Spec.JobAnnotations,
Commands: defaultCommands,
ServiceAccount: os.Getenv("POD_SERVICE_ACCOUNT"),
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: clusterImage.APIVersion,
@@ -4,6 +4,7 @@ import (
"context"
"crypto/md5"
"fmt"
"os"
"strings"
"time"
@@ -138,6 +139,7 @@ func (r *ClusterImageExportReconciler) Reconcile(ctx context.Context, req ctrl.R
Storage: clusterImageExport.Spec.Storage.StorageTarget,
ExportName: clusterImageExport.Name,
ExportPath: clusterImageExport.Spec.BasePath,
JobAnnotations: clusterImageExport.Spec.JobAnnotations,
},
}
@@ -267,10 +269,12 @@ func (r *ClusterImageExportReconciler) runCleanupJob(ctx context.Context, cluste
}
jobParams := shared.JobParams{
Name: normalisedImageName,
Namespace: clusterImageExport.Namespace,
Image: shared.BACKUP_JOB_IMAGE,
Commands: defaultCommands,
Name: normalisedImageName,
Namespace: clusterImageExport.Namespace,
Image: shared.BACKUP_JOB_IMAGE,
Commands: defaultCommands,
Annotations: clusterImageExport.Spec.JobAnnotations,
ServiceAccount: os.Getenv("POD_SERVICE_ACCOUNT"),
}
cleanupJob := shared.CreateJob(jobParams, func(raczylocomv1.ClusterImageExport) []string { return nil })
+6 -1
View File
@@ -14,10 +14,12 @@ import (
type JobParams struct {
Name string
Namespace string
Annotations map[string]string
Image string
Commands []string
EnvVars []corev1.EnvVar
OwnerReferences []metav1.OwnerReference
ServiceAccount string
}
func CreateJob[T any](params JobParams, setupFunc func(T) []string) *batchv1.Job {
@@ -29,6 +31,7 @@ func CreateJob[T any](params JobParams, setupFunc func(T) []string) *batchv1.Job
Labels: map[string]string{
"app": "image-export",
},
Annotations: params.Annotations,
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
@@ -36,9 +39,11 @@ func CreateJob[T any](params JobParams, setupFunc func(T) []string) *batchv1.Job
Labels: map[string]string{
"app": "image-export",
},
Annotations: params.Annotations,
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyOnFailure,
RestartPolicy: corev1.RestartPolicyOnFailure,
ServiceAccountName: params.ServiceAccount,
Containers: []corev1.Container{
{
Name: "export",