1
0
mirror of https://github.com/rancher/rke.git synced 2025-09-25 14:48:06 +00:00

Merge pull request #139 from superseb/check_local_image

Check image locally before pull
This commit is contained in:
Alena Prokharchyk
2017-12-13 12:26:00 -08:00
committed by GitHub
4 changed files with 43 additions and 13 deletions

View File

@@ -36,12 +36,10 @@ func DoRunContainer(dClient *client.Client, imageCfg *container.Config, hostCfg
return nil return nil
} }
logrus.Infof("[%s] Pulling Image on host [%s]", plane, hostname) err = UseLocalOrPull(dClient, hostname, imageCfg.Image, plane)
err = PullImage(dClient, hostname, imageCfg.Image)
if err != nil { if err != nil {
return err return err
} }
logrus.Infof("[%s] Successfully pulled [%s] image on host [%s]", plane, containerName, hostname)
resp, err := dClient.ContainerCreate(context.Background(), imageCfg, hostCfg, nil, containerName) resp, err := dClient.ContainerCreate(context.Background(), imageCfg, hostCfg, nil, containerName)
if err != nil { if err != nil {
return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err) return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err)
@@ -64,12 +62,10 @@ func DoRollingUpdateContainer(dClient *client.Client, imageCfg *container.Config
logrus.Infof("[%s] Container %s is not running on host [%s]", plane, containerName, hostname) logrus.Infof("[%s] Container %s is not running on host [%s]", plane, containerName, hostname)
return nil return nil
} }
logrus.Infof("[%s] Pulling Image on host [%s]", plane, hostname) err = UseLocalOrPull(dClient, hostname, imageCfg.Image, plane)
err = PullImage(dClient, hostname, imageCfg.Image)
if err != nil { if err != nil {
return err return err
} }
logrus.Infof("[%s] Successfully pulled [%s] image on host [%s]", plane, containerName, hostname)
logrus.Debugf("[%s] Stopping old container", plane) logrus.Debugf("[%s] Stopping old container", plane)
oldContainerName := "old-" + containerName oldContainerName := "old-" + containerName
if err := StopRenameContainer(dClient, hostname, containerName, oldContainerName); err != nil { if err := StopRenameContainer(dClient, hostname, containerName, oldContainerName); err != nil {
@@ -130,7 +126,21 @@ func IsContainerRunning(dClient *client.Client, hostname string, containerName s
return false, nil return false, nil
} }
func PullImage(dClient *client.Client, hostname string, containerImage string) error { func localImageExists(dClient *client.Client, hostname string, containerImage string) (bool, error) {
logrus.Debugf("Checking if image [%s] exists on host [%s]", containerImage, hostname)
_, _, err := dClient.ImageInspectWithRaw(context.Background(), containerImage)
if err != nil {
if client.IsErrNotFound(err) {
logrus.Debugf("Image [%s] does not exist on host [%s]: %v", containerImage, hostname, err)
return false, nil
}
return false, fmt.Errorf("Error checking if image [%s] exists on host [%s]: %v", containerImage, hostname, err)
}
logrus.Debugf("Image [%s] exists on host [%s]", containerImage, hostname)
return true, nil
}
func pullImage(dClient *client.Client, hostname string, containerImage string) error {
out, err := dClient.ImagePull(context.Background(), containerImage, types.ImagePullOptions{}) out, err := dClient.ImagePull(context.Background(), containerImage, types.ImagePullOptions{})
if err != nil { if err != nil {
return fmt.Errorf("Can't pull Docker image [%s] for host [%s]: %v", containerImage, hostname, err) return fmt.Errorf("Can't pull Docker image [%s] for host [%s]: %v", containerImage, hostname, err)
@@ -145,6 +155,24 @@ func PullImage(dClient *client.Client, hostname string, containerImage string) e
return nil return nil
} }
func UseLocalOrPull(dClient *client.Client, hostname string, containerImage string, plane string) error {
logrus.Infof("[%s] Checking image [%s] on host [%s]", plane, containerImage, hostname)
imageExists, err := localImageExists(dClient, hostname, containerImage)
if err != nil {
return err
}
if imageExists {
logrus.Infof("[%s] No pull necessary, image [%s] exists on host [%s]", plane, containerImage, hostname)
return nil
}
logrus.Infof("[%s] Pulling image [%s] on host [%s]", plane, containerImage, hostname)
if err := pullImage(dClient, hostname, containerImage); err != nil {
return err
}
logrus.Infof("[%s] Successfully pulled image [%s] on host [%s]", plane, containerImage, hostname)
return nil
}
func RemoveContainer(dClient *client.Client, hostname string, containerName string) error { func RemoveContainer(dClient *client.Client, hostname string, containerName string) error {
err := dClient.ContainerRemove(context.Background(), containerName, types.ContainerRemoveOptions{}) err := dClient.ContainerRemove(context.Background(), containerName, types.ContainerRemoveOptions{})
if err != nil { if err != nil {

View File

@@ -1,8 +1,9 @@
package pki package pki
const ( const (
CrtDownloaderContainer = "cert-deployer" CertificatesServiceName = "certificates"
CertificatesSecretName = "k8s-certs" CrtDownloaderContainer = "cert-deployer"
CertificatesSecretName = "k8s-certs"
CACertName = "kube-ca" CACertName = "kube-ca"
CACertENVName = "KUBE_CA" CACertENVName = "KUBE_CA"

View File

@@ -62,8 +62,7 @@ func DeployCertificatesOnWorkers(workerHosts []*hosts.Host, crtMap map[string]Ce
} }
func doRunDeployer(host *hosts.Host, containerEnv []string, certDownloaderImage string) error { func doRunDeployer(host *hosts.Host, containerEnv []string, certDownloaderImage string) error {
logrus.Debugf("[certificates] Pulling Certificate downloader Image on host [%s]", host.Address) if err := docker.UseLocalOrPull(host.DClient, host.Address, certDownloaderImage, CertificatesServiceName); err != nil {
if err := docker.PullImage(host.DClient, host.Address, certDownloaderImage); err != nil {
return err return err
} }
imageCfg := &container.Config{ imageCfg := &container.Config{

View File

@@ -15,6 +15,8 @@ const (
ControlRole = "controlplane" ControlRole = "controlplane"
WorkerRole = "worker" WorkerRole = "worker"
SidekickServiceName = "sidekick"
KubeAPIContainerName = "kube-api" KubeAPIContainerName = "kube-api"
KubeletContainerName = "kubelet" KubeletContainerName = "kubelet"
KubeproxyContainerName = "kube-proxy" KubeproxyContainerName = "kube-proxy"
@@ -56,11 +58,11 @@ func runSidekick(host *hosts.Host, sidekickImage string) error {
return err return err
} }
if isRunning { if isRunning {
logrus.Infof("[sidekick] Sidekick container already created on host [%s]", host.Address) logrus.Infof("[%s] Sidekick container already created on host [%s]", SidekickServiceName, host.Address)
return nil return nil
} }
imageCfg, hostCfg := buildSidekickConfig(sidekickImage) imageCfg, hostCfg := buildSidekickConfig(sidekickImage)
if err := docker.PullImage(host.DClient, host.Address, sidekickImage); err != nil { if err := docker.UseLocalOrPull(host.DClient, host.Address, sidekickImage, SidekickServiceName); err != nil {
return err return err
} }
if _, err := docker.CreateContiner(host.DClient, host.Address, SidekickContainerName, imageCfg, hostCfg); err != nil { if _, err := docker.CreateContiner(host.DClient, host.Address, SidekickContainerName, imageCfg, hostCfg); err != nil {