/
container_status.go
177 lines (158 loc) · 4.96 KB
/
container_status.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
package server
import (
"fmt"
"time"
"github.com/cri-o/cri-o/internal/log"
oci "github.com/cri-o/cri-o/internal/oci"
"github.com/cri-o/cri-o/internal/storage"
json "github.com/json-iterator/go"
spec "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
)
const (
oomKilledReason = "OOMKilled"
seccompKilledReason = "seccomp killed"
completedReason = "Completed"
errorReason = "Error"
)
// ContainerStatus returns status of the container.
func (s *Server) ContainerStatus(ctx context.Context, req *types.ContainerStatusRequest) (*types.ContainerStatusResponse, error) {
ctx, span := log.StartSpan(ctx)
defer span.End()
c, err := s.GetContainerFromShortID(ctx, req.ContainerId)
if err != nil {
return nil, status.Errorf(codes.NotFound, "could not find container %q: %v", req.ContainerId, err)
}
containerID := c.ID()
imageRef := c.CRIContainer().ImageRef
imageNameInSpec := ""
if imageName := c.ImageName(); imageName != nil {
imageNameInSpec = imageName.StringForOutOfProcessConsumptionOnly()
}
resp := &types.ContainerStatusResponse{
Status: &types.ContainerStatus{
Id: containerID,
Metadata: c.Metadata(),
Labels: c.Labels(),
Annotations: c.Annotations(),
ImageRef: imageRef,
Image: &types.ImageSpec{
Image: imageNameInSpec,
},
},
}
mounts := []*types.Mount{}
for _, cv := range c.Volumes() {
mounts = append(mounts, &types.Mount{
ContainerPath: cv.ContainerPath,
HostPath: cv.HostPath,
Readonly: cv.Readonly,
Propagation: cv.Propagation,
SelinuxRelabel: cv.SelinuxRelabel,
})
}
resp.Status.Mounts = mounts
containerSpec := c.Spec()
if containerSpec.Linux != nil {
resp.Status.Resources = c.GetResources()
}
cState := c.StateNoLock()
rStatus := types.ContainerState_CONTAINER_UNKNOWN
// If we defaulted to exit code not set earlier then we attempt to
// get the exit code from the exit file again.
if cState.Status == oci.ContainerStateStopped && cState.ExitCode == nil {
err := s.Runtime().UpdateContainerStatus(ctx, c)
if err != nil {
log.Warnf(ctx, "Failed to UpdateStatus of container %s: %v", c.ID(), err)
}
cState = c.State()
}
resp.Status.CreatedAt = c.CreatedAt().UnixNano()
switch cState.Status {
case oci.ContainerStateCreated:
rStatus = types.ContainerState_CONTAINER_CREATED
case oci.ContainerStateRunning, oci.ContainerStatePaused:
rStatus = types.ContainerState_CONTAINER_RUNNING
started := cState.Started.UnixNano()
resp.Status.StartedAt = started
case oci.ContainerStateStopped:
rStatus = types.ContainerState_CONTAINER_EXITED
started := cState.Started.UnixNano()
resp.Status.StartedAt = started
finished := cState.Finished.UnixNano()
resp.Status.FinishedAt = finished
if cState.ExitCode == nil {
resp.Status.ExitCode = -1
} else {
resp.Status.ExitCode = *cState.ExitCode
}
switch {
case cState.OOMKilled:
resp.Status.Reason = oomKilledReason
case cState.SeccompKilled:
resp.Status.Reason = seccompKilledReason
resp.Status.Message = cState.Error
case resp.Status.ExitCode == 0:
resp.Status.Reason = completedReason
default:
resp.Status.Reason = errorReason
resp.Status.Message = cState.Error
}
}
resp.Status.State = rStatus
resp.Status.LogPath = c.LogPath()
if req.Verbose {
info, err := s.createContainerInfo(c)
if err != nil {
return nil, fmt.Errorf("creating container info: %w", err)
}
resp.Info = info
}
return resp, nil
}
type containerInfo struct {
SandboxID string `json:"sandboxID"`
Pid int `json:"pid"`
RuntimeSpec spec.Spec `json:"runtimeSpec"`
Privileged bool `json:"privileged"`
}
type containerInfoCheckpointRestore struct {
CheckpointedAt time.Time `json:"checkpointedAt"`
Restored bool `json:"restored"`
}
func (s *Server) createContainerInfo(container *oci.Container) (map[string]string, error) {
metadata, err := s.StorageRuntimeServer().GetContainerMetadata(container.ID())
if err != nil {
return nil, fmt.Errorf("getting container metadata: %w", err)
}
bytes, err := func(metadata *storage.RuntimeContainerMetadata) ([]byte, error) {
localContainerInfo := containerInfo{
SandboxID: container.Sandbox(),
Pid: container.StateNoLock().InitPid,
RuntimeSpec: container.Spec(),
Privileged: metadata.Privileged,
}
if s.config.CheckpointRestore() {
localContainerInfoCheckpointRestore := containerInfoCheckpointRestore{
CheckpointedAt: container.CheckpointedAt(),
Restored: container.Restore(),
}
info := struct {
containerInfo
containerInfoCheckpointRestore
}{
localContainerInfo,
localContainerInfoCheckpointRestore,
}
return json.Marshal(info)
}
return json.Marshal(localContainerInfo)
}(&metadata)
if err != nil {
return nil, fmt.Errorf("marshal data: %w", err)
}
return map[string]string{"info": string(bytes)}, nil
}