forked from containerd/containerd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic bundle, spec, and config types
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
- Loading branch information
1 parent
0808a5c
commit dd39b4d
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package bundle | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/opencontainers/runtime-spec/specs-go" | ||
) | ||
|
||
const configName = "config.json" | ||
|
||
func New(path string, s *specs.Spec) (*Bundle, error) { | ||
if err := os.Mkdir(path, 0700); err != nil { | ||
return nil, err | ||
} | ||
b := &Bundle{ | ||
Path: path, | ||
} | ||
if err := os.Mkdir(filepath.Join(path, "rootfs"), 0700); err != nil { | ||
b.Delete() | ||
return nil, err | ||
} | ||
f, err := os.Create(filepath.Join(path, configName)) | ||
if err != nil { | ||
b.Delete() | ||
return nil, err | ||
} | ||
err = json.NewEncoder(f).Encode(s) | ||
f.Close() | ||
if err != nil { | ||
b.Delete() | ||
return nil, err | ||
} | ||
return b, nil | ||
} | ||
|
||
func Load(path string) (*Bundle, error) { | ||
// TODO: do validation | ||
return &Bundle{ | ||
Path: path, | ||
}, nil | ||
} | ||
|
||
type Bundle struct { | ||
Path string | ||
} | ||
|
||
func (b *Bundle) Config() (*specs.Spec, error) { | ||
var s specs.Spec | ||
f, err := os.Open(filepath.Join(b.Path, configName)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = json.NewDecoder(f).Decode(&s) | ||
f.Close() | ||
return &s, err | ||
} | ||
|
||
func (b *Bundle) Delete() error { | ||
return os.RemoveAll(b.Path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package containerd | ||
|
||
type Process struct { | ||
Args []string | ||
Env []string | ||
Cwd string | ||
Uid int | ||
Gid int | ||
TTY bool | ||
} | ||
|
||
type Config struct { | ||
Process Process | ||
Hostname string | ||
Domain string | ||
Labels map[string]string | ||
StopSignal int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package specification | ||
|
||
import ( | ||
"runtime" | ||
|
||
"github.com/docker/containerd" | ||
"github.com/opencontainers/runtime-spec/specs-go" | ||
) | ||
|
||
var rwm = "rwm" | ||
|
||
func Default(config containerd.Config, mounts []containerd.Mount) *specs.Spec { | ||
s := &specs.Spec{ | ||
Version: specs.Version, | ||
Platform: specs.Platform{ | ||
OS: runtime.GOOS, | ||
Arch: runtime.GOARCH, | ||
}, | ||
Root: specs.Root{ | ||
Path: "rootfs", | ||
Readonly: false, | ||
}, | ||
Process: specs.Process{ | ||
Args: config.Process.Args, | ||
Env: config.Process.Env, | ||
Terminal: config.Process.TTY, | ||
Cwd: config.Process.Cwd, | ||
NoNewPrivileges: true, | ||
}, | ||
Hostname: config.Hostname, | ||
Linux: &specs.Linux{ | ||
Resources: &specs.LinuxResources{ | ||
Devices: []specs.LinuxDeviceCgroup{ | ||
{ | ||
Allow: false, | ||
Access: &rwm, | ||
}, | ||
}, | ||
}, | ||
Namespaces: []specs.LinuxNamespace{ | ||
{ | ||
Type: "pid", | ||
}, | ||
{ | ||
Type: "ipc", | ||
}, | ||
{ | ||
Type: "uts", | ||
}, | ||
{ | ||
Type: "mount", | ||
}, | ||
{ | ||
Type: "network", | ||
}, | ||
}, | ||
}, | ||
Annotations: config.Labels, | ||
} | ||
// apply snapshot mounts | ||
for _, m := range mounts { | ||
s.Mounts = append(s.Mounts, specs.Mount{ | ||
Source: m.Source, | ||
Destination: "/", | ||
Type: m.Type, | ||
Options: m.Options, | ||
}) | ||
} | ||
return s | ||
} |