-
Notifications
You must be signed in to change notification settings - Fork 1k
/
inspect.go
256 lines (236 loc) · 8 KB
/
inspect.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
package server
import (
"errors"
"fmt"
"math"
"net/http"
"net/http/pprof"
"github.com/containers/storage/pkg/idtools"
"github.com/cri-o/cri-o/internal/lib/sandbox"
"github.com/cri-o/cri-o/internal/log"
"github.com/cri-o/cri-o/internal/oci"
"github.com/cri-o/cri-o/pkg/types"
"github.com/go-chi/chi/v5"
json "github.com/json-iterator/go"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
func (s *Server) getIDMappingsInfo() types.IDMappings {
max := int64(int(^uint(0) >> 1))
if max > math.MaxUint32 {
max = math.MaxUint32
}
if s.defaultIDMappings == nil {
fullMapping := idtools.IDMap{
ContainerID: 0,
HostID: 0,
Size: int(max),
}
return types.IDMappings{
Uids: []idtools.IDMap{fullMapping},
Gids: []idtools.IDMap{fullMapping},
}
}
return types.IDMappings{
Uids: s.defaultIDMappings.UIDs(),
Gids: s.defaultIDMappings.GIDs(),
}
}
func (s *Server) getInfo() types.CrioInfo {
return types.CrioInfo{
StorageDriver: s.config.Storage,
StorageRoot: s.config.Root,
StorageImage: s.config.ImageStore,
CgroupDriver: s.config.CgroupManager().Name(),
DefaultIDMappings: s.getIDMappingsInfo(),
}
}
var (
errCtrNotFound = errors.New("container not found")
errCtrStateNil = errors.New("container state is nil")
errSandboxNotFound = errors.New("sandbox for container not found")
)
func (s *Server) getContainerInfo(ctx context.Context, id string, getContainerFunc, getInfraContainerFunc func(ctx context.Context, id string) *oci.Container, getSandboxFunc func(ctx context.Context, id string) *sandbox.Sandbox) (types.ContainerInfo, error) {
ctx, span := log.StartSpan(ctx)
defer span.End()
ctr := getContainerFunc(ctx, id)
isInfra := false
if ctr == nil {
ctr = getInfraContainerFunc(ctx, id)
if ctr == nil {
return types.ContainerInfo{}, errCtrNotFound
}
isInfra = true
}
// TODO(mrunalp): should we call UpdateStatus()?
ctrState := ctr.StateNoLock()
if ctrState == nil {
return types.ContainerInfo{}, errCtrStateNil
}
sb := getSandboxFunc(ctx, ctr.Sandbox())
if sb == nil {
log.Debugf(ctx, "Can't find sandbox %s for container %s", ctr.Sandbox(), id)
return types.ContainerInfo{}, errSandboxNotFound
}
pidToReturn := ctrState.InitPid
if isInfra && pidToReturn == 0 {
// It is possible the infra container doesn't report a PID.
// That can either happen if we're using a vm based runtime,
// or if we've dropped the infra container.
// Since the Pid is used exclusively to find the network stats,
// and pods share their network (whether it's host or pod level)
// we can return the pid of a running container in the pod.
for _, c := range sb.Containers().List() {
ctrPid, err := c.Pid()
if ctrPid > 0 && err == nil {
pidToReturn = ctrPid
break
}
}
}
image := ""
if imageName := ctr.ImageName(); imageName != nil {
image = imageName.StringForOutOfProcessConsumptionOnly()
}
imageRef := ctr.CRIContainer().ImageRef
return types.ContainerInfo{
Name: ctr.Name(),
Pid: pidToReturn,
Image: image,
ImageRef: imageRef,
CreatedTime: ctrState.Created.UnixNano(),
Labels: ctr.Labels(),
Annotations: ctr.Annotations(),
CrioAnnotations: ctr.CrioAnnotations(),
Root: ctr.MountPoint(),
LogPath: ctr.LogPath(),
Sandbox: ctr.Sandbox(),
IPs: sb.IPs(),
}, nil
}
const (
InspectConfigEndpoint = "/config"
InspectContainersEndpoint = "/containers"
InspectInfoEndpoint = "/info"
InspectPauseEndpoint = "/pause"
InspectUnpauseEndpoint = "/unpause"
)
// GetExtendInterfaceMux returns the mux used to serve extend interface requests
func (s *Server) GetExtendInterfaceMux(enableProfile bool) *chi.Mux {
mux := chi.NewMux()
mux.Get(InspectConfigEndpoint, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
b, err := s.config.ToBytes()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/toml")
if _, err := w.Write(b); err != nil {
logrus.Errorf("Unable to write response TOML: %v", err)
}
}))
mux.Get(InspectInfoEndpoint, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ci := s.getInfo()
js, err := json.Marshal(ci)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(js); err != nil {
logrus.Errorf("Unable to write response JSON: %v", err)
}
}))
mux.Get(InspectContainersEndpoint+"/{id}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := context.TODO()
containerID := chi.URLParam(req, "id")
ci, err := s.getContainerInfo(ctx, containerID, s.GetContainer, s.getInfraContainer, s.getSandbox)
if err != nil {
switch {
case errors.Is(err, errCtrNotFound):
http.Error(w, "can't find the container with id "+containerID, http.StatusNotFound)
case errors.Is(err, errCtrStateNil):
http.Error(w, "can't find container state for container with id "+containerID, http.StatusInternalServerError)
case errors.Is(err, errSandboxNotFound):
http.Error(w, "can't find the sandbox for container id "+containerID, http.StatusNotFound)
default:
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
js, err := json.Marshal(ci)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(js); err != nil {
logrus.Errorf("Unable to write response JSON: %v", err)
}
}))
mux.Get(InspectPauseEndpoint+"/{id}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
containerID := chi.URLParam(req, "id")
ctx := context.TODO()
ctr := s.GetContainer(ctx, containerID)
if ctr == nil {
http.Error(w, "can't find the container with id "+containerID, http.StatusNotFound)
return
}
ctrStatus := ctr.State().Status
if ctrStatus != oci.ContainerStateRunning && ctrStatus != oci.ContainerStateCreated {
http.Error(w,
fmt.Sprintf("container is not in running or created state, now is %s", ctrStatus),
http.StatusConflict)
return
}
if err := s.Runtime().PauseContainer(s.stream.ctx, ctr); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := s.Runtime().UpdateContainerStatus(s.stream.ctx, ctr); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
if _, err := w.Write([]byte("200 OK")); err != nil {
logrus.Errorf("Unable to write response JSON: %v", err)
}
}))
mux.Get(InspectUnpauseEndpoint+"/{id}", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
containerID := chi.URLParam(req, "id")
ctx := context.TODO()
ctr := s.GetContainer(ctx, containerID)
if ctr == nil {
http.Error(w, "can't find the container with id "+containerID, http.StatusNotFound)
return
}
ctrStatus := ctr.State().Status
if ctrStatus != oci.ContainerStatePaused {
http.Error(w,
fmt.Sprintf("container is not in paused state, now is %s", ctrStatus),
http.StatusConflict)
return
}
if err := s.Runtime().UnpauseContainer(s.stream.ctx, ctr); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := s.Runtime().UpdateContainerStatus(s.stream.ctx, ctr); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
if _, err := w.Write([]byte("200 OK")); err != nil {
logrus.Errorf("Unable to write response JSON: %v", err)
}
}))
// Add pprof handlers
if enableProfile {
mux.Get("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Get("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Get("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Get("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
mux.Get("/debug/pprof/*", http.HandlerFunc(pprof.Index))
}
return mux
}