Skip to content

Commit

Permalink
Remove open-coding of ParentLayerPathsFlag
Browse files Browse the repository at this point in the history
containerd/mount.Mount provides an API for this now, although in one
case we have to create an instance of that from the protobuf-marshelled
types.Mount.

Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
  • Loading branch information
TBBle committed Nov 10, 2023
1 parent a276184 commit f210c20
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
17 changes: 5 additions & 12 deletions cmd/containerd-shim-runhcs-v1/rootfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,10 @@ func parseLegacyRootfsMount(m *types.Mount) (string, []string, error) {
//
// The OCI spec expects:
// layerN, layerN-1, ..., layer0, scratch
var parentLayerPaths []string
for _, option := range m.Options {
if strings.HasPrefix(option, mount.ParentLayerPathsFlag) {
err := json.Unmarshal([]byte(option[len(mount.ParentLayerPathsFlag):]), &parentLayerPaths)
if err != nil {
return "", nil, fmt.Errorf("unmarshal parent layer paths from mount: %v: %w", err, errdefs.ErrFailedPrecondition)
}
// Would perhaps be worthwhile to check for unrecognized options and return an error,
// but since this is a legacy layer mount we don't do that to avoid breaking anyone.
break
}
containerdMount := mount.Mount{Type: m.Type, Source: m.Source, Target: m.Target, Options: m.Options}
parentLayerPaths, err := containerdMount.GetParentPaths()
if err != nil {
return "", nil, fmt.Errorf("failed to get mount's parent layer paths: %v: %w", err, errdefs.ErrFailedPrecondition)
}
return m.Source, parentLayerPaths, nil
}
Expand Down Expand Up @@ -100,7 +93,7 @@ func getLCOWLayers(rootfs []*types.Mount, layerFolders []string) (*layers.LCOWLa
m := rootfs[0]
switch m.Type {
case "lcow-layer":
scratchLayer, parentLayers, err := parseLegacyRootfsMount(rootfs[0])
scratchLayer, parentLayers, err := parseLegacyRootfsMount(m)
if err != nil {
return nil, err
}
Expand Down
12 changes: 3 additions & 9 deletions test/internal/layers/layerfolders.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package layers

import (
"context"
"encoding/json"
"strings"
"testing"

"github.com/containerd/containerd"
Expand Down Expand Up @@ -35,13 +33,9 @@ func FromChainID(ctx context.Context, tb testing.TB, client *containerd.Client,
// FromMount returns the layer paths of a given mount
func FromMount(_ context.Context, tb testing.TB, m mount.Mount) (layers []string) {
tb.Helper()
for _, option := range m.Options {
if strings.HasPrefix(option, mount.ParentLayerPathsFlag) {
err := json.Unmarshal([]byte(option[len(mount.ParentLayerPathsFlag):]), &layers)
if err != nil {
tb.Fatalf("failed to unmarshal parent layer paths from mount: %v", err)
}
}
layers, err := m.GetParentPaths()
if err != nil {
tb.Fatalf("failed to get mount's parent layer paths: %v", err)
}
layers = append(layers, m.Source)

Expand Down

0 comments on commit f210c20

Please # to comment.