@@ -3087,20 +3087,24 @@ func TestGenerateResourceRequirements(t *testing.T) {
3087
3087
3088
3088
func TestGenerateLogicalBackupJob (t * testing.T ) {
3089
3089
clusterName := "acid-test-cluster"
3090
+ teamId := "test"
3090
3091
configResources := config.Resources {
3092
+ ClusterNameLabel : "cluster-name" ,
3091
3093
DefaultCPURequest : "100m" ,
3092
3094
DefaultCPULimit : "1" ,
3093
3095
DefaultMemoryRequest : "100Mi" ,
3094
3096
DefaultMemoryLimit : "500Mi" ,
3095
3097
}
3096
3098
3097
3099
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
3104
3108
}{
3105
3109
{
3106
3110
subTest : "test generation of logical backup pod resources when not configured" ,
@@ -3120,6 +3124,8 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
3120
3124
ResourceRequests : acidv1.ResourceDescription {CPU : "100m" , Memory : "100Mi" },
3121
3125
ResourceLimits : acidv1.ResourceDescription {CPU : "1" , Memory : "500Mi" },
3122
3126
},
3127
+ expectedLabel : map [string ]string {configResources .ClusterNameLabel : clusterName , "team" : teamId },
3128
+ expectedAnnotation : nil ,
3123
3129
},
3124
3130
{
3125
3131
subTest : "test generation of logical backup pod resources when configured" ,
@@ -3143,6 +3149,8 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
3143
3149
ResourceRequests : acidv1.ResourceDescription {CPU : "10m" , Memory : "50Mi" },
3144
3150
ResourceLimits : acidv1.ResourceDescription {CPU : "300m" , Memory : "300Mi" },
3145
3151
},
3152
+ expectedLabel : map [string ]string {configResources .ClusterNameLabel : clusterName , "team" : teamId },
3153
+ expectedAnnotation : nil ,
3146
3154
},
3147
3155
{
3148
3156
subTest : "test generation of logical backup pod resources when partly configured" ,
@@ -3164,6 +3172,8 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
3164
3172
ResourceRequests : acidv1.ResourceDescription {CPU : "50m" , Memory : "100Mi" },
3165
3173
ResourceLimits : acidv1.ResourceDescription {CPU : "250m" , Memory : "500Mi" },
3166
3174
},
3175
+ expectedLabel : map [string ]string {configResources .ClusterNameLabel : clusterName , "team" : teamId },
3176
+ expectedAnnotation : nil ,
3167
3177
},
3168
3178
{
3169
3179
subTest : "test generation of logical backup pod resources with SetMemoryRequestToLimit enabled" ,
@@ -3185,6 +3195,52 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
3185
3195
ResourceRequests : acidv1.ResourceDescription {CPU : "100m" , Memory : "200Mi" },
3186
3196
ResourceLimits : acidv1.ResourceDescription {CPU : "1" , Memory : "200Mi" },
3187
3197
},
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" },
3188
3244
},
3189
3245
}
3190
3246
@@ -3193,12 +3249,19 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
3193
3249
Config {
3194
3250
OpConfig : tt .config ,
3195
3251
}, k8sutil .NewMockKubernetesClient (), acidv1.Postgresql {}, logger , eventRecorder )
3196
-
3197
3252
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"
3198
3262
cluster .Spec .LogicalBackupSchedule = tt .specSchedule
3199
3263
cronJob , err := cluster .generateLogicalBackupJob ()
3200
3264
assert .NoError (t , err )
3201
-
3202
3265
if cronJob .Spec .Schedule != tt .expectedSchedule {
3203
3266
t .Errorf ("%s - %s: expected schedule %s, got %s" , t .Name (), tt .subTest , tt .expectedSchedule , cronJob .Spec .Schedule )
3204
3267
}
@@ -3207,6 +3270,14 @@ func TestGenerateLogicalBackupJob(t *testing.T) {
3207
3270
t .Errorf ("%s - %s: expected job name %s, got %s" , t .Name (), tt .subTest , tt .expectedJobName , cronJob .Name )
3208
3271
}
3209
3272
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
+
3210
3281
containers := cronJob .Spec .JobTemplate .Spec .Template .Spec .Containers
3211
3282
clusterResources , err := parseResourceRequirements (containers [0 ].Resources )
3212
3283
assert .NoError (t , err )
0 commit comments