virtcontainers: rename GetOCISpec to GetPatchedOCISpec

GetOCISpec returns a patched version of the original OCI spec, it was modified
to support:
* capabilities
* Ephemeral storage
* k8s empty dir

In order to avoid consusions and make api clear, rename GetOCISpec
to GetPatchedOCISpec and ContainerConfig.Spec to ContainerConfig.CustomSpec

fixes #2252

Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
Julio Montes 2019-11-22 14:56:18 +00:00
parent dd15db3250
commit 613fd0fb60
10 changed files with 43 additions and 41 deletions

View File

@ -620,7 +620,7 @@ func statusContainer(sandbox *Sandbox, containerID string) (ContainerStatus, err
PID: container.process.Pid, PID: container.process.Pid,
StartTime: container.process.StartTime, StartTime: container.process.StartTime,
RootFs: container.config.RootFs.Target, RootFs: container.config.RootFs.Target,
Spec: container.GetOCISpec(), Spec: container.GetPatchedOCISpec(),
Annotations: container.config.Annotations, Annotations: container.config.Annotations,
}, nil }, nil
} }

View File

@ -81,7 +81,7 @@ func newTestSandboxConfigNoop() SandboxConfig {
RootFs: RootFs{Target: bundlePath, Mounted: true}, RootFs: RootFs{Target: bundlePath, Mounted: true},
Cmd: newBasicTestCmd(), Cmd: newBasicTestCmd(),
Annotations: containerAnnotations, Annotations: containerAnnotations,
Spec: emptySpec, CustomSpec: emptySpec,
} }
// Sets the hypervisor configuration. // Sets the hypervisor configuration.
@ -717,7 +717,7 @@ func newTestContainerConfigNoop(contID string) ContainerConfig {
RootFs: RootFs{Target: filepath.Join(testDir, testBundle), Mounted: true}, RootFs: RootFs{Target: filepath.Join(testDir, testBundle), Mounted: true},
Cmd: newBasicTestCmd(), Cmd: newBasicTestCmd(),
Annotations: containerAnnotations, Annotations: containerAnnotations,
Spec: newEmptySpec(), CustomSpec: newEmptySpec(),
} }
return container return container

View File

@ -175,7 +175,7 @@ func TestUpdateCgroups(t *testing.T) {
}, },
config: &ContainerConfig{ config: &ContainerConfig{
Annotations: containerAnnotations, Annotations: containerAnnotations,
Spec: newEmptySpec(), CustomSpec: newEmptySpec(),
}, },
}, },
"xyz": { "xyz": {
@ -184,7 +184,7 @@ func TestUpdateCgroups(t *testing.T) {
}, },
config: &ContainerConfig{ config: &ContainerConfig{
Annotations: containerAnnotations, Annotations: containerAnnotations,
Spec: newEmptySpec(), CustomSpec: newEmptySpec(),
}, },
}, },
} }

View File

@ -252,7 +252,7 @@ type ContainerConfig struct {
Resources specs.LinuxResources Resources specs.LinuxResources
// Raw OCI specification, it won't be saved to disk. // Raw OCI specification, it won't be saved to disk.
Spec *specs.Spec `json:"-"` CustomSpec *specs.Spec `json:"-"`
} }
// valid checks that the container configuration is valid. // valid checks that the container configuration is valid.
@ -406,9 +406,17 @@ func (c *Container) GetAnnotations() map[string]string {
return c.config.Annotations return c.config.Annotations
} }
// GetOCISpec returns container's OCI specification // GetPatchedOCISpec returns container's OCI specification
func (c *Container) GetOCISpec() *specs.Spec { // This OCI specification was patched when the sandbox was created
return c.config.Spec // by containerCapabilities(), SetEphemeralStorageType() and others
// in order to support:
// * capabilities
// * Ephemeral storage
// * k8s empty dir
// If you need the original (vanilla) OCI spec,
// use compatoci.GetContainerSpec() instead.
func (c *Container) GetPatchedOCISpec() *specs.Spec {
return c.config.CustomSpec
} }
// storeContainer stores a container config. // storeContainer stores a container config.
@ -1469,7 +1477,7 @@ func (c *Container) detachDevices() error {
// cgroupsCreate creates cgroups on the host for the associated container // cgroupsCreate creates cgroups on the host for the associated container
func (c *Container) cgroupsCreate() (err error) { func (c *Container) cgroupsCreate() (err error) {
spec := c.GetOCISpec() spec := c.GetPatchedOCISpec()
if spec == nil { if spec == nil {
return errorMissingOCISpec return errorMissingOCISpec
} }

View File

@ -746,7 +746,7 @@ func (k *kataAgent) setProxyFromGrpc(proxy proxy, pid int, url string) {
} }
func (k *kataAgent) getDNS(sandbox *Sandbox) ([]string, error) { func (k *kataAgent) getDNS(sandbox *Sandbox) ([]string, error) {
ociSpec := sandbox.GetOCISpec() ociSpec := sandbox.GetPatchedOCISpec()
if ociSpec == nil { if ociSpec == nil {
k.Logger().Debug("Sandbox OCI spec not found. Sandbox DNS will not be set.") k.Logger().Debug("Sandbox OCI spec not found. Sandbox DNS will not be set.")
return nil, nil return nil, nil
@ -1283,7 +1283,7 @@ func (k *kataAgent) createContainer(sandbox *Sandbox, c *Container) (p *Process,
ctrStorages = append(ctrStorages, rootfs) ctrStorages = append(ctrStorages, rootfs)
} }
ociSpec := c.GetOCISpec() ociSpec := c.GetPatchedOCISpec()
if ociSpec == nil { if ociSpec == nil {
return nil, errorMissingOCISpec return nil, errorMissingOCISpec
} }

View File

@ -728,7 +728,7 @@ func TestAgentCreateContainer(t *testing.T) {
Fstype: "xfs", Fstype: "xfs",
}, },
config: &ContainerConfig{ config: &ContainerConfig{
Spec: &specs.Spec{}, CustomSpec: &specs.Spec{},
Annotations: map[string]string{}, Annotations: map[string]string{},
}, },
} }

View File

@ -861,7 +861,10 @@ func ContainerConfig(ocispec specs.Spec, bundlePath, cid, console string, detach
Mounts: containerMounts(ocispec), Mounts: containerMounts(ocispec),
DeviceInfos: deviceInfos, DeviceInfos: deviceInfos,
Resources: *ocispec.Linux.Resources, Resources: *ocispec.Linux.Resources,
Spec: &ocispec,
// This is a custom OCI spec modified at SetEphemeralStorageType()
// to support ephemeral storage and k8s empty dir.
CustomSpec: &ocispec,
} }
cType, err := ContainerType(ocispec) cType, err := ContainerType(ocispec)

View File

@ -156,7 +156,7 @@ func TestMinimalSandboxConfig(t *testing.T) {
Resources: specs.LinuxResources{Devices: []specs.LinuxDeviceCgroup{ Resources: specs.LinuxResources{Devices: []specs.LinuxDeviceCgroup{
{Allow: false, Type: "", Major: (*int64)(nil), Minor: (*int64)(nil), Access: "rwm"}, {Allow: false, Type: "", Major: (*int64)(nil), Minor: (*int64)(nil), Access: "rwm"},
}}, }},
Spec: &spec, CustomSpec: &spec,
} }
expectedNetworkConfig := vc.NetworkConfig{} expectedNetworkConfig := vc.NetworkConfig{}

View File

@ -1130,7 +1130,7 @@ func (s *Sandbox) fetchContainers() error {
if err != nil { if err != nil {
return err return err
} }
contConfig.Spec = &spec contConfig.CustomSpec = &spec
s.config.Containers[i] = contConfig s.config.Containers[i] = contConfig
c, err := newContainer(s, &s.config.Containers[i]) c, err := newContainer(s, &s.config.Containers[i])
@ -2213,7 +2213,7 @@ func (s *Sandbox) cpuResources() *specs.LinuxCPU {
// setupSandboxCgroup creates and joins sandbox cgroups for the sandbox config // setupSandboxCgroup creates and joins sandbox cgroups for the sandbox config
func (s *Sandbox) setupSandboxCgroup() error { func (s *Sandbox) setupSandboxCgroup() error {
spec := s.GetOCISpec() spec := s.GetPatchedOCISpec()
if spec == nil { if spec == nil {
return errorMissingOCISpec return errorMissingOCISpec
@ -2242,9 +2242,16 @@ func (s *Sandbox) setupSandboxCgroup() error {
return nil return nil
} }
func (s *Sandbox) sandboxContConf() *ContainerConfig { // GetPatchedOCISpec returns sandbox's OCI specification
var podSandboxConfig *ContainerConfig // This OCI specification was patched when the sandbox was created
// by containerCapabilities(), SetEphemeralStorageType() and others
// in order to support:
// * capabilities
// * Ephemeral storage
// * k8s empty dir
// If you need the original (vanilla) OCI spec,
// use compatoci.GetContainerSpec() instead.
func (s *Sandbox) GetPatchedOCISpec() *specs.Spec {
if s.config == nil { if s.config == nil {
return nil return nil
} }
@ -2254,25 +2261,9 @@ func (s *Sandbox) sandboxContConf() *ContainerConfig {
// cgroup path from this container. // cgroup path from this container.
for _, cConfig := range s.config.Containers { for _, cConfig := range s.config.Containers {
if cConfig.Annotations[annotations.ContainerTypeKey] == string(PodSandbox) { if cConfig.Annotations[annotations.ContainerTypeKey] == string(PodSandbox) {
podSandboxConfig = &cConfig return cConfig.CustomSpec
break
} }
} }
if podSandboxConfig == nil {
return nil return nil
} }
return podSandboxConfig
}
// GetOCISpec returns sandbox's OCI specification
func (s *Sandbox) GetOCISpec() *specs.Spec {
conf := s.sandboxContConf()
if conf == nil {
return nil
}
// First container is sandbox container as default
return conf.Spec
}

View File

@ -649,7 +649,7 @@ func TestContainerStateSetFstype(t *testing.T) {
{ {
ID: "100", ID: "100",
Annotations: containerAnnotations, Annotations: containerAnnotations,
Spec: newEmptySpec(), CustomSpec: newEmptySpec(),
}, },
} }
@ -1524,7 +1524,7 @@ func TestSandbox_SetupSandboxCgroup(t *testing.T) {
sandboxContainer.Annotations[annotations.ContainerTypeKey] = string(PodSandbox) sandboxContainer.Annotations[annotations.ContainerTypeKey] = string(PodSandbox)
emptyJSONLinux := ContainerConfig{ emptyJSONLinux := ContainerConfig{
Spec: newEmptySpec(), CustomSpec: newEmptySpec(),
} }
emptyJSONLinux.Annotations = make(map[string]string) emptyJSONLinux.Annotations = make(map[string]string)
emptyJSONLinux.Annotations[annotations.ContainerTypeKey] = string(PodSandbox) emptyJSONLinux.Annotations[annotations.ContainerTypeKey] = string(PodSandbox)
@ -1532,7 +1532,7 @@ func TestSandbox_SetupSandboxCgroup(t *testing.T) {
cloneSpec1 := newEmptySpec() cloneSpec1 := newEmptySpec()
cloneSpec1.Linux.CgroupsPath = "/myRuntime/myContainer" cloneSpec1.Linux.CgroupsPath = "/myRuntime/myContainer"
successfulContainer := ContainerConfig{ successfulContainer := ContainerConfig{
Spec: cloneSpec1, CustomSpec: cloneSpec1,
} }
successfulContainer.Annotations = make(map[string]string) successfulContainer.Annotations = make(map[string]string)
successfulContainer.Annotations[annotations.ContainerTypeKey] = string(PodSandbox) successfulContainer.Annotations[annotations.ContainerTypeKey] = string(PodSandbox)