@@ -40,6 +40,7 @@ import (
4040 "github.com/crunchydata/postgres-operator/internal/pgbackrest"
4141 "github.com/crunchydata/postgres-operator/internal/pki"
4242 "github.com/crunchydata/postgres-operator/internal/testing/cmp"
43+ "github.com/crunchydata/postgres-operator/internal/testing/events"
4344 "github.com/crunchydata/postgres-operator/internal/testing/require"
4445 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
4546)
@@ -2601,6 +2602,17 @@ func TestCopyConfigurationResources(t *testing.T) {
26012602}
26022603
26032604func TestGenerateBackupJobIntent (t * testing.T ) {
2605+ _ , cc := setupKubernetes (t )
2606+ require .ParallelCapacity (t , 0 )
2607+ ns := setupNamespace (t , cc )
2608+
2609+ recorder := events .NewRecorder (t , runtime .Scheme )
2610+ r := & Reconciler {
2611+ Client : cc ,
2612+ Recorder : recorder ,
2613+ Owner : ControllerName ,
2614+ }
2615+
26042616 ctx := context .Background ()
26052617 cluster := v1beta1.PostgresCluster {}
26062618 cluster .Name = "hippo-test"
@@ -2609,7 +2621,7 @@ func TestGenerateBackupJobIntent(t *testing.T) {
26092621 // If repo.Volume is nil, the code interprets this as a cloud repo backup,
26102622 // therefore, an "empty" input results in a job spec for a cloud repo backup
26112623 t .Run ("empty" , func (t * testing.T ) {
2612- spec := generateBackupJobSpecIntent (ctx ,
2624+ spec := r . generateBackupJobSpecIntent (ctx ,
26132625 & cluster , v1beta1.PGBackRestRepo {},
26142626 "" ,
26152627 nil , nil ,
@@ -2670,7 +2682,7 @@ volumes:
26702682 })
26712683
26722684 t .Run ("volumeRepo" , func (t * testing.T ) {
2673- spec := generateBackupJobSpecIntent (ctx ,
2685+ spec := r . generateBackupJobSpecIntent (ctx ,
26742686 & cluster , v1beta1.PGBackRestRepo {
26752687 Volume : & v1beta1.RepoPVC {
26762688 VolumeClaimSpec : v1beta1.VolumeClaimSpec {},
@@ -2747,7 +2759,7 @@ volumes:
27472759 ImagePullPolicy : corev1 .PullAlways ,
27482760 },
27492761 }
2750- job := generateBackupJobSpecIntent (ctx ,
2762+ job := r . generateBackupJobSpecIntent (ctx ,
27512763 cluster , v1beta1.PGBackRestRepo {},
27522764 "" ,
27532765 nil , nil ,
@@ -2762,7 +2774,7 @@ volumes:
27622774 cluster .Spec .Backups = v1beta1.Backups {
27632775 PGBackRest : v1beta1.PGBackRestArchive {},
27642776 }
2765- job := generateBackupJobSpecIntent (ctx ,
2777+ job := r . generateBackupJobSpecIntent (ctx ,
27662778 cluster , v1beta1.PGBackRestRepo {},
27672779 "" ,
27682780 nil , nil ,
@@ -2779,7 +2791,7 @@ volumes:
27792791 },
27802792 },
27812793 }
2782- job := generateBackupJobSpecIntent (ctx ,
2794+ job := r . generateBackupJobSpecIntent (ctx ,
27832795 cluster , v1beta1.PGBackRestRepo {},
27842796 "" ,
27852797 nil , nil ,
@@ -2818,7 +2830,7 @@ volumes:
28182830 },
28192831 },
28202832 }
2821- job := generateBackupJobSpecIntent (ctx ,
2833+ job := r . generateBackupJobSpecIntent (ctx ,
28222834 cluster , v1beta1.PGBackRestRepo {},
28232835 "" ,
28242836 nil , nil ,
@@ -2831,7 +2843,7 @@ volumes:
28312843 cluster .Spec .Backups .PGBackRest .Jobs = & v1beta1.BackupJobs {
28322844 PriorityClassName : initialize .String ("some-priority-class" ),
28332845 }
2834- job := generateBackupJobSpecIntent (ctx ,
2846+ job := r . generateBackupJobSpecIntent (ctx ,
28352847 cluster , v1beta1.PGBackRestRepo {},
28362848 "" ,
28372849 nil , nil ,
@@ -2849,7 +2861,7 @@ volumes:
28492861 cluster .Spec .Backups .PGBackRest .Jobs = & v1beta1.BackupJobs {
28502862 Tolerations : tolerations ,
28512863 }
2852- job := generateBackupJobSpecIntent (ctx ,
2864+ job := r . generateBackupJobSpecIntent (ctx ,
28532865 cluster , v1beta1.PGBackRestRepo {},
28542866 "" ,
28552867 nil , nil ,
@@ -2863,14 +2875,14 @@ volumes:
28632875 t .Run ("Undefined" , func (t * testing.T ) {
28642876 cluster .Spec .Backups .PGBackRest .Jobs = nil
28652877
2866- spec := generateBackupJobSpecIntent (ctx ,
2878+ spec := r . generateBackupJobSpecIntent (ctx ,
28672879 cluster , v1beta1.PGBackRestRepo {}, "" , nil , nil ,
28682880 )
28692881 assert .Assert (t , spec .TTLSecondsAfterFinished == nil )
28702882
28712883 cluster .Spec .Backups .PGBackRest .Jobs = & v1beta1.BackupJobs {}
28722884
2873- spec = generateBackupJobSpecIntent (ctx ,
2885+ spec = r . generateBackupJobSpecIntent (ctx ,
28742886 cluster , v1beta1.PGBackRestRepo {}, "" , nil , nil ,
28752887 )
28762888 assert .Assert (t , spec .TTLSecondsAfterFinished == nil )
@@ -2881,7 +2893,7 @@ volumes:
28812893 TTLSecondsAfterFinished : initialize .Int32 (0 ),
28822894 }
28832895
2884- spec := generateBackupJobSpecIntent (ctx ,
2896+ spec := r . generateBackupJobSpecIntent (ctx ,
28852897 cluster , v1beta1.PGBackRestRepo {}, "" , nil , nil ,
28862898 )
28872899 if assert .Check (t , spec .TTLSecondsAfterFinished != nil ) {
@@ -2894,14 +2906,169 @@ volumes:
28942906 TTLSecondsAfterFinished : initialize .Int32 (100 ),
28952907 }
28962908
2897- spec := generateBackupJobSpecIntent (ctx ,
2909+ spec := r . generateBackupJobSpecIntent (ctx ,
28982910 cluster , v1beta1.PGBackRestRepo {}, "" , nil , nil ,
28992911 )
29002912 if assert .Check (t , spec .TTLSecondsAfterFinished != nil ) {
29012913 assert .Equal (t , * spec .TTLSecondsAfterFinished , int32 (100 ))
29022914 }
29032915 })
29042916 })
2917+
2918+ t .Run ("CloudLogVolumeAnnotationNoPvc" , func (t * testing.T ) {
2919+ cluster .Namespace = ns .Name
2920+ cluster .Annotations = map [string ]string {}
2921+ cluster .Annotations [naming .PGBackRestCloudLogVolume ] = "some-pvc"
2922+ spec := r .generateBackupJobSpecIntent (ctx ,
2923+ & cluster , v1beta1.PGBackRestRepo {},
2924+ "" ,
2925+ nil , nil ,
2926+ )
2927+ assert .Assert (t , cmp .MarshalMatches (spec .Template .Spec , `
2928+ containers:
2929+ - command:
2930+ - /bin/pgbackrest
2931+ - backup
2932+ - --stanza=db
2933+ - --repo=
2934+ name: pgbackrest
2935+ resources: {}
2936+ securityContext:
2937+ allowPrivilegeEscalation: false
2938+ capabilities:
2939+ drop:
2940+ - ALL
2941+ privileged: false
2942+ readOnlyRootFilesystem: true
2943+ runAsNonRoot: true
2944+ seccompProfile:
2945+ type: RuntimeDefault
2946+ volumeMounts:
2947+ - mountPath: /etc/pgbackrest/conf.d
2948+ name: pgbackrest-config
2949+ readOnly: true
2950+ - mountPath: /tmp
2951+ name: tmp
2952+ enableServiceLinks: false
2953+ restartPolicy: Never
2954+ securityContext:
2955+ fsGroup: 26
2956+ fsGroupChangePolicy: OnRootMismatch
2957+ volumes:
2958+ - name: pgbackrest-config
2959+ projected:
2960+ sources:
2961+ - configMap:
2962+ items:
2963+ - key: pgbackrest_cloud.conf
2964+ path: pgbackrest_cloud.conf
2965+ name: hippo-test-pgbackrest-config
2966+ - secret:
2967+ items:
2968+ - key: pgbackrest.ca-roots
2969+ path: ~postgres-operator/tls-ca.crt
2970+ - key: pgbackrest-client.crt
2971+ path: ~postgres-operator/client-tls.crt
2972+ - key: pgbackrest-client.key
2973+ mode: 384
2974+ path: ~postgres-operator/client-tls.key
2975+ name: hippo-test-pgbackrest
2976+ - emptyDir:
2977+ sizeLimit: 16Mi
2978+ name: tmp
2979+ ` ))
2980+
2981+ assert .Equal (t , len (recorder .Events ), 1 )
2982+ assert .Equal (t , recorder .Events [0 ].Regarding .Name , cluster .Name )
2983+ assert .Equal (t , recorder .Events [0 ].Reason , "PGBackRestCloudLogVolumeNotFound" )
2984+ assert .Equal (t , recorder .Events [0 ].Note , "persistentvolumeclaims \" some-pvc\" not found" )
2985+ })
2986+
2987+ t .Run ("CloudLogVolumeAnnotationPvcInPlace" , func (t * testing.T ) {
2988+ cluster .Namespace = ns .Name
2989+ cluster .Annotations = map [string ]string {}
2990+ cluster .Annotations [naming .PGBackRestCloudLogVolume ] = "another-pvc"
2991+
2992+ pvc := & corev1.PersistentVolumeClaim {
2993+ ObjectMeta : metav1.ObjectMeta {
2994+ Name : "another-pvc" ,
2995+ Namespace : ns .Name ,
2996+ },
2997+ Spec : corev1 .PersistentVolumeClaimSpec (testVolumeClaimSpec ()),
2998+ }
2999+ err := r .Client .Create (ctx , pvc )
3000+ assert .NilError (t , err )
3001+
3002+ spec := r .generateBackupJobSpecIntent (ctx ,
3003+ & cluster , v1beta1.PGBackRestRepo {},
3004+ "" ,
3005+ nil , nil ,
3006+ )
3007+ assert .Assert (t , cmp .MarshalMatches (spec .Template .Spec , `
3008+ containers:
3009+ - command:
3010+ - /bin/pgbackrest
3011+ - backup
3012+ - --stanza=db
3013+ - --repo=
3014+ name: pgbackrest
3015+ resources: {}
3016+ securityContext:
3017+ allowPrivilegeEscalation: false
3018+ capabilities:
3019+ drop:
3020+ - ALL
3021+ privileged: false
3022+ readOnlyRootFilesystem: true
3023+ runAsNonRoot: true
3024+ seccompProfile:
3025+ type: RuntimeDefault
3026+ volumeMounts:
3027+ - mountPath: /etc/pgbackrest/conf.d
3028+ name: pgbackrest-config
3029+ readOnly: true
3030+ - mountPath: /tmp
3031+ name: tmp
3032+ - mountPath: /volumes/another-pvc
3033+ name: another-pvc
3034+ enableServiceLinks: false
3035+ restartPolicy: Never
3036+ securityContext:
3037+ fsGroup: 26
3038+ fsGroupChangePolicy: OnRootMismatch
3039+ volumes:
3040+ - name: pgbackrest-config
3041+ projected:
3042+ sources:
3043+ - configMap:
3044+ items:
3045+ - key: pgbackrest_cloud.conf
3046+ path: pgbackrest_cloud.conf
3047+ name: hippo-test-pgbackrest-config
3048+ - secret:
3049+ items:
3050+ - key: pgbackrest.ca-roots
3051+ path: ~postgres-operator/tls-ca.crt
3052+ - key: pgbackrest-client.crt
3053+ path: ~postgres-operator/client-tls.crt
3054+ - key: pgbackrest-client.key
3055+ mode: 384
3056+ path: ~postgres-operator/client-tls.key
3057+ name: hippo-test-pgbackrest
3058+ - emptyDir:
3059+ sizeLimit: 16Mi
3060+ name: tmp
3061+ - name: another-pvc
3062+ persistentVolumeClaim:
3063+ claimName: another-pvc
3064+ ` ))
3065+
3066+ // No new events
3067+ assert .Equal (t , len (recorder .Events ), 1 )
3068+ assert .Equal (t , recorder .Events [0 ].Regarding .Name , cluster .Name )
3069+ assert .Equal (t , recorder .Events [0 ].Reason , "PGBackRestCloudLogVolumeNotFound" )
3070+ assert .Equal (t , recorder .Events [0 ].Note , "persistentvolumeclaims \" some-pvc\" not found" )
3071+ })
29053072}
29063073
29073074func TestGenerateRepoHostIntent (t * testing.T ) {
0 commit comments