Skip to content

Commit

Permalink
virtcontainers: rename GetOCISpec to GetPatchedOCISpec
Browse files Browse the repository at this point in the history
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 kata-containers#2252

Signed-off-by: Julio Montes <julio.montes@intel.com>
  • Loading branch information
Julio Montes committed Nov 25, 2019
1 parent dd15db3 commit 613fd0f
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion virtcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func statusContainer(sandbox *Sandbox, containerID string) (ContainerStatus, err
PID: container.process.Pid,
StartTime: container.process.StartTime,
RootFs: container.config.RootFs.Target,
Spec: container.GetOCISpec(),
Spec: container.GetPatchedOCISpec(),
Annotations: container.config.Annotations,
}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func newTestSandboxConfigNoop() SandboxConfig {
RootFs: RootFs{Target: bundlePath, Mounted: true},
Cmd: newBasicTestCmd(),
Annotations: containerAnnotations,
Spec: emptySpec,
CustomSpec: emptySpec,
}

// Sets the hypervisor configuration.
Expand Down Expand Up @@ -717,7 +717,7 @@ func newTestContainerConfigNoop(contID string) ContainerConfig {
RootFs: RootFs{Target: filepath.Join(testDir, testBundle), Mounted: true},
Cmd: newBasicTestCmd(),
Annotations: containerAnnotations,
Spec: newEmptySpec(),
CustomSpec: newEmptySpec(),
}

return container
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/cgroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestUpdateCgroups(t *testing.T) {
},
config: &ContainerConfig{
Annotations: containerAnnotations,
Spec: newEmptySpec(),
CustomSpec: newEmptySpec(),
},
},
"xyz": {
Expand All @@ -184,7 +184,7 @@ func TestUpdateCgroups(t *testing.T) {
},
config: &ContainerConfig{
Annotations: containerAnnotations,
Spec: newEmptySpec(),
CustomSpec: newEmptySpec(),
},
},
}
Expand Down
18 changes: 13 additions & 5 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ type ContainerConfig struct {
Resources specs.LinuxResources

// 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.
Expand Down Expand Up @@ -406,9 +406,17 @@ func (c *Container) GetAnnotations() map[string]string {
return c.config.Annotations
}

// GetOCISpec returns container's OCI specification
func (c *Container) GetOCISpec() *specs.Spec {
return c.config.Spec
// GetPatchedOCISpec returns container's OCI specification
// 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 (c *Container) GetPatchedOCISpec() *specs.Spec {
return c.config.CustomSpec
}

// storeContainer stores a container config.
Expand Down Expand Up @@ -1469,7 +1477,7 @@ func (c *Container) detachDevices() error {

// cgroupsCreate creates cgroups on the host for the associated container
func (c *Container) cgroupsCreate() (err error) {
spec := c.GetOCISpec()
spec := c.GetPatchedOCISpec()
if spec == nil {
return errorMissingOCISpec
}
Expand Down
4 changes: 2 additions & 2 deletions virtcontainers/kata_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ func (k *kataAgent) setProxyFromGrpc(proxy proxy, pid int, url string) {
}

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

ociSpec := c.GetOCISpec()
ociSpec := c.GetPatchedOCISpec()
if ociSpec == nil {
return nil, errorMissingOCISpec
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/kata_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ func TestAgentCreateContainer(t *testing.T) {
Fstype: "xfs",
},
config: &ContainerConfig{
Spec: &specs.Spec{},
CustomSpec: &specs.Spec{},
Annotations: map[string]string{},
},
}
Expand Down
5 changes: 4 additions & 1 deletion virtcontainers/pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,10 @@ func ContainerConfig(ocispec specs.Spec, bundlePath, cid, console string, detach
Mounts: containerMounts(ocispec),
DeviceInfos: deviceInfos,
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)
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/pkg/oci/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestMinimalSandboxConfig(t *testing.T) {
Resources: specs.LinuxResources{Devices: []specs.LinuxDeviceCgroup{
{Allow: false, Type: "", Major: (*int64)(nil), Minor: (*int64)(nil), Access: "rwm"},
}},
Spec: &spec,
CustomSpec: &spec,
}

expectedNetworkConfig := vc.NetworkConfig{}
Expand Down
37 changes: 14 additions & 23 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ func (s *Sandbox) fetchContainers() error {
if err != nil {
return err
}
contConfig.Spec = &spec
contConfig.CustomSpec = &spec
s.config.Containers[i] = contConfig

c, err := newContainer(s, &s.config.Containers[i])
Expand Down Expand Up @@ -2213,7 +2213,7 @@ func (s *Sandbox) cpuResources() *specs.LinuxCPU {

// setupSandboxCgroup creates and joins sandbox cgroups for the sandbox config
func (s *Sandbox) setupSandboxCgroup() error {
spec := s.GetOCISpec()
spec := s.GetPatchedOCISpec()

if spec == nil {
return errorMissingOCISpec
Expand Down Expand Up @@ -2242,9 +2242,16 @@ func (s *Sandbox) setupSandboxCgroup() error {
return nil
}

func (s *Sandbox) sandboxContConf() *ContainerConfig {
var podSandboxConfig *ContainerConfig

// GetPatchedOCISpec returns sandbox's OCI specification
// 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 {
return nil
}
Expand All @@ -2254,25 +2261,9 @@ func (s *Sandbox) sandboxContConf() *ContainerConfig {
// cgroup path from this container.
for _, cConfig := range s.config.Containers {
if cConfig.Annotations[annotations.ContainerTypeKey] == string(PodSandbox) {
podSandboxConfig = &cConfig
break
return cConfig.CustomSpec
}
}

if podSandboxConfig == 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
return nil
}
6 changes: 3 additions & 3 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ func TestContainerStateSetFstype(t *testing.T) {
{
ID: "100",
Annotations: containerAnnotations,
Spec: newEmptySpec(),
CustomSpec: newEmptySpec(),
},
}

Expand Down Expand Up @@ -1524,15 +1524,15 @@ func TestSandbox_SetupSandboxCgroup(t *testing.T) {
sandboxContainer.Annotations[annotations.ContainerTypeKey] = string(PodSandbox)

emptyJSONLinux := ContainerConfig{
Spec: newEmptySpec(),
CustomSpec: newEmptySpec(),
}
emptyJSONLinux.Annotations = make(map[string]string)
emptyJSONLinux.Annotations[annotations.ContainerTypeKey] = string(PodSandbox)

cloneSpec1 := newEmptySpec()
cloneSpec1.Linux.CgroupsPath = "/myRuntime/myContainer"
successfulContainer := ContainerConfig{
Spec: cloneSpec1,
CustomSpec: cloneSpec1,
}
successfulContainer.Annotations = make(map[string]string)
successfulContainer.Annotations[annotations.ContainerTypeKey] = string(PodSandbox)
Expand Down

0 comments on commit 613fd0f

Please sign in to comment.