Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #64 from StrongMonkey/add-timeout-to-all-docker-ps
Browse files Browse the repository at this point in the history
add timeout to all container list
  • Loading branch information
ibuildthecloud authored Oct 17, 2016
2 parents dce0399 + fa08655 commit 85283b5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
46 changes: 31 additions & 15 deletions core/compute/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/rancher/agent/core/storage"
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/docker"
"github.com/rancher/agent/utilities/utils"
"golang.org/x/net/context"
"strings"
Expand Down Expand Up @@ -103,7 +104,7 @@ func DoInstanceActivate(instance model.Instance, host model.Host, progress *prog
}
containerID := container.ID
created := false
if len(containerID) == 0 {
if containerID == "" {
newID, err := createContainer(dockerClient, &config, &hostConfig, imageTag, instance, name, progress)
if err != nil {
return errors.Wrap(err, constants.DoInstanceActivateError+"failed to create container")
Expand Down Expand Up @@ -205,31 +206,46 @@ func DoInstanceForceStop(request model.InstanceForceStop, dockerClient *client.C
}

func DoInstanceInspect(inspect model.InstanceInspect, dockerClient *client.Client) (types.ContainerJSON, error) {
containerID := inspect.ID
containerList, err := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{All: true})
clientWithTimeout, err := docker.NewEnvClientWithTimeout(time.Duration(2) * time.Second)
if err != nil {
return types.ContainerJSON{}, errors.Wrap(err, constants.DoInstanceInspectError+"failed to list containers")
return types.ContainerJSON{}, errors.New("Can't initialize docker client")
}
result, find := utils.FindFirst(containerList, func(c types.Container) bool {
return utils.IDFilter(containerID, c)
})
if !find {
clientWithTimeout.UpdateClientVersion(constants.DefaultVersion)
containerID := inspect.ID
if containerID != "" {
// inspect by id
containerInspect, err := clientWithTimeout.ContainerInspect(context.Background(), containerID)
if err != nil && !client.IsErrContainerNotFound(err) {
return types.ContainerJSON{}, errors.Wrap(err, constants.DoInstanceInspectError+"Failed to inspect container")
} else if err == nil {
return containerInspect, nil
}
}
if inspect.Name != "" {
// inspect by name
containerList, err := clientWithTimeout.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
return types.ContainerJSON{}, errors.Wrap(err, constants.DoInstanceInspectError+"failed to list containers")
}
find := false
result := types.Container{}
name := fmt.Sprintf("/%s", inspect.Name)
if resultWithNameInspect, ok := utils.FindFirst(containerList, func(c types.Container) bool {
return utils.NameFilter(name, c)
}); ok {
result = resultWithNameInspect
find = true
}
}
if find {
inspectResp, err := dockerClient.ContainerInspect(context.Background(), result.ID)
if err != nil {
return types.ContainerJSON{}, errors.Wrap(err, constants.DoInstanceInspectError+"failed to inspect container")

if find {
inspectResp, err := clientWithTimeout.ContainerInspect(context.Background(), result.ID)
if err != nil && !client.IsErrContainerNotFound(err) {
return types.ContainerJSON{}, errors.Wrap(err, constants.DoInstanceInspectError+"failed to inspect container")
}
return inspectResp, nil
}
return inspectResp, nil
}
return types.ContainerJSON{}, fmt.Errorf("container with id [%v] not found", containerID)
return types.ContainerJSON{}, errors.Errorf("container with id [%v] not found", containerID)
}

func DoInstanceRemove(instance model.Instance, dockerClient *client.Client) error {
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func main() {
os.Exit(0)
}

if os.Getenv("CATTLE_DEBUG") != "" {
logrus.SetLevel(logrus.DebugLevel)
}

logrus.Info("Launching agent")

url := os.Getenv("CATTLE_URL")
Expand Down
31 changes: 14 additions & 17 deletions utilities/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/rancher/agent/core/progress"
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/docker"
revents "github.com/rancher/event-subscriber/events"
"github.com/rancher/go-rancher/v2"
"golang.org/x/net/context"
Expand Down Expand Up @@ -354,25 +355,21 @@ func ParseRepoTag(name string) model.RepoTag {
}

func GetContainer(client *engineCli.Client, instance model.Instance, byAgent bool) (types.Container, error) {
// First look for UUID label directly
args := filters.NewArgs()
args.Add("label", fmt.Sprintf("%s=%s", constants.UUIDLabel, instance.UUID))
options := types.ContainerListOptions{All: true, Filter: args}
labeledContainers, err := client.ContainerList(context.Background(), options)
if err == nil && len(labeledContainers) > 0 {
return labeledContainers[0], nil
} else if err != nil {
return types.Container{}, errors.Wrap(err, constants.GetContainerError+"failed to list containers")
clientWithTimeout, err := docker.NewEnvClientWithTimeout(time.Duration(2) * time.Second)
if err != nil {
return types.Container{}, errors.Wrap(err, constants.GetContainerError+"failed to get docker client")
}

// Next look by UUID using fallback method
options = types.ContainerListOptions{All: true}
containerList, err := client.ContainerList(context.Background(), options)
clientWithTimeout.UpdateClientVersion(constants.DefaultVersion)
containers, err := clientWithTimeout.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
return types.Container{}, errors.Wrap(err, constants.GetContainerError+"failed to list containers")
}

if container, ok := FindFirst(containerList, func(c types.Container) bool {
for _, container := range containers {
if uuid, ok := container.Labels[constants.UUIDLabel]; ok && uuid == instance.UUID {
return container, nil
}
}
if container, ok := FindFirst(containers, func(c types.Container) bool {
if GetUUID(c) == instance.UUID {
return true
}
Expand All @@ -382,7 +379,7 @@ func GetContainer(client *engineCli.Client, instance model.Instance, byAgent boo
}

if externalID := instance.ExternalID; externalID != "" {
if container, ok := FindFirst(containerList, func(c types.Container) bool {
if container, ok := FindFirst(containers, func(c types.Container) bool {
return IDFilter(externalID, c)
}); ok {
return container, nil
Expand All @@ -391,7 +388,7 @@ func GetContainer(client *engineCli.Client, instance model.Instance, byAgent boo

if byAgent {
agentID := instance.AgentID
if container, ok := FindFirst(containerList, func(c types.Container) bool {
if container, ok := FindFirst(containers, func(c types.Container) bool {
return AgentIDFilter(strconv.Itoa(agentID), c)
}); ok {
return container, nil
Expand Down

0 comments on commit 85283b5

Please sign in to comment.