diff --git a/pkg/cluster/__debug_bin4170763078 b/pkg/cluster/__debug_bin4170763078 new file mode 100755 index 000000000..3cf8f0afc Binary files /dev/null and b/pkg/cluster/__debug_bin4170763078 differ diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index ffe01c4a1..654df1ac9 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -2221,11 +2221,12 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) { nil, ) - labels := map[string]string{ - c.OpConfig.ClusterNameLabel: c.Name, - "application": "spilo-logical-backup", + logicalBackupJobLabel := map[string]string{ + "application": "spilo-logical-backup", } + labels := labels.Merge(c.labelsSet(true), logicalBackupJobLabel) + nodeAffinity := c.nodeAffinity(c.OpConfig.NodeReadinessLabel, nil) podAffinity := podAffinity( labels, @@ -2241,7 +2242,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) { if podTemplate, err = c.generatePodTemplate( c.Namespace, labels, - annotations, + c.annotationsSet(annotations), logicalBackupContainer, []v1.Container{}, []v1.Container{}, diff --git a/pkg/cluster/k8sres_test.go b/pkg/cluster/k8sres_test.go index 397c25358..700b75f14 100644 --- a/pkg/cluster/k8sres_test.go +++ b/pkg/cluster/k8sres_test.go @@ -3087,7 +3087,9 @@ func TestGenerateResourceRequirements(t *testing.T) { func TestGenerateLogicalBackupJob(t *testing.T) { clusterName := "acid-test-cluster" + teamId := "test" configResources := config.Resources{ + ClusterNameLabel: "cluster-name", DefaultCPURequest: "100m", DefaultCPULimit: "1", DefaultMemoryRequest: "100Mi", @@ -3095,12 +3097,14 @@ func TestGenerateLogicalBackupJob(t *testing.T) { } tests := []struct { - subTest string - config config.Config - specSchedule string - expectedSchedule string - expectedJobName string - expectedResources acidv1.Resources + subTest string + config config.Config + specSchedule string + expectedSchedule string + expectedJobName string + expectedResources acidv1.Resources + expectedAnnotation map[string]string + expectedLabel map[string]string }{ { subTest: "test generation of logical backup pod resources when not configured", @@ -3120,6 +3124,8 @@ func TestGenerateLogicalBackupJob(t *testing.T) { ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "100Mi"}, ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "500Mi"}, }, + expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId}, + expectedAnnotation: nil, }, { subTest: "test generation of logical backup pod resources when configured", @@ -3143,6 +3149,8 @@ func TestGenerateLogicalBackupJob(t *testing.T) { ResourceRequests: acidv1.ResourceDescription{CPU: "10m", Memory: "50Mi"}, ResourceLimits: acidv1.ResourceDescription{CPU: "300m", Memory: "300Mi"}, }, + expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId}, + expectedAnnotation: nil, }, { subTest: "test generation of logical backup pod resources when partly configured", @@ -3164,6 +3172,8 @@ func TestGenerateLogicalBackupJob(t *testing.T) { ResourceRequests: acidv1.ResourceDescription{CPU: "50m", Memory: "100Mi"}, ResourceLimits: acidv1.ResourceDescription{CPU: "250m", Memory: "500Mi"}, }, + expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId}, + expectedAnnotation: nil, }, { subTest: "test generation of logical backup pod resources with SetMemoryRequestToLimit enabled", @@ -3185,6 +3195,52 @@ func TestGenerateLogicalBackupJob(t *testing.T) { ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "200Mi"}, ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "200Mi"}, }, + expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId}, + expectedAnnotation: nil, + }, + { + subTest: "test generation of pod annotations when cluster InheritedLabel is set", + config: config.Config{ + Resources: config.Resources{ + ClusterNameLabel: "cluster-name", + InheritedLabels: []string{"labelKey"}, + DefaultCPURequest: "100m", + DefaultCPULimit: "1", + DefaultMemoryRequest: "100Mi", + DefaultMemoryLimit: "500Mi", + }, + }, + specSchedule: "", + expectedJobName: "acid-test-cluster", + expectedSchedule: "", + expectedResources: acidv1.Resources{ + ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "100Mi"}, + ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "500Mi"}, + }, + expectedLabel: map[string]string{"labelKey": "labelValue", "cluster-name": clusterName, "team": teamId}, + expectedAnnotation: nil, + }, + { + subTest: "test generation of pod annotations when cluster InheritedAnnotations is set", + config: config.Config{ + Resources: config.Resources{ + ClusterNameLabel: "cluster-name", + InheritedAnnotations: []string{"annotationKey"}, + DefaultCPURequest: "100m", + DefaultCPULimit: "1", + DefaultMemoryRequest: "100Mi", + DefaultMemoryLimit: "500Mi", + }, + }, + specSchedule: "", + expectedJobName: "acid-test-cluster", + expectedSchedule: "", + expectedResources: acidv1.Resources{ + ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "100Mi"}, + ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "500Mi"}, + }, + expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId}, + expectedAnnotation: map[string]string{"annotationKey": "annotationValue"}, }, } @@ -3193,12 +3249,19 @@ func TestGenerateLogicalBackupJob(t *testing.T) { Config{ OpConfig: tt.config, }, k8sutil.NewMockKubernetesClient(), acidv1.Postgresql{}, logger, eventRecorder) - cluster.ObjectMeta.Name = clusterName + cluster.Spec.TeamID = teamId + if cluster.ObjectMeta.Labels == nil { + cluster.ObjectMeta.Labels = make(map[string]string) + } + if cluster.ObjectMeta.Annotations == nil { + cluster.ObjectMeta.Annotations = make(map[string]string) + } + cluster.ObjectMeta.Labels["labelKey"] = "labelValue" + cluster.ObjectMeta.Annotations["annotationKey"] = "annotationValue" cluster.Spec.LogicalBackupSchedule = tt.specSchedule cronJob, err := cluster.generateLogicalBackupJob() assert.NoError(t, err) - if cronJob.Spec.Schedule != tt.expectedSchedule { t.Errorf("%s - %s: expected schedule %s, got %s", t.Name(), tt.subTest, tt.expectedSchedule, cronJob.Spec.Schedule) } @@ -3207,6 +3270,14 @@ func TestGenerateLogicalBackupJob(t *testing.T) { t.Errorf("%s - %s: expected job name %s, got %s", t.Name(), tt.subTest, tt.expectedJobName, cronJob.Name) } + if !reflect.DeepEqual(cronJob.Labels, tt.expectedLabel) { + t.Errorf("%s - %s: expected labels %s, got %s", t.Name(), tt.subTest, tt.expectedLabel, cronJob.Labels) + } + + if !reflect.DeepEqual(cronJob.Annotations, tt.expectedAnnotation) { + t.Errorf("%s - %s: expected annotations %s, got %s", t.Name(), tt.subTest, tt.expectedAnnotation, cronJob.Annotations) + } + containers := cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers clusterResources, err := parseResourceRequirements(containers[0].Resources) assert.NoError(t, err)