mirror of
https://github.com/rancher/rke.git
synced 2025-08-31 14:36:32 +00:00
Add context.Context to everything and also make logging pluggable
This commit is contained in:
123
docker/docker.go
123
docker/docker.go
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/rancher/rke/log"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -19,101 +20,101 @@ var K8sDockerVersions = map[string][]string{
|
||||
"1.8": {"1.12.6", "1.13.1", "17.03.2"},
|
||||
}
|
||||
|
||||
func DoRunContainer(dClient *client.Client, imageCfg *container.Config, hostCfg *container.HostConfig, containerName string, hostname string, plane string) error {
|
||||
isRunning, err := IsContainerRunning(dClient, hostname, containerName, false)
|
||||
func DoRunContainer(ctx context.Context, dClient *client.Client, imageCfg *container.Config, hostCfg *container.HostConfig, containerName string, hostname string, plane string) error {
|
||||
isRunning, err := IsContainerRunning(ctx, dClient, hostname, containerName, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isRunning {
|
||||
logrus.Infof("[%s] Container [%s] is already running on host [%s]", plane, containerName, hostname)
|
||||
isUpgradable, err := IsContainerUpgradable(dClient, imageCfg, containerName, hostname, plane)
|
||||
log.Infof(ctx, "[%s] Container [%s] is already running on host [%s]", plane, containerName, hostname)
|
||||
isUpgradable, err := IsContainerUpgradable(ctx, dClient, imageCfg, containerName, hostname, plane)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isUpgradable {
|
||||
return DoRollingUpdateContainer(dClient, imageCfg, hostCfg, containerName, hostname, plane)
|
||||
return DoRollingUpdateContainer(ctx, dClient, imageCfg, hostCfg, containerName, hostname, plane)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err = UseLocalOrPull(dClient, hostname, imageCfg.Image, plane)
|
||||
err = UseLocalOrPull(ctx, dClient, hostname, imageCfg.Image, plane)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := dClient.ContainerCreate(context.Background(), imageCfg, hostCfg, nil, containerName)
|
||||
resp, err := dClient.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
if err := dClient.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil {
|
||||
if err := dClient.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
|
||||
return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
logrus.Debugf("[%s] Successfully started [%s] container: [%s]", plane, containerName, resp.ID)
|
||||
logrus.Infof("[%s] Successfully started [%s] container on host [%s]", plane, containerName, hostname)
|
||||
log.Infof(ctx, "[%s] Successfully started [%s] container on host [%s]", plane, containerName, hostname)
|
||||
return nil
|
||||
}
|
||||
|
||||
func DoRollingUpdateContainer(dClient *client.Client, imageCfg *container.Config, hostCfg *container.HostConfig, containerName, hostname, plane string) error {
|
||||
func DoRollingUpdateContainer(ctx context.Context, dClient *client.Client, imageCfg *container.Config, hostCfg *container.HostConfig, containerName, hostname, plane string) error {
|
||||
logrus.Debugf("[%s] Checking for deployed [%s]", plane, containerName)
|
||||
isRunning, err := IsContainerRunning(dClient, hostname, containerName, false)
|
||||
isRunning, err := IsContainerRunning(ctx, dClient, hostname, containerName, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !isRunning {
|
||||
logrus.Infof("[%s] Container %s is not running on host [%s]", plane, containerName, hostname)
|
||||
log.Infof(ctx, "[%s] Container %s is not running on host [%s]", plane, containerName, hostname)
|
||||
return nil
|
||||
}
|
||||
err = UseLocalOrPull(dClient, hostname, imageCfg.Image, plane)
|
||||
err = UseLocalOrPull(ctx, dClient, hostname, imageCfg.Image, plane)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debugf("[%s] Stopping old container", plane)
|
||||
oldContainerName := "old-" + containerName
|
||||
if err := StopRenameContainer(dClient, hostname, containerName, oldContainerName); err != nil {
|
||||
if err := StopRenameContainer(ctx, dClient, hostname, containerName, oldContainerName); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("[%s] Successfully stopped old container %s on host [%s]", plane, containerName, hostname)
|
||||
_, err = CreateContiner(dClient, hostname, containerName, imageCfg, hostCfg)
|
||||
log.Infof(ctx, "[%s] Successfully stopped old container %s on host [%s]", plane, containerName, hostname)
|
||||
_, err = CreateContiner(ctx, dClient, hostname, containerName, imageCfg, hostCfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
if err := StartContainer(dClient, hostname, containerName); err != nil {
|
||||
if err := StartContainer(ctx, dClient, hostname, containerName); err != nil {
|
||||
return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
logrus.Infof("[%s] Successfully updated [%s] container on host [%s]", plane, containerName, hostname)
|
||||
log.Infof(ctx, "[%s] Successfully updated [%s] container on host [%s]", plane, containerName, hostname)
|
||||
logrus.Debugf("[%s] Removing old container", plane)
|
||||
err = RemoveContainer(dClient, hostname, oldContainerName)
|
||||
err = RemoveContainer(ctx, dClient, hostname, oldContainerName)
|
||||
return err
|
||||
}
|
||||
|
||||
func DoRemoveContainer(dClient *client.Client, containerName, hostname string) error {
|
||||
logrus.Infof("[remove/%s] Checking if container is running on host [%s]", containerName, hostname)
|
||||
func DoRemoveContainer(ctx context.Context, dClient *client.Client, containerName, hostname string) error {
|
||||
log.Infof(ctx, "[remove/%s] Checking if container is running on host [%s]", containerName, hostname)
|
||||
// not using the wrapper to check if the error is a NotFound error
|
||||
_, err := dClient.ContainerInspect(context.Background(), containerName)
|
||||
_, err := dClient.ContainerInspect(ctx, containerName)
|
||||
if err != nil {
|
||||
if client.IsErrNotFound(err) {
|
||||
logrus.Infof("[remove/%s] Container doesn't exist on host [%s]", containerName, hostname)
|
||||
log.Infof(ctx, "[remove/%s] Container doesn't exist on host [%s]", containerName, hostname)
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
logrus.Infof("[remove/%s] Stopping container on host [%s]", containerName, hostname)
|
||||
err = StopContainer(dClient, hostname, containerName)
|
||||
log.Infof(ctx, "[remove/%s] Stopping container on host [%s]", containerName, hostname)
|
||||
err = StopContainer(ctx, dClient, hostname, containerName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Infof("[remove/%s] Removing container on host [%s]", containerName, hostname)
|
||||
err = RemoveContainer(dClient, hostname, containerName)
|
||||
log.Infof(ctx, "[remove/%s] Removing container on host [%s]", containerName, hostname)
|
||||
err = RemoveContainer(ctx, dClient, hostname, containerName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("[remove/%s] Successfully removed container on host [%s]", containerName, hostname)
|
||||
log.Infof(ctx, "[remove/%s] Successfully removed container on host [%s]", containerName, hostname)
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsContainerRunning(dClient *client.Client, hostname string, containerName string, all bool) (bool, error) {
|
||||
func IsContainerRunning(ctx context.Context, dClient *client.Client, hostname string, containerName string, all bool) (bool, error) {
|
||||
logrus.Debugf("Checking if container [%s] is running on host [%s]", containerName, hostname)
|
||||
containers, err := dClient.ContainerList(context.Background(), types.ContainerListOptions{All: all})
|
||||
containers, err := dClient.ContainerList(ctx, types.ContainerListOptions{All: all})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("Can't get Docker containers for host [%s]: %v", hostname, err)
|
||||
|
||||
@@ -126,9 +127,9 @@ func IsContainerRunning(dClient *client.Client, hostname string, containerName s
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func localImageExists(dClient *client.Client, hostname string, containerImage string) (bool, error) {
|
||||
func localImageExists(ctx context.Context, 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)
|
||||
_, _, err := dClient.ImageInspectWithRaw(ctx, containerImage)
|
||||
if err != nil {
|
||||
if client.IsErrNotFound(err) {
|
||||
logrus.Debugf("Image [%s] does not exist on host [%s]: %v", containerImage, hostname, err)
|
||||
@@ -140,8 +141,8 @@ func localImageExists(dClient *client.Client, hostname string, containerImage st
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func pullImage(dClient *client.Client, hostname string, containerImage string) error {
|
||||
out, err := dClient.ImagePull(context.Background(), containerImage, types.ImagePullOptions{})
|
||||
func pullImage(ctx context.Context, dClient *client.Client, hostname string, containerImage string) error {
|
||||
out, err := dClient.ImagePull(ctx, containerImage, types.ImagePullOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can't pull Docker image [%s] for host [%s]: %v", containerImage, hostname, err)
|
||||
}
|
||||
@@ -155,84 +156,84 @@ 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)
|
||||
func UseLocalOrPull(ctx context.Context, dClient *client.Client, hostname string, containerImage string, plane string) error {
|
||||
log.Infof(ctx, "[%s] Checking image [%s] on host [%s]", plane, containerImage, hostname)
|
||||
imageExists, err := localImageExists(ctx, 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)
|
||||
log.Infof(ctx, "[%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 {
|
||||
log.Infof(ctx, "[%s] Pulling image [%s] on host [%s]", plane, containerImage, hostname)
|
||||
if err := pullImage(ctx, dClient, hostname, containerImage); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("[%s] Successfully pulled image [%s] on host [%s]", plane, containerImage, hostname)
|
||||
log.Infof(ctx, "[%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{})
|
||||
func RemoveContainer(ctx context.Context, dClient *client.Client, hostname string, containerName string) error {
|
||||
err := dClient.ContainerRemove(ctx, containerName, types.ContainerRemoveOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can't remove Docker container [%s] for host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func StopContainer(dClient *client.Client, hostname string, containerName string) error {
|
||||
err := dClient.ContainerStop(context.Background(), containerName, nil)
|
||||
func StopContainer(ctx context.Context, dClient *client.Client, hostname string, containerName string) error {
|
||||
err := dClient.ContainerStop(ctx, containerName, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can't stop Docker container [%s] for host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RenameContainer(dClient *client.Client, hostname string, oldContainerName string, newContainerName string) error {
|
||||
err := dClient.ContainerRename(context.Background(), oldContainerName, newContainerName)
|
||||
func RenameContainer(ctx context.Context, dClient *client.Client, hostname string, oldContainerName string, newContainerName string) error {
|
||||
err := dClient.ContainerRename(ctx, oldContainerName, newContainerName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can't rename Docker container [%s] for host [%s]: %v", oldContainerName, hostname, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func StartContainer(dClient *client.Client, hostname string, containerName string) error {
|
||||
if err := dClient.ContainerStart(context.Background(), containerName, types.ContainerStartOptions{}); err != nil {
|
||||
func StartContainer(ctx context.Context, dClient *client.Client, hostname string, containerName string) error {
|
||||
if err := dClient.ContainerStart(ctx, containerName, types.ContainerStartOptions{}); err != nil {
|
||||
return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateContiner(dClient *client.Client, hostname string, containerName string, imageCfg *container.Config, hostCfg *container.HostConfig) (container.ContainerCreateCreatedBody, error) {
|
||||
created, err := dClient.ContainerCreate(context.Background(), imageCfg, hostCfg, nil, containerName)
|
||||
func CreateContiner(ctx context.Context, dClient *client.Client, hostname string, containerName string, imageCfg *container.Config, hostCfg *container.HostConfig) (container.ContainerCreateCreatedBody, error) {
|
||||
created, err := dClient.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName)
|
||||
if err != nil {
|
||||
return container.ContainerCreateCreatedBody{}, fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return created, nil
|
||||
}
|
||||
|
||||
func InspectContainer(dClient *client.Client, hostname string, containerName string) (types.ContainerJSON, error) {
|
||||
inspection, err := dClient.ContainerInspect(context.Background(), containerName)
|
||||
func InspectContainer(ctx context.Context, dClient *client.Client, hostname string, containerName string) (types.ContainerJSON, error) {
|
||||
inspection, err := dClient.ContainerInspect(ctx, containerName)
|
||||
if err != nil {
|
||||
return types.ContainerJSON{}, fmt.Errorf("Failed to inspect [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return inspection, nil
|
||||
}
|
||||
|
||||
func StopRenameContainer(dClient *client.Client, hostname string, oldContainerName string, newContainerName string) error {
|
||||
if err := StopContainer(dClient, hostname, oldContainerName); err != nil {
|
||||
func StopRenameContainer(ctx context.Context, dClient *client.Client, hostname string, oldContainerName string, newContainerName string) error {
|
||||
if err := StopContainer(ctx, dClient, hostname, oldContainerName); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := WaitForContainer(dClient, oldContainerName); err != nil {
|
||||
if err := WaitForContainer(ctx, dClient, oldContainerName); err != nil {
|
||||
return nil
|
||||
}
|
||||
err := RenameContainer(dClient, hostname, oldContainerName, newContainerName)
|
||||
err := RenameContainer(ctx, dClient, hostname, oldContainerName, newContainerName)
|
||||
return err
|
||||
}
|
||||
|
||||
func WaitForContainer(dClient *client.Client, containerName string) error {
|
||||
statusCh, errCh := dClient.ContainerWait(context.Background(), containerName, container.WaitConditionNotRunning)
|
||||
func WaitForContainer(ctx context.Context, dClient *client.Client, containerName string) error {
|
||||
statusCh, errCh := dClient.ContainerWait(ctx, containerName, container.WaitConditionNotRunning)
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
@@ -243,11 +244,11 @@ func WaitForContainer(dClient *client.Client, containerName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsContainerUpgradable(dClient *client.Client, imageCfg *container.Config, containerName string, hostname string, plane string) (bool, error) {
|
||||
func IsContainerUpgradable(ctx context.Context, dClient *client.Client, imageCfg *container.Config, containerName string, hostname string, plane string) (bool, error) {
|
||||
logrus.Debugf("[%s] Checking if container [%s] is eligible for upgrade on host [%s]", plane, containerName, hostname)
|
||||
// this should be moved to a higher layer.
|
||||
|
||||
containerInspect, err := InspectContainer(dClient, hostname, containerName)
|
||||
containerInspect, err := InspectContainer(ctx, dClient, hostname, containerName)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user