-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Before this change, the driver failed to work on Bottlerocket because it tries to write files into the host directory /etc/amazon/efs. On Bottlerocket that directory is not writable by the container. We want to move the host mount point to /var/amazon/efs, but we need to support backward compatibility with hosts that may already have configs written to /etc/amazon/efs. To solve this, we mount both host paths and check for the presence of config files at container runtime. If we find files in the old location, we symlink to that location, otherwise we symlink to the new location.
- Loading branch information
Showing
6 changed files
with
421 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package driver | ||
|
||
import ( | ||
"fmt" | ||
"k8s.io/klog" | ||
"os" | ||
"path" | ||
) | ||
|
||
// InitConfigDir decides which of two directories will be used to store driver config files. It creates a symlink at | ||
// etcAmazonEfs to the chosen location (either legacyDir or preferredDir). | ||
// | ||
// - legacyDir is the path to a config directory where previous versions of this driver may have written config | ||
// files. In previous versions of this driver, a host path that was not writeable on Bottlerocket hosts | ||
// was being used, so we introduce preferredDir. | ||
// | ||
// - preferredDir is the path to config directory that we will use so long as we do not find files in legacyDir. | ||
// | ||
// - etcAmazonEfs is the path where the symlink will be written. In practice, this will always be /etc/amazon/efs, but | ||
// we take it as an input so the function can be tested. | ||
// Examples: | ||
// On a host that has EFS mounts created by an earlier version of this driver, InitConfigDir will detect a conf file in | ||
// legacyDir and write a symlink at etcAmazonEfs pointing to legacyDir. | ||
// | ||
// On a host that does not have pre-existing legacy EFS mount configs, InitConfigDir will detect no files in legacyDir | ||
// and will create a symlink at etcAmazonEfs pointing to preferredDir. | ||
// | ||
// If a symlink already existing at etcAmazonEfs, InitConfigDir does nothing. If something other than a symlink exists | ||
// at etcAmazonEfs, InitConfigDir returns an error. | ||
func InitConfigDir(legacyDir, preferredDir, etcAmazonEfs string) error { | ||
|
||
// if there is already a symlink in place, we have nothing to do | ||
if info, err := os.Lstat(etcAmazonEfs); err == nil { | ||
if info.Mode()&os.ModeSymlink != 0 { | ||
klog.Infof("Symlink exists at '%s', no need to create one", etcAmazonEfs) | ||
return nil | ||
} else { | ||
return fmt.Errorf("something already exists at '%s' and it is not a symlink", etcAmazonEfs) | ||
} | ||
} | ||
|
||
// check if a conf file exists in the legacy directory and symlink to the directory if so | ||
existingConfFile := path.Join(legacyDir, "efs-utils.conf") | ||
if _, err := os.Stat(existingConfFile); err == nil { | ||
if err = os.Symlink(legacyDir, etcAmazonEfs); err != nil { | ||
return fmt.Errorf( | ||
"unable to create symlink from '%s' to '%s': %s", | ||
etcAmazonEfs, | ||
legacyDir, | ||
err.Error()) | ||
} | ||
klog.Infof("Pre-existing config files are being used from '%s'", legacyDir) | ||
return nil | ||
} | ||
|
||
klog.Infof("Creating symlink from '%s' to '%s'", etcAmazonEfs, preferredDir) | ||
|
||
// make sure the config directory exists | ||
if target, err := os.Stat(preferredDir); err != nil { | ||
return fmt.Errorf("config directory '%s' does not exist: '%s'", preferredDir, err.Error()) | ||
} else if !target.IsDir() { | ||
return fmt.Errorf("config directory '%s' is not a directory", preferredDir) | ||
} | ||
|
||
// create a symlink to the config directory | ||
if err := os.Symlink(preferredDir, etcAmazonEfs); err != nil { | ||
return fmt.Errorf( | ||
"unable to create symlink from '%s' to '%s': %s", | ||
etcAmazonEfs, | ||
preferredDir, | ||
err.Error()) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.