aboutsummaryrefslogtreecommitdiffhomepage
path: root/hst/fsephemeral.go
blob: 9da79003914149f0f970303a3a1bb5f9374eacf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package hst

import (
	"encoding/gob"
	"os"
	"strings"

	"hakurei.app/check"
)

func init() { gob.Register(new(FSEphemeral)) }

// FilesystemEphemeral is the type string of a mount point with ephemeral state.
const FilesystemEphemeral = "ephemeral"

// FSEphemeral represents an ephemeral container mount point.
type FSEphemeral struct {
	// Pathname in the container mount namespace.
	Target *check.Absolute `json:"dst"`
	// Do not mount filesystem read-only.
	Write bool `json:"write,omitempty"`
	// Upper limit on the size of the filesystem.
	Size int `json:"size,omitempty"`
	// Initial permission bits of the new filesystem.
	Perm os.FileMode `json:"perm,omitempty"`
}

func (e *FSEphemeral) Valid() bool { return e != nil && e.Target != nil }

func (e *FSEphemeral) Path() *check.Absolute {
	if !e.Valid() {
		return nil
	}
	return e.Target
}

func (e *FSEphemeral) Host() []*check.Absolute { return nil }

const fsEphemeralDefaultPerm = os.FileMode(0755)

func (e *FSEphemeral) Apply(z *ApplyState) {
	if !e.Valid() {
		return
	}

	size := e.Size
	if size < 0 {
		size = 0
	}

	perm := e.Perm
	if perm == 0 {
		perm = fsEphemeralDefaultPerm
	}

	if e.Write {
		z.Tmpfs(e.Target, size, perm)
	} else {
		z.Readonly(e.Target, perm)
	}
}

func (e *FSEphemeral) String() string {
	if !e.Valid() {
		return "<invalid>"
	}

	expr := new(strings.Builder)
	expr.Grow(15 + len(FilesystemEphemeral) + len(e.Target.String()))

	if e.Write {
		expr.WriteString("w")
	}
	expr.WriteString("+" + FilesystemEphemeral + "(")
	if e.Perm != 0 {
		expr.WriteString(e.Perm.String())
	} else {
		expr.WriteString(fsEphemeralDefaultPerm.String())
	}
	expr.WriteString("):" + e.Target.String())

	return expr.String()
}