diff --git a/docker/docker.go b/docker/docker.go index f3f06e2c..a2ae7ee8 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -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 { diff --git a/pki/constants.go b/pki/constants.go index e555eeeb..e23fc628 100644 --- a/pki/constants.go +++ b/pki/constants.go @@ -1,8 +1,9 @@ package pki const ( - CrtDownloaderContainer = "cert-deployer" - CertificatesSecretName = "k8s-certs" + CertificatesServiceName = "certificates" + CrtDownloaderContainer = "cert-deployer" + CertificatesSecretName = "k8s-certs" CACertName = "kube-ca" CACertENVName = "KUBE_CA" diff --git a/pki/deploy.go b/pki/deploy.go index 8ee4fc10..25c92da1 100644 --- a/pki/deploy.go +++ b/pki/deploy.go @@ -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{ diff --git a/services/services.go b/services/services.go index 7649fb6c..ad4baaba 100644 --- a/services/services.go +++ b/services/services.go @@ -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 {