summaryrefslogtreecommitdiff
path: root/libkpod/container_data.go
blob: 2ade63ba09a398452d2ac9264df41c3fb77f7f0f (plain)
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
package libkpod

import (
	"encoding/json"
	"os"
	"time"

	"k8s.io/apimachinery/pkg/fields"
	pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"

	"github.com/kubernetes-incubator/cri-o/libpod/driver"
	"github.com/kubernetes-incubator/cri-o/libpod/images"
	"github.com/kubernetes-incubator/cri-o/oci"
	"github.com/opencontainers/image-spec/specs-go/v1"
	specs "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/pkg/errors"
)

// ContainerData handles the data used when inspecting a container
type ContainerData struct {
	ID               string
	Name             string
	LogPath          string
	Labels           fields.Set
	Annotations      fields.Set
	State            *ContainerState
	Metadata         *pb.ContainerMetadata
	BundlePath       string
	StopSignal       string
	FromImage        string `json:"Image,omitempty"`
	FromImageID      string `json:"ImageID"`
	MountPoint       string `json:"Mountpoint,omitempty"`
	MountLabel       string
	Mounts           []specs.Mount
	AppArmorProfile  string
	ImageAnnotations map[string]string `json:"Annotations,omitempty"`
	ImageCreatedBy   string            `json:"CreatedBy,omitempty"`
	Config           v1.ImageConfig    `json:"Config,omitempty"`
	SizeRw           uint              `json:"SizeRw,omitempty"`
	SizeRootFs       uint              `json:"SizeRootFs,omitempty"`
	Args             []string
	ResolvConfPath   string
	HostnamePath     string
	HostsPath        string
	GraphDriver      driverData
}

type driverData struct {
	Name string
	Data map[string]string
}

// ContainerState represents the status of a container.
type ContainerState struct {
	specs.State
	Created   time.Time `json:"created"`
	Started   time.Time `json:"started,omitempty"`
	Finished  time.Time `json:"finished,omitempty"`
	ExitCode  int32     `json:"exitCode"`
	OOMKilled bool      `json:"oomKilled,omitempty"`
	Error     string    `json:"error,omitempty"`
}

// GetContainerData gets the ContainerData for a container with the given name in the given store.
// If size is set to true, it will also determine the size of the container
func (c *ContainerServer) GetContainerData(name string, size bool) (*ContainerData, error) {
	ctr, err := c.inspectContainer(name)
	if err != nil {
		return nil, errors.Wrapf(err, "error reading build container %q", name)
	}
	container, err := c.store.Container(name)
	if err != nil {
		return nil, errors.Wrapf(err, "error reading container data")
	}

	// The runtime configuration won't exist if the container has never been started by cri-o or kpod,
	// so treat a not-exist error as non-fatal.
	m := getBlankSpec()
	config, err := c.store.FromContainerDirectory(ctr.ID(), "config.json")
	if err != nil && !os.IsNotExist(errors.Cause(err)) {
		return nil, err
	}
	if len(config) > 0 {
		if err = json.Unmarshal(config, &m); err != nil {
			return nil, err
		}
	}

	if container.ImageID == "" {
		return nil, errors.Errorf("error reading container image data: container is not based on an image")
	}
	imageData, err := images.GetData(c.store, container.ImageID)
	if err != nil {
		return nil, errors.Wrapf(err, "error reading container image data")
	}

	driverName, err := driver.GetDriverName(c.store)
	if err != nil {
		return nil, err
	}
	topLayer, err := c.GetContainerTopLayerID(ctr.ID())
	if err != nil {
		return nil, err
	}
	layer, err := c.store.Layer(topLayer)
	if err != nil {
		return nil, err
	}
	driverMetadata, err := driver.GetDriverMetadata(c.store, topLayer)
	if err != nil {
		return nil, err
	}
	imageName := ""
	if len(imageData.Tags) > 0 {
		imageName = imageData.Tags[0]
	} else if len(imageData.Digests) > 0 {
		imageName = imageData.Digests[0]
	}
	data := &ContainerData{
		ID:               ctr.ID(),
		Name:             ctr.Name(),
		LogPath:          ctr.LogPath(),
		Labels:           ctr.Labels(),
		Annotations:      ctr.Annotations(),
		State:            c.State(ctr),
		Metadata:         ctr.Metadata(),
		BundlePath:       ctr.BundlePath(),
		StopSignal:       ctr.GetStopSignal(),
		Args:             m.Process.Args,
		FromImage:        imageName,
		FromImageID:      container.ImageID,
		MountPoint:       layer.MountPoint,
		ImageAnnotations: imageData.Annotations,
		ImageCreatedBy:   imageData.CreatedBy,
		Config:           imageData.Config,
		GraphDriver: driverData{
			Name: driverName,
			Data: driverMetadata,
		},
		MountLabel:      m.Linux.MountLabel,
		Mounts:          m.Mounts,
		AppArmorProfile: m.Process.ApparmorProfile,
		ResolvConfPath:  "",
		HostnamePath:    "",
		HostsPath:       "",
	}

	if size {
		sizeRootFs, err := c.GetContainerRootFsSize(data.ID)
		if err != nil {

			return nil, errors.Wrapf(err, "error reading size for container %q", name)
		}
		data.SizeRootFs = uint(sizeRootFs)
		sizeRw, err := c.GetContainerRwSize(data.ID)
		if err != nil {
			return nil, errors.Wrapf(err, "error reading RWSize for container %q", name)
		}
		data.SizeRw = uint(sizeRw)
	}

	return data, nil
}

// Get an oci.Container and update its status
func (c *ContainerServer) inspectContainer(container string) (*oci.Container, error) {
	ociCtr, err := c.LookupContainer(container)
	if err != nil {
		return nil, err
	}
	// call runtime.UpdateStatus()
	err = c.Runtime().UpdateStatus(ociCtr)
	if err != nil {
		return nil, err
	}
	return ociCtr, nil
}

func getBlankSpec() specs.Spec {
	return specs.Spec{
		Process:     &specs.Process{},
		Root:        &specs.Root{},
		Mounts:      []specs.Mount{},
		Hooks:       &specs.Hooks{},
		Annotations: make(map[string]string),
		Linux:       &specs.Linux{},
		Solaris:     &specs.Solaris{},
		Windows:     &specs.Windows{},
	}
}

// State copies the crio container state to ContainerState type for kpod
func (c *ContainerServer) State(ctr *oci.Container) *ContainerState {
	crioState := ctr.State()
	specState := specs.State{
		Version:     crioState.Version,
		ID:          crioState.ID,
		Status:      crioState.Status,
		Pid:         crioState.Pid,
		Bundle:      crioState.Bundle,
		Annotations: crioState.Annotations,
	}
	cState := &ContainerState{
		Started:  crioState.Started,
		Created:  crioState.Created,
		Finished: crioState.Finished,
	}
	cState.State = specState
	return cState
}