-
Notifications
You must be signed in to change notification settings - Fork 208
/
docker.go
82 lines (67 loc) · 2.63 KB
/
docker.go
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
package cnab
import (
"encoding/json"
"errors"
"fmt"
)
const (
// DockerExtensionShortHand is the short suffix of the DockerExtensionKey.
DockerExtensionShortHand = "docker"
// DockerExtensionKey represents the full key for the Docker Extension
DockerExtensionKey = OfficialExtensionsPrefix + DockerExtensionShortHand
// DockerExtensionSchema represents the schema for the Docker Extension
DockerExtensionSchema = "schema/io-cnab-docker.schema.json"
)
// DockerExtension represents a required extension enabling access to the host Docker daemon
var DockerExtension = RequiredExtension{
Shorthand: DockerExtensionShortHand,
Key: DockerExtensionKey,
Schema: "schema/io-cnab-docker.schema.json",
Reader: DockerExtensionReader,
}
// Docker describes the set of custom extension metadata associated with the Docker extension
type Docker struct {
// Privileged represents whether or not the Docker container should run as --privileged
Privileged bool `json:"privileged,omitempty"`
}
// DockerExtensionReader is a Reader for the DockerExtension,
// which reads from the applicable section in the provided bundle and
// returns the raw data in the form of an interface
func DockerExtensionReader(bun ExtendedBundle) (interface{}, error) {
return bun.DockerExtensionReader()
}
// DockerExtensionReader is a Reader for the DockerExtension,
// which reads from the applicable section in the provided bundle and
// returns the raw data in the form of an interface
func (b ExtendedBundle) DockerExtensionReader() (interface{}, error) {
data, ok := b.Custom[DockerExtensionKey]
if !ok {
return nil, errors.New("no custom extension configuration found")
}
dataB, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("could not marshal the untyped %q extension data %q: %w",
DockerExtensionKey, string(dataB), err)
}
dha := Docker{}
err = json.Unmarshal(dataB, &dha)
if err != nil {
return nil, fmt.Errorf("could not unmarshal the %q extension %q: %w",
DockerExtensionKey, string(dataB), err)
}
return dha, nil
}
// GetDocker checks if the docker extension is present and returns its
// extension configuration.
func (e ProcessedExtensions) GetDocker() (dockerExt Docker, dockerRequired bool, err error) {
ext, extensionRequired := e[DockerExtensionKey]
dockerExt, ok := ext.(Docker)
if !ok && extensionRequired {
return Docker{}, extensionRequired, fmt.Errorf("unable to parse Docker extension config: %+v", dockerExt)
}
return dockerExt, extensionRequired, nil
}
// SupportsDocker checks if the bundle supports docker.
func (b ExtendedBundle) SupportsDocker() bool {
return b.SupportsExtension(DockerExtensionKey)
}