osd: use cp -a command for copying init-containers
remove copy-binaries cmd and related code Signed-off-by: Sheetal Pamecha <spamecha@redhat.com>
This commit is contained in:
1 parent
1cf207947c
commit
d86fa2abbc
8 files changed
+15
-244
No files matched your search
@@ -42,7 +42,6 @@ func addCommands() {
|
||||
|
||||
// util commands
|
||||
util.CmdReporterCmd,
|
||||
util.CopyBinsCmd,
|
||||
)
|
||||
// do double diligence to ensure non-user-facing commands are hidden from end users
|
||||
for _, cmd := range rook.RootCmd.Commands() {
|
||||
|
||||
@@ -56,9 +56,6 @@ var (
|
||||
commandString string
|
||||
configMapName string
|
||||
namespace string
|
||||
|
||||
// copy-binaries sub-command
|
||||
copyToDir string
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
Copyright 2019 The Rook Authors. All rights reserved.
|
||||
|
||||
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 util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/rook/rook/cmd/rook/rook"
|
||||
"github.com/rook/rook/pkg/daemon/util"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// CopyBinsCmd defines a top-level utility command which copies rook binary from the Rook
|
||||
// container image to a directory.
|
||||
var CopyBinsCmd = &cobra.Command{
|
||||
Use: "copy-binaries",
|
||||
Short: "Copy 'rook' binary from a container to a given directory.",
|
||||
Long: `Copy 'rook' binary from a container to a given directory.
|
||||
As an example, 'cmd-reporter run' may often need to be run from a container
|
||||
other than the container containing the 'rook' binary. Use this command to copy
|
||||
the 'rook' binary from the container containing the 'rook'
|
||||
binary to a Kubernetes EmptyDir volume mounted at the given directory in a pod's
|
||||
init container. From the pod's main container, mount the volume which now
|
||||
contains the 'rook' binary, and call 'rook cmd-reporter run' from
|
||||
'rook' in order to run the desired command from a non-Rook container.`,
|
||||
Args: cobra.NoArgs,
|
||||
RunE: runCopyBins,
|
||||
Hidden: true, // do not advertise to end users
|
||||
}
|
||||
|
||||
func init() {
|
||||
// copy-binaries
|
||||
CopyBinsCmd.Flags().StringVar(©ToDir, "copy-to-dir", "",
|
||||
"The directory into which 'rook' binary will be copied.")
|
||||
if err := CopyBinsCmd.MarkFlagRequired("copy-to-dir"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func runCopyBins(cCmd *cobra.Command, cArgs []string) error {
|
||||
if err := util.CopyBinaries(copyToDir); err != nil {
|
||||
rook.TerminateFatal(fmt.Errorf("could not copy binary to %s. %+v", copyToDir, err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
Copyright 2019 The Rook Authors. All rights reserved.
|
||||
|
||||
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 util
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// override these for unit testing
|
||||
var defaultRookDir = "/usr/local/bin"
|
||||
|
||||
// CopyBinaries copies the "rook" binary to a shared volume at the target path.
|
||||
func CopyBinaries(target string) error {
|
||||
return copyBinary(defaultRookDir, target, "rook")
|
||||
}
|
||||
|
||||
//nolint:gosec // Calling defer to close the file without checking the error return is not a risk for a simple file open and close
|
||||
func copyBinary(sourceDir, targetDir, filename string) error {
|
||||
sourcePath := path.Join(sourceDir, filename)
|
||||
targetPath := path.Join(targetDir, filename)
|
||||
logger.Infof("copying %s to %s", sourcePath, targetPath)
|
||||
|
||||
// Check if the source path exists, and look in PATH if it doesn't
|
||||
if _, err := os.Stat(sourcePath); err != nil {
|
||||
if sourcePath, err = exec.LookPath(filename); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the target path exists, and skip the copy if it does
|
||||
if _, err := os.Stat(targetPath); err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
sourceFile, err := os.Open(filepath.Clean(sourcePath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sourceFile.Close()
|
||||
|
||||
destinationFile, err := os.Create(filepath.Clean(targetPath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer destinationFile.Close()
|
||||
if _, err := io.Copy(destinationFile, sourceFile); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := destinationFile.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
//nolint:gosec // targetPath requires the permission to execute
|
||||
return os.Chmod(targetPath, 0700)
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
Copyright 2019 The Rook Authors. All rights reserved.
|
||||
|
||||
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 util
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCopyBinary(t *testing.T) {
|
||||
createTestBinary := func(binPath string) {
|
||||
f, err := os.Create(binPath)
|
||||
assert.NoError(t, err)
|
||||
// the binary should just be the text showing where it was copied from so we can verify it
|
||||
_, err = f.WriteString(binPath)
|
||||
assert.NoError(t, err)
|
||||
err = os.Chmod(binPath, 0700)
|
||||
assert.NoError(t, err)
|
||||
f.Close()
|
||||
}
|
||||
|
||||
mkdir := func(dir string) {
|
||||
err := os.MkdirAll(dir, 0700)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
cleanDir := func(dir string) {
|
||||
err := os.RemoveAll(dir)
|
||||
assert.NoError(t, err)
|
||||
mkdir(dir)
|
||||
}
|
||||
|
||||
fileText := func(binPath string) string {
|
||||
b, err := os.ReadFile(binPath)
|
||||
assert.NoError(t, err)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// set up the initial test directory tree without bins
|
||||
// testRootDir/
|
||||
// bin/
|
||||
// copy-to-dir/
|
||||
testRootDir := t.TempDir()
|
||||
// create a test PATH="testRootDir/bin"
|
||||
envPath := path.Join(testRootDir, "bin")
|
||||
mkdir(envPath)
|
||||
t.Setenv("PATH", envPath)
|
||||
// create initial copy-to-dir
|
||||
copyToDir := path.Join(testRootDir, "copy-to-dir")
|
||||
mkdir(copyToDir)
|
||||
// set default rook dir for unit tests
|
||||
oDefaultRookDir := defaultRookDir
|
||||
defaultRookDir = path.Join(testRootDir, "/usr/local/bin")
|
||||
defer func() { defaultRookDir = oDefaultRookDir }()
|
||||
|
||||
// expect success if binary is available in path
|
||||
// testRootDir/
|
||||
// bin/
|
||||
// rook
|
||||
// copy-to-dir/
|
||||
createTestBinary(path.Join(envPath, "rook"))
|
||||
cleanDir(copyToDir)
|
||||
|
||||
err := CopyBinaries(copyToDir)
|
||||
assert.NoError(t, err)
|
||||
r := fileText(path.Join(copyToDir, "rook"))
|
||||
assert.Contains(t, r, path.Join(testRootDir, "bin/rook"))
|
||||
|
||||
// expect success if the binary is available in default locations AND in path
|
||||
// additionally expect that the binary will be taken from the default location in this case
|
||||
// testRootDir/
|
||||
// usr/local/bin/
|
||||
// rook
|
||||
// bin/
|
||||
// rook
|
||||
// copy-to-dir/
|
||||
mkdir(defaultRookDir)
|
||||
createTestBinary(path.Join(defaultRookDir, "rook"))
|
||||
cleanDir(copyToDir)
|
||||
|
||||
err = CopyBinaries(copyToDir)
|
||||
assert.NoError(t, err)
|
||||
r = fileText(path.Join(copyToDir, "rook"))
|
||||
assert.Contains(t, r, path.Join(testRootDir, "usr/local/bin/rook"))
|
||||
}
|
||||
@@ -796,9 +796,14 @@ func (c *Cluster) getCopyBinariesContainer() (v1.Volume, *v1.Container) {
|
||||
mount := v1.VolumeMount{Name: rookBinariesVolumeName, MountPath: rookBinariesMountPath}
|
||||
|
||||
return volume, &v1.Container{
|
||||
Command: []string{"cp"},
|
||||
Args: []string{
|
||||
"copy-binaries",
|
||||
"--copy-to-dir", rookBinariesMountPath},
|
||||
"--archive",
|
||||
"--force",
|
||||
"--verbose",
|
||||
"/usr/local/bin/rook",
|
||||
rookBinariesMountPath,
|
||||
},
|
||||
Name: "copy-bins",
|
||||
Image: c.rookVersion,
|
||||
ImagePullPolicy: controller.GetContainerImagePullPolicy(c.spec.CephVersion.ImagePullPolicy),
|
||||
|
||||
@@ -57,7 +57,7 @@ func TestPodContainer(t *testing.T) {
|
||||
assert.Equal(t, "custom-scheduler", c.Spec.SchedulerName)
|
||||
container := c.Spec.InitContainers[0]
|
||||
logger.Infof("container: %+v", container)
|
||||
assert.Equal(t, "copy-binaries", container.Args[0])
|
||||
assert.Equal(t, "cp", container.Command[0])
|
||||
container = c.Spec.Containers[0]
|
||||
assert.Equal(t, "/rook/rook", container.Command[0])
|
||||
assert.Equal(t, "ceph", container.Args[0])
|
||||
|
||||
@@ -333,11 +333,14 @@ func (cr *cmdReporterCfg) initContainers() []v1.Container {
|
||||
}
|
||||
|
||||
c := v1.Container{
|
||||
Name: CopyBinariesInitContainerName,
|
||||
// Command: the rook command is the default entrypoint of rook images already
|
||||
Name: CopyBinariesInitContainerName,
|
||||
Command: []string{"cp"},
|
||||
Args: []string{
|
||||
"copy-binaries",
|
||||
"--copy-to-dir", CopyBinariesMountDir,
|
||||
"--archive",
|
||||
"--force",
|
||||
"--verbose",
|
||||
"/usr/local/bin/rook",
|
||||
CopyBinariesMountDir,
|
||||
},
|
||||
Image: cr.rookImage,
|
||||
ImagePullPolicy: cr.imagePullPolicy,
|
||||
|
||||
Reference in new issue
Block a user