summaryrefslogtreecommitdiffhomepage
path: root/pkg/shim/v1/utils
diff options
context:
space:
mode:
authorAdin Scannell <ascannell@google.com>2020-05-05 22:00:14 -0700
committerAdin Scannell <ascannell@google.com>2020-07-09 17:39:47 -0700
commit2afff44403e046078301de39f0252bb57fc018c7 (patch)
treef944f66ffaab8f10029b451755bcb20a3ff40269 /pkg/shim/v1/utils
parent2f24ab339736315659f26699ab50aa2982d7e890 (diff)
Update shim to build using bazel.
The go.mod dependency tree for the shim was somehow contradictory. After resolving these issues (e.g. explicitly imported k8s 1.14, pulling a specific dbus version), and adding all dependencies, the shim can now be build as part of the regular bazel tree. As part of this process, minor cleanup was done in all the source files: headers were standardized (and include "The gVisor Authors" in addition to the "The containerd Authors" if originally derived from containerd sources), and comments were cleaned up to meet coding standards. This change makes the containerd installation dynamic, so that multiple versions can be tested, and drops the static installer for the VM image itself. This change also updates test/root/crictl_test.go and related utilities, so that the containerd tests can be run on any version (and in cases where it applies, they can be run on both v1 and v2 as parameterized tests).
Diffstat (limited to 'pkg/shim/v1/utils')
-rw-r--r--pkg/shim/v1/utils/BUILD26
-rw-r--r--pkg/shim/v1/utils/volumes.go41
2 files changed, 47 insertions, 20 deletions
diff --git a/pkg/shim/v1/utils/BUILD b/pkg/shim/v1/utils/BUILD
new file mode 100644
index 000000000..9045781e1
--- /dev/null
+++ b/pkg/shim/v1/utils/BUILD
@@ -0,0 +1,26 @@
+load("//tools:defs.bzl", "go_library", "go_test")
+
+package(licenses = ["notice"])
+
+go_library(
+ name = "utils",
+ srcs = [
+ "utils.go",
+ "volumes.go",
+ ],
+ visibility = [
+ "//pkg/shim:__subpackages__",
+ "//shim:__subpackages__",
+ ],
+ deps = [
+ "@com_github_containerd_cri//pkg/annotations:go_default_library",
+ "@com_github_opencontainers_runtime-spec//specs-go:go_default_library",
+ ],
+)
+
+go_test(
+ name = "utils_test",
+ size = "small",
+ srcs = ["volumes_test.go"],
+ library = ":utils",
+)
diff --git a/pkg/shim/v1/utils/volumes.go b/pkg/shim/v1/utils/volumes.go
index 7323e7245..e4e9bf9b1 100644
--- a/pkg/shim/v1/utils/volumes.go
+++ b/pkg/shim/v1/utils/volumes.go
@@ -23,8 +23,6 @@ import (
"github.com/containerd/cri/pkg/annotations"
specs "github.com/opencontainers/runtime-spec/specs-go"
- "github.com/pkg/errors"
- "github.com/sirupsen/logrus"
)
const volumeKeyPrefix = "dev.gvisor.spec.mount."
@@ -32,13 +30,13 @@ const volumeKeyPrefix = "dev.gvisor.spec.mount."
var kubeletPodsDir = "/var/lib/kubelet/pods"
// volumeName gets volume name from volume annotation key, example:
-// dev.gvisor.spec.mount.NAME.share
+// dev.gvisor.spec.mount.NAME.share
func volumeName(k string) string {
return strings.SplitN(strings.TrimPrefix(k, volumeKeyPrefix), ".", 2)[0]
}
// volumeFieldName gets volume field name from volume annotation key, example:
-// `type` is the field of dev.gvisor.spec.mount.NAME.type
+// `type` is the field of dev.gvisor.spec.mount.NAME.type
func volumeFieldName(k string) string {
parts := strings.Split(strings.TrimPrefix(k, volumeKeyPrefix), ".")
return parts[len(parts)-1]
@@ -48,16 +46,16 @@ func volumeFieldName(k string) string {
func podUID(s *specs.Spec) (string, error) {
sandboxLogDir := s.Annotations[annotations.SandboxLogDir]
if sandboxLogDir == "" {
- return "", errors.New("no sandbox log path annotation")
+ return "", fmt.Errorf("no sandbox log path annotation")
}
fields := strings.Split(filepath.Base(sandboxLogDir), "_")
switch len(fields) {
- case 1: // This is the old CRI logging path
+ case 1: // This is the old CRI logging path.
return fields[0], nil
- case 3: // This is the new CRI logging path
+ case 3: // This is the new CRI logging path.
return fields[2], nil
}
- return "", errors.Errorf("unexpected sandbox log path %q", sandboxLogDir)
+ return "", fmt.Errorf("unexpected sandbox log path %q", sandboxLogDir)
}
// isVolumeKey checks whether an annotation key is for volume.
@@ -79,7 +77,7 @@ func volumePath(volume, uid string) (string, error) {
return "", err
}
if len(dirs) != 1 {
- return "", errors.Errorf("unexpected matched volume list %v", dirs)
+ return "", fmt.Errorf("unexpected matched volume list %v", dirs)
}
return dirs[0], nil
}
@@ -103,7 +101,7 @@ func UpdateVolumeAnnotations(bundle string, s *specs.Spec) error {
if err != nil {
// Skip if we can't get pod UID, because this doesn't work
// for containerd 1.1.
- logrus.WithError(err).Error("Can't get pod uid")
+ fmt.Errorf("Can't get pod uid: %w", err)
return nil
}
}
@@ -117,22 +115,25 @@ func UpdateVolumeAnnotations(bundle string, s *specs.Spec) error {
}
volume := volumeName(k)
if uid != "" {
- // This is a sandbox
+ // This is a sandbox.
path, err := volumePath(volume, uid)
if err != nil {
- return errors.Wrapf(err, "get volume path for %q", volume)
+ return fmt.Errorf("get volume path for %q: %w", volume, err)
}
s.Annotations[volumeSourceKey(volume)] = path
updated = true
} else {
- // This is a container
+ // This is a container.
for i := range s.Mounts {
- // An error is returned for sandbox if source annotation
- // is not successfully applied, so it is guaranteed that
- // the source annotation for sandbox has already been
- // successfully applied at this point.
- // The volume name is unique inside a pod, so matching without
- // podUID is fine here.
+ // An error is returned for sandbox if source
+ // annotation is not successfully applied, so
+ // it is guaranteed that the source annotation
+ // for sandbox has already been successfully
+ // applied at this point.
+ //
+ // The volume name is unique inside a pod, so
+ // matching without podUID is fine here.
+ //
// TODO: Pass podUID down to shim for containers to do
// more accurate matching.
if yes, _ := isVolumePath(volume, s.Mounts[i].Source); yes {
@@ -147,7 +148,7 @@ func UpdateVolumeAnnotations(bundle string, s *specs.Spec) error {
if !updated {
return nil
}
- // Update bundle
+ // Update bundle.
b, err := json.Marshal(s)
if err != nil {
return err