-
Notifications
You must be signed in to change notification settings - Fork 15
/
metadata.go
278 lines (229 loc) · 8.42 KB
/
metadata.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// SPDX-License-Identifier: Apache-2.0
package metadata
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"time"
cnitypes "github.com/containernetworking/cni/pkg/types/current"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
type CheckpointedPod struct {
PodUID string `json:"io.kubernetes.pod.uid,omitempty"`
ID string `json:"SandboxID,omitempty"`
Name string `json:"io.kubernetes.pod.name,omitempty"`
TerminationGracePeriod int64 `json:"io.kubernetes.pod.terminationGracePeriod,omitempty"`
Namespace string `json:"io.kubernetes.pod.namespace,omitempty"`
ConfigSource string `json:"kubernetes.io/config.source,omitempty"`
ConfigSeen string `json:"kubernetes.io/config.seen,omitempty"`
Manager string `json:"io.container.manager,omitempty"`
Containers []CheckpointedContainer `json:"Containers"`
HostIP string `json:"hostIP,omitempty"`
PodIP string `json:"podIP,omitempty"`
PodIPs []string `json:"podIPs,omitempty"`
}
type CheckpointedContainer struct {
Name string `json:"io.kubernetes.container.name,omitempty"`
ID string `json:"id,omitempty"`
TerminationMessagePath string `json:"io.kubernetes.container.terminationMessagePath,omitempty"`
TerminationMessagePolicy string `json:"io.kubernetes.container.terminationMessagePolicy,omitempty"`
RestartCounter int32 `json:"io.kubernetes.container.restartCount,omitempty"`
TerminationMessagePathUID string `json:"terminationMessagePathUID,omitempty"`
Image string `json:"Image"`
}
type CheckpointMetadata struct {
Version int `json:"version"`
CheckpointedPods []CheckpointedPod
}
const (
// kubelet archive
CheckpointedPodsFile = "checkpointed.pods"
// container archive
ConfigDumpFile = "config.dump"
SpecDumpFile = "spec.dump"
NetworkStatusFile = "network.status"
CheckpointDirectory = "checkpoint"
RootFsDiffTar = "rootfs-diff.tar"
DeletedFilesFile = "deleted.files"
// pod archive
PodOptionsFile = "pod.options"
PodDumpFile = "pod.dump"
)
type CheckpointType int
const (
// The checkpoint archive contains a kubelet checkpoint
// One or multiple pods and kubelet metadata (checkpointed.pods)
Kubelet CheckpointType = iota
// The checkpoint archive contains one pod including one or multiple containers
Pod
// The checkpoint archive contains a single container
Container
Unknown
)
// This is a reduced copy of what Podman uses to store checkpoint metadata
type ContainerConfig struct {
ID string `json:"id"`
Name string `json:"name"`
RootfsImageName string `json:"rootfsImageName,omitempty"`
OCIRuntime string `json:"runtime,omitempty"`
CreatedTime time.Time `json:"createdTime"`
}
// This is metadata stored inside of a Pod checkpoint archive
type CheckpointedPodOptions struct {
Version int `json:"version"`
Containers []string `json:"containers,omitempty"`
MountLabel string `json:"mountLabel"`
ProcessLabel string `json:"processLabel"`
}
// This is metadata stored inside of Pod checkpoint archive
type PodSandboxConfig struct {
Metadata SandboxMetadta `json:"metadata"`
Hostname string `json:"hostname"`
}
type SandboxMetadta struct {
Name string `json:"name"`
UID string `json:"uid"`
Namespace string `json:"namespace"`
}
func checkForFile(checkpointDirectory, file string) (bool, error) {
_, err := os.Stat(filepath.Join(checkpointDirectory, file))
if err != nil && !os.IsNotExist(err) {
return false, errors.Wrapf(err, "Failed to access %q\n", file)
}
if os.IsNotExist(err) {
return false, nil
}
return true, nil
}
func DetectCheckpointArchiveType(checkpointDirectory string) (CheckpointType, error) {
kubelet, err := checkForFile(checkpointDirectory, CheckpointedPodsFile)
if os.IsNotExist(err) {
return Unknown, err
}
container, err := checkForFile(checkpointDirectory, ConfigDumpFile)
if os.IsNotExist(err) {
return Unknown, err
}
pod, err := checkForFile(checkpointDirectory, PodDumpFile)
if os.IsNotExist(err) {
return Unknown, err
}
if pod && !container && !kubelet {
return Pod, nil
}
if !pod && container && !kubelet {
return Container, nil
}
if !pod && !container && kubelet {
return Kubelet, nil
}
return Unknown, nil
}
func ReadPodCheckpointDumpFile(checkpointDirectory string) (*PodSandboxConfig, string, error) {
var podSandboxConfig PodSandboxConfig
podDumpFile, err := ReadJSONFile(&podSandboxConfig, checkpointDirectory, PodDumpFile)
return &podSandboxConfig, podDumpFile, err
}
func ReadPodCheckpointOptionsFile(checkpointDirectory string) (*CheckpointedPodOptions, string, error) {
var checkpointedPodOptions CheckpointedPodOptions
podOptionsFile, err := ReadJSONFile(&checkpointedPodOptions, checkpointDirectory, PodOptionsFile)
return &checkpointedPodOptions, podOptionsFile, err
}
func ReadContainerCheckpointSpecDump(checkpointDirectory string) (*spec.Spec, string, error) {
var specDump spec.Spec
specDumpFile, err := ReadJSONFile(&specDump, checkpointDirectory, SpecDumpFile)
return &specDump, specDumpFile, err
}
func ReadContainerCheckpointConfigDump(checkpointDirectory string) (*ContainerConfig, string, error) {
var containerConfig ContainerConfig
configDumpFile, err := ReadJSONFile(&containerConfig, checkpointDirectory, ConfigDumpFile)
return &containerConfig, configDumpFile, err
}
func ReadContainerCheckpointDeletedFiles(checkpointDirectory string) ([]string, string, error) {
var deletedFiles []string
deletedFilesFile, err := ReadJSONFile(&deletedFiles, checkpointDirectory, DeletedFilesFile)
return deletedFiles, deletedFilesFile, err
}
func ReadContainerCheckpointNetworkStatus(checkpointDirectory string) ([]*cnitypes.Result, string, error) {
var networkStatus []*cnitypes.Result
networkStatusFile, err := ReadJSONFile(&networkStatus, checkpointDirectory, NetworkStatusFile)
return networkStatus, networkStatusFile, err
}
func ReadKubeletCheckpoints(checkpointsDirectory string) (*CheckpointMetadata, string, error) {
var checkpointMetadata CheckpointMetadata
checkpointMetadataPath, err := ReadJSONFile(&checkpointMetadata, checkpointsDirectory, CheckpointedPodsFile)
return &checkpointMetadata, checkpointMetadataPath, err
}
func GetIPFromNetworkStatus(networkStatus []*cnitypes.Result) net.IP {
if len(networkStatus) == 0 {
return nil
}
// Take the first IP address
if len(networkStatus[0].IPs) == 0 {
return nil
}
IP := networkStatus[0].IPs[0].Address.IP
return IP
}
func GetMACFromNetworkStatus(networkStatus []*cnitypes.Result) net.HardwareAddr {
if len(networkStatus) == 0 {
return nil
}
// Take the first device with a defined sandbox
if len(networkStatus[0].Interfaces) == 0 {
return nil
}
var MAC net.HardwareAddr
MAC = nil
for _, n := range networkStatus[0].Interfaces {
if n.Sandbox != "" {
MAC, _ = net.ParseMAC(n.Mac)
break
}
}
return MAC
}
// WriteJSONFile marshalls and writes the given data to a JSON file
func WriteJSONFile(v interface{}, dir, file string) (string, error) {
fileJSON, err := json.MarshalIndent(v, "", " ")
if err != nil {
return "", errors.Wrapf(err, "Error marshalling JSON")
}
file = filepath.Join(dir, file)
if err := ioutil.WriteFile(file, fileJSON, 0o600); err != nil {
return "", errors.Wrapf(err, "Error writing to %q", file)
}
return file, nil
}
func ReadJSONFile(v interface{}, dir, file string) (string, error) {
file = filepath.Join(dir, file)
content, err := ioutil.ReadFile(file)
if err != nil {
return "", errors.Wrapf(err, "failed to read %s", file)
}
if err = json.Unmarshal(content, v); err != nil {
return "", errors.Wrapf(err, "failed to unmarshal %s", file)
}
return file, nil
}
func WriteKubeletCheckpointsMetadata(checkpointMetadata *CheckpointMetadata, dir string) error {
_, err := WriteJSONFile(checkpointMetadata, dir, CheckpointedPodsFile)
return err
}
func ByteToString(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}