mirror of
https://github.com/rancher/rke.git
synced 2025-09-01 15:06:23 +00:00
Refactor reconcilation
Add role based add/delete for services Use pointers for hosts Consistent log format
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
@@ -19,7 +20,7 @@ func DoRunContainer(dClient *client.Client, imageCfg *container.Config, hostCfg
|
||||
return err
|
||||
}
|
||||
if isRunning {
|
||||
logrus.Infof("[%s] Container %s is already running on host [%s]", plane, containerName, hostname)
|
||||
logrus.Infof("[%s] Container [%s] is already running on host [%s]", plane, containerName, hostname)
|
||||
isUpgradable, err := IsContainerUpgradable(dClient, imageCfg, containerName, hostname, plane)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -35,21 +36,21 @@ func DoRunContainer(dClient *client.Client, imageCfg *container.Config, hostCfg
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("[%s] Successfully pulled %s image on host [%s]", plane, containerName, hostname)
|
||||
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)
|
||||
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 {
|
||||
return fmt.Errorf("Failed to start %s container on host [%s]: %v", containerName, hostname, err)
|
||||
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)
|
||||
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)
|
||||
return nil
|
||||
}
|
||||
|
||||
func DoRollingUpdateContainer(dClient *client.Client, imageCfg *container.Config, hostCfg *container.HostConfig, containerName, hostname, plane string) error {
|
||||
logrus.Debugf("[%s] Checking for deployed %s", plane, containerName)
|
||||
logrus.Debugf("[%s] Checking for deployed [%s]", plane, containerName)
|
||||
isRunning, err := IsContainerRunning(dClient, hostname, containerName)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -63,7 +64,7 @@ func DoRollingUpdateContainer(dClient *client.Client, imageCfg *container.Config
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("[%s] Successfully pulled %s image on host [%s]", plane, containerName, hostname)
|
||||
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 {
|
||||
@@ -72,12 +73,12 @@ func DoRollingUpdateContainer(dClient *client.Client, imageCfg *container.Config
|
||||
logrus.Infof("[%s] Successfully stopped old container %s on host [%s]", plane, containerName, hostname)
|
||||
_, err = CreateContiner(dClient, hostname, containerName, imageCfg, hostCfg)
|
||||
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)
|
||||
}
|
||||
if err := StartContainer(dClient, hostname, containerName); err != nil {
|
||||
return fmt.Errorf("Failed to start %s container on host [%s]: %v", containerName, hostname, err)
|
||||
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)
|
||||
logrus.Infof("[%s] Successfully updated [%s] container on host [%s]", plane, containerName, hostname)
|
||||
logrus.Debugf("[%s] Removing old container", plane)
|
||||
err = RemoveContainer(dClient, hostname, oldContainerName)
|
||||
return err
|
||||
@@ -110,7 +111,7 @@ func DoRemoveContainer(dClient *client.Client, containerName, hostname string) e
|
||||
}
|
||||
|
||||
func IsContainerRunning(dClient *client.Client, hostname string, containerName string) (bool, error) {
|
||||
logrus.Debugf("Checking if container %s is running on host [%s]", containerName, hostname)
|
||||
logrus.Debugf("Checking if container [%s] is running on host [%s]", containerName, hostname)
|
||||
containers, err := dClient.ContainerList(context.Background(), types.ContainerListOptions{})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("Can't get Docker containers for host [%s]: %v", hostname, err)
|
||||
@@ -127,7 +128,7 @@ func IsContainerRunning(dClient *client.Client, hostname string, containerName s
|
||||
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)
|
||||
return fmt.Errorf("Can't pull Docker image [%s] for host [%s]: %v", containerImage, hostname, err)
|
||||
}
|
||||
defer out.Close()
|
||||
if logrus.GetLevel() == logrus.DebugLevel {
|
||||
@@ -142,7 +143,7 @@ func PullImage(dClient *client.Client, hostname string, containerImage string) e
|
||||
func RemoveContainer(dClient *client.Client, hostname string, containerName string) error {
|
||||
err := dClient.ContainerRemove(context.Background(), containerName, types.ContainerRemoveOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can't remove Docker container %s for host [%s]: %v", containerName, hostname, err)
|
||||
return fmt.Errorf("Can't remove Docker container [%s] for host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -150,7 +151,7 @@ func RemoveContainer(dClient *client.Client, hostname string, containerName stri
|
||||
func StopContainer(dClient *client.Client, hostname string, containerName string) error {
|
||||
err := dClient.ContainerStop(context.Background(), containerName, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can't stop Docker container %s for host [%s]: %v", containerName, hostname, err)
|
||||
return fmt.Errorf("Can't stop Docker container [%s] for host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -158,14 +159,14 @@ func StopContainer(dClient *client.Client, hostname string, containerName string
|
||||
func RenameContainer(dClient *client.Client, hostname string, oldContainerName string, newContainerName string) error {
|
||||
err := dClient.ContainerRename(context.Background(), oldContainerName, newContainerName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can't rename Docker container %s for host [%s]: %v", oldContainerName, hostname, err)
|
||||
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 {
|
||||
return fmt.Errorf("Failed to start %s container on host [%s]: %v", containerName, hostname, err)
|
||||
return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -173,7 +174,7 @@ func StartContainer(dClient *client.Client, hostname string, containerName strin
|
||||
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)
|
||||
if err != nil {
|
||||
return container.ContainerCreateCreatedBody{}, fmt.Errorf("Failed to create %s container on host [%s]: %v", containerName, hostname, err)
|
||||
return container.ContainerCreateCreatedBody{}, fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return created, nil
|
||||
}
|
||||
@@ -181,7 +182,7 @@ func CreateContiner(dClient *client.Client, hostname string, containerName strin
|
||||
func InspectContainer(dClient *client.Client, hostname string, containerName string) (types.ContainerJSON, error) {
|
||||
inspection, err := dClient.ContainerInspect(context.Background(), containerName)
|
||||
if err != nil {
|
||||
return types.ContainerJSON{}, fmt.Errorf("Failed to inspect %s container on host [%s]: %v", containerName, hostname, err)
|
||||
return types.ContainerJSON{}, fmt.Errorf("Failed to inspect [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||
}
|
||||
return inspection, nil
|
||||
}
|
||||
@@ -210,17 +211,17 @@ func WaitForContainer(dClient *client.Client, containerName string) error {
|
||||
}
|
||||
|
||||
func IsContainerUpgradable(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)
|
||||
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)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if containerInspect.Config.Image == imageCfg.Image {
|
||||
logrus.Debugf("[%s] Container %s is not eligible for updgrade on host [%s]", plane, containerName, hostname)
|
||||
return false, nil
|
||||
if containerInspect.Config.Image != imageCfg.Image || !reflect.DeepEqual(containerInspect.Config.Cmd, imageCfg.Cmd) {
|
||||
logrus.Debugf("[%s] Container [%s] is eligible for updgrade on host [%s]", plane, containerName, hostname)
|
||||
return true, nil
|
||||
}
|
||||
logrus.Debugf("[%s] Container %s is eligible for updgrade on host [%s]", plane, containerName, hostname)
|
||||
return true, nil
|
||||
logrus.Debugf("[%s] Container [%s] is not eligible for updgrade on host [%s]", plane, containerName, hostname)
|
||||
return false, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user