Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
agent: write OCI spec to config.json in bundle path
Browse files Browse the repository at this point in the history
In order to comply with the OCI specification, the
spec must be written to a file named 'config.json'
located in the bundle directory.

Fixes: #349

Signed-off-by: Edward Guzman <eguzman@nvidia.com>
  • Loading branch information
eguzman3 committed Aug 30, 2018
1 parent 5765593 commit d92a539
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -524,6 +525,47 @@ func (a *agentGRPC) rollbackFailingContainerCreation(ctr *container) {
}
}

func changeToBundlePath(spec *specs.Spec) (string, error) {
cwd, err := os.Getwd()
if err != nil {
return cwd, err
}

rootfsPath := spec.Root.Path
if !filepath.IsAbs(rootfsPath) {
rootfsPath = filepath.Join(cwd, rootfsPath)
}

bundlePath := filepath.Dir(rootfsPath)

err = os.Chdir(bundlePath)
if err != nil {
return cwd, err
}

return cwd, nil
}

func writeSpecToFile(spec *specs.Spec) error {
cwd, err := os.Getwd()
if err != nil {
return err
}

configPath := path.Join(cwd, "config.json")
f, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}

defer f.Close()
if err = json.NewEncoder(f).Encode(&spec); err != nil {
return err
}

return nil
}

func (a *agentGRPC) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (resp *gpb.Empty, err error) {
if err := a.createContainerChecks(req); err != nil {
return emptyResp, err
Expand Down Expand Up @@ -579,6 +621,12 @@ func (a *agentGRPC) CreateContainer(ctx context.Context, req *pb.CreateContainer
return emptyResp, err
}

// Change cwd because runc sets the bundle path to cwd
oldcwd, err := changeToBundlePath(ociSpec)
if err != nil {
return emptyResp, err
}

// Convert the OCI specification into a libcontainer configuration.
config, err := specconv.CreateLibcontainerConfig(&specconv.CreateOpts{
CgroupName: req.ContainerId,
Expand All @@ -590,6 +638,20 @@ func (a *agentGRPC) CreateContainer(ctx context.Context, req *pb.CreateContainer
return emptyResp, err
}

// In order to comply with the OCI specification, the spec must be
// written to a file named 'config.json' located in the bundle
// directory. https://github.com/opencontainers/runtime-spec
err = writeSpecToFile(ociSpec)
if err != nil {
return nil, err
}

// Change back to root dir
err = os.Chdir(oldcwd)
if err != nil {
return nil, err
}

// Update libcontainer configuration for specific cases not handled
// by the specconv converter.
if err = a.updateContainerConfig(ociSpec, config, ctr); err != nil {
Expand Down

0 comments on commit d92a539

Please sign in to comment.