Skip to content

Commit 18e11cc

Browse files
committed
Pre-compile S3 Repo Type RegEx
The regex used to determine if an S3 restore was requested for bootstrapping a new cluster is now precompiled in order to simplify error checking and handling when the S3RepoTypeCLIOptionExists function is used.
1 parent 096112e commit 18e11cc

File tree

3 files changed

+7
-20
lines changed

3 files changed

+7
-20
lines changed

internal/apiserver/clusterservice/clusterimpl.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,10 +2145,7 @@ func validateDataSourceParms(request *msgs.CreateClusterRequest) error {
21452145

21462146
// now detect if an 's3' repo type was specified via the restore opts, and if so verify that s3
21472147
// settings are present in backrest repo secret for the backup being restored from
2148-
s3Restore, err := backrest.S3RepoTypeCLIOptionExists(restoreOpts)
2149-
if err != nil {
2150-
return fmt.Errorf("%s: %w", ErrInvalidDataSource, err)
2151-
}
2148+
s3Restore := backrest.S3RepoTypeCLIOptionExists(restoreOpts)
21522149
if s3Restore && isMissingExistingDataSourceS3Config(backrestRepoSecret) {
21532150
return fmt.Errorf("Secret %s is missing the S3 configuration required to restore "+
21542151
"from an S3 repository", backrestRepoSecret.GetName())

internal/operator/backrest/repo.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ import (
3838
"k8s.io/client-go/rest"
3939
)
4040

41-
// S3RepoTypeRegexString defines a regex to detect if an S3 restore has been specified using the
41+
// s3RepoTypeRegex defines a regex to detect if an S3 restore has been specified using the
4242
// pgBackRest --repo-type option
43-
const S3RepoTypeRegexString = `--repo-type=["']?s3["']?`
43+
var s3RepoTypeRegex = regexp.MustCompile(`--repo-type=["']?s3["']?`)
4444

4545
type RepoDeploymentTemplateFields struct {
4646
SecurityContext string
@@ -188,10 +188,7 @@ func setBootstrapRepoOverrides(clientset kubernetes.Interface, cluster *crv1.Pgc
188188

189189
// if an s3 restore is detected, override or set the pgbackrest S3 env vars, otherwise do
190190
// not set the s3 env vars at all
191-
s3Restore, err := S3RepoTypeCLIOptionExists(cluster.Spec.PGDataSource.RestoreOpts)
192-
if err != nil {
193-
return err
194-
}
191+
s3Restore := S3RepoTypeCLIOptionExists(cluster.Spec.PGDataSource.RestoreOpts)
195192
if s3Restore {
196193
// Now override any backrest S3 env vars for the bootstrap job
197194
repoFields.PgbackrestS3EnvVars = operator.GetPgbackrestBootstrapS3EnvVars(cluster,
@@ -303,10 +300,6 @@ func createService(clientset kubernetes.Interface, fields *RepoServiceTemplateFi
303300

304301
// S3RepoTypeCLIOptionExists detects if a S3 restore was requested via the '--repo-type'
305302
// command line option
306-
func S3RepoTypeCLIOptionExists(opts string) (exists bool, err error) {
307-
exists, err = regexp.MatchString(S3RepoTypeRegexString, opts)
308-
if err != nil {
309-
return false, err
310-
}
311-
return exists, nil
303+
func S3RepoTypeCLIOptionExists(opts string) bool {
304+
return s3RepoTypeRegex.MatchString(opts)
312305
}

internal/operator/cluster/clusterlogic.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,7 @@ func getBootstrapJobFields(clientset kubernetes.Interface, client *rest.RESTClie
243243

244244
// if an s3 restore is detected, override or set the pgbackrest S3 env vars, otherwise do
245245
// not set the s3 env vars at all
246-
s3Restore, err := backrest.S3RepoTypeCLIOptionExists(cluster.Spec.PGDataSource.RestoreOpts)
247-
if err != nil {
248-
return bootstrapFields, err
249-
}
246+
s3Restore := backrest.S3RepoTypeCLIOptionExists(cluster.Spec.PGDataSource.RestoreOpts)
250247
if s3Restore {
251248
// Now override any backrest S3 env vars for the bootstrap job
252249
bootstrapFields.PgbackrestS3EnvVars = operator.GetPgbackrestBootstrapS3EnvVars(cluster,

0 commit comments

Comments
 (0)