volumes.go 4.7 KB
Newer Older
M
Medya Gh 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
Copyright 2020 The Kubernetes 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 oci

import (
20 21
	"bufio"
	"bytes"
22
	"context"
M
Medya Gh 已提交
23
	"fmt"
M
Medya Gh 已提交
24
	"os/exec"
25
	"runtime"
26
	"strings"
M
Medya Gh 已提交
27 28

	"github.com/pkg/errors"
29 30

	"k8s.io/klog/v2"
M
Medya Gh 已提交
31 32
)

M
Medya Gh 已提交
33 34
// DeleteAllVolumesByLabel deletes all volumes that have a specific label
// if there is no volume to delete it will return nil
35
func DeleteAllVolumesByLabel(ctx context.Context, ociBin string, label string, warnSlow ...bool) []error {
36
	var deleteErrs []error
37
	klog.Infof("trying to delete all %s volumes with label %s", ociBin, label)
38 39

	vs, err := allVolumesByLabel(ociBin, label)
40

41
	if err != nil {
42
		return []error{fmt.Errorf("listing volumes by label %q: %v", label, err)}
43
	}
44

M
lint  
Medya Gh 已提交
45
	for _, v := range vs {
46
		if _, err := runCmd(exec.CommandContext(ctx, ociBin, "volume", "rm", "--force", v), warnSlow...); err != nil {
47
			deleteErrs = append(deleteErrs, fmt.Errorf("deleting %q", v))
48 49
		}
	}
50

51 52 53 54 55 56
	return deleteErrs
}

// PruneAllVolumesByLabel deletes all volumes that have a specific label
// if there is no volume to delete it will return nil
// example: docker volume prune -f --filter label=name.minikube.sigs.k8s.io=minikube
57
func PruneAllVolumesByLabel(ctx context.Context, ociBin string, label string, warnSlow ...bool) []error {
58
	var deleteErrs []error
59
	klog.Infof("trying to prune all %s volumes with label %s", ociBin, label)
60
	cmd := exec.CommandContext(ctx, ociBin, "volume", "prune", "-f", "--filter", "label="+label)
M
Medya Gh 已提交
61
	if _, err := runCmd(cmd, warnSlow...); err != nil {
62
		deleteErrs = append(deleteErrs, errors.Wrapf(err, "prune volume by label %s", label))
M
Medya Gh 已提交
63
	}
64

65 66 67 68 69 70
	return deleteErrs
}

// allVolumesByLabel returns name of all docker volumes by a specific label
// will not return error if there is no volume found.
func allVolumesByLabel(ociBin string, label string) ([]string, error) {
M
Medya Gh 已提交
71
	rr, err := runCmd(exec.Command(ociBin, "volume", "ls", "--filter", "label="+label, "--format", "{{.Name}}"))
72
	s := bufio.NewScanner(bytes.NewReader(rr.Stdout.Bytes()))
M
Medya Gh 已提交
73
	var vols []string
74 75 76 77
	for s.Scan() {
		v := strings.TrimSpace(s.Text())
		if v != "" {
			vols = append(vols, v)
M
Medya Gh 已提交
78
		}
79
	}
M
Medya Gh 已提交
80
	return vols, err
M
Medya Gh 已提交
81 82
}

P
Priya Wadhwa 已提交
83
// ExtractTarballToVolume runs a docker image imageName which extracts the tarball at tarballPath
P
Priya Wadhwa 已提交
84
// to the volume named volumeName
85
func ExtractTarballToVolume(ociBin string, tarballPath, volumeName, imageName string) error {
86 87 88 89 90 91 92 93
	cmdArgs := []string{"run", "--rm", "--entrypoint", "/usr/bin/tar"}
	// Podman:
	// when selinux setenforce is enforced, normal mount will lead to file permissions error (-?????????)
	// - option 1: label the file as container private (mount option :Z), but will alter the file in the host machine
	// - option 2*: keep the file untouched and set --security-opt label=disable (no changes to file)
	if ociBin == Podman && runtime.GOOS == "linux" {
		cmdArgs = append(cmdArgs, "--security-opt", "label=disable")
	}
L
Ling Samuel 已提交
94
	cmdArgs = append(cmdArgs, "-v", fmt.Sprintf("%s:/preloaded.tar:ro", tarballPath), "-v", fmt.Sprintf("%s:/extractDir", volumeName), imageName, "-I", "lz4", "-xf", "/preloaded.tar", "-C", "/extractDir")
95
	cmd := exec.Command(ociBin, cmdArgs...)
M
Medya Gh 已提交
96
	if _, err := runCmd(cmd); err != nil {
M
Medya Gh 已提交
97
		return err
98 99 100 101
	}
	return nil
}

102
// createVolume creates a volume to be attached to the container with correct labels and prefixes based on profile name
M
Medya Gh 已提交
103 104
// Caution ! if volume already exists does NOT return an error and will not apply the minikube labels on it.
// TODO: this should be fixed as a part of https://github.com/kubernetes/minikube/issues/6530
105 106
func createVolume(ociBin string, profile string, nodeName string) error {
	if _, err := runCmd(exec.Command(ociBin, "volume", "create", nodeName, "--label", fmt.Sprintf("%s=%s", ProfileLabelKey, profile), "--label", fmt.Sprintf("%s=%s", CreatedByLabelKey, "true"))); err != nil {
M
Medya Gh 已提交
107
		return err
M
Medya Gh 已提交
108 109 110
	}
	return nil
}
111 112 113 114 115 116 117 118 119 120

// prepareVolume will copy the initial content of the mount point by starting a container to check the expected content
func prepareVolume(ociBin string, imageName string, nodeName string) error {
	cmdArgs := []string{"run", "--rm", "--entrypoint", "/usr/bin/test"}
	cmdArgs = append(cmdArgs, "-v", fmt.Sprintf("%s:/var", nodeName), imageName, "-d", "/var/lib")
	cmd := exec.Command(ociBin, cmdArgs...)
	if _, err := runCmd(cmd); err != nil {
		return err
	}
	return nil
121
}