minikube/pkg/drivers/kic/oci/volumes.go

88 lines
3.2 KiB
Go
Raw Normal View History

2020-02-14 09:39:16 +00:00
/*
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 (
2020-02-19 21:26:24 +00:00
"fmt"
2020-02-14 09:39:16 +00:00
"os/exec"
"strings"
2020-02-14 09:39:16 +00:00
2020-02-16 19:54:56 +00:00
"github.com/golang/glog"
2020-02-14 09:39:16 +00:00
"github.com/pkg/errors"
)
2020-02-17 18:55:53 +00:00
// DeleteAllVolumesByLabel deletes all volumes that have a specific label
// if there is no volume to delete it will return nil
2020-02-14 09:39:16 +00:00
// example: docker volume prune -f --filter label=name.minikube.sigs.k8s.io=minikube
func DeleteAllVolumesByLabel(ociBin string, label string) []error {
var deleteErrs []error
2020-02-16 19:54:56 +00:00
glog.Infof("trying to prune all %s volumes with label %s", ociBin, label)
2020-02-14 09:39:16 +00:00
if ociBin == Docker {
if err := PointToHostDockerDaemon(); err != nil {
return []error{errors.Wrap(err, "point host docker-daemon")}
2020-02-14 09:39:16 +00:00
}
}
vs, err := allVolumesByLabel(ociBin, label)
if err != nil {
glog.Infof("error listing volumes by label %q: %v", label, err)
}
if vs != nil {
for _, v := range vs {
cmd := exec.Command(ociBin, "volume", "rm", "--force", v)
if out, err := cmd.CombinedOutput(); err != nil {
glog.Infof("error deleting volume %s: output: %s", v, string(out))
deleteErrs = append(deleteErrs, err)
}
}
}
// try to prune afterwards just in case delete didn't go through
cmd := exec.Command(ociBin, "volume", "prune", "-f", "--filter", "label="+label)
2020-02-14 09:39:16 +00:00
if out, err := cmd.CombinedOutput(); err != nil {
deleteErrs = append(deleteErrs, errors.Wrapf(err, "prune volume %s", string(out)))
2020-02-14 09:39:16 +00:00
}
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) {
cmd := exec.Command(ociBin, "volume", "ls", "--filter", "label="+label, "--format", "{{.Name}}")
stdout, err := cmd.Output()
outs := strings.Split(strings.Replace(string(stdout), "\r", "", -1), "\n")
var tirmOuts []string
for _, o := range outs {
tirmOuts = append(tirmOuts, strings.TrimSpace(o))
}
return tirmOuts, err
2020-02-14 09:39:16 +00:00
}
// createDockerVolume creates a docker volume to be attached to the container with correct labels and prefixes based on profile name
// 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
func createDockerVolume(name string) error {
if err := PointToHostDockerDaemon(); err != nil {
return errors.Wrap(err, "point host docker-daemon")
}
2020-02-19 21:26:24 +00:00
cmd := exec.Command(Docker, "volume", "create", name, "--label", fmt.Sprintf("%s=%s", ProfileLabelKey, name), "--label", fmt.Sprintf("%s=%s", CreatedByLabelKey, "true"))
2020-02-14 09:39:16 +00:00
if out, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "output %s", string(out))
}
return nil
}