forked from rancher/agent
/
response.go
66 lines (58 loc) · 2.06 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package handlers
import (
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/rancher/agent/progress"
"github.com/rancher/agent/utils"
v3 "github.com/rancher/go-rancher/v3"
"golang.org/x/net/context"
)
const (
linkName = "eth0"
cniStateBaseDir = "/var/lib/rancher/state/cni"
UUIDLabel = "io.rancher.container.uuid"
)
func constructDeploymentSyncReply(containerSpec v3.Container, client *client.Client, networkKind string, pro *progress.Progress) (interface{}, error) {
response := v3.DeploymentSyncResponse{}
containerID, err := utils.FindContainer(client, containerSpec, false)
if err != nil && !utils.IsContainerNotFoundError(err) {
return map[string]interface{}{}, errors.Wrap(err, "failed to get container")
}
if containerID == "" {
status := v3.InstanceStatus{}
status.InstanceUuid = containerSpec.Uuid
response.InstanceStatus = []v3.InstanceStatus{status}
return response, nil
}
inspect, err := client.ContainerInspect(context.Background(), containerID)
if err != nil {
return map[string]interface{}{}, errors.Wrap(err, "failed to inspect container")
}
dockerIP, err := getIP(inspect, networkKind, pro)
if err != nil && !utils.IsNoOp(containerSpec) {
if running, err2 := isRunning(inspect.ID, client); err2 != nil {
return nil, errors.Wrap(err2, "failed to inspect running container")
} else if running {
return nil, errors.Wrap(err, "failed to get ip of the container")
}
}
status := v3.InstanceStatus{}
status.ExternalId = inspect.ID
dockerInspect, err := utils.StructToMap(inspect)
if err != nil {
return nil, errors.Wrap(err, "failed to unMarshall docker inspect")
}
status.DockerInspect = dockerInspect
status.InstanceUuid = containerSpec.Uuid
status.PrimaryIpAddress = dockerIP
status.State = inspect.State.Status
response.InstanceStatus = []v3.InstanceStatus{status}
return response, nil
}
func isRunning(id string, client *client.Client) (bool, error) {
inspect, err := client.ContainerInspect(context.Background(), id)
if err != nil {
return false, err
}
return inspect.State.Pid != 0, nil
}