-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinternal.go
More file actions
152 lines (139 loc) · 4.27 KB
/
internal.go
File metadata and controls
152 lines (139 loc) · 4.27 KB
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
package mapping
import (
"net"
core "github.com/ernoaapa/eliot/pkg/api/core"
containers "github.com/ernoaapa/eliot/pkg/api/services/containers/v1"
node "github.com/ernoaapa/eliot/pkg/api/services/node/v1"
pods "github.com/ernoaapa/eliot/pkg/api/services/pods/v1"
"github.com/ernoaapa/eliot/pkg/model"
)
// MapInfoToAPIModel maps internal node info model to API model
func MapInfoToAPIModel(info *model.NodeInfo) *node.Info {
return &node.Info{
Uptime: info.Uptime,
Labels: mapLabelsToAPIModel(info.Labels),
Hostname: info.Hostname,
Addresses: addressesToString(info.Addresses),
GrpcPort: int64(info.GrpcPort),
MachineID: info.MachineID,
SystemUUID: info.SystemUUID,
BootID: info.BootID,
Arch: info.Arch,
Os: info.OS,
Version: info.Version,
Filesystems: mapFilesystemsToAPIModel(info.Filesystems),
}
}
func mapLabelsToAPIModel(labels map[string]string) (result []*node.Label) {
for key, value := range labels {
result = append(result, &node.Label{Key: key, Value: value})
}
return result
}
func addressesToString(addresses []net.IP) (result []string) {
for _, ip := range addresses {
result = append(result, ip.String())
}
return result
}
// MapPodsToAPIModel maps list of internal pod models to API model
func MapPodsToAPIModel(pods []model.Pod) (result []*pods.Pod) {
for _, pod := range pods {
result = append(result, MapPodToAPIModel(pod))
}
return result
}
// MapPodToAPIModel maps internal Pod model to API model
func MapPodToAPIModel(pod model.Pod) *pods.Pod {
return &pods.Pod{
Metadata: &core.ResourceMetadata{
Name: pod.Metadata.Name,
Namespace: pod.Metadata.Namespace,
},
Spec: &pods.PodSpec{
Containers: MapContainersToAPIModel(pod.Spec.Containers),
HostNetwork: pod.Spec.HostNetwork,
HostPID: pod.Spec.HostPID,
RestartPolicy: pod.Spec.RestartPolicy,
},
Status: &pods.PodStatus{
Hostname: pod.Status.Hostname,
ContainerStatuses: MapContainerStatusesToAPIModel(pod.Status.ContainerStatuses),
},
}
}
// MapContainersToAPIModel maps list of internal Container models to API model
func MapContainersToAPIModel(source []model.Container) (result []*containers.Container) {
for _, container := range source {
result = append(result, &containers.Container{
Name: container.Name,
Image: container.Image,
WorkingDir: container.WorkingDir,
Args: container.Args,
Env: container.Env,
Devices: mapDevicesToAPIModel(container.Devices),
Mounts: mapMountsToAPIModel(container.Mounts),
Pipe: mapPipeToAPIModel(container.Pipe),
})
}
return result
}
func mapDevicesToAPIModel(devices []model.Device) (result []*containers.Device) {
for _, device := range devices {
result = append(result, &containers.Device{
DeviceType: device.DeviceType,
Minorid: device.MinorId,
Majorid: device.MajorId,
})
}
return result
}
func mapMountsToAPIModel(mounts []model.Mount) (result []*containers.Mount) {
for _, mount := range mounts {
result = append(result, &containers.Mount{
Type: mount.Type,
Source: mount.Source,
Destination: mount.Destination,
Options: mount.Options,
})
}
return result
}
func mapPipeToAPIModel(pipe *model.PipeSet) *containers.PipeSet {
if pipe == nil {
return nil
}
return &containers.PipeSet{
Stdout: &containers.PipeFromStdout{
Stdin: &containers.PipeToStdin{
Name: pipe.Stdout.Stdin.Name,
},
},
}
}
// MapContainerStatusesToAPIModel maps list of internal ContainerStatus models to API model
func MapContainerStatusesToAPIModel(statuses []model.ContainerStatus) (result []*containers.ContainerStatus) {
for _, status := range statuses {
result = append(result, &containers.ContainerStatus{
ContainerID: status.ContainerID,
Name: status.Name,
Image: status.Image,
State: status.State,
RestartCount: int32(status.RestartCount),
})
}
return result
}
func mapFilesystemsToAPIModel(disks []model.Filesystem) (result []*node.Filesystem) {
for _, disk := range disks {
result = append(result, &node.Filesystem{
Filesystem: disk.Filesystem,
TypeName: disk.TypeName,
MountDir: disk.MountDir,
Total: disk.Total,
Free: disk.Free,
Available: disk.Available,
})
}
return result
}