network

package module
v0.1.0-alpha003 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2025 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cleanup

func Cleanup(tb testing.TB, nw TerminableNetwork)

Cleanup is a helper function that schedules the network to be removed when the test ends. This should be the first call after New in a test before any error check. If network is nil, it's a no-op.

func CleanupByID

func CleanupByID(tb testing.TB, id string)

CleanupByID is a helper function that schedules the network to be removed, identified by its ID, when the test ends. This should be the first call after NewNetwork(...) in a test before any error check. If network is nil, it's a no-op. It uses a new docker client to terminate the network, which is automatically closed when the test ends.

func Version

func Version() string

Version returns the version of the network package.

Types

type InspectOptions

type InspectOptions func(opts *inspectOptions) error

InspectOptions is a function that modifies the inspect options.

func WithInspectOptions

func WithInspectOptions(opts network.InspectOptions) InspectOptions

WithInspectOptions returns an InspectOptions that sets the inspect options.

func WithNoCache

func WithNoCache() InspectOptions

WithNoCache returns an InspectOptions that disables caching the result of the inspection. If passed, the Docker daemon will be queried for the latest information, so it can be used for refreshing the cached result of a previous inspection.

type Network

type Network struct {
	// contains filtered or unexported fields
}

Network represents a Docker network.

func New

func New(ctx context.Context, opts ...Option) (*Network, error)

New creates a new network.

Example
package main

import (
	"context"
	"fmt"

	"github.com/docker/go-sdk/network"
)

func main() {
	nw, err := network.New(context.Background())
	fmt.Println(err)
	fmt.Println(nw.Name() != "")

	err = nw.Terminate(context.Background())
	fmt.Println(err)

}
Output:
<nil>
true
<nil>
Example (WithClient)
package main

import (
	"context"
	"fmt"

	"github.com/docker/go-sdk/client"
	"github.com/docker/go-sdk/network"
)

func main() {
	dockerClient, err := client.New(context.Background())
	fmt.Println(err)

	nw, err := network.New(context.Background(), network.WithClient(dockerClient))
	fmt.Println(err)
	fmt.Println(nw.Name() != "")

	err = nw.Terminate(context.Background())
	fmt.Println(err)

}
Output:
<nil>
<nil>
true
<nil>
Example (WithOptions)
package main

import (
	"context"
	"fmt"
	"runtime"

	"github.com/docker/go-sdk/network"
)

func main() {
	name := "test-network"

	driver := "bridge"
	if runtime.GOOS == "windows" {
		driver = "nat"
	}

	nw, err := network.New(
		context.Background(),
		network.WithName(name),
		network.WithDriver(driver),
		network.WithLabels(map[string]string{"test": "test"}),
		network.WithAttachable(),
	)
	fmt.Println(err)

	fmt.Println(nw.Name())
	fmt.Println(nw.Driver() != "")

	err = nw.Terminate(context.Background())
	fmt.Println(err)

}
Output:
<nil>
test-network
true
<nil>

func (*Network) Driver

func (n *Network) Driver() string

Driver returns the driver of the network.

func (*Network) ID

func (n *Network) ID() string

ID returns the ID of the network.

func (*Network) Inspect

func (n *Network) Inspect(ctx context.Context, opts ...InspectOptions) (network.Inspect, error)

Inspect inspects the network, caching the results.

Example
package main

import (
	"context"
	"fmt"

	"github.com/docker/go-sdk/network"
)

func main() {
	name := "test-network-inspect"
	nw, err := network.New(context.Background(), network.WithName(name))
	fmt.Println(err)

	inspect, err := nw.Inspect(context.Background())
	fmt.Println(err)
	fmt.Println(inspect.Name)

	err = nw.Terminate(context.Background())
	fmt.Println(err)

}
Output:
<nil>
<nil>
test-network-inspect
<nil>
Example (WithOptions)
package main

import (
	"context"
	"fmt"

	apinetwork "github.com/docker/docker/api/types/network"
	"github.com/docker/go-sdk/network"
)

func main() {
	name := "test-network-inspect-options"
	nw, err := network.New(context.Background(), network.WithName(name))
	fmt.Println(err)

	inspect, err := nw.Inspect(
		context.Background(),
		network.WithNoCache(),
		network.WithInspectOptions(apinetwork.InspectOptions{
			Verbose: true,
			Scope:   "local",
		}),
	)
	fmt.Println(err)
	fmt.Println(inspect.Name)

	err = nw.Terminate(context.Background())
	fmt.Println(err)

}
Output:
<nil>
<nil>
test-network-inspect-options
<nil>

func (*Network) Name

func (n *Network) Name() string

Name returns the name of the network.

func (*Network) Terminate

func (n *Network) Terminate(ctx context.Context) error

Terminate is used to remove the network. It is usually triggered by as defer function.

Example
package main

import (
	"context"
	"fmt"

	"github.com/docker/go-sdk/network"
)

func main() {
	nw, err := network.New(context.Background())
	fmt.Println(err)

	err = nw.Terminate(context.Background())
	fmt.Println(err)

}
Output:
<nil>
<nil>

type Option

type Option func(*options) error

Option is a function that modifies the options to create a network.

func WithAttachable

func WithAttachable() Option

WithAttachable makes the network attachable.

func WithClient

func WithClient(client *client.Client) Option

WithClient sets the docker client.

func WithDriver

func WithDriver(driver string) Option

WithDriver sets the driver of the network.

func WithEnableIPv6

func WithEnableIPv6() Option

WithEnableIPv6 enables IPv6 on the network.

func WithIPAM

func WithIPAM(ipam *network.IPAM) Option

WithIPAM sets the IPAM of the network.

func WithInternal

func WithInternal() Option

WithInternal makes the network internal.

func WithLabels

func WithLabels(labels map[string]string) Option

WithLabels sets the labels of the network.

func WithName

func WithName(name string) Option

WithName sets the name of the network.

type TerminableNetwork

type TerminableNetwork interface {
	Terminate(ctx context.Context) error
}

TerminableNetwork is a network that can be terminated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL