Skip to content

chore: move ParseVolumeMounts to the k8s package #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/opencontainers/image-spec v1.1.0
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/pterm/pterm v0.12.80
github.com/stretchr/testify v1.10.0
go.opencensus.io v0.24.0
go.opentelemetry.io/otel v1.32.0
go.opentelemetry.io/otel/sdk v1.32.0
Expand Down Expand Up @@ -131,6 +132,7 @@ require (
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.61.0 // indirect
Expand Down
24 changes: 1 addition & 23 deletions internal/cmd/local/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package local
import (
"context"
"fmt"
"strings"

"github.com/airbytehq/abctl/internal/common"
"github.com/airbytehq/abctl/internal/helm"
Expand Down Expand Up @@ -37,7 +36,7 @@ func (i *InstallCmd) InstallOpts(ctx context.Context, user string) (*service.Ins
ctx, span := trace.NewSpan(ctx, "InstallCmd.InstallOpts")
defer span.End()

extraVolumeMounts, err := parseVolumeMounts(i.Volume)
extraVolumeMounts, err := k8s.ParseVolumeMounts(i.Volume)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -211,24 +210,3 @@ func (i *InstallCmd) Run(ctx context.Context, provider k8s.Provider, telClient t
return nil
})
}

func parseVolumeMounts(specs []string) ([]k8s.ExtraVolumeMount, error) {
if len(specs) == 0 {
return nil, nil
}

mounts := make([]k8s.ExtraVolumeMount, len(specs))

for i, spec := range specs {
parts := strings.Split(spec, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("volume %s is not a valid volume spec, must be <HOST_PATH>:<GUEST_PATH>", spec)
}
mounts[i] = k8s.ExtraVolumeMount{
HostPath: parts[0],
ContainerPath: parts[1],
}
}

return mounts, nil
}
34 changes: 34 additions & 0 deletions internal/k8s/volumes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package k8s

import (
"fmt"
"strings"
)

// errInvalidVolumeMountSpec returns an error for an invalid volume mount spec.
func errInvalidVolumeMountSpec(spec string) error {
return fmt.Errorf("volume %s is not a valid volume spec, must be <HOST_PATH>:<GUEST_PATH>", spec)
}

// ParseVolumeMounts parses a slice of volume mount specs in the format <HOST_PATH>:<GUEST_PATH>
// and returns a slice of ExtraVolumeMount. Returns an error if any spec is invalid.
func ParseVolumeMounts(specs []string) ([]ExtraVolumeMount, error) {
if len(specs) == 0 {
return nil, nil
}

mounts := make([]ExtraVolumeMount, len(specs))

for i, spec := range specs {
parts := strings.Split(spec, ":")
if len(parts) != 2 {
return nil, errInvalidVolumeMountSpec(spec)
}
mounts[i] = ExtraVolumeMount{
HostPath: parts[0],
ContainerPath: parts[1],
}
}

return mounts, nil
}
55 changes: 55 additions & 0 deletions internal/k8s/volumes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package k8s

import (
"testing"

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

func TestParseVolumeMounts(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input []string
expectMounts []ExtraVolumeMount
expectErr error
}{
{
name: "empty input",
},
{
name: "single valid mount",
input: []string{"/host:/container"},
expectMounts: []ExtraVolumeMount{{HostPath: "/host", ContainerPath: "/container"}},
},
{
name: "multiple valid mounts",
input: []string{"/a:/b", "/c:/d"},
expectMounts: []ExtraVolumeMount{{HostPath: "/a", ContainerPath: "/b"}, {HostPath: "/c", ContainerPath: "/d"}},
},
{
name: "invalid spec (missing colon)",
input: []string{"/hostcontainer"},
expectErr: errInvalidVolumeMountSpec("/hostcontainer"),
},
{
name: "invalid spec (too many colons)",
input: []string{"/a:/b:/c"},
expectErr: errInvalidVolumeMountSpec("/a:/b:/c"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mounts, err := ParseVolumeMounts(tt.input)
assert.Equal(t, tt.expectMounts, mounts, "mounts should match")
if tt.expectErr != nil {
assert.EqualError(t, err, tt.expectErr.Error(), "errors should match")
} else {
assert.NoError(t, err)
}
})
}
}