Skip to content

Commit

Permalink
Add support for mounts in hostconfig #30 #68
Browse files Browse the repository at this point in the history
  • Loading branch information
joyrex2001 committed Jan 17, 2024
1 parent 8521b93 commit 83cc8dc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
12 changes: 12 additions & 0 deletions internal/model/types/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Container struct {
Cmd []string
Env []string
Binds []string
Mounts []Mount
PreArchives []PreArchive
HostIP string
ExposedPorts map[string]interface{}
Expand All @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
19 changes: 14 additions & 5 deletions internal/model/types/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions internal/server/routes/docker/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{},
}

Expand Down Expand Up @@ -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,
Expand All @@ -242,6 +266,7 @@ func getContainerInfo(cr *common.ContextRouter, tainr *types.Container, detail b
"Type": "json-file",
"Config": gin.H{},
},
"Mounts": mounts,
},
}
if detail {
Expand Down
9 changes: 9 additions & 0 deletions internal/server/routes/docker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
}

0 comments on commit 83cc8dc

Please sign in to comment.