mirror of
https://github.com/rancher/rke.git
synced 2025-09-25 06:33:56 +00:00
Merge pull request #139 from superseb/check_local_image
Check image locally before pull
This commit is contained in:
@@ -36,12 +36,10 @@ func DoRunContainer(dClient *client.Client, imageCfg *container.Config, hostCfg
|
||||
return nil
|
||||
}
|
||||
|
||||
logrus.Infof("[%s] Pulling Image on host [%s]", plane, hostname)
|
||||
err = PullImage(dClient, hostname, imageCfg.Image)
|
||||
err = UseLocalOrPull(dClient, hostname, imageCfg.Image, plane)
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
return nil
|
||||
}
|
||||
logrus.Infof("[%s] Pulling Image on host [%s]", plane, hostname)
|
||||
err = PullImage(dClient, hostname, imageCfg.Image)
|
||||
err = UseLocalOrPull(dClient, hostname, imageCfg.Image, plane)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("[%s] Successfully pulled [%s] image on host [%s]", plane, containerName, hostname)
|
||||
logrus.Debugf("[%s] Stopping old container", plane)
|
||||
oldContainerName := "old-" + containerName
|
||||
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
|
||||
}
|
||||
|
||||
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{})
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
err := dClient.ContainerRemove(context.Background(), containerName, types.ContainerRemoveOptions{})
|
||||
if err != nil {
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package pki
|
||||
|
||||
const (
|
||||
CertificatesServiceName = "certificates"
|
||||
CrtDownloaderContainer = "cert-deployer"
|
||||
CertificatesSecretName = "k8s-certs"
|
||||
|
||||
|
@@ -62,8 +62,7 @@ func DeployCertificatesOnWorkers(workerHosts []*hosts.Host, crtMap map[string]Ce
|
||||
}
|
||||
|
||||
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.PullImage(host.DClient, host.Address, certDownloaderImage); err != nil {
|
||||
if err := docker.UseLocalOrPull(host.DClient, host.Address, certDownloaderImage, CertificatesServiceName); err != nil {
|
||||
return err
|
||||
}
|
||||
imageCfg := &container.Config{
|
||||
|
@@ -15,6 +15,8 @@ const (
|
||||
ControlRole = "controlplane"
|
||||
WorkerRole = "worker"
|
||||
|
||||
SidekickServiceName = "sidekick"
|
||||
|
||||
KubeAPIContainerName = "kube-api"
|
||||
KubeletContainerName = "kubelet"
|
||||
KubeproxyContainerName = "kube-proxy"
|
||||
@@ -56,11 +58,11 @@ func runSidekick(host *hosts.Host, sidekickImage string) error {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
if _, err := docker.CreateContiner(host.DClient, host.Address, SidekickContainerName, imageCfg, hostCfg); err != nil {
|
||||
|
Reference in New Issue
Block a user