@@ -122,9 +122,9 @@ type RepoResources struct {
122
122
// strategy.
123
123
func (r * Reconciler ) applyRepoHostIntent (ctx context.Context , postgresCluster * v1beta1.PostgresCluster ,
124
124
repoHostName string , repoResources * RepoResources ,
125
- observedInstances * observedInstances ) (* appsv1.StatefulSet , error ) {
125
+ observedInstances * observedInstances , saName string ) (* appsv1.StatefulSet , error ) {
126
126
127
- repo , err := r .generateRepoHostIntent (ctx , postgresCluster , repoHostName , repoResources , observedInstances )
127
+ repo , err := r .generateRepoHostIntent (ctx , postgresCluster , repoHostName , repoResources , observedInstances , saName )
128
128
if err != nil {
129
129
return nil , err
130
130
}
@@ -567,7 +567,7 @@ func (r *Reconciler) setScheduledJobStatus(ctx context.Context,
567
567
// as needed to create and reconcile a pgBackRest dedicated repository host within the kubernetes
568
568
// cluster.
569
569
func (r * Reconciler ) generateRepoHostIntent (ctx context.Context , postgresCluster * v1beta1.PostgresCluster ,
570
- repoHostName string , repoResources * RepoResources , observedInstances * observedInstances ,
570
+ repoHostName string , repoResources * RepoResources , observedInstances * observedInstances , saName string ,
571
571
) (* appsv1.StatefulSet , error ) {
572
572
573
573
annotations := naming .Merge (
@@ -681,6 +681,8 @@ func (r *Reconciler) generateRepoHostIntent(ctx context.Context, postgresCluster
681
681
682
682
repo .Spec .Template .Spec .SecurityContext = postgres .PodSecurityContext (postgresCluster )
683
683
684
+ repo .Spec .Template .Spec .ServiceAccountName = saName
685
+
684
686
pgbackrest .AddServerToRepoPod (ctx , postgresCluster , & repo .Spec .Template .Spec )
685
687
686
688
if pgbackrest .RepoHostVolumeDefined (postgresCluster ) {
@@ -1380,10 +1382,18 @@ func (r *Reconciler) reconcilePGBackRest(ctx context.Context,
1380
1382
return result , nil
1381
1383
}
1382
1384
1385
+ // reconcile the RBAC required to run the pgBackRest Repo Host
1386
+ repoHostSA , err := r .reconcileRepoHostRBAC (ctx , postgresCluster )
1387
+ if err != nil {
1388
+ log .Error (err , "unable to reconcile pgBackRest repo host RBAC" )
1389
+ result .Requeue = true
1390
+ return result , nil
1391
+ }
1392
+
1383
1393
var repoHost * appsv1.StatefulSet
1384
1394
var repoHostName string
1385
1395
// reconcile the pgbackrest repository host
1386
- repoHost , err = r .reconcileDedicatedRepoHost (ctx , postgresCluster , repoResources , instances )
1396
+ repoHost , err = r .reconcileDedicatedRepoHost (ctx , postgresCluster , repoResources , instances , repoHostSA . GetName () )
1387
1397
if err != nil {
1388
1398
log .Error (err , "unable to reconcile pgBackRest repo host" )
1389
1399
result .Requeue = true
@@ -2118,12 +2128,39 @@ func (r *Reconciler) reconcilePGBackRestRBAC(ctx context.Context,
2118
2128
return sa , nil
2119
2129
}
2120
2130
2131
+ // +kubebuilder:rbac:groups="",resources="serviceaccounts",verbs={create,patch}
2132
+
2133
+ // reconcileRepoHostRBAC reconciles the ServiceAccount for the pgBackRest repo host
2134
+ func (r * Reconciler ) reconcileRepoHostRBAC (ctx context.Context ,
2135
+ postgresCluster * v1beta1.PostgresCluster ) (* corev1.ServiceAccount , error ) {
2136
+
2137
+ sa := & corev1.ServiceAccount {ObjectMeta : naming .RepoHostRBAC (postgresCluster )}
2138
+ sa .SetGroupVersionKind (corev1 .SchemeGroupVersion .WithKind ("ServiceAccount" ))
2139
+
2140
+ if err := r .setControllerReference (postgresCluster , sa ); err != nil {
2141
+ return nil , errors .WithStack (err )
2142
+ }
2143
+
2144
+ sa .Annotations = naming .Merge (postgresCluster .Spec .Metadata .GetAnnotationsOrNil (),
2145
+ postgresCluster .Spec .Backups .PGBackRest .Metadata .GetAnnotationsOrNil ())
2146
+ sa .Labels = naming .Merge (postgresCluster .Spec .Metadata .GetLabelsOrNil (),
2147
+ postgresCluster .Spec .Backups .PGBackRest .Metadata .GetLabelsOrNil (),
2148
+ naming .PGBackRestLabels (postgresCluster .GetName ()))
2149
+
2150
+ if err := r .apply (ctx , sa ); err != nil {
2151
+ return nil , errors .WithStack (err )
2152
+ }
2153
+
2154
+ return sa , nil
2155
+ }
2156
+
2121
2157
// reconcileDedicatedRepoHost is responsible for reconciling a pgBackRest dedicated repository host
2122
2158
// StatefulSet according to a specific PostgresCluster custom resource.
2123
2159
func (r * Reconciler ) reconcileDedicatedRepoHost (ctx context.Context ,
2124
2160
postgresCluster * v1beta1.PostgresCluster ,
2125
2161
repoResources * RepoResources ,
2126
- observedInstances * observedInstances ) (* appsv1.StatefulSet , error ) {
2162
+ observedInstances * observedInstances ,
2163
+ saName string ) (* appsv1.StatefulSet , error ) {
2127
2164
2128
2165
log := logging .FromContext (ctx ).WithValues ("reconcileResource" , "repoHost" )
2129
2166
@@ -2164,7 +2201,7 @@ func (r *Reconciler) reconcileDedicatedRepoHost(ctx context.Context,
2164
2201
}
2165
2202
repoHostName := repoResources .hosts [0 ].Name
2166
2203
repoHost , err := r .applyRepoHostIntent (ctx , postgresCluster , repoHostName , repoResources ,
2167
- observedInstances )
2204
+ observedInstances , saName )
2168
2205
if err != nil {
2169
2206
log .Error (err , "reconciling repository host" )
2170
2207
return nil , err
0 commit comments