Skip to content

Commit

Permalink
Moved Mount to tork package
Browse files Browse the repository at this point in the history
  • Loading branch information
runabol committed Oct 1, 2023
1 parent 64c986a commit 2b1047c
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 69 deletions.
3 changes: 1 addition & 2 deletions datastore/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/runabol/tork"
"github.com/runabol/tork/mount"
)

type PostgresDatastore struct {
Expand Down Expand Up @@ -160,7 +159,7 @@ func (r taskRecord) toTask() (*tork.Task, error) {
return nil, errors.Wrapf(err, "error deserializing task.registry")
}
}
var mounts []mount.Mount
var mounts []tork.Mount
if r.Mounts != nil {
if err := json.Unmarshal(r.Mounts, &mounts); err != nil {
return nil, errors.Wrapf(err, "error deserializing task.registry")
Expand Down
9 changes: 4 additions & 5 deletions input/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package input

import (
"github.com/runabol/tork"
"github.com/runabol/tork/mount"
"golang.org/x/exp/maps"
)

Expand Down Expand Up @@ -48,8 +47,8 @@ type AuxTask struct {
Timeout string `json:"timeout,omitempty" yaml:"timeout,omitempty"`
}

func (m Mount) toMount() mount.Mount {
return mount.Mount{
func (m Mount) toMount() tork.Mount {
return tork.Mount{
Type: m.Type,
Source: m.Source,
Target: m.Target,
Expand Down Expand Up @@ -144,8 +143,8 @@ func (i Task) toTask() *tork.Task {
}
}

func toMounts(ms []Mount) []mount.Mount {
result := make([]mount.Mount, len(ms))
func toMounts(ms []Mount) []tork.Mount {
result := make([]tork.Mount, len(ms))
for i, m := range ms {
result[i] = m.toMount()
}
Expand Down
8 changes: 4 additions & 4 deletions input/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"time"

"github.com/go-playground/validator/v10"
"github.com/runabol/tork"
"github.com/runabol/tork/internal/eval"
"github.com/runabol/tork/mount"
"github.com/runabol/tork/mq"
)

Expand Down Expand Up @@ -43,11 +43,11 @@ func validateMount(sl validator.StructLevel) {
mnt := sl.Current().Interface().(Mount)
if mnt.Type == "" {
sl.ReportError(mnt, "mount", "Mount", "typerequired", "")
} else if mnt.Type == mount.TypeVolume && mnt.Source != "" {
} else if mnt.Type == tork.MountTypeVolume && mnt.Source != "" {
sl.ReportError(mnt, "mount", "Mount", "sourcenotempty", "")
} else if mnt.Type == mount.TypeVolume && mnt.Target == "" {
} else if mnt.Type == tork.MountTypeVolume && mnt.Target == "" {
sl.ReportError(mnt, "mount", "Mount", "targetrequired", "")
} else if mnt.Type == mount.TypeBind && mnt.Source == "" {
} else if mnt.Type == tork.MountTypeBind && mnt.Source == "" {
sl.ReportError(mnt, "mount", "Mount", "sourcerequired", "")
} else if mnt.Source != "" && !mountPattern.MatchString(mnt.Source) {
sl.ReportError(mnt, "mount", "Mount", "invalidsource", "")
Expand Down
16 changes: 8 additions & 8 deletions input/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/go-playground/validator/v10"
"github.com/runabol/tork/mount"
"github.com/runabol/tork"
"github.com/runabol/tork/mq"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestValidateMounts(t *testing.T) {
Run: "some script",
Mounts: []Mount{
{
Type: mount.TypeVolume,
Type: tork.MountTypeVolume,
Target: "/some/target",
},
},
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestValidateMounts(t *testing.T) {
Run: "some script",
Mounts: []Mount{
{
Type: mount.TypeBind,
Type: tork.MountTypeBind,
Source: "", // missing
Target: "/some/target",
},
Expand All @@ -436,7 +436,7 @@ func TestValidateMounts(t *testing.T) {
Run: "some script",
Mounts: []Mount{
{
Type: mount.TypeBind,
Type: tork.MountTypeBind,
Source: "/some/source",
Target: "/some/target",
},
Expand All @@ -456,7 +456,7 @@ func TestValidateMounts(t *testing.T) {
Run: "some script",
Mounts: []Mount{
{
Type: mount.TypeBind,
Type: tork.MountTypeBind,
Source: "../some/source", // invalid
Target: "/some/target",
},
Expand All @@ -476,7 +476,7 @@ func TestValidateMounts(t *testing.T) {
Run: "some script",
Mounts: []Mount{
{
Type: mount.TypeBind,
Type: tork.MountTypeBind,
Source: "/some#/source", // invalid
Target: "/some/target",
},
Expand All @@ -496,7 +496,7 @@ func TestValidateMounts(t *testing.T) {
Run: "some script",
Mounts: []Mount{
{
Type: mount.TypeBind,
Type: tork.MountTypeBind,
Source: "/some/source",
Target: "/some:/target", // invalid
},
Expand All @@ -516,7 +516,7 @@ func TestValidateMounts(t *testing.T) {
Run: "some script",
Mounts: []Mount{
{
Type: mount.TypeBind,
Type: tork.MountTypeBind,
Source: "/some/source",
Target: "/tork",
},
Expand Down
9 changes: 4 additions & 5 deletions internal/worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/runabol/tork"
"github.com/runabol/tork/internal/uuid"
"github.com/runabol/tork/middleware/task"
"github.com/runabol/tork/mount"
"github.com/runabol/tork/mq"
"github.com/runabol/tork/runtime/docker"

Expand Down Expand Up @@ -81,9 +80,9 @@ func Test_handleTaskRun(t *testing.T) {
State: tork.TaskStateScheduled,
Image: "ubuntu:mantic",
CMD: []string{"ls"},
Mounts: []mount.Mount{
Mounts: []tork.Mount{
{
Type: mount.TypeVolume,
Type: tork.MountTypeVolume,
Target: "/somevolume",
},
},
Expand Down Expand Up @@ -154,9 +153,9 @@ func Test_handleTaskRunWithPrePost(t *testing.T) {
State: tork.TaskStateScheduled,
Image: "ubuntu:mantic",
Run: "cat /somevolume/pre > $TORK_OUTPUT",
Mounts: []mount.Mount{
Mounts: []tork.Mount{
{
Type: mount.TypeVolume,
Type: tork.MountTypeVolume,
Target: "/somevolume",
},
},
Expand Down
12 changes: 12 additions & 0 deletions mount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tork

const (
MountTypeVolume string = "volume"
MountTypeBind string = "bind"
)

type Mount struct {
Type string `json:"type,omitempty"`
Source string `json:"source,omitempty"`
Target string `json:"target,omitempty"`
}
5 changes: 3 additions & 2 deletions mount/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/pkg/errors"
"github.com/runabol/tork"
"github.com/runabol/tork/internal/wildcard"
)

Expand All @@ -23,7 +24,7 @@ func NewBindMounter(cfg BindConfig) *BindMounter {
}
}

func (m *BindMounter) Mount(ctx context.Context, mnt *Mount) error {
func (m *BindMounter) Mount(ctx context.Context, mnt *tork.Mount) error {
if !m.cfg.Allowed {
return errors.New("bind mounts are not allowed")
}
Expand All @@ -40,6 +41,6 @@ func (m *BindMounter) Mount(ctx context.Context, mnt *Mount) error {
return errors.Errorf("mount point not allowed: %s", mnt.Source)
}

func (m *BindMounter) Unmount(ctx context.Context, mnt *Mount) error {
func (m *BindMounter) Unmount(ctx context.Context, mnt *tork.Mount) error {
return nil
}
15 changes: 8 additions & 7 deletions mount/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/runabol/tork"
"github.com/stretchr/testify/assert"
)

Expand All @@ -12,8 +13,8 @@ func Test_createMountBindNotAllowed(t *testing.T) {
Allowed: false,
}}

err := m.Mount(context.Background(), &Mount{
Type: TypeBind,
err := m.Mount(context.Background(), &tork.Mount{
Type: tork.MountTypeBind,
Source: "/tmp",
Target: "/somevol",
})
Expand All @@ -25,8 +26,8 @@ func Test_createMountBindDenylist(t *testing.T) {
Allowed: true,
Denylist: []string{"/tmp"},
}}
err := m.Mount(context.Background(), &Mount{
Type: TypeBind,
err := m.Mount(context.Background(), &tork.Mount{
Type: tork.MountTypeBind,
Source: "/tmp",
Target: "/somevol",
})
Expand All @@ -38,8 +39,8 @@ func Test_createMountBindAllowlist(t *testing.T) {
Allowed: true,
Allowlist: []string{"/tmp"},
}}
mnt := Mount{
Type: TypeBind,
mnt := tork.Mount{
Type: tork.MountTypeBind,
Source: "/tmp",
Target: "/somevol",
}
Expand All @@ -51,5 +52,5 @@ func Test_createMountBindAllowlist(t *testing.T) {
}()
assert.Equal(t, "/somevol", mnt.Target)
assert.Equal(t, "/tmp", mnt.Source)
assert.Equal(t, TypeBind, mnt.Type)
assert.Equal(t, tork.MountTypeBind, mnt.Type)
}
15 changes: 3 additions & 12 deletions mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@ package mount

import (
"context"
)

const (
TypeVolume string = "volume"
TypeBind string = "bind"
"github.com/runabol/tork"
)

type Mounter interface {
Mount(ctx context.Context, mnt *Mount) error
Unmount(ctx context.Context, mnt *Mount) error
}

type Mount struct {
Type string `json:"type,omitempty"`
Source string `json:"source,omitempty"`
Target string `json:"target,omitempty"`
Mount(ctx context.Context, mnt *tork.Mount) error
Unmount(ctx context.Context, mnt *tork.Mount) error
}
5 changes: 3 additions & 2 deletions mount/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"

"github.com/pkg/errors"
"github.com/runabol/tork"
)

type MultiMounter struct {
Expand All @@ -18,7 +19,7 @@ func NewMultiMounter() *MultiMounter {
}
}

func (m *MultiMounter) Mount(ctx context.Context, mnt *Mount) error {
func (m *MultiMounter) Mount(ctx context.Context, mnt *tork.Mount) error {
m.mu.RLock()
defer m.mu.RUnlock()
mounter, ok := m.mounters[mnt.Type]
Expand All @@ -28,7 +29,7 @@ func (m *MultiMounter) Mount(ctx context.Context, mnt *Mount) error {
return mounter.Mount(ctx, mnt)
}

func (m *MultiMounter) Unmount(ctx context.Context, mnt *Mount) error {
func (m *MultiMounter) Unmount(ctx context.Context, mnt *tork.Mount) error {
m.mu.RLock()
defer m.mu.RUnlock()
mounter, ok := m.mounters[mnt.Type]
Expand Down
7 changes: 4 additions & 3 deletions mount/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"context"
"testing"

"github.com/runabol/tork"
"github.com/stretchr/testify/assert"
)

func TestMultiVolumeMount(t *testing.T) {
m := NewMultiMounter()
vm, err := NewVolumeMounter()
assert.NoError(t, err)
m.RegisterMounter(TypeVolume, vm)
m.RegisterMounter(tork.MountTypeVolume, vm)
ctx := context.Background()
mnt := &Mount{Type: TypeVolume, Target: "/mnt"}
mnt := &tork.Mount{Type: tork.MountTypeVolume, Target: "/mnt"}
err = m.Mount(ctx, mnt)
defer func() {
err := m.Unmount(ctx, mnt)
Expand All @@ -25,7 +26,7 @@ func TestMultiVolumeMount(t *testing.T) {
func TestMultiBadTypeMount(t *testing.T) {
m := NewMultiMounter()
ctx := context.Background()
mnt := &Mount{Type: "badone", Target: "/mnt"}
mnt := &tork.Mount{Type: "badone", Target: "/mnt"}
err := m.Mount(ctx, mnt)
assert.Error(t, err)
}
5 changes: 3 additions & 2 deletions mount/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/runabol/tork"
"github.com/runabol/tork/internal/uuid"
)

Expand All @@ -23,7 +24,7 @@ func NewVolumeMounter() (*VolumeMounter, error) {
return &VolumeMounter{client: dc}, nil
}

func (m *VolumeMounter) Mount(ctx context.Context, mn *Mount) error {
func (m *VolumeMounter) Mount(ctx context.Context, mn *tork.Mount) error {
name := uuid.NewUUID()
mn.Source = name
v, err := m.client.VolumeCreate(ctx, volume.CreateOptions{Name: name})
Expand All @@ -35,7 +36,7 @@ func (m *VolumeMounter) Mount(ctx context.Context, mn *Mount) error {
return nil
}

func (m *VolumeMounter) Unmount(ctx context.Context, mn *Mount) error {
func (m *VolumeMounter) Unmount(ctx context.Context, mn *tork.Mount) error {
ls, err := m.client.VolumeList(ctx, filters.NewArgs(filters.Arg("name", mn.Source)))
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions mount/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/docker/docker/api/types/filters"
"github.com/runabol/tork"
"github.com/stretchr/testify/assert"
)

Expand All @@ -13,7 +14,7 @@ func TestCreateVolume(t *testing.T) {
assert.NoError(t, err)

ctx := context.Background()
mnt := &Mount{}
mnt := &tork.Mount{}
err = vm.Mount(ctx, mnt)
assert.NoError(t, err)

Expand Down Expand Up @@ -43,8 +44,8 @@ func Test_createMountVolume(t *testing.T) {
m, err := NewVolumeMounter()
assert.NoError(t, err)

mnt := &Mount{
Type: TypeVolume,
mnt := &tork.Mount{
Type: tork.MountTypeVolume,
Target: "/somevol",
}

Expand Down
Loading

0 comments on commit 2b1047c

Please sign in to comment.