diff --git a/internal/model/types/container.go b/internal/model/types/container.go index 1ac92ea..fdd88ed 100644 --- a/internal/model/types/container.go +++ b/internal/model/types/container.go @@ -27,6 +27,7 @@ type Container struct { Cmd []string Env []string Binds []string + Mounts []Mount PreArchives []PreArchive HostIP string ExposedPorts map[string]interface{} @@ -53,6 +54,14 @@ type PreArchive struct { Archive []byte } +// Mount contains the details of a mounted volume/binding. +type Mount struct { + Type string + Source string + Target string + ReadOnly bool +} + const ( // LabelRequestCPU is the label to be used to specify cpu request/limits LabelRequestCPU = "com.joyrex2001.kubedock.request-cpu" @@ -330,6 +339,9 @@ func (co *Container) GetVolumes() map[string]string { f := strings.Split(bind, ":") mounts[f[1]] = f[0] } + for _, mount := range co.Mounts { + mounts[mount.Target] = mount.Source + } return mounts } diff --git a/internal/model/types/container_test.go b/internal/model/types/container_test.go index c25c5a0..326098f 100644 --- a/internal/model/types/container_test.go +++ b/internal/model/types/container_test.go @@ -634,15 +634,24 @@ func TestVolumes(t *testing.T) { sock bool }{ { - in: &Container{Binds: []string{ - "container_test.go:/tmp/container_test.go:ro", - "../types:/tmp/types:ro", - "/var/run/docker.sock:/var/run/docker.sock:rw", - }}, + in: &Container{ + Binds: []string{ + "container_test.go:/tmp/container_test.go:ro", + "../types:/tmp/types:ro", + "/var/run/docker.sock:/var/run/docker.sock:rw", + }, + Mounts: []Mount{{ + Source: "/abc", + Target: "def", + ReadOnly: false, + Type: "bind", + }}, + }, all: map[string]string{ "/tmp/container_test.go": "container_test.go", "/tmp/types": "../types", "/var/run/docker.sock": "/var/run/docker.sock", + "def": "/abc", }, files: map[string]string{ "/tmp/container_test.go": "container_test.go", diff --git a/internal/server/routes/docker/containers.go b/internal/server/routes/docker/containers.go index 8e7014c..0239515 100644 --- a/internal/server/routes/docker/containers.go +++ b/internal/server/routes/docker/containers.go @@ -58,6 +58,20 @@ func ContainerCreate(cr *common.ContextRouter, c *gin.Context) { } in.Labels[types.LabelServiceAccount] = cr.Config.ServiceAccount + mounts := []types.Mount{} + for _, m := range in.HostConfig.Mounts { + if m.Type != "bind" { + klog.Infof("mount '%s:%s' with type '%s' not supported, ignoring", m.Source, m.Target, m.Type) + continue + } + mounts = append(mounts, types.Mount{ + Type: m.Type, + Source: m.Source, + Target: m.Target, + ReadOnly: m.ReadOnly, + }) + } + tainr := &types.Container{ Name: in.Name, Image: in.Image, @@ -68,6 +82,7 @@ func ContainerCreate(cr *common.ContextRouter, c *gin.Context) { ImagePorts: map[string]interface{}{}, Labels: in.Labels, Binds: in.HostConfig.Binds, + Mounts: mounts, PreArchives: []types.PreArchive{}, } @@ -226,6 +241,15 @@ func getContainerInfo(cr *common.ContextRouter, tainr *types.Container, detail b "IPAddress": "127.0.0.1", } } + mounts := []gin.H{} + for _, m := range tainr.Mounts { + mounts = append(mounts, gin.H{ + "Source": m.Source, + "Target": m.Target, + "Type": m.Type, + "ReadOnly": m.ReadOnly, + }) + } res := gin.H{ "Id": tainr.ID, "Name": "/" + tainr.Name, @@ -242,6 +266,7 @@ func getContainerInfo(cr *common.ContextRouter, tainr *types.Container, detail b "Type": "json-file", "Config": gin.H{}, }, + "Mounts": mounts, }, } if detail { diff --git a/internal/server/routes/docker/types.go b/internal/server/routes/docker/types.go index a3f02b0..3327774 100644 --- a/internal/server/routes/docker/types.go +++ b/internal/server/routes/docker/types.go @@ -38,6 +38,7 @@ type NetworkDisconnectRequest struct { // HostConfig contains to be mounted files from the host system. type HostConfig struct { Binds []string `json:"Binds"` + Mounts []Mount `json:"Mounts"` PortBindings map[string][]PortBinding Memory int `json:"Memory"` NanoCpus int `json:"NanoCpus"` @@ -63,3 +64,11 @@ type EndpointConfig struct { Aliases []string `json:"Aliases"` NetworkID string `json:"NetworkID"` } + +// Mount contains information about mounted volumes/bindings +type Mount struct { + Type string `json:"Type"` + Source string `json:"Source"` + Target string `json:"Target"` + ReadOnly bool `json:"ReadOnly"` +}