Skip to content

Commit

Permalink
Use different labels for lookout pruner
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Parraga <[email protected]>
  • Loading branch information
Sovietaced committed Nov 18, 2024
1 parent 0cf5bc4 commit 5710e79
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 7 deletions.
16 changes: 9 additions & 7 deletions internal/controller/install/lookout_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,30 +468,32 @@ func createLookoutCronJob(lookout *installv1alpha1.Lookout, serviceAccountName s
return nil, err
}

name := lookout.Name + "-db-pruner"

job := batchv1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: lookout.Name + "-db-pruner",
Name: name,
Namespace: lookout.Namespace,
Labels: AllLabels(lookout.Name, lookout.Labels),
Labels: AllLabels(name, lookout.Labels),
Annotations: map[string]string{"checksum/config": GenerateChecksumConfig(lookout.Spec.ApplicationConfig.Raw)},
},
Spec: batchv1.CronJobSpec{
Schedule: dbPruningSchedule,
JobTemplate: batchv1.JobTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: lookout.Name + "-db-pruner",
Name: name,
Namespace: lookout.Namespace,
Labels: AllLabels(lookout.Name, lookout.Labels),
Labels: AllLabels(name, lookout.Labels),
},
Spec: batchv1.JobSpec{
Parallelism: &parallelism,
Completions: &completions,
BackoffLimit: &backoffLimit,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: lookout.Name + "-db-pruner",
Name: name,
Namespace: lookout.Namespace,
Labels: AllLabels(lookout.Name, lookout.Labels),
Labels: AllLabels(name, lookout.Labels),
},
Spec: corev1.PodSpec{
ServiceAccountName: serviceAccountName,
Expand Down Expand Up @@ -522,7 +524,7 @@ func createLookoutCronJob(lookout *installv1alpha1.Lookout, serviceAccountName s
},
}},
Containers: []corev1.Container{{
Name: "lookout-db-pruner",
Name: name,
ImagePullPolicy: corev1.PullIfNotPresent,
Image: ImageString(lookout.Spec.Image),
Args: []string{
Expand Down
152 changes: 152 additions & 0 deletions internal/controller/install/lookout_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/testing/protocmp"

"github.com/armadaproject/armada-operator/internal/controller/builders"

"k8s.io/utils/ptr"
Expand Down Expand Up @@ -344,6 +347,155 @@ func TestLookoutReconciler_CreateCronJobErrorDueToApplicationConfig(t *testing.T
assert.Equal(t, "yaml: line 1: did not find expected ',' or '}'", err.Error())
}

func TestLookoutReconciler_CreateCronJob(t *testing.T) {
t.Parallel()

dbPruningEnabled := true
dbPruningSchedule := "1d"

lookout := v1alpha1.Lookout{
TypeMeta: metav1.TypeMeta{
Kind: "Lookout",
APIVersion: "install.armadaproject.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "lookout",
DeletionTimestamp: &metav1.Time{Time: time.Now()},
Finalizers: []string{operatorFinalizer},
},
Spec: v1alpha1.LookoutSpec{
CommonSpecBase: installv1alpha1.CommonSpecBase{
Labels: nil,
Image: v1alpha1.Image{
Repository: "testrepo",
Tag: "1.0.0",
},
ApplicationConfig: runtime.RawExtension{Raw: []byte(`{}`)},
Resources: &corev1.ResourceRequirements{},
},
Replicas: ptr.To[int32](2),
ClusterIssuer: "test",
Ingress: &v1alpha1.IngressConfig{
IngressClass: "nginx",
},
DbPruningEnabled: &dbPruningEnabled,
DbPruningSchedule: &dbPruningSchedule,
},
}
cronJob, err := createLookoutCronJob(&lookout, "lookout")
assert.NoError(t, err)

var expectedParallelism int32 = 1
var expectedCompletions int32 = 1
var expectedBackoffLimit int32 = 0
var expectedTerminationGracePeriodSeconds int64 = 0

expectedCronJob := &batchv1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: "lookout-db-pruner",
Namespace: "default",
Labels: map[string]string{
"app": "lookout-db-pruner",
"release": "lookout-db-pruner",
},
Annotations: map[string]string{
"checksum/config": "44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
},
},
Spec: batchv1.CronJobSpec{
JobTemplate: batchv1.JobTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "lookout-db-pruner",
Namespace: "default",
Labels: map[string]string{
"app": "lookout-db-pruner",
"release": "lookout-db-pruner",
},
},
Spec: batchv1.JobSpec{
Parallelism: &expectedParallelism,
Completions: &expectedCompletions,
BackoffLimit: &expectedBackoffLimit,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "lookout-db-pruner",
Namespace: "default",
Labels: map[string]string{
"app": "lookout-db-pruner",
"release": "lookout-db-pruner",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Args: []string{
"--pruneDatabase",
"--config",
"/config/application_config.yaml",
},
Image: "testrepo:1.0.0",
ImagePullPolicy: "IfNotPresent",
Name: "lookout-db-pruner",
VolumeMounts: []corev1.VolumeMount{
{
Name: "user-config",
ReadOnly: true,
MountPath: appConfigFilepath,
SubPath: "lookout-config.yaml",
},
},
},
},
InitContainers: []corev1.Container{
{
Name: "lookout-db-pruner-db-wait",
Image: "alpine:3.10",
Command: []string{
"/bin/sh",
"-c",
`echo "Waiting for Postres..."
while ! nc -z $PGHOST $PGPORT; do
sleep 1
done
echo "Postres started!"`,
},
Env: []corev1.EnvVar{
{
Name: "PGHOST",
},
{
Name: "PGPORT",
},
},
},
},
RestartPolicy: "Never",
ServiceAccountName: "lookout",
TerminationGracePeriodSeconds: &expectedTerminationGracePeriodSeconds,
Volumes: []corev1.Volume{
{
Name: "user-config",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "lookout",
},
},
},
},
},
},
},
},
Schedule: "1d",
},
}

if !cmp.Equal(expectedCronJob, cronJob, protocmp.Transform()) {
t.Fatalf("cronjob is not the same %s", cmp.Diff(expectedCronJob, cronJob, protocmp.Transform()))
}
}

func TestLookoutReconciler_ReconcileDeletingLookout(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 5710e79

Please sign in to comment.