Skip to content

Commit

Permalink
feature: add container's network files
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Li <lcy041536@gmail.com>
  • Loading branch information
shaloulcy committed May 25, 2018
1 parent 76c9e6f commit e70ef26
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 18 deletions.
3 changes: 3 additions & 0 deletions cri/v1alpha1/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ func applySandboxSecurityContext(lc *runtime.LinuxPodSandboxConfig, config *apit

// applySandboxLinuxOptions applies LinuxPodSandboxConfig to pouch's HostConfig and ContainerCreateConfig.
func applySandboxLinuxOptions(hc *apitypes.HostConfig, lc *runtime.LinuxPodSandboxConfig, createConfig *apitypes.ContainerCreateConfig, image string) error {
// apply the sandbox network_mode, "none" is default.
hc.NetworkMode = namespaceModeNone

if lc == nil {
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ func applySandboxSecurityContext(lc *runtime.LinuxPodSandboxConfig, config *apit

// applySandboxLinuxOptions applies LinuxPodSandboxConfig to pouch's HostConfig and ContainerCreateConfig.
func applySandboxLinuxOptions(hc *apitypes.HostConfig, lc *runtime.LinuxPodSandboxConfig, createConfig *apitypes.ContainerCreateConfig, image string) error {
// apply the sandbox network_mode, "none" is default.
hc.NetworkMode = namespaceModeNone

if lc == nil {
return nil
}
Expand Down
60 changes: 42 additions & 18 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
return nil, errors.Wrap(errtypes.ErrAlreadyExisted, "container name: "+name)
}

// set hostname.
if config.Hostname.String() == "" {
config.Hostname = strfmt.Hostname(id)
}

// set container runtime
if config.HostConfig.Runtime == "" {
config.HostConfig.Runtime = mgr.Config.DefaultRuntime
Expand Down Expand Up @@ -322,13 +327,12 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
networkMode := config.HostConfig.NetworkMode
if networkMode == "" {
config.HostConfig.NetworkMode = "bridge"
container.Config.NetworkDisabled = true
}
container.NetworkSettings = new(types.NetworkSettings)
if len(config.NetworkingConfig.EndpointsConfig) > 0 {
container.NetworkSettings.Networks = config.NetworkingConfig.EndpointsConfig
}
if container.NetworkSettings.Networks == nil && networkMode != "" && !IsContainer(networkMode) {
if container.NetworkSettings.Networks == nil && !IsContainer(config.HostConfig.NetworkMode) {
container.NetworkSettings.Networks = make(map[string]*types.EndpointSettings)
container.NetworkSettings.Networks[config.HostConfig.NetworkMode] = new(types.EndpointSettings)
}
Expand Down Expand Up @@ -454,33 +458,53 @@ func (mgr *ContainerManager) start(ctx context.Context, c *Container, detachKeys
c.ResolvConfPath = origContainer.ResolvConfPath
c.Config.Hostname = origContainer.Config.Hostname
c.Config.Domainname = origContainer.Config.Domainname
}
} else {
// initialise host network mode
if IsHost(networkMode) {
hostname, err := os.Hostname()
if err != nil {
return err
}
c.Config.Hostname = strfmt.Hostname(hostname)
}

// initialise host network mode
if IsHost(networkMode) {
hostname, err := os.Hostname()
if err != nil {
// build the network related path.
if err := mgr.buildNetworkRelatedPath(c); err != nil {
return err
}
c.Config.Hostname = strfmt.Hostname(hostname)
}

// initialise network endpoint
if c.NetworkSettings != nil {
for name, endpointSetting := range c.NetworkSettings.Networks {
endpoint := mgr.buildContainerEndpoint(c)
endpoint.Name = name
endpoint.EndpointConfig = endpointSetting
if _, err := mgr.NetworkMgr.EndpointCreate(ctx, endpoint); err != nil {
logrus.Errorf("failed to create endpoint: %v", err)
return err
// initialise network endpoint
if c.NetworkSettings != nil {
for name, endpointSetting := range c.NetworkSettings.Networks {
endpoint := mgr.buildContainerEndpoint(c)
endpoint.Name = name
endpoint.EndpointConfig = endpointSetting
if _, err := mgr.NetworkMgr.EndpointCreate(ctx, endpoint); err != nil {
logrus.Errorf("failed to create endpoint: %v", err)
return err
}
}
}
}

return mgr.createContainerdContainer(ctx, c)
}

// buildNetworkRelatedPath build the network related path.
func (mgr *ContainerManager) buildNetworkRelatedPath(c *Container) error {
// set the hosts file path.
c.HostsPath = path.Join(mgr.Store.Path(c.ID), "hosts")

// set the resolv.conf file path.
c.ResolvConfPath = path.Join(mgr.Store.Path(c.ID), "resolv.conf")

// set the hostname file path.
c.HostnamePath = path.Join(mgr.Store.Path(c.ID), "hostname")

// write the hostname file, other files are filled by libnetwork.
return ioutil.WriteFile(c.HostnamePath, []byte(c.Config.Hostname+"\n"), 0644)
}

func (mgr *ContainerManager) createContainerdContainer(ctx context.Context, c *Container) error {
// CgroupParent from HostConfig will be first priority to use,
// then will be value from mgr.Config.CgroupParent
Expand Down
83 changes: 83 additions & 0 deletions daemon/mgr/spec_mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package mgr
import (
"context"
"fmt"
"os"

"github.com/alibaba/pouch/apis/types"

specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
)

func clearReadonly(m *specs.Mount) {
Expand All @@ -31,6 +35,11 @@ func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error {
return nil
}
for _, mp := range c.Mounts {
if isSetupNetworkMount(mp, c) {
// ignore the network mount, we will handle it later.
continue
}

// check duplicate mountpoint
for _, sm := range mounts {
if sm.Destination == mp.Destination {
Expand Down Expand Up @@ -69,6 +78,10 @@ func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error {
Options: opts,
})
}

// generate network mounts.
mounts = append(mounts, generateNetworkMounts(c)...)

s.Mounts = mounts

if c.HostConfig.Privileged {
Expand All @@ -83,3 +96,73 @@ func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error {
}
return nil
}

// generateNetworkMounts will generate network mounts.
func generateNetworkMounts(c *Container) []specs.Mount {
mounts := make([]specs.Mount, 0)

if c.HostnamePath != "" {
_, err := os.Stat(c.HostnamePath)
if err != nil {
logrus.Warnf("HostnamePath set to %v, but stat error: %v, skip it", c.HostnamePath, err)
} else {
mounts = append(mounts, specs.Mount{
Source: c.HostnamePath,
Destination: "/etc/hostname",
Type: "bind",
Options: []string{"rbind", "rprivate"},
})
}
}

if c.HostsPath != "" {
_, err := os.Stat(c.HostsPath)
if err != nil {
logrus.Warnf("HostsPath set to %v, but stat error: %v, skip it", c.HostsPath, err)
} else {
mounts = append(mounts, specs.Mount{
Source: c.HostsPath,
Destination: "/etc/hosts",
Type: "bind",
Options: []string{"rbind", "rprivate"},
})
}
}

if c.ResolvConfPath != "" {
_, err := os.Stat(c.ResolvConfPath)
if err != nil {
logrus.Warnf("ResolvConfPath set to %v, but stat error: %v, skip it", c.ResolvConfPath, err)
} else {
mounts = append(mounts, specs.Mount{
Source: c.ResolvConfPath,
Destination: "/etc/resolv.conf",
Type: "bind",
Options: []string{"rbind", "rprivate"},
})
}

}

return mounts
}

// isSetupNetworkMount checks whether set network mount.
func isSetupNetworkMount(mount *types.MountPoint, c *Container) bool {
if mount.Destination == "/etc/hostname" {
c.HostnamePath = mount.Source
return true
}

if mount.Destination == "/etc/hosts" {
c.HostsPath = mount.Source
return true
}

if mount.Destination == "/etc/resolv.conf" {
c.ResolvConfPath = mount.Source
return true
}

return false
}

0 comments on commit e70ef26

Please sign in to comment.