From 2234a3b2f3074e79fdd7e7b32bc04193b0b71379 Mon Sep 17 00:00:00 2001 From: Simon Beal <5381483+muddyfish@users.noreply.github.com> Date: Thu, 27 Jun 2024 11:46:09 +0100 Subject: [PATCH] Move `maxAttempts` to more generic handling code for adding environment variables (#214) * Migrate `maxAttempts` to more generic handling code for adding environment variables. --------- Co-authored-by: Simon Beal --- pkg/driver/mount.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/pkg/driver/mount.go b/pkg/driver/mount.go index 0627d67..eba446e 100644 --- a/pkg/driver/mount.go +++ b/pkg/driver/mount.go @@ -239,20 +239,7 @@ func (m *S3Mounter) Mount(bucketName string, target string, if credentials != nil { env = credentials.Env() } - - // Max attempts is passed to the driver as a mount option, but is passed to mp via env variable - // so we need to remove it from the options and add it to the env. - maxAttemptsIdx := -1 - for i, o := range options { - if strings.HasPrefix(o, awsMaxAttemptsOption) { - maxAttemptsIdx = i - break - } - } - if maxAttemptsIdx != -1 { - env = append(env, strings.Replace(options[maxAttemptsIdx], awsMaxAttemptsOption, awsMaxAttemptsEnv, 1)) - options = append(options[:maxAttemptsIdx], options[maxAttemptsIdx+1:]...) - } + options, env = moveOptionToEnvironmentVariables(awsMaxAttemptsOption, awsMaxAttemptsEnv, options, env) output, err := m.Runner.StartService(timeoutCtx, &system.ExecConfig{ Name: "mount-s3-" + m.MpVersion + "-" + uuid.New().String() + ".service", @@ -272,6 +259,25 @@ func (m *S3Mounter) Mount(bucketName string, target string, return nil } +// Moves a parameter optionName from the options list to MP's environment variable list. We need this as options are +// passed to the driver in a single field, but MP sometimes only supports config from environment variables. +// Returns an updated options and environment. +func moveOptionToEnvironmentVariables(optionName string, envName string, options []string, env []string) ([]string, []string) { + optionIdx := -1 + for i, o := range options { + if strings.HasPrefix(o, optionName) { + optionIdx = i + break + } + } + if optionIdx != -1 { + // We can do replace here as we've just verified it has the right prefix + env = append(env, strings.Replace(options[optionIdx], optionName, envName, 1)) + options = append(options[:optionIdx], options[optionIdx+1:]...) + } + return options, env +} + // method to add the user agent prefix to the Mountpoint headers // https://github.com/awslabs/mountpoint-s3/pull/548 func addUserAgentToOptions(options []string) []string {