oci: introduce WithSpecFromFile combinator
We introduce a WithSpecFromFile option combinator to allow creation simpler creation of OCI specs from a file name. Often used as the first option in a `SpecOpts` slice, it simplifies choosing between a local file and the built-in default. The code in `ctr run` has been updated to use the new option, with out changing the order of operations or functionality present there. Signed-off-by: Stephen Day <stephen.day@getcruise.com>
This commit is contained in:
@@ -18,10 +18,13 @@ package oci
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/containerd/containers"
|
||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// SpecOpts sets spec specific information to a newly generated OCI spec
|
||||
@@ -46,6 +49,38 @@ func setProcess(s *Spec) {
|
||||
}
|
||||
}
|
||||
|
||||
// WithDefaultSpec returns a SpecOpts that will populate the spec with default
|
||||
// values.
|
||||
//
|
||||
// Use as the first option to clear the spec, then apply options afterwards.
|
||||
func WithDefaultSpec() SpecOpts {
|
||||
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
|
||||
return populateDefaultSpec(ctx, s, c.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// WithSpecFromBytes loads the the spec from the provided byte slice.
|
||||
func WithSpecFromBytes(p []byte) SpecOpts {
|
||||
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
|
||||
*s = Spec{} // make sure spec is cleared.
|
||||
if err := json.Unmarshal(p, s); err != nil {
|
||||
return errors.Wrapf(err, "decoding spec config file failed, current supported OCI runtime-spec : v%s", specs.Version)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithSpecFromFile loads the specification from the provided filename.
|
||||
func WithSpecFromFile(filename string) SpecOpts {
|
||||
return func(ctx context.Context, c Client, container *containers.Container, s *Spec) error {
|
||||
p, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot load spec config file")
|
||||
}
|
||||
return WithSpecFromBytes(p)(ctx, c, container, s)
|
||||
}
|
||||
}
|
||||
|
||||
// WithProcessArgs replaces the args on the generated spec
|
||||
func WithProcessArgs(args ...string) SpecOpts {
|
||||
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
|
||||
|
||||
Reference in New Issue
Block a user