Skip to content
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

Ignore missing config.v2.json files on migration #303

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion pkg/storagemigration/failcleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
// FailCleanup should be run after a failed migration.
// It will remove any files left over from the migration process
// and migrate containers back to aufs.
//
func failCleanup(root string) error {
logrus.WithField("storage_root", root).Warning("recovering from failed aufs to overlay migration")

Expand Down
12 changes: 9 additions & 3 deletions pkg/storagemigration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func Migrate(root string) (err error) {
if err != nil {
return fmt.Errorf("Error migrating containers to overlay2: %v", err)
}

return nil
}

Expand Down Expand Up @@ -336,10 +335,17 @@ func SwitchAllContainersStorageDriver(root, newStorageDriver string) error {
logrus.Debugf("migrating %v container(s) to %s", len(containerIDs), newStorageDriver)
for _, containerID := range containerIDs {
err := switchContainerStorageDriver(root, containerID, newStorageDriver)
if err != nil {
switch err {
case nil:
logrus.WithField("container_id", containerID).Debugf("reconfigured storage-driver to %s", newStorageDriver)
case errNoConfigV2JSON:
if newStorageDriver == "overlay2" {
return fmt.Errorf("Error containerID %s: %v", containerID, err)
}
logrus.WithField("container_id", containerID).Errorf("has no config.v2.json %s", newStorageDriver)
default:
return fmt.Errorf("Error rewriting container config for %s: %v", containerID, err)
}
logrus.WithField("container_id", containerID).Debugf("reconfigured storage-driver to %s", newStorageDriver)
}
return nil
}
35 changes: 35 additions & 0 deletions pkg/storagemigration/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func setup(t *testing.T) (*fs.Dir, *State, func()) {
),
),
)

state := &State{
Layers: []Layer{
{
Expand Down Expand Up @@ -132,3 +133,37 @@ func TestFailCleanup(t *testing.T) {
_, err = os.Stat(root.Join("aufs"))
assert.NilError(t, err)
}

func TestFailCleanupContainer(t *testing.T) {
root, _, cleanup := setup(t)
defer cleanup()

// delete config.v2.json to force SwitchAllContainersStorageDriver to fail
os.Remove(root.Join("containers", "bebe92422caf828ab21ae39974a0c003a29970ec09c6e5529bbb24f71eb9ca2ef", "config.v2.json"))

err := Migrate(root.Path())
if err == nil {
// won't err on rollback to aufs fail
assert.NilError(t, err)
} else {
// only errs on migration to overlay2 fail
assert.ErrorContains(t, err, "bebe92422caf828ab21ae39974a0c003a29970ec09c6e5529bbb24f71eb9ca2ef")

// // config.v2.json should not exists
_, err = os.Stat(root.Join("containers", "bebe92422caf828ab21ae39974a0c003a29970ec09c6e5529bbb24f71eb9ca2ef", "config.v2.json"))
assert.ErrorType(t, err, os.IsNotExist)

// migration logfile should exists
_, err = os.Stat(root.Join("migrate.log"))
assert.NilError(t, err)

// overlay2 directory should not exists
_, err = os.Stat(root.Join("overlay2"))
println("err", root.Join("overlay2"), err)
assert.ErrorType(t, err, os.IsNotExist)

// aufs directory should still exists
_, err = os.Stat(root.Join("aufs"))
assert.NilError(t, err)
}
}
7 changes: 7 additions & 0 deletions pkg/storagemigration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storagemigration

import (
"encoding/json"
"errors"
"os"
"path/filepath"

Expand All @@ -11,6 +12,8 @@ import (
"golang.org/x/sys/unix"
)

var errNoConfigV2JSON = errors.New("config.v2.json not found for container")

// exists checks if a file (or if isDir is set to "true" a directory) exists
func exists(path string, isDir bool) (bool, error) {
fi, err := os.Stat(path)
Expand Down Expand Up @@ -60,6 +63,10 @@ func replicate(sourceDir, targetDir string) error {
// this is the only change needed to make it work after the migration
func switchContainerStorageDriver(root, containerID, newStorageDriver string) error {
containerConfigPath := filepath.Join(root, "containers", containerID, "config.v2.json")
if ok, _ := exists(containerConfigPath, false); !ok {
return errNoConfigV2JSON
}

f, err := os.OpenFile(containerConfigPath, os.O_RDWR, os.ModePerm)
if err != nil {
return err
Expand Down