Pass in container runtime to preload tarball name

This way we don't have to individually check if we support a container runtime
pull/7044/head
Priya Wadhwa 2020-03-23 12:29:49 -07:00
parent 0e5974367f
commit bfe8aee3b5
4 changed files with 26 additions and 28 deletions

View File

@ -61,7 +61,7 @@ func main() {
for _, kv := range k8sVersions {
for _, cr := range containerRuntimes {
tf := download.TarballName(kv)
tf := download.TarballName(kv, cr)
if tarballExists(tf) {
fmt.Printf("A preloaded tarball for k8s version %s already exists, skipping generation.\n", kv)
continue

View File

@ -126,7 +126,7 @@ func (d *Driver) Create() error {
t := time.Now()
glog.Infof("Starting extracting preloaded images to volume")
// Extract preloaded images to container
if err := oci.ExtractTarballToVolume(download.TarballPath(d.NodeConfig.KubernetesVersion), params.Name, BaseImage); err != nil {
if err := oci.ExtractTarballToVolume(download.TarballPath(d.NodeConfig.KubernetesVersion, d.NodeConfig.ContainerRuntime), params.Name, BaseImage); err != nil {
glog.Infof("Unable to extract preloaded tarball to volume: %v", err)
} else {
glog.Infof("Took %f seconds to extract preloaded images to volume", time.Since(t).Seconds())

View File

@ -291,6 +291,7 @@ func (r *Docker) SystemLogCmd(len int) string {
// 3. Remove the tarball within the VM
func (r *Docker) Preload(cfg config.KubernetesConfig) error {
k8sVersion := cfg.KubernetesVersion
cRuntime := cfg.ContainerRuntime
// If images already exist, return
images, err := images.Kubeadm(cfg.ImageRepository, k8sVersion)
@ -307,7 +308,7 @@ func (r *Docker) Preload(cfg config.KubernetesConfig) error {
glog.Infof("error saving reference store: %v", err)
}
tarballPath := download.TarballPath(k8sVersion)
tarballPath := download.TarballPath(k8sVersion, cRuntime)
targetDir := "/"
targetName := "preloaded.tar.lz4"
dest := path.Join(targetDir, targetName)

View File

@ -46,13 +46,13 @@ const (
)
// TarballName returns name of the tarball
func TarballName(k8sVersion string) string {
return fmt.Sprintf("preloaded-images-k8s-%s-%s-docker-overlay2-%s.tar.lz4", PreloadVersion, k8sVersion, runtime.GOOS)
func TarballName(k8sVersion, containerRuntime string) string {
return fmt.Sprintf("preloaded-images-k8s-%s-%s-%s-overlay2-%s.tar.lz4", PreloadVersion, k8sVersion, containerRuntime, runtime.GOOS)
}
// returns the name of the checksum file
func checksumName(k8sVersion string) string {
return fmt.Sprintf("%s.checksum", TarballName(k8sVersion))
func checksumName(k8sVersion, containerRuntime string) string {
return fmt.Sprintf("%s.checksum", TarballName(k8sVersion, containerRuntime))
}
// returns target dir for all cached items related to preloading
@ -61,18 +61,18 @@ func targetDir() string {
}
// PreloadChecksumPath returns path to checksum file
func PreloadChecksumPath(k8sVersion string) string {
return path.Join(targetDir(), checksumName(k8sVersion))
func PreloadChecksumPath(k8sVersion, containerRuntime string) string {
return path.Join(targetDir(), checksumName(k8sVersion, containerRuntime))
}
// TarballPath returns the path to the preloaded tarball
func TarballPath(k8sVersion string) string {
return path.Join(targetDir(), TarballName(k8sVersion))
func TarballPath(k8sVersion, containerRuntime string) string {
return path.Join(targetDir(), TarballName(k8sVersion, containerRuntime))
}
// remoteTarballURL returns the URL for the remote tarball in GCS
func remoteTarballURL(k8sVersion string) string {
return fmt.Sprintf("https://storage.googleapis.com/%s/%s", PreloadBucket, TarballName(k8sVersion))
func remoteTarballURL(k8sVersion, containerRuntime string) string {
return fmt.Sprintf("https://storage.googleapis.com/%s/%s", PreloadBucket, TarballName(k8sVersion, containerRuntime))
}
// PreloadExists returns true if there is a preloaded tarball that can be used
@ -82,13 +82,13 @@ func PreloadExists(k8sVersion, containerRuntime string) bool {
}
// Omit remote check if tarball exists locally
targetPath := TarballPath(k8sVersion)
targetPath := TarballPath(k8sVersion, containerRuntime)
if _, err := os.Stat(targetPath); err == nil {
glog.Infof("Found local preload: %s", targetPath)
return true
}
url := remoteTarballURL(k8sVersion)
url := remoteTarballURL(k8sVersion, containerRuntime)
resp, err := http.Head(url)
if err != nil {
glog.Warningf("%s fetch error: %v", url, err)
@ -107,10 +107,7 @@ func PreloadExists(k8sVersion, containerRuntime string) bool {
// Preload caches the preloaded images tarball on the host machine
func Preload(k8sVersion, containerRuntime string) error {
if containerRuntime != "docker" {
return nil
}
targetPath := TarballPath(k8sVersion)
targetPath := TarballPath(k8sVersion, containerRuntime)
if _, err := os.Stat(targetPath); err == nil {
glog.Infof("Found %s in cache, skipping download", targetPath)
@ -124,7 +121,7 @@ func Preload(k8sVersion, containerRuntime string) error {
}
out.T(out.FileDownload, "Downloading preloaded images tarball for k8s {{.version}} ...", out.V{"version": k8sVersion})
url := remoteTarballURL(k8sVersion)
url := remoteTarballURL(k8sVersion, containerRuntime)
tmpDst := targetPath + ".download"
client := &getter.Client{
@ -139,34 +136,34 @@ func Preload(k8sVersion, containerRuntime string) error {
return errors.Wrapf(err, "download failed: %s", url)
}
if err := saveChecksumFile(k8sVersion); err != nil {
if err := saveChecksumFile(k8sVersion, containerRuntime); err != nil {
return errors.Wrap(err, "saving checksum file")
}
if err := verifyChecksum(k8sVersion, tmpDst); err != nil {
if err := verifyChecksum(k8sVersion, containerRuntime, tmpDst); err != nil {
return errors.Wrap(err, "verify")
}
return os.Rename(tmpDst, targetPath)
}
func saveChecksumFile(k8sVersion string) error {
glog.Infof("saving checksum for %s ...", TarballName(k8sVersion))
func saveChecksumFile(k8sVersion, containerRuntime string) error {
glog.Infof("saving checksum for %s ...", TarballName(k8sVersion, containerRuntime))
ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithoutAuthentication())
if err != nil {
return errors.Wrap(err, "getting storage client")
}
attrs, err := client.Bucket(PreloadBucket).Object(TarballName(k8sVersion)).Attrs(ctx)
attrs, err := client.Bucket(PreloadBucket).Object(TarballName(k8sVersion, containerRuntime)).Attrs(ctx)
if err != nil {
return errors.Wrap(err, "getting storage object")
}
checksum := attrs.MD5
return ioutil.WriteFile(PreloadChecksumPath(k8sVersion), checksum, 0644)
return ioutil.WriteFile(PreloadChecksumPath(k8sVersion, containerRuntime), checksum, 0644)
}
// verifyChecksum returns true if the checksum of the local binary matches
// the checksum of the remote binary
func verifyChecksum(k8sVersion string, path string) error {
func verifyChecksum(k8sVersion, containerRuntime, path string) error {
glog.Infof("verifying checksumm of %s ...", path)
// get md5 checksum of tarball path
contents, err := ioutil.ReadFile(path)
@ -175,7 +172,7 @@ func verifyChecksum(k8sVersion string, path string) error {
}
checksum := md5.Sum(contents)
remoteChecksum, err := ioutil.ReadFile(PreloadChecksumPath(k8sVersion))
remoteChecksum, err := ioutil.ReadFile(PreloadChecksumPath(k8sVersion, containerRuntime))
if err != nil {
return errors.Wrap(err, "reading checksum file")
}