Create volume of preloaded images and mount it in

pull/6863/head
Priya Wadhwa 2020-02-19 23:02:36 -08:00
parent d506aa18b4
commit bb11bc666b
8 changed files with 108 additions and 34 deletions

View File

@ -26,6 +26,7 @@ import (
"k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/localpath"
) )
// CachePreloadedTarball caches the preloaded images tarball on the host machine
func CachePreloadedTarball(k8sVersion string) error { func CachePreloadedTarball(k8sVersion string) error {
targetDir := localpath.MakeMiniPath("cache", "preloaded-tarball") targetDir := localpath.MakeMiniPath("cache", "preloaded-tarball")
targetFilepath := path.Join(targetDir, fmt.Sprintf("%s-k8s-%s.tar", Version, k8sVersion)) targetFilepath := path.Join(targetDir, fmt.Sprintf("%s-k8s-%s.tar", Version, k8sVersion))

View File

@ -88,7 +88,12 @@ func (d *Driver) Create() error {
ContainerPort: constants.DockerDaemonPort, ContainerPort: constants.DockerDaemonPort,
}, },
) )
err := oci.CreateContainerNode(params) volumeName, err := oci.CreatePreloadedImagesVolume(Version, d.NodeConfig.KubernetesVersion)
if err != nil {
glog.Infof("Unable to create preloaded images volume: %v", err)
}
params.PreloadedVolume = volumeName
err = oci.CreateContainerNode(params)
if err != nil { if err != nil {
return errors.Wrap(err, "create kic node") return errors.Wrap(err, "create kic node")
} }

View File

@ -74,11 +74,17 @@ func CreateContainerNode(p CreateParams) error {
runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var:exec", hostVarVolPath)) runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var:exec", hostVarVolPath))
} }
if p.OCIBinary == Docker { if p.OCIBinary == Docker {
if err := createDockerVolume(p.Name); err != nil { volumeName := p.PreloadedVolume
return errors.Wrapf(err, "creating volume for %s container", p.Name) if volumeName == "" {
fmt.Println("volume name was empty!!")
os.Exit(1)
if err := createDockerVolume(p.Name); err != nil {
return errors.Wrapf(err, "creating volume for %s container", p.Name)
}
glog.Infof("Successfully created a docker volume %s", p.Name)
volumeName = p.Name
} }
glog.Infof("Successfully created a docker volume %s", p.Name) runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var", volumeName))
runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var", p.Name))
// setting resource limit in privileged mode is only supported by docker // setting resource limit in privileged mode is only supported by docker
// podman error: "Error: invalid configuration, cannot set resources with rootless containers not using cgroups v2 unified mode" // podman error: "Error: invalid configuration, cannot set resources with rootless containers not using cgroups v2 unified mode"
runArgs = append(runArgs, fmt.Sprintf("--cpus=%s", p.CPUs), fmt.Sprintf("--memory=%s", p.Memory)) runArgs = append(runArgs, fmt.Sprintf("--cpus=%s", p.CPUs), fmt.Sprintf("--memory=%s", p.Memory))

View File

@ -31,18 +31,19 @@ const (
// CreateParams are parameters needed to create a container // CreateParams are parameters needed to create a container
type CreateParams struct { type CreateParams struct {
Name string // used for container name and hostname Name string // used for container name and hostname
Image string // container image to use to create the node. Image string // container image to use to create the node.
ClusterLabel string // label the containers we create using minikube so we can clean up ClusterLabel string // label the containers we create using minikube so we can clean up
Role string // currently only role supported is control-plane Role string // currently only role supported is control-plane
Mounts []Mount // volume mounts Mounts []Mount // volume mounts
APIServerPort int // kubernetes api server port APIServerPort int // kubernetes api server port
PortMappings []PortMapping // ports to map to container from host PortMappings []PortMapping // ports to map to container from host
CPUs string // number of cpu cores assign to container CPUs string // number of cpu cores assign to container
Memory string // memory (mbs) to assign to the container Memory string // memory (mbs) to assign to the container
Envs map[string]string // environment variables to pass to the container Envs map[string]string // environment variables to pass to the container
ExtraArgs []string // a list of any extra option to pass to oci binary during creation time, for example --expose 8080... ExtraArgs []string // a list of any extra option to pass to oci binary during creation time, for example --expose 8080...
OCIBinary string // docker or podman OCIBinary string // docker or podman
PreloadedVolume string // volume of preloaded images
} }
// createOpt is an option for Create // createOpt is an option for Create

View File

@ -17,10 +17,14 @@ limitations under the License.
package oci package oci
import ( import (
"fmt"
"os/exec" "os/exec"
"path"
"strings"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/localpath"
) )
// DeleteAllVolumesByLabel deletes all volumes that have a specific label // DeleteAllVolumesByLabel deletes all volumes that have a specific label
@ -40,6 +44,56 @@ func DeleteAllVolumesByLabel(ociBin string, label string) error {
return nil return nil
} }
func CreatePreloadedImagesVolume(kicVersion, k8sVersion string) (string, error) {
if err := PointToHostDockerDaemon(); err != nil {
return "", errors.Wrap(err, "point host docker-daemon")
}
volumeName := fmt.Sprintf("%s-k8s-%s", kicVersion, k8sVersion)
if dockerVolumeExists(volumeName) {
return volumeName, nil
}
if err := createDockerVolume(volumeName); err != nil {
return "", errors.Wrap(err, "creating docker volume")
}
targetDir := localpath.MakeMiniPath("cache", "preloaded-tarball")
tarballPath := path.Join(targetDir, fmt.Sprintf("%s-k8s-%s.tar", kicVersion, k8sVersion))
if err := extractTarballToVolume(tarballPath, volumeName); err != nil {
return "", errors.Wrap(err, "extracting tarball to volume")
}
return volumeName, nil
}
func dockerVolumeExists(name string) bool {
if err := PointToHostDockerDaemon(); err != nil {
return false
}
cmd := exec.Command(Docker, "volume", "ls", "-q")
out, err := cmd.CombinedOutput()
if err != nil {
return false
}
names := strings.Split(string(out), "\n")
for _, n := range names {
if n == name {
return true
}
}
return false
}
func extractTarballToVolume(tarballPath, volumeName string) error {
if err := PointToHostDockerDaemon(); err != nil {
return errors.Wrap(err, "point host docker-daemon")
}
cmd := exec.Command(Docker, "-v", fmt.Sprintf("%s:/preloaded.tar:ro", tarballPath), "-v", fmt.Sprintf("%s:/extractDir", volumeName), "busybox", "tar", "xvf", "/preloaded.tar", "-C", "/extractDir")
fmt.Println(cmd.Args)
if out, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "output %s", string(out))
}
return nil
}
// createDockerVolume creates a docker volume to be attached to the container with correct labels and prefixes based on profile name // 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. // 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 // TODO: this should be fixed as a part of https://github.com/kubernetes/minikube/issues/6530

View File

@ -36,14 +36,15 @@ const (
// Config is configuration for the kic driver used by registry // Config is configuration for the kic driver used by registry
type Config struct { type Config struct {
MachineName string // maps to the container name being created MachineName string // maps to the container name being created
CPU int // Number of CPU cores assigned to the container CPU int // Number of CPU cores assigned to the container
Memory int // max memory in MB Memory int // max memory in MB
StorePath string // libmachine store path StorePath string // libmachine store path
OCIBinary string // oci tool to use (docker, podman,...) OCIBinary string // oci tool to use (docker, podman,...)
ImageDigest string // image name with sha to use for the node ImageDigest string // image name with sha to use for the node
Mounts []oci.Mount // mounts Mounts []oci.Mount // mounts
APIServerPort int // kubernetes api server port inside the container APIServerPort int // kubernetes api server port inside the container
PortMappings []oci.PortMapping // container port mappings PortMappings []oci.PortMapping // container port mappings
Envs map[string]string // key,value of environment variables passed to the node Envs map[string]string // key,value of environment variables passed to the node
KubernetesVersion string // kubernetes version to install
} }

View File

@ -62,6 +62,11 @@ func CacheImagesForBootstrapper(imageRepository string, version string, clusterB
// LoadImages loads previously cached images into the container runtime // LoadImages loads previously cached images into the container runtime
func LoadImages(cc *config.MachineConfig, runner command.Runner, images []string, cacheDir string) error { func LoadImages(cc *config.MachineConfig, runner command.Runner, images []string, cacheDir string) error {
glog.Info("skipping load images")
if true {
return nil
}
glog.Infof("LoadImages start: %s", images) glog.Infof("LoadImages start: %s", images)
start := time.Now() start := time.Now()

View File

@ -45,13 +45,14 @@ func init() {
func configure(mc config.MachineConfig) (interface{}, error) { func configure(mc config.MachineConfig) (interface{}, error) {
return kic.NewDriver(kic.Config{ return kic.NewDriver(kic.Config{
MachineName: mc.Name, MachineName: mc.Name,
StorePath: localpath.MiniPath(), StorePath: localpath.MiniPath(),
ImageDigest: kic.BaseImage, ImageDigest: kic.BaseImage,
CPU: mc.CPUs, CPU: mc.CPUs,
Memory: mc.Memory, Memory: mc.Memory,
OCIBinary: oci.Docker, OCIBinary: oci.Docker,
APIServerPort: mc.Nodes[0].Port, APIServerPort: mc.Nodes[0].Port,
KubernetesVersion: mc.KubernetesConfig.KubernetesVersion,
}), nil }), nil
} }