From 31e07f1b6cbf361783c4d7adf9e4b8da30c67384 Mon Sep 17 00:00:00 2001
From: jiangpengfei <jiangpengfei9@huawei.com>
Date: Tue, 18 Aug 2020 22:05:25 +0800
Subject: [PATCH 41/50] storage: mount nfs and gpath with given annotation

reason: when run container with annotation about storage spec,
prepare basic info in kata-runtime

Signed-off-by: jiangpengfei <jiangpengfei9@huawei.com>
---
 virtcontainers/kata_agent.go                  | 14 +++++++++++++-
 virtcontainers/pkg/annotations/annotations.go |  3 +++
 virtcontainers/pkg/oci/utils.go               | 16 ++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/virtcontainers/kata_agent.go b/virtcontainers/kata_agent.go
index d82a7f2d..ac64817a 100644
--- a/virtcontainers/kata_agent.go
+++ b/virtcontainers/kata_agent.go
@@ -30,10 +30,11 @@ import (
 	ns "github.com/kata-containers/runtime/virtcontainers/pkg/nsenter"
 	"github.com/kata-containers/runtime/virtcontainers/pkg/rootless"
 	vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types"
-	"github.com/kata-containers/runtime/virtcontainers/utils"
 	"github.com/kata-containers/runtime/virtcontainers/pkg/uuid"
+	"github.com/kata-containers/runtime/virtcontainers/storage"
 	"github.com/kata-containers/runtime/virtcontainers/store"
 	"github.com/kata-containers/runtime/virtcontainers/types"
+	"github.com/kata-containers/runtime/virtcontainers/utils"
 	"github.com/opencontainers/runtime-spec/specs-go"
 	opentracing "github.com/opentracing/opentracing-go"
 	"github.com/sirupsen/logrus"
@@ -1427,6 +1428,9 @@ func (k *kataAgent) createContainer(sandbox *Sandbox, c *Container) (p *Process,
 	localStorages := k.handleLocalStorage(ociSpec.Mounts, sandbox.id, c.rootfsSuffix)
 	ctrStorages = append(ctrStorages, localStorages...)
 
+	remoteStoragtes := k.handleRemoteStorage(ociSpec, sandbox.id)
+	ctrStorages = append(ctrStorages, remoteStoragtes...)
+
 	// We replace all OCI mount sources that match our container mount
 	// with the right source path (The guest one).
 	if err = k.replaceOCIMountSource(ociSpec, newMounts); err != nil {
@@ -1510,6 +1514,14 @@ func (k *kataAgent) createContainer(sandbox *Sandbox, c *Container) (p *Process,
 		k.state.URL, consoleURL, c.config.Cmd, createNSList, enterNSList)
 }
 
+func (k *kataAgent) handleRemoteStorage(spec *specs.Spec, sandboxId string) []*grpc.Storage {
+	if value, ok := spec.Annotations[vcAnnotations.StorageSpecTypeKey]; ok {
+		return storage.GetGrpcStorageAndAppendMount(kataGuestStorageDir, value, spec, sandboxId)
+	}
+
+	return []*grpc.Storage{}
+}
+
 // handleEphemeralStorage handles ephemeral storages by
 // creating a Storage from corresponding source of the mount point
 func (k *kataAgent) handleEphemeralStorage(mounts []specs.Mount) []*grpc.Storage {
diff --git a/virtcontainers/pkg/annotations/annotations.go b/virtcontainers/pkg/annotations/annotations.go
index 903c7f03..e50a697c 100644
--- a/virtcontainers/pkg/annotations/annotations.go
+++ b/virtcontainers/pkg/annotations/annotations.go
@@ -68,6 +68,9 @@ const (
 	// AssetHashType is the hash type used for assets verification
 	AssetHashType = kataAnnotationsPrefix + "asset_hash_type"
 
+	// StorageSpecTypeKey is the annotation key to fetch storage_spec
+	StorageSpecTypeKey = kataAnnotationsPrefix + "storage_spec"
+
 	//
 	//	Generic annotations
 	//
diff --git a/virtcontainers/pkg/oci/utils.go b/virtcontainers/pkg/oci/utils.go
index 948bd3cb..d032227e 100644
--- a/virtcontainers/pkg/oci/utils.go
+++ b/virtcontainers/pkg/oci/utils.go
@@ -22,6 +22,7 @@ import (
 	exp "github.com/kata-containers/runtime/virtcontainers/experimental"
 	vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
 	dockershimAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations/dockershim"
+	"github.com/kata-containers/runtime/virtcontainers/storage"
 	"github.com/kata-containers/runtime/virtcontainers/types"
 	"github.com/kata-containers/runtime/virtcontainers/utils"
 	specs "github.com/opencontainers/runtime-spec/specs-go"
@@ -340,6 +341,17 @@ func SandboxID(spec specs.Spec) (string, error) {
 	return "", fmt.Errorf("Could not find sandbox ID")
 }
 
+func validateStorageSpec(spec specs.Spec) error {
+	if storageSpec, ok := spec.Annotations[vcAnnotations.StorageSpecTypeKey]; ok {
+		err := storage.ValidateStorageValue(storageSpec)
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
 func addAnnotations(ocispec specs.Spec, config *vc.SandboxConfig) error {
 	addAssetAnnotations(ocispec, config)
 	if err := addHypervisorConfigOverrides(ocispec, config); err != nil {
@@ -873,6 +885,10 @@ func SandboxConfig(ocispec specs.Spec, runtime RuntimeConfig, bundlePath, cid, c
 // ContainerConfig converts an OCI compatible runtime configuration
 // file to a virtcontainers container configuration structure.
 func ContainerConfig(ocispec specs.Spec, bundlePath, cid, console string, detach bool) (vc.ContainerConfig, error) {
+	err := validateStorageSpec(ocispec)
+	if err != nil {
+		return vc.ContainerConfig{}, err
+	}
 	rootfs := vc.RootFs{Target: ocispec.Root.Path, Mounted: true}
 	if !filepath.IsAbs(rootfs.Target) {
 		rootfs.Target = filepath.Join(bundlePath, ocispec.Root.Path)
-- 
2.14.3 (Apple Git-98)

