Populating crd labels and annotations in logical backup job pod manifest (#2456)

* Adding custom pod labels to logical backup job
* Adding custom annotations to logical backup job pod
* Adding job InheritedAnnotations and InheritedLabel tests
This commit is contained in:
andrejshapal 2024-01-04 15:03:16 +02:00 committed by GitHub
parent dad5b132ec
commit 0367a07ba8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 12 deletions

BIN
pkg/cluster/__debug_bin4170763078 Executable file

Binary file not shown.

View File

@ -2221,11 +2221,12 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
nil, nil,
) )
labels := map[string]string{ logicalBackupJobLabel := map[string]string{
c.OpConfig.ClusterNameLabel: c.Name, "application": "spilo-logical-backup",
"application": "spilo-logical-backup",
} }
labels := labels.Merge(c.labelsSet(true), logicalBackupJobLabel)
nodeAffinity := c.nodeAffinity(c.OpConfig.NodeReadinessLabel, nil) nodeAffinity := c.nodeAffinity(c.OpConfig.NodeReadinessLabel, nil)
podAffinity := podAffinity( podAffinity := podAffinity(
labels, labels,
@ -2241,7 +2242,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
if podTemplate, err = c.generatePodTemplate( if podTemplate, err = c.generatePodTemplate(
c.Namespace, c.Namespace,
labels, labels,
annotations, c.annotationsSet(annotations),
logicalBackupContainer, logicalBackupContainer,
[]v1.Container{}, []v1.Container{},
[]v1.Container{}, []v1.Container{},

View File

@ -3087,7 +3087,9 @@ func TestGenerateResourceRequirements(t *testing.T) {
func TestGenerateLogicalBackupJob(t *testing.T) { func TestGenerateLogicalBackupJob(t *testing.T) {
clusterName := "acid-test-cluster" clusterName := "acid-test-cluster"
teamId := "test"
configResources := config.Resources{ configResources := config.Resources{
ClusterNameLabel: "cluster-name",
DefaultCPURequest: "100m", DefaultCPURequest: "100m",
DefaultCPULimit: "1", DefaultCPULimit: "1",
DefaultMemoryRequest: "100Mi", DefaultMemoryRequest: "100Mi",
@ -3095,12 +3097,14 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
} }
tests := []struct { tests := []struct {
subTest string subTest string
config config.Config config config.Config
specSchedule string specSchedule string
expectedSchedule string expectedSchedule string
expectedJobName string expectedJobName string
expectedResources acidv1.Resources expectedResources acidv1.Resources
expectedAnnotation map[string]string
expectedLabel map[string]string
}{ }{
{ {
subTest: "test generation of logical backup pod resources when not configured", 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"}, ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "100Mi"},
ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "500Mi"}, 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", 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"}, ResourceRequests: acidv1.ResourceDescription{CPU: "10m", Memory: "50Mi"},
ResourceLimits: acidv1.ResourceDescription{CPU: "300m", Memory: "300Mi"}, 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", 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"}, ResourceRequests: acidv1.ResourceDescription{CPU: "50m", Memory: "100Mi"},
ResourceLimits: acidv1.ResourceDescription{CPU: "250m", Memory: "500Mi"}, 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", 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"}, ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "200Mi"},
ResourceLimits: acidv1.ResourceDescription{CPU: "1", 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{ Config{
OpConfig: tt.config, OpConfig: tt.config,
}, k8sutil.NewMockKubernetesClient(), acidv1.Postgresql{}, logger, eventRecorder) }, k8sutil.NewMockKubernetesClient(), acidv1.Postgresql{}, logger, eventRecorder)
cluster.ObjectMeta.Name = clusterName 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 cluster.Spec.LogicalBackupSchedule = tt.specSchedule
cronJob, err := cluster.generateLogicalBackupJob() cronJob, err := cluster.generateLogicalBackupJob()
assert.NoError(t, err) assert.NoError(t, err)
if cronJob.Spec.Schedule != tt.expectedSchedule { if cronJob.Spec.Schedule != tt.expectedSchedule {
t.Errorf("%s - %s: expected schedule %s, got %s", t.Name(), tt.subTest, tt.expectedSchedule, cronJob.Spec.Schedule) 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) 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 containers := cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers
clusterResources, err := parseResourceRequirements(containers[0].Resources) clusterResources, err := parseResourceRequirements(containers[0].Resources)
assert.NoError(t, err) assert.NoError(t, err)