Skip to content

Commit

Permalink
create protobuf definition for config types, and moved the config par…
Browse files Browse the repository at this point in the history
…sing into a separate package
  • Loading branch information
oclaussen committed Feb 12, 2020
1 parent 3c7e042 commit 33c10cb
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 50 deletions.
6 changes: 3 additions & 3 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func NewContainer(config *types.Backdrop, authConfigs map[string]dockerapi.AuthC
}

func (c *Container) Build() error {
c.config.Image.ForceRebuild = true
c.config.Build.ForceRebuild = true

img, err := image.NewImage(c.client, c.authConfigs, c.config.Image)
img, err := image.NewImage(c.client, c.authConfigs, c.config.Build)
if err != nil {
return err
}
Expand All @@ -68,7 +68,7 @@ func (c *Container) Build() error {
}

func (c *Container) Run() error {
img, err := image.NewImage(c.client, c.authConfigs, c.config.Image)
img, err := image.NewImage(c.client, c.authConfigs, c.config.Build)
if err != nil {
return err
}
Expand Down
77 changes: 58 additions & 19 deletions pkg/container/create.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package container

import (
"fmt"
"path"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
)

func (c *Container) create(image string) (string, error) {
Expand All @@ -19,26 +21,19 @@ func (c *Container) create(image string) (string, error) {
Tty: hasTTY() && !c.daemon,
OpenStdin: !c.daemon,
StdinOnce: !c.daemon,
Env: c.config.Environment.Strings(),
Env: c.dockerEnvironment(),
Cmd: command,
Image: image,
WorkingDir: c.config.WorkingDir,
Entrypoint: entrypoint,
ExposedPorts: c.config.Ports.PortSet(),
ExposedPorts: c.dockerPortSet(),
},
&container.HostConfig{
AutoRemove: func() bool {
if c.daemon {
return false
}
if c.config.Remove == nil {
return true
}
return *c.config.Remove
return !c.daemon
}(),
Binds: c.config.Volumes.Strings(),
VolumesFrom: c.config.VolumesFrom,
PortBindings: c.config.Ports.PortMap(),
Binds: c.dockerVolumes(),
PortBindings: c.dockerPortMap(),
RestartPolicy: c.dockerRestartPolicy(),
Resources: container.Resources{
Devices: c.dockerDevices(),
Expand All @@ -52,8 +47,8 @@ func (c *Container) create(image string) (string, error) {
return "", err
}

if len(c.config.Script) > 0 {
if err := c.UploadFile(response.ID, "entrypoint", []byte(c.config.Script+"\n")); err != nil {
if len(c.config.Entrypoint.Script) > 0 {
if err := c.UploadFile(response.ID, "entrypoint", []byte(c.config.Entrypoint.Script+"\n")); err != nil {
return "", err
}
}
Expand All @@ -63,14 +58,14 @@ func (c *Container) create(image string) (string, error) {

func (c *Container) dockerEntrypoint() ([]string, []string) {
entrypoint := []string{"/bin/sh"}
command := c.config.Command
command := c.config.Entrypoint.Arguments

if c.config.Interpreter != nil {
entrypoint = c.config.Interpreter
if c.config.Entrypoint.Interpreter != nil {
entrypoint = c.config.Entrypoint.Interpreter
}
if c.config.Interactive {
if c.config.Entrypoint.Interactive {
command = nil
} else if len(c.config.Script) > 0 {
} else if len(c.config.Entrypoint.Script) > 0 {
entrypoint = append(entrypoint, path.Join(c.tmpPath, "entrypoint"))
}

Expand Down Expand Up @@ -109,3 +104,47 @@ func (c *Container) dockerDeviceCgroupRules() []string {
}
return result
}

func (c *Container) dockerPortMap() nat.PortMap {
result := map[nat.Port][]nat.PortBinding{}
for _, port := range c.config.Ports {
portSpec, _ := nat.NewPort(port.Protocol, port.Target)
result[portSpec] = append(result[portSpec], nat.PortBinding{HostPort: port.Published})
}
return result
}

func (c *Container) dockerPortSet() nat.PortSet {
result := map[nat.Port]struct{}{}
for _, port := range c.config.Ports {
portSpec, _ := nat.NewPort(port.Protocol, port.Target)
result[portSpec] = struct{}{}
}
return result
}

func (c *Container) dockerEnvironment() []string {
result := []string{}
for _, kv := range c.config.Environment {
result = append(result, fmt.Sprintf("%s=%s", kv.Key, kv.Value))
}
return result
}

func (c *Container) dockerVolumes() []string {
result := []string{}
for _, v := range c.config.Volumes {
var volumeString string

if v.Target == "" && !v.Readonly {
volumeString = fmt.Sprintf("%s:%s", v.Source, v.Source)
} else if !v.Readonly {
volumeString = fmt.Sprintf("%s:%s", v.Source, v.Target)
} else {
volumeString = fmt.Sprintf("%s:%s:ro", v.Source, v.Target)
}

result = append(result, volumeString)
}
return result
}
6 changes: 1 addition & 5 deletions pkg/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ func (c *Container) run(containerID string, tty bool) error {
streamErrorChannel := make(chan error, 1)
go streamContainer(c.context, streamErrorChannel, attach, tty)

condition := container.WaitConditionNextExit
if c.config.Remove == nil || *c.config.Remove == true {
condition = container.WaitConditionRemoved
}
waitChannel, waitErrorChannel := c.client.ContainerWait(
c.context,
containerID,
condition,
container.WaitConditionRemoved,
)

err = c.client.ContainerStart(
Expand Down
20 changes: 7 additions & 13 deletions pkg/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"net"
"os"
"strings"

"github.com/containerd/console"
"github.com/docker/docker/api/types"
Expand All @@ -23,14 +22,14 @@ import (
)

func (image *Image) Get() (string, error) {
if image.config.ForceRebuild || len(image.config.Name) == 0 {
if image.config.ForceRebuild || len(image.config.ImageName) == 0 {
return image.Build()
}

imgs, err := image.client.ImageList(
context.Background(),
types.ImageListOptions{
Filters: filters.NewArgs(filters.Arg("reference", image.config.Name)),
Filters: filters.NewArgs(filters.Arg("reference", image.config.ImageName)),
},
)
if err != nil || len(imgs) == 0 {
Expand All @@ -41,7 +40,7 @@ func (image *Image) Get() (string, error) {
}

func (image *Image) Build() (string, error) {
for _, name := range image.config.Requires {
for _, name := range image.config.Dependencies {
// TODO: refactor here, the dependency on config is uncomfortable
conf, err := config.LoadImage(name)
if err != nil {
Expand Down Expand Up @@ -114,18 +113,13 @@ func (image *Image) Build() (string, error) {

func (image *Image) runBuild(contextData *contextData, displayCh chan *client.SolveStatus) (string, error) {
args := map[string]*string{}
for _, arg := range image.config.Args.Strings() {
switch values := strings.SplitN(arg, "=", 2); len(values) {
case 1:
args[values[0]] = nil
case 2:
args[values[0]] = &values[1]
}
for _, arg := range image.config.Arguments {
args[arg.Key] = &arg.Value
}

var tags []string
if image.config.Name != "" {
tags = append(tags, image.config.Name)
if image.config.ImageName != "" {
tags = append(tags, image.config.ImageName)
}

response, err := image.client.ImageBuild(
Expand Down
47 changes: 39 additions & 8 deletions pkg/image/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
"path/filepath"

"github.com/docker/docker/pkg/urlutil"
buildkit "github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/session/filesync"
"github.com/moby/buildkit/session/secrets/secretsprovider"
"github.com/moby/buildkit/session/sshforward/sshprovider"
"github.com/oclaussen/dodo/pkg/types"
"github.com/pkg/errors"
fstypes "github.com/tonistiigi/fsutil/types"
Expand Down Expand Up @@ -41,7 +44,7 @@ func (data *contextData) cleanup() {
}
}

func prepareContext(config *types.Image, session session) (*contextData, error) {
func prepareContext(config *types.BuildInfo, session session) (*contextData, error) {
data := contextData{
remote: "",
dockerfileName: config.Dockerfile,
Expand Down Expand Up @@ -76,9 +79,9 @@ func prepareContext(config *types.Image, session session) (*contextData, error)
return nil, errors.Errorf("Context directory does not exist: %v", config.Context)
}

if len(config.Steps) > 0 {
if len(config.InlineDockerfile) > 0 {
steps := ""
for _, step := range config.Steps {
for _, step := range config.InlineDockerfile {
steps = steps + step + "\n"
}

Expand Down Expand Up @@ -108,14 +111,14 @@ func prepareContext(config *types.Image, session session) (*contextData, error)
Dir: dockerfileDir,
})

} else if config.Name != "" && data.remote == clientSession {
} else if config.ImageName != "" && data.remote == clientSession {
dir, err := data.tempdir()
if err != nil {
data.cleanup()
return nil, err
}
tempfile := filepath.Join(dir, "Dockerfile")
if err := writeDockerfile(tempfile, fmt.Sprintf("FROM %s", config.Name)); err != nil {
if err := writeDockerfile(tempfile, fmt.Sprintf("FROM %s", config.ImageName)); err != nil {
data.cleanup()
return nil, err
}
Expand All @@ -134,14 +137,14 @@ func prepareContext(config *types.Image, session session) (*contextData, error)

session.Allow(authprovider.NewDockerAuthProvider())
if len(config.Secrets) > 0 {
provider, err := config.Secrets.SecretsProvider()
provider, err := secretsProvider(config)
if err != nil {
return nil, err
}
session.Allow(provider)
}
if len(config.SSHAgents) > 0 {
provider, err := config.SSHAgents.SSHAgentProvider()
if len(config.SshAgents) > 0 {
provider, err := sshAgentProvider(config)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -171,3 +174,31 @@ func writeDockerfile(path string, content string) error {

return nil
}

func secretsProvider(config *types.BuildInfo) (buildkit.Attachable, error) {
sources := make([]secretsprovider.FileSource, 0, len(config.Secrets))
for _, secret := range config.Secrets {
source := secretsprovider.FileSource{
ID: secret.Id,
FilePath: secret.Path,
}
sources = append(sources, source)
}
store, err := secretsprovider.NewFileStore(sources)
if err != nil {
return nil, err
}
return secretsprovider.NewSecretProvider(store), nil
}

func sshAgentProvider(config *types.BuildInfo) (buildkit.Attachable, error) {
configs := make([]sshprovider.AgentConfig, 0, len(config.SshAgents))
for _, agent := range config.SshAgents {
config := sshprovider.AgentConfig{
ID: agent.Id,
Paths: []string{agent.IdentityFile},
}
configs = append(configs, config)
}
return sshprovider.NewSSHAgentProvider(configs)
}
4 changes: 2 additions & 2 deletions pkg/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (

// Image represents the data necessary to build a docker image
type Image struct {
config *dodotypes.Image
config *dodotypes.BuildInfo
client Client
authConfigs map[string]types.AuthConfig
session session
Expand All @@ -31,7 +31,7 @@ type Client interface {
}

// NewImage initializes and validates a new Image object
func NewImage(client Client, authConfigs map[string]types.AuthConfig, config *dodotypes.Image) (*Image, error) {
func NewImage(client Client, authConfigs map[string]types.AuthConfig, config *dodotypes.BuildInfo) (*Image, error) {
if client == nil {
return nil, errors.New("client may not be nil")
}
Expand Down

0 comments on commit 33c10cb

Please sign in to comment.