Skip to content

Commit 9f45ec7

Browse files
authored
Merge pull request containerd#48 from jterry75/automanage_vhd_cleanup
Remove automange-vhd's on ContainerCreate failure
2 parents 2164b9a + 7d006ac commit 9f45ec7

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

pkg/server/container_create_windows.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package server
2020

2121
import (
2222
"fmt"
23+
"os"
2324
"path/filepath"
2425
"regexp"
2526
"strings"
@@ -35,6 +36,7 @@ import (
3536
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
3637
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
3738
"github.com/pkg/errors"
39+
"github.com/sirupsen/logrus"
3840
"golang.org/x/net/context"
3941
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
4042

@@ -157,6 +159,11 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
157159
if err != nil {
158160
return nil, errors.Wrapf(err, "failed to generate container %q spec", id)
159161
}
162+
defer func() {
163+
if retErr != nil {
164+
cleanupAutomanageVhdFiles(ctx, id, sandboxID, config)
165+
}
166+
}()
160167

161168
log.G(ctx).Debugf("Container %q spec: %#+v", id, spew.NewFormatter(spec))
162169

@@ -472,3 +479,41 @@ func setOCIDevicesPrivileged(g *generator) error {
472479
g.AddAnnotation("io.microsoft.virtualmachine.lcow.privileged", "true")
473480
return nil
474481
}
482+
483+
// cleanupAutomanageVhdFiles is used to remove any automanage-vhd:// HostPaths
484+
// that were created as part of the ContainerCreate call that resulted in a
485+
// failure. This is because the expectation of the mount is that it will be
486+
// controlled in the lifetime of the shim that owns the container but if the
487+
// container fails activation it is unclear what stage that may have happened.
488+
// So only CRI can clean up the file copies that it did.
489+
func cleanupAutomanageVhdFiles(ctx context.Context, id, sandboxID string, config *runtime.ContainerConfig) {
490+
automanageVhdIndex := 0
491+
for _, m := range config.GetMounts() {
492+
if strings.HasPrefix(m.HostPath, "automanage-vhd://") {
493+
if formattedSource, err := filepath.EvalSymlinks(strings.TrimPrefix(m.HostPath, "automanage-vhd://")); err != nil {
494+
log.G(ctx).WithFields(logrus.Fields{
495+
"path": m.HostPath,
496+
logrus.ErrorKey: err,
497+
}).Warn("failed to EvalSymlinks for automanage-vhd://")
498+
} else {
499+
if s, err := os.Stat(formattedSource); err != nil {
500+
log.G(ctx).WithFields(logrus.Fields{
501+
"path": formattedSource,
502+
logrus.ErrorKey: err,
503+
}).Warn("failed to Stat automanage-vhd://")
504+
} else {
505+
if s.IsDir() {
506+
formattedSource = filepath.Join(formattedSource, fmt.Sprintf("%s-%s.%d.vhdx", sandboxID, id, automanageVhdIndex))
507+
automanageVhdIndex++
508+
}
509+
if err := os.Remove(formattedSource); err != nil && !os.IsNotExist(err) {
510+
log.G(ctx).WithFields(logrus.Fields{
511+
"path": formattedSource,
512+
logrus.ErrorKey: err,
513+
}).Warn("failed to remove automanage-vhd://")
514+
}
515+
}
516+
}
517+
}
518+
}
519+
}

0 commit comments

Comments
 (0)