kubernetes / kubernetes/kompose

[BUG] kompose convert panics on a volume that is only a name and an access mode

Open Beginner friendly
#2,109 1 comment 0 reactions 0 assignees View on GitHub
kind/bug
Dominant language
Go
Stars
10.6k
Forks
816
PR merge metrics
No merged PRs in 30d

Description

### Expected Behavior

`kompose convert` should report a malformed volume entry as an error and exit, the way it already does for other bad volume strings.

### Actual Behavior

It panics with `index out of range [-1]` and prints a Go stack trace. Exit code is 2.

```
panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/kubernetes/kompose/pkg/transformer.parseVolume({0x398b7b3b6350, 0x8})
/kompose/pkg/transformer/utils.go:112 +0x4bc
github.com/kubernetes/kompose/pkg/transformer.ParseVolume({0x398b7b3b6350, 0x8})
/kompose/pkg/transformer/utils.go:72 +0x60
github.com/kubernetes/kompose/pkg/loader/compose.ParseVols({0x398b7b5a01a0?, 0x1, 0x0?}, {0x398b7b3b63b0, 0x3})
/kompose/pkg/loader/compose/compose.go:928 +0xf8
github.com/kubernetes/kompose/pkg/loader/compose.retrieveVolume(...)
/kompose/pkg/loader/compose/compose.go:903 +0xc8
github.com/kubernetes/kompose/pkg/loader/compose.handleVolume(...)
/kompose/pkg/loader/compose/compose.go:837 +0xb8
github.com/kubernetes/kompose/pkg/loader/compose.dockerComposeToKomposeMapping(...)
/kompose/pkg/loader/compose/compose.go:609 +0xb94
github.com/kubernetes/kompose/pkg/app.Convert(...)
/kompose/pkg/app/app.go:221 +0xbc
```

### Steps To Reproduce

1. Save the compose file below.
2. Run `kompose convert --stdout`.
3. It panics instead of printing an error.

`myvol:rw`, `myvol:z` and `myvol:Z` do the same thing. Well-formed entries such as `myvol:/data:ro` and `/host:/data:ro` are unaffected.

### Kompose Version

```Text
1.38.0 (HEAD)
```

built from `main` at c2dd9614fd5026b8f6cb95d61bba802626088b13.

### Docker-Compose file

```YAML
services:
web:
image: nginx
volumes:
- "myvol:ro"

volumes:
myvol:
```

### Anything else?

`parseVolume` in `pkg/transformer/utils.go` takes the volume name off the front of the split list and a trailing `rw`/`ro`/`z`/`Z` off the back, then reads the container path with `volumeStrings[len(volumeStrings)-1]`. For `myvol:ro` those two steps consume the whole list, so the index goes to -1.

The entry is not usable in the first place. Compose reads the second field as the container target rather than as an access mode, and the engine then rejects it:

```
Error response from daemon: invalid volume specification: 'repro-vol_myvol:ro:rw': invalid mount config for type "volume": invalid mount path: 'ro' mount path must be absolute
```

So an error is the right outcome here, and `parseVolume` already returns `invalid volume format` for a bare `ro` via the same length check one branch earlier. #2108 adds the equivalent check after the access mode is stripped.

Contributor guide

Open the contributing guide

Research direction

Start in pkg/transformer/utils.go at parseVolume, following the documented handling of volume names and access modes. Reproduce the myvol:ro example with kompose convert --stdout, then verify malformed volume entries return an error without a panic or stack trace while well-formed entries remain unaffected.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker-compose, go, kubernetes
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
86/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.