forked from rancher/agent
/
response.go
executable file
·186 lines (170 loc) · 6.23 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package utils
import (
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
"github.com/rancher/agent/utilities/config"
"github.com/rancher/agent/utilities/constants"
revents "github.com/rancher/event-subscriber/events"
"golang.org/x/net/context"
)
func InstanceHostMapReply(event *revents.Event, client *client.Client, cache *cache.Cache) (map[string]interface{}, error) {
resp, err := getInstanceHostMapData(event, client, cache)
if err != nil {
return map[string]interface{}{}, errors.Wrap(err, constants.GetResponseDataError+"failed to get instance host map")
}
return map[string]interface{}{event.ResourceType: resp}, nil
}
func VolumeStoragePoolMapReply() (map[string]interface{}, error) {
return map[string]interface{}{
"volumeStoragePoolMap": map[string]interface{}{
"volume": map[string]interface{}{
"format": "docker",
},
},
}, nil
}
func InstancePullReply(event *revents.Event, client *client.Client) (map[string]interface{}, error) {
resp, err := getInstancePullData(event, client)
if err != nil {
return map[string]interface{}{}, errors.Wrap(err, constants.GetResponseDataError+"failed to get instance pull data")
}
return map[string]interface{}{
"fields": map[string]interface{}{
"dockerImage": resp,
},
}, nil
}
func ImageStoragePoolMapReply() (map[string]interface{}, error) {
return map[string]interface{}{
"imageStoragePoolMap": map[string]interface{}{},
}, 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
}
func getInstanceHostMapData(event *revents.Event, client *client.Client, cache *cache.Cache) (map[string]interface{}, error) {
instance, _, err := GetInstanceAndHost(event)
if err != nil {
return map[string]interface{}{}, errors.Wrap(err, constants.GetInstanceHostMapDataError+"failed to marshall instancehostmap")
}
container, err := GetContainer(client, instance, false)
if err != nil && !IsContainerNotFoundError(err) {
return map[string]interface{}{}, errors.Wrap(err, constants.GetInstanceHostMapDataError+"failed to get container")
}
if container.ID == "" {
update := map[string]interface{}{
"instance": map[string]interface{}{
"+data": map[string]interface{}{
"dockerInspect": nil,
"dockerContainer": nil,
"+fields": map[string]interface{}{
"dockerHostIp": config.DockerHostIP(),
"dockerPorts": nil,
"dockerIp": nil,
},
},
},
}
in, _ := GetFieldsIfExist(update, "instance")
instanceMap := InterfaceToMap(in)
// in this case agent can't find the container so externalID will be populated from instance data
instanceMap["externalId"] = instance.ExternalID
return update, nil
}
dockerPorts := []string{}
dockerMounts := []types.MountPoint{}
inspect, err := client.ContainerInspect(context.Background(), container.ID)
if err != nil {
return map[string]interface{}{}, errors.Wrap(err, constants.GetInstanceHostMapDataError+"failed to inspect container")
}
dockerMounts, err = getMountData(container.ID, client)
if err != nil {
return map[string]interface{}{}, errors.Wrap(err, constants.GetInstanceHostMapDataError+"failed to get mount data")
}
dockerIP, err := getIP(inspect, cache)
if err != nil && !IsNoopEvent(event) {
if running, err2 := isRunning(inspect.ID, client); err2 != nil {
return nil, errors.Wrap(err2, constants.GetInstanceHostMapDataError+"failed to inspect running container")
} else if running {
return nil, errors.Wrap(err, constants.GetInstanceHostMapDataError+"failed to get ip of the container")
}
}
if container.Ports != nil && len(container.Ports) > 0 {
for _, port := range container.Ports {
privatePort := fmt.Sprintf("%v/%v", port.PrivatePort, port.Type)
portSpec := privatePort
bindAddr := ""
if port.IP != "" {
bindAddr = fmt.Sprintf("%s:", port.IP)
}
publicPort := ""
if port.PublicPort > 0 {
publicPort = fmt.Sprintf("%v:", port.PublicPort)
} else if port.IP != "" {
publicPort = ":"
}
portSpec = bindAddr + publicPort + portSpec
dockerPorts = append(dockerPorts, portSpec)
}
}
if len(dockerPorts) == 0 {
image, _, err := client.ImageInspectWithRaw(context.Background(), container.ImageID)
if err == nil && image.Config != nil {
for k := range image.Config.ExposedPorts {
dockerPorts = append(dockerPorts, string(k))
}
}
}
update := map[string]interface{}{
"instance": map[string]interface{}{
"+data": map[string]interface{}{
"dockerContainer": container,
"dockerInspect": inspect,
"+fields": map[string]interface{}{
"dockerHostIp": config.DockerHostIP(),
"dockerPorts": dockerPorts,
"dockerIp": dockerIP,
},
},
},
}
in, _ := GetFieldsIfExist(update, "instance")
instanceMap := InterfaceToMap(in)
instanceMap["externalId"] = container.ID
if dockerMounts != nil {
da, _ := GetFieldsIfExist(update, "instance", "+data")
dataMap := InterfaceToMap(da)
dataMap["dockerMounts"] = dockerMounts
}
return update, nil
}
func getMountData(containerID string, client *client.Client) ([]types.MountPoint, error) {
inspect, err := client.ContainerInspect(context.Background(), containerID)
if err != nil {
return []types.MountPoint{}, errors.Wrap(err, constants.GetMountDataError+"failed to inspect container")
}
return inspect.Mounts, nil
}
func getInstancePullData(event *revents.Event, dockerClient *client.Client) (types.ImageInspect, error) {
imageName, ok := GetFieldsIfExist(event.Data, "instancePull", "image", "data", "dockerImage", "fullName")
if !ok {
return types.ImageInspect{}, errors.New("Failed to get instance pull data: Can't get image name from event")
}
tag, ok := GetFieldsIfExist(event.Data, "instancePull", "tag")
if !ok {
return types.ImageInspect{}, errors.New("Failed to get instance pull data: Can't get image tag from event")
}
inspect, _, err := dockerClient.ImageInspectWithRaw(context.Background(),
fmt.Sprintf("%v%v", imageName, tag))
if err != nil && !client.IsErrImageNotFound(err) {
return types.ImageInspect{}, errors.Wrap(err, constants.GetInstancePullDataError+"failed to inspect images")
}
return inspect, nil
}