Establishing a Network with PouchContainer

How PouchContainer Establishes a Network




Embedded Network Modes of PouchContainer

Bridge Mode

Host Mode

Container Mode

None Mode

Relationship between the CNM and Network Mode

Source Code for Network Connect Function

// Connect is used to connect a container to a network.func (mgr *ContainerManager) Connect(ctx context.Context, name string, networkIDOrName string, epConfig *types.EndpointSettings) error {    c, err := mgr.container(name)    ⋯⋯    n, err := mgr.NetworkMgr.Get(context.Background(), networkIDOrName)    ⋯⋯    if c.State.Status != types.StatusRunning {        if c.State.Status == types.StatusDead {            ⋯⋯        }        if err := mgr.updateNetworkConfig(c, n.Name, epConfig); err != nil {            return err        }    } else if err := mgr.connectToNetwork(ctx, c, networkIDOrName, epConfig); err != nil {        return err    }    return c.Write(mgr.Store)}
func (mgr *ContainerManager) connectToNetwork(ctx context.Context, container *Container, networkIDOrName string, epConfig *types.EndpointSettings) (err error) {    ⋯⋯    endpoint := mgr.buildContainerEndpoint(container)

endpoint.Name = network.Name
endpoint.EndpointConfig = epConfig if _, err := mgr.NetworkMgr.EndpointCreate(ctx, endpoint); err != nil { ⋯⋯ } return mgr.updateNetworkConfig(container, networkIDOrName, endpoint.EndpointConfig)}
// EndpointCreate is used to create network endpoint.func (nm *NetworkManager) EndpointCreate(ctx context.Context, endpoint *types.Endpoint) (string, error) {    ⋯⋯    // create endpoint    epOptions, err := endpointOptions(n, endpoint)    if err != nil {        return "", err    }    endpointName := containerID[:8]    ep, err := n.CreateEndpoint(endpointName, epOptions...)    if err != nil {        return "", err    }         ⋯⋯    // create sandbox    sb := nm.getNetworkSandbox(containerID)    if sb == nil {        sandboxOptions, err := buildSandboxOptions(nm.config, endpoint)        ⋯⋯        sb, err = nm.controller.NewSandbox(containerID, sandboxOptions...)        ⋯⋯    }    // endpoint joins into sandbox    joinOptions, err := joinOptions(endpoint)    ⋯⋯    if err := ep.Join(sb, joinOptions...); err != nil {        return "", fmt.Errorf("failed to join sandbox(%v)", err)    }    // update endpoint settings    epInfo := ep.Info()    if epInfo.Gateway() != nil {        endpointConfig.Gateway = epInfo.Gateway().String()    }    if epInfo.GatewayIPv6().To16() != nil {        endpointConfig.IPV6Gateway = epInfo.GatewayIPv6().String()    }    endpoint.ID = ep.ID()    endpointConfig.EndpointID = ep.ID()    endpointConfig.NetworkID = n.ID()    iface := epInfo.Iface()    if iface != nil {        if iface.Address() != nil {            mask, _ := iface.Address().Mask.Size()            endpointConfig.IPPrefixLen = int64(mask)            endpointConfig.IPAddress = iface.Address().IP.String()        }        if iface.MacAddress() != nil {            endpointConfig.MacAddress = iface.MacAddress().String()        }    }    return endpointName, nil}





Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Smart UI … adding more sprinkles

Boundary Traversal

Authenticating GitHub Organizations with SSO Enabled

Continuous Evolution and Development of Data Warehouse Architecture

A Small Introduction to Django

The Power of Using a Domain Model for Time in .NET

IT Network Fixes Every Small Business Owner Should Know

My Story:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:

More from Medium

Quickly And Efficiently Assessing Application Logs

Adding/Replacing Cassandra Nodes, You might wanna cleanup

Itchy guy and his quest for Zen

How to use Kubeflow and the MPI Operator on OpenShift