Skip to content

Commit

Permalink
chore: linting is always fun
Browse files Browse the repository at this point in the history
  • Loading branch information
oclaussen committed Aug 29, 2021
1 parent c359c9f commit 36ff128
Show file tree
Hide file tree
Showing 11 changed files with 665 additions and 72 deletions.
17 changes: 3 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,18 @@ module github.com/dodo-cli/dodo-docker
go 1.16

require (
github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5 // indirect
github.com/containerd/containerd v1.3.4 // indirect
github.com/containerd/containerd v1.5.4 // indirect
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/docker v20.10.2+incompatible
github.com/docker/go-connections v0.4.0
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/dodo-cli/dodo-core v0.1.0
github.com/gogo/protobuf v1.3.1 // indirect
github.com/gorilla/mux v1.7.4 // indirect
github.com/hashicorp/go-hclog v0.15.0
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/oclaussen/go-gimme/configfiles v0.0.0-20200205175519-d9560e60c720
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
google.golang.org/grpc v1.36.0 // indirect
gotest.tools v2.2.0+incompatible // indirect
gotest.tools/v3 v3.0.3 // indirect
)
617 changes: 596 additions & 21 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pkg/client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"

"github.com/docker/docker/api/types"
Expand All @@ -26,6 +27,7 @@ func LoadAuthConfig() map[string]types.AuthConfig {
Filter: func(configFile *configfiles.ConfigFile) bool {
var config map[string]*json.RawMessage
err := json.Unmarshal(configFile.Content, &config)

return err == nil && config["auths"] != nil
},
})
Expand Down Expand Up @@ -65,7 +67,7 @@ func decodeAuth(authStr string) (string, string, error) {

n, err := base64.StdEncoding.Decode(decoded, authByte)
if err != nil {
return "", "", err
return "", "", fmt.Errorf("invalid base64 string: %w", err)
}

if n > decLen {
Expand Down
6 changes: 5 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func GetDockerClient() (*client.Client, error) {
}

if len(certPath) > 0 {
mutators = append(mutators, client.WithTLSClientConfig(filepath.Join(certPath, "ca.pem"), filepath.Join(certPath, "cert.pem"), filepath.Join(certPath, "key.pem")))
mutators = append(mutators, client.WithTLSClientConfig(
filepath.Join(certPath, "ca.pem"),
filepath.Join(certPath, "cert.pem"),
filepath.Join(certPath, "key.pem"),
))
}

return client.NewClientWithOpts(mutators...)
Expand Down
19 changes: 11 additions & 8 deletions pkg/plugin/runtime/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

func (c *ContainerRuntime) CreateContainer(config *api.Backdrop, tty bool, stdio bool) (string, error) {
// TODO: share tmpPath?
tmpPath := fmt.Sprintf("/tmp/dodo-%s/", stringid.GenerateRandomID()[:20])
entrypoint, command := entrypoint(config, tmpPath)

Expand Down Expand Up @@ -56,15 +55,19 @@ func (c *ContainerRuntime) CreateContainer(config *api.Backdrop, tty bool, stdio
},
},
&network.NetworkingConfig{},
nil, //TODO: what goes here?
nil,
config.ContainerName,
)
if err != nil {
return "", err
return "", fmt.Errorf("could not create container: %w", err)
}

if len(config.Entrypoint.Script) > 0 {
if err := c.UploadFile(response.ID, path.Join(tmpPath, "entrypoint"), []byte(config.Entrypoint.Script+"\n")); err != nil {
if err := c.UploadFile(
response.ID,
path.Join(tmpPath, "entrypoint"),
[]byte(config.Entrypoint.Script+"\n"),
); err != nil {
return "", err
}
}
Expand All @@ -91,7 +94,6 @@ func (c *ContainerRuntime) CreateContainer(config *api.Backdrop, tty bool, stdio
return "", err
}
}

}

return response.ID, nil
Expand Down Expand Up @@ -198,11 +200,12 @@ func volumes(config *api.Backdrop) []string {
for _, v := range config.Volumes {
var volumeString string

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

Expand Down
19 changes: 11 additions & 8 deletions pkg/plugin/runtime/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package runtime
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"

"github.com/docker/distribution/reference"
Expand All @@ -19,7 +21,7 @@ func (c *ContainerRuntime) ResolveImage(name string) (string, error) {

ref, err := reference.ParseAnyReference(name)
if err != nil {
return "", err
return "", fmt.Errorf("could not parse image name: %w", err)
}

client, err := c.Client()
Expand All @@ -29,12 +31,13 @@ func (c *ContainerRuntime) ResolveImage(name string) (string, error) {

if _, _, err := client.ImageInspectWithRaw(context.Background(), ref.String()); err == nil {
log.L().Debug("found image locally", "ref", ref.String())

return ref.String(), nil
}

parsed, err := reference.ParseNormalizedNamed(name)
if err != nil {
return "", err
return "", fmt.Errorf("could not parse image name: %w", err)
}

if reference.IsNameOnly(parsed) {
Expand All @@ -43,7 +46,7 @@ func (c *ContainerRuntime) ResolveImage(name string) (string, error) {

repoInfo, err := registry.ParseRepositoryInfo(parsed)
if err != nil {
return "", err
return "", fmt.Errorf("could not parse image name: %w", err)
}

configKey := repoInfo.Index.Name
Expand All @@ -61,7 +64,7 @@ func (c *ContainerRuntime) ResolveImage(name string) (string, error) {

buf, err := json.Marshal(authConfigs[configKey])
if err != nil {
return "", err
return "", fmt.Errorf("could not parse auth config: %w", err)
}

response, err := client.ImagePull(
Expand All @@ -72,12 +75,12 @@ func (c *ContainerRuntime) ResolveImage(name string) (string, error) {
},
)
if err != nil {
return "", err
return "", fmt.Errorf("could not pull image: %w", err)
}
defer response.Close()

if err = streamPull(response); err != nil {
return "", err
return "", fmt.Errorf("error during container stream: %w", err)
}

return parsed.String(), nil
Expand All @@ -89,11 +92,11 @@ func streamPull(result io.Reader) error {
for {
var msg jsonmessage.JSONMessage
if err := decoder.Decode(&msg); err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}

return err
return fmt.Errorf("invalid json: %w", err)
}

if msg.Error != nil {
Expand Down
10 changes: 4 additions & 6 deletions pkg/plugin/runtime/remove.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package runtime

import (
"fmt"

"github.com/docker/docker/api/types"
"golang.org/x/net/context"
)
Expand All @@ -12,12 +14,8 @@ func (c *ContainerRuntime) DeleteContainer(id string) error {
}

if err := client.ContainerStop(context.Background(), id, nil); err != nil {
return err
}

if err := client.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{}); err != nil {
return err
return fmt.Errorf("could not stop container: %w", err)
}

return nil
return client.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{})
}
16 changes: 11 additions & 5 deletions pkg/plugin/runtime/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runtime

import (
"context"
"fmt"
"io"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -31,11 +32,13 @@ func (c *ContainerRuntime) RunAndWaitContainer(id string, height uint32, width u
waitCh, errorCh := client.ContainerWait(context.Background(), id, container.WaitConditionRemoved)

if err := c.StartContainer(id); err != nil {
return err
return fmt.Errorf("could not stop container: %w", err)
}

if height != 0 || width != 0 {
c.ResizeContainer(id, height, width)
if err := c.ResizeContainer(id, height, width); err != nil {
log.L().Error("error during resize", "error", err)
}
}

select {
Expand Down Expand Up @@ -63,7 +66,7 @@ func (c *ContainerRuntime) StreamContainer(id string, stream *plugin.StreamConfi

config, err := client.ContainerInspect(ctx, id)
if err != nil {
return err
return fmt.Errorf("could not inspect container: %w", err)
}

attach, err := client.ContainerAttach(
Expand All @@ -78,7 +81,7 @@ func (c *ContainerRuntime) StreamContainer(id string, stream *plugin.StreamConfi
},
)
if err != nil {
return err
return fmt.Errorf("could not attach to container: %w", err)
}

defer func() {
Expand Down Expand Up @@ -106,7 +109,10 @@ func (c *ContainerRuntime) StreamContainer(id string, stream *plugin.StreamConfi
})

eg.Go(func() error {
inCopier.Copy()
if err := inCopier.Copy(); err != nil {
log.L().Error("could not copy container input", "error", err)
}

return nil
})

Expand Down
8 changes: 5 additions & 3 deletions pkg/plugin/runtime/runtime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package runtime

import (
"fmt"

docker "github.com/docker/docker/client"
api "github.com/dodo-cli/dodo-core/api/v1alpha1"
"github.com/dodo-cli/dodo-core/pkg/plugin"
Expand All @@ -24,15 +26,15 @@ func NewFromClient(client *docker.Client) *ContainerRuntime {
return &ContainerRuntime{client: client}
}

func (c *ContainerRuntime) Type() plugin.Type {
func (*ContainerRuntime) Type() plugin.Type {
return runtime.Type
}

func (c *ContainerRuntime) Client() (*docker.Client, error) {
if c.client == nil {
dockerClient, err := client.GetDockerClient()
if err != nil {
return nil, err
return nil, fmt.Errorf("could not get docker config: %w", err)
}

c.client = dockerClient
Expand All @@ -41,6 +43,6 @@ func (c *ContainerRuntime) Client() (*docker.Client, error) {
return c.client, nil
}

func (p *ContainerRuntime) PluginInfo() (*api.PluginInfo, error) {
func (*ContainerRuntime) PluginInfo() (*api.PluginInfo, error) {
return &api.PluginInfo{Name: name}, nil
}
15 changes: 11 additions & 4 deletions pkg/plugin/runtime/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package runtime
import (
"archive/tar"
"context"
"fmt"
"io"

"github.com/docker/docker/api/types"
"golang.org/x/sync/errgroup"
)

const (
filemode = 0644
)

func (c *ContainerRuntime) UploadFile(containerID string, path string, contents []byte) error {
eg, ctx := errgroup.WithContext(context.Background())
reader, writer := io.Pipe()
Expand Down Expand Up @@ -37,15 +42,17 @@ func (c *ContainerRuntime) UploadFile(containerID string, path string, contents

if err := tarWriter.WriteHeader(&tar.Header{
Name: path,
Mode: 0644,
Mode: filemode,
Size: int64(len(contents)),
}); err != nil {
return err
return fmt.Errorf("could not write tar stream: %w", err)
}

_, err := tarWriter.Write(contents)
if _, err := tarWriter.Write(contents); err != nil {
return fmt.Errorf("could not write tar stream: %w", err)
}

return err
return nil
})

return eg.Wait()
Expand Down
6 changes: 5 additions & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package plugin
import (
"github.com/dodo-cli/dodo-core/pkg/plugin"
"github.com/dodo-cli/dodo-docker/pkg/plugin/runtime"
log "github.com/hashicorp/go-hclog"
)

func RunMe() int {
plugin.ServePlugins(runtime.New())
if err := plugin.ServePlugins(runtime.New()); err != nil {
log.L().Error("error serving plugin", "error", err)
}

return 0
}

Expand Down

0 comments on commit 36ff128

Please sign in to comment.