Skip to content

Commit 0367a07

Browse files
author
andrejshapal
authored
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
1 parent dad5b13 commit 0367a07

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

pkg/cluster/__debug_bin4170763078

66.1 MB
Binary file not shown.

pkg/cluster/k8sres.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,11 +2221,12 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
22212221
nil,
22222222
)
22232223

2224-
labels := map[string]string{
2225-
c.OpConfig.ClusterNameLabel: c.Name,
2226-
"application": "spilo-logical-backup",
2224+
logicalBackupJobLabel := map[string]string{
2225+
"application": "spilo-logical-backup",
22272226
}
22282227

2228+
labels := labels.Merge(c.labelsSet(true), logicalBackupJobLabel)
2229+
22292230
nodeAffinity := c.nodeAffinity(c.OpConfig.NodeReadinessLabel, nil)
22302231
podAffinity := podAffinity(
22312232
labels,
@@ -2241,7 +2242,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
22412242
if podTemplate, err = c.generatePodTemplate(
22422243
c.Namespace,
22432244
labels,
2244-
annotations,
2245+
c.annotationsSet(annotations),
22452246
logicalBackupContainer,
22462247
[]v1.Container{},
22472248
[]v1.Container{},

pkg/cluster/k8sres_test.go

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,20 +3087,24 @@ func TestGenerateResourceRequirements(t *testing.T) {
30873087

30883088
func TestGenerateLogicalBackupJob(t *testing.T) {
30893089
clusterName := "acid-test-cluster"
3090+
teamId := "test"
30903091
configResources := config.Resources{
3092+
ClusterNameLabel: "cluster-name",
30913093
DefaultCPURequest: "100m",
30923094
DefaultCPULimit: "1",
30933095
DefaultMemoryRequest: "100Mi",
30943096
DefaultMemoryLimit: "500Mi",
30953097
}
30963098

30973099
tests := []struct {
3098-
subTest string
3099-
config config.Config
3100-
specSchedule string
3101-
expectedSchedule string
3102-
expectedJobName string
3103-
expectedResources acidv1.Resources
3100+
subTest string
3101+
config config.Config
3102+
specSchedule string
3103+
expectedSchedule string
3104+
expectedJobName string
3105+
expectedResources acidv1.Resources
3106+
expectedAnnotation map[string]string
3107+
expectedLabel map[string]string
31043108
}{
31053109
{
31063110
subTest: "test generation of logical backup pod resources when not configured",
@@ -3120,6 +3124,8 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
31203124
ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "100Mi"},
31213125
ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "500Mi"},
31223126
},
3127+
expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId},
3128+
expectedAnnotation: nil,
31233129
},
31243130
{
31253131
subTest: "test generation of logical backup pod resources when configured",
@@ -3143,6 +3149,8 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
31433149
ResourceRequests: acidv1.ResourceDescription{CPU: "10m", Memory: "50Mi"},
31443150
ResourceLimits: acidv1.ResourceDescription{CPU: "300m", Memory: "300Mi"},
31453151
},
3152+
expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId},
3153+
expectedAnnotation: nil,
31463154
},
31473155
{
31483156
subTest: "test generation of logical backup pod resources when partly configured",
@@ -3164,6 +3172,8 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
31643172
ResourceRequests: acidv1.ResourceDescription{CPU: "50m", Memory: "100Mi"},
31653173
ResourceLimits: acidv1.ResourceDescription{CPU: "250m", Memory: "500Mi"},
31663174
},
3175+
expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId},
3176+
expectedAnnotation: nil,
31673177
},
31683178
{
31693179
subTest: "test generation of logical backup pod resources with SetMemoryRequestToLimit enabled",
@@ -3185,6 +3195,52 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
31853195
ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "200Mi"},
31863196
ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "200Mi"},
31873197
},
3198+
expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId},
3199+
expectedAnnotation: nil,
3200+
},
3201+
{
3202+
subTest: "test generation of pod annotations when cluster InheritedLabel is set",
3203+
config: config.Config{
3204+
Resources: config.Resources{
3205+
ClusterNameLabel: "cluster-name",
3206+
InheritedLabels: []string{"labelKey"},
3207+
DefaultCPURequest: "100m",
3208+
DefaultCPULimit: "1",
3209+
DefaultMemoryRequest: "100Mi",
3210+
DefaultMemoryLimit: "500Mi",
3211+
},
3212+
},
3213+
specSchedule: "",
3214+
expectedJobName: "acid-test-cluster",
3215+
expectedSchedule: "",
3216+
expectedResources: acidv1.Resources{
3217+
ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "100Mi"},
3218+
ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "500Mi"},
3219+
},
3220+
expectedLabel: map[string]string{"labelKey": "labelValue", "cluster-name": clusterName, "team": teamId},
3221+
expectedAnnotation: nil,
3222+
},
3223+
{
3224+
subTest: "test generation of pod annotations when cluster InheritedAnnotations is set",
3225+
config: config.Config{
3226+
Resources: config.Resources{
3227+
ClusterNameLabel: "cluster-name",
3228+
InheritedAnnotations: []string{"annotationKey"},
3229+
DefaultCPURequest: "100m",
3230+
DefaultCPULimit: "1",
3231+
DefaultMemoryRequest: "100Mi",
3232+
DefaultMemoryLimit: "500Mi",
3233+
},
3234+
},
3235+
specSchedule: "",
3236+
expectedJobName: "acid-test-cluster",
3237+
expectedSchedule: "",
3238+
expectedResources: acidv1.Resources{
3239+
ResourceRequests: acidv1.ResourceDescription{CPU: "100m", Memory: "100Mi"},
3240+
ResourceLimits: acidv1.ResourceDescription{CPU: "1", Memory: "500Mi"},
3241+
},
3242+
expectedLabel: map[string]string{configResources.ClusterNameLabel: clusterName, "team": teamId},
3243+
expectedAnnotation: map[string]string{"annotationKey": "annotationValue"},
31883244
},
31893245
}
31903246

@@ -3193,12 +3249,19 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
31933249
Config{
31943250
OpConfig: tt.config,
31953251
}, k8sutil.NewMockKubernetesClient(), acidv1.Postgresql{}, logger, eventRecorder)
3196-
31973252
cluster.ObjectMeta.Name = clusterName
3253+
cluster.Spec.TeamID = teamId
3254+
if cluster.ObjectMeta.Labels == nil {
3255+
cluster.ObjectMeta.Labels = make(map[string]string)
3256+
}
3257+
if cluster.ObjectMeta.Annotations == nil {
3258+
cluster.ObjectMeta.Annotations = make(map[string]string)
3259+
}
3260+
cluster.ObjectMeta.Labels["labelKey"] = "labelValue"
3261+
cluster.ObjectMeta.Annotations["annotationKey"] = "annotationValue"
31983262
cluster.Spec.LogicalBackupSchedule = tt.specSchedule
31993263
cronJob, err := cluster.generateLogicalBackupJob()
32003264
assert.NoError(t, err)
3201-
32023265
if cronJob.Spec.Schedule != tt.expectedSchedule {
32033266
t.Errorf("%s - %s: expected schedule %s, got %s", t.Name(), tt.subTest, tt.expectedSchedule, cronJob.Spec.Schedule)
32043267
}
@@ -3207,6 +3270,14 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
32073270
t.Errorf("%s - %s: expected job name %s, got %s", t.Name(), tt.subTest, tt.expectedJobName, cronJob.Name)
32083271
}
32093272

3273+
if !reflect.DeepEqual(cronJob.Labels, tt.expectedLabel) {
3274+
t.Errorf("%s - %s: expected labels %s, got %s", t.Name(), tt.subTest, tt.expectedLabel, cronJob.Labels)
3275+
}
3276+
3277+
if !reflect.DeepEqual(cronJob.Annotations, tt.expectedAnnotation) {
3278+
t.Errorf("%s - %s: expected annotations %s, got %s", t.Name(), tt.subTest, tt.expectedAnnotation, cronJob.Annotations)
3279+
}
3280+
32103281
containers := cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers
32113282
clusterResources, err := parseResourceRequirements(containers[0].Resources)
32123283
assert.NoError(t, err)

0 commit comments

Comments
 (0)