Skip to content

Commit f686b6c

Browse files
authored
chore: move ParseVolumeMounts to the k8s package (#169)
1 parent 71b6059 commit f686b6c

File tree

4 files changed

+92
-23
lines changed

4 files changed

+92
-23
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/opencontainers/image-spec v1.1.0
1717
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
1818
github.com/pterm/pterm v0.12.80
19+
github.com/stretchr/testify v1.10.0
1920
go.opencensus.io v0.24.0
2021
go.opentelemetry.io/otel v1.32.0
2122
go.opentelemetry.io/otel/sdk v1.32.0
@@ -131,6 +132,7 @@ require (
131132
github.com/pelletier/go-toml v1.9.5 // indirect
132133
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
133134
github.com/pkg/errors v0.9.1 // indirect
135+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
134136
github.com/prometheus/client_golang v1.20.5 // indirect
135137
github.com/prometheus/client_model v0.6.1 // indirect
136138
github.com/prometheus/common v0.61.0 // indirect

internal/cmd/local/install.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package local
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

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

40-
extraVolumeMounts, err := parseVolumeMounts(i.Volume)
39+
extraVolumeMounts, err := k8s.ParseVolumeMounts(i.Volume)
4140
if err != nil {
4241
return nil, err
4342
}
@@ -211,24 +210,3 @@ func (i *InstallCmd) Run(ctx context.Context, provider k8s.Provider, telClient t
211210
return nil
212211
})
213212
}
214-
215-
func parseVolumeMounts(specs []string) ([]k8s.ExtraVolumeMount, error) {
216-
if len(specs) == 0 {
217-
return nil, nil
218-
}
219-
220-
mounts := make([]k8s.ExtraVolumeMount, len(specs))
221-
222-
for i, spec := range specs {
223-
parts := strings.Split(spec, ":")
224-
if len(parts) != 2 {
225-
return nil, fmt.Errorf("volume %s is not a valid volume spec, must be <HOST_PATH>:<GUEST_PATH>", spec)
226-
}
227-
mounts[i] = k8s.ExtraVolumeMount{
228-
HostPath: parts[0],
229-
ContainerPath: parts[1],
230-
}
231-
}
232-
233-
return mounts, nil
234-
}

internal/k8s/volumes.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package k8s
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// errInvalidVolumeMountSpec returns an error for an invalid volume mount spec.
9+
func errInvalidVolumeMountSpec(spec string) error {
10+
return fmt.Errorf("volume %s is not a valid volume spec, must be <HOST_PATH>:<GUEST_PATH>", spec)
11+
}
12+
13+
// ParseVolumeMounts parses a slice of volume mount specs in the format <HOST_PATH>:<GUEST_PATH>
14+
// and returns a slice of ExtraVolumeMount. Returns an error if any spec is invalid.
15+
func ParseVolumeMounts(specs []string) ([]ExtraVolumeMount, error) {
16+
if len(specs) == 0 {
17+
return nil, nil
18+
}
19+
20+
mounts := make([]ExtraVolumeMount, len(specs))
21+
22+
for i, spec := range specs {
23+
parts := strings.Split(spec, ":")
24+
if len(parts) != 2 {
25+
return nil, errInvalidVolumeMountSpec(spec)
26+
}
27+
mounts[i] = ExtraVolumeMount{
28+
HostPath: parts[0],
29+
ContainerPath: parts[1],
30+
}
31+
}
32+
33+
return mounts, nil
34+
}

internal/k8s/volumes_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package k8s
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestParseVolumeMounts(t *testing.T) {
10+
t.Parallel()
11+
12+
tests := []struct {
13+
name string
14+
input []string
15+
expectMounts []ExtraVolumeMount
16+
expectErr error
17+
}{
18+
{
19+
name: "empty input",
20+
},
21+
{
22+
name: "single valid mount",
23+
input: []string{"/host:/container"},
24+
expectMounts: []ExtraVolumeMount{{HostPath: "/host", ContainerPath: "/container"}},
25+
},
26+
{
27+
name: "multiple valid mounts",
28+
input: []string{"/a:/b", "/c:/d"},
29+
expectMounts: []ExtraVolumeMount{{HostPath: "/a", ContainerPath: "/b"}, {HostPath: "/c", ContainerPath: "/d"}},
30+
},
31+
{
32+
name: "invalid spec (missing colon)",
33+
input: []string{"/hostcontainer"},
34+
expectErr: errInvalidVolumeMountSpec("/hostcontainer"),
35+
},
36+
{
37+
name: "invalid spec (too many colons)",
38+
input: []string{"/a:/b:/c"},
39+
expectErr: errInvalidVolumeMountSpec("/a:/b:/c"),
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
t.Parallel()
46+
mounts, err := ParseVolumeMounts(tt.input)
47+
assert.Equal(t, tt.expectMounts, mounts, "mounts should match")
48+
if tt.expectErr != nil {
49+
assert.EqualError(t, err, tt.expectErr.Error(), "errors should match")
50+
} else {
51+
assert.NoError(t, err)
52+
}
53+
})
54+
}
55+
}

0 commit comments

Comments
 (0)