Skip to content

Commit b107d77

Browse files
Merge pull request #16732 from flouthoc/network-update
network: add support for `podman network update` and `--network-dns-server`
2 parents 5cb4fe3 + 882cd17 commit b107d77

File tree

16 files changed

+320
-16
lines changed

16 files changed

+320
-16
lines changed

cmd/podman/networks/create.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ func networkCreateFlags(cmd *cobra.Command) {
8080
flags.BoolVar(&networkCreateOptions.DisableDNS, "disable-dns", false, "disable dns plugin")
8181

8282
flags.BoolVar(&networkCreateOptions.IgnoreIfExists, "ignore", false, "Don't fail if network already exists")
83+
dnsserverFlagName := "dns"
84+
flags.StringArrayVar(&networkCreateOptions.NetworkDNSServers, dnsserverFlagName, nil, "DNS servers this network will use")
85+
_ = cmd.RegisterFlagCompletionFunc(dnsserverFlagName, completion.AutocompleteNone)
8386
}
8487
func init() {
8588
registry.Commands = append(registry.Commands, registry.CliCommand{
@@ -107,13 +110,14 @@ func networkCreate(cmd *cobra.Command, args []string) error {
107110
}
108111

109112
network := types.Network{
110-
Name: name,
111-
Driver: networkCreateOptions.Driver,
112-
Options: networkCreateOptions.Options,
113-
Labels: networkCreateOptions.Labels,
114-
IPv6Enabled: networkCreateOptions.IPv6,
115-
DNSEnabled: !networkCreateOptions.DisableDNS,
116-
Internal: networkCreateOptions.Internal,
113+
Name: name,
114+
Driver: networkCreateOptions.Driver,
115+
Options: networkCreateOptions.Options,
116+
Labels: networkCreateOptions.Labels,
117+
IPv6Enabled: networkCreateOptions.IPv6,
118+
DNSEnabled: !networkCreateOptions.DisableDNS,
119+
NetworkDNSServers: networkCreateOptions.NetworkDNSServers,
120+
Internal: networkCreateOptions.Internal,
117121
}
118122

119123
if cmd.Flags().Changed(ipamDriverFlagName) {

cmd/podman/networks/update.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package network
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/containers/common/pkg/completion"
7+
"github.com/containers/podman/v4/cmd/podman/common"
8+
"github.com/containers/podman/v4/cmd/podman/registry"
9+
"github.com/containers/podman/v4/pkg/domain/entities"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var (
14+
networkUpdateDescription = `Update an existing podman network`
15+
networkUpdateCommand = &cobra.Command{
16+
Use: "update [options] NETWORK",
17+
Short: "update an existing podman network",
18+
Long: networkUpdateDescription,
19+
RunE: networkUpdate,
20+
Args: cobra.ExactArgs(1),
21+
ValidArgsFunction: common.AutocompleteNetworks,
22+
Example: `podman network update podman1`,
23+
}
24+
)
25+
26+
var (
27+
networkUpdateOptions entities.NetworkUpdateOptions
28+
)
29+
30+
func networkUpdateFlags(cmd *cobra.Command) {
31+
flags := cmd.Flags()
32+
33+
addDNSServerFlagName := "dns-add"
34+
flags.StringArrayVar(&networkUpdateOptions.AddDNSServers, addDNSServerFlagName, nil, "add network level nameservers")
35+
removeDNSServerFlagName := "dns-drop"
36+
flags.StringArrayVar(&networkUpdateOptions.RemoveDNSServers, removeDNSServerFlagName, nil, "remove network level nameservers")
37+
_ = cmd.RegisterFlagCompletionFunc(addDNSServerFlagName, completion.AutocompleteNone)
38+
_ = cmd.RegisterFlagCompletionFunc(removeDNSServerFlagName, completion.AutocompleteNone)
39+
}
40+
func init() {
41+
registry.Commands = append(registry.Commands, registry.CliCommand{
42+
Command: networkUpdateCommand,
43+
Parent: networkCmd,
44+
})
45+
networkUpdateFlags(networkUpdateCommand)
46+
}
47+
48+
func networkUpdate(cmd *cobra.Command, args []string) error {
49+
name := args[0]
50+
51+
err := registry.ContainerEngine().NetworkUpdate(registry.Context(), name, networkUpdateOptions)
52+
if err != nil {
53+
return err
54+
}
55+
fmt.Println(name)
56+
return nil
57+
}

docs/source/markdown/podman-network-create.1.md

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ release because it is used as a special network mode in **podman run/create --ne
2424
Disables the DNS plugin for this network which if enabled, can perform container to container name
2525
resolution.
2626

27+
#### **--dns**=*ip*
28+
29+
Set network-scoped DNS resolver/nameserver for containers in this network. If not set, the host servers from `/etc/resolv.conf` will be used. It can be overwritten on the container level with the `podman run/create --dns` option. This option can be specified multiple times to set more than one IP.
30+
2731
#### **--driver**, **-d**
2832

2933
Driver to manage the network. Currently `bridge`, `macvlan` and `ipvlan` are supported. Defaults to `bridge`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
% podman-network-update 1
2+
3+
## NAME
4+
podman\-network\-update - Update an existing Podman network
5+
6+
## SYNOPSIS
7+
**podman network update** [*options*] *network*
8+
9+
## DESCRIPTION
10+
Allow changes to existing container networks. At present, only changes to the DNS servers in use by a network is supported.
11+
12+
NOTE: Only supported with the netavark network backend.
13+
14+
15+
## OPTIONS
16+
#### **--dns-add**
17+
18+
Accepts array of DNS resolvers and add it to the existing list of resolvers configured for a network.
19+
20+
#### **--dns-drop**
21+
22+
Accepts array of DNS resolvers and removes them from the existing list of resolvers configured for a network.
23+
24+
## EXAMPLE
25+
26+
Update a network
27+
```
28+
$ podman network update network1 --dns-add 8.8.8.8,1.1.1.1
29+
```
30+
31+
Update a network and add/remove dns servers
32+
```
33+
$ podman network update network1 --dns-drop 8.8.8.8 --dns-add 3.3.3.3
34+
```
35+
## SEE ALSO
36+
**[podman(1)](podman.1.md)**, **[podman-network(1)](podman-network.1.md)**, **[podman-network-inspect(1)](podman-network-inspect.1.md)**, **[podman-network-ls(1)](podman-network-ls.1.md)**

docs/source/markdown/podman-network.1.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ so networks have to be created again after a backend change.
3232
| prune | [podman-network-prune(1)](podman-network-prune.1.md) | Remove all unused networks |
3333
| reload | [podman-network-reload(1)](podman-network-reload.1.md) | Reload network configuration for containers |
3434
| rm | [podman-network-rm(1)](podman-network-rm.1.md) | Remove one or more networks |
35+
| update | [podman-network-upate(1)](podman-network-update.1.md) | Update an existing Podman network |
3536

3637
## SEE ALSO
3738
**[podman(1)](podman.1.md)**, **[containers.conf(5)](https://github.com/containers/common/blob/main/docs/containers.conf.5.md)**

pkg/api/handlers/libpod/networks.go

+20
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ func CreateNetwork(w http.ResponseWriter, r *http.Request) {
5353
utils.WriteResponse(w, http.StatusOK, report)
5454
}
5555

56+
func UpdateNetwork(w http.ResponseWriter, r *http.Request) {
57+
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
58+
ic := abi.ContainerEngine{Libpod: runtime}
59+
60+
networkUpdateOptions := entities.NetworkUpdateOptions{}
61+
if err := json.NewDecoder(r.Body).Decode(&networkUpdateOptions); err != nil {
62+
utils.Error(w, http.StatusBadRequest, fmt.Errorf("failed to decode request JSON payload: %w", err))
63+
return
64+
}
65+
66+
name := utils.GetName(r)
67+
68+
err := ic.NetworkUpdate(r.Context(), name, networkUpdateOptions)
69+
if err != nil {
70+
utils.Error(w, http.StatusInternalServerError, err)
71+
}
72+
73+
utils.WriteResponse(w, http.StatusNoContent, nil)
74+
}
75+
5676
func ListNetworks(w http.ResponseWriter, r *http.Request) {
5777
if v, err := utils.SupportedVersion(r, ">=4.0.0"); err != nil {
5878
utils.BadRequest(w, "version", v.String(), err)

pkg/api/handlers/swagger/models.go

+4
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ type networkDisconnectRequest types.NetworkDisconnect
4444
// Network connect
4545
// swagger:model
4646
type networkConnectRequestLibpod entities.NetworkConnectOptions
47+
48+
// Network update
49+
// swagger:model
50+
type networkUpdateRequestLibpod entities.NetworkUpdateOptions

pkg/api/server/register_networks.go

+27
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,33 @@ func (s *APIServer) registerNetworkHandlers(r *mux.Router) error {
234234
// 500:
235235
// $ref: "#/responses/internalError"
236236
r.HandleFunc(VersionedPath("/libpod/networks/{name}"), s.APIHandler(libpod.RemoveNetwork)).Methods(http.MethodDelete)
237+
// swagger:operation POST /libpod/networks/{name}/update libpod NetworkUpdateLibpod
238+
// ---
239+
// tags:
240+
// - networks
241+
// summary: Update exisiting podman network
242+
// description: Update exisiting podman network
243+
// produces:
244+
// - application/json
245+
// parameters:
246+
// - in: path
247+
// name: name
248+
// type: string
249+
// required: true
250+
// description: the name or ID of the network
251+
// - in: body
252+
// name: update
253+
// description: attributes for updating a netavark network
254+
// schema:
255+
// $ref: "#/definitions/networkUpdateRequestLibpod"
256+
// responses:
257+
// 200:
258+
// description: OK
259+
// 400:
260+
// $ref: "#/responses/badParamError"
261+
// 500:
262+
// $ref: "#/responses/internalError"
263+
r.HandleFunc(VersionedPath("/libpod/networks/{name}/update"), s.APIHandler(libpod.UpdateNetwork)).Methods(http.MethodPost)
237264
// swagger:operation GET /libpod/networks/{name}/exists libpod NetworkExistsLibpod
238265
// ---
239266
// tags:

pkg/bindings/network/network.go

+19
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ func CreateWithOptions(ctx context.Context, network *types.Network, extraCreateO
5050
return report, response.Process(&report)
5151
}
5252

53+
// Updates an existing netavark network config
54+
func Update(ctx context.Context, netNameOrID string, options *UpdateOptions) error {
55+
conn, err := bindings.GetClient(ctx)
56+
if err != nil {
57+
return err
58+
}
59+
networkConfig, err := jsoniter.MarshalToString(options)
60+
if err != nil {
61+
return err
62+
}
63+
reader := strings.NewReader(networkConfig)
64+
response, err := conn.DoRequest(ctx, reader, http.MethodPost, "/networks/%s/update", nil, nil, netNameOrID)
65+
if err != nil {
66+
return err
67+
}
68+
defer response.Body.Close()
69+
return response.Process(nil)
70+
}
71+
5372
// Inspect returns information about a network configuration
5473
func Inspect(ctx context.Context, nameOrID string, _ *InspectOptions) (types.Network, error) {
5574
var net types.Network

pkg/bindings/network/types.go

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ type ListOptions struct {
5858
Filters map[string][]string
5959
}
6060

61+
// NetworkUpdateOptions describes options to update a network
62+
//
63+
//go:generate go run ../generator/generator.go UpdateOptions
64+
type UpdateOptions struct {
65+
AddDNSServers []string `json:"adddnsservers"`
66+
RemoveDNSServers []string `json:"removednsservers"`
67+
}
68+
6169
// DisconnectOptions are optional options for disconnecting
6270
// containers from a network
6371
//

pkg/bindings/network/types_update_options.go

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/domain/entities/engine_container.go

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type ContainerEngine interface { //nolint:interfacebloat
6464
KubeApply(ctx context.Context, body io.Reader, opts ApplyOptions) error
6565
NetworkConnect(ctx context.Context, networkname string, options NetworkConnectOptions) error
6666
NetworkCreate(ctx context.Context, network types.Network, createOptions *types.NetworkCreateOptions) (*types.Network, error)
67+
NetworkUpdate(ctx context.Context, networkname string, options NetworkUpdateOptions) error
6768
NetworkDisconnect(ctx context.Context, networkname string, options NetworkDisconnectOptions) error
6869
NetworkExists(ctx context.Context, networkname string) (*BoolReport, error)
6970
NetworkInspect(ctx context.Context, namesOrIds []string, options InspectOptions) ([]types.Network, []error, error)

pkg/domain/entities/network.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,28 @@ type NetworkRmReport struct {
4141

4242
// NetworkCreateOptions describes options to create a network
4343
type NetworkCreateOptions struct {
44-
DisableDNS bool
45-
Driver string
46-
Gateways []net.IP
47-
Internal bool
48-
Labels map[string]string
49-
MacVLAN string
50-
Ranges []string
51-
Subnets []string
52-
IPv6 bool
44+
DisableDNS bool
45+
Driver string
46+
Gateways []net.IP
47+
Internal bool
48+
Labels map[string]string
49+
MacVLAN string
50+
NetworkDNSServers []string
51+
Ranges []string
52+
Subnets []string
53+
IPv6 bool
5354
// Mapping of driver options and values.
5455
Options map[string]string
5556
// IgnoreIfExists if true, do not fail if the network already exists
5657
IgnoreIfExists bool
5758
}
5859

60+
// NetworkUpdateOptions describes options to update a network
61+
type NetworkUpdateOptions struct {
62+
AddDNSServers []string `json:"adddnsservers"`
63+
RemoveDNSServers []string `json:"removednsservers"`
64+
}
65+
5966
// NetworkCreateReport describes a created network for the cli
6067
type NetworkCreateReport struct {
6168
Name string

pkg/domain/infra/abi/network.go

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ import (
1313
"github.com/containers/podman/v4/pkg/domain/entities"
1414
)
1515

16+
func (ic *ContainerEngine) NetworkUpdate(ctx context.Context, netName string, options entities.NetworkUpdateOptions) error {
17+
var networkUpdateOptions types.NetworkUpdateOptions
18+
networkUpdateOptions.AddDNSServers = options.AddDNSServers
19+
networkUpdateOptions.RemoveDNSServers = options.RemoveDNSServers
20+
err := ic.Libpod.Network().NetworkUpdate(netName, networkUpdateOptions)
21+
if err != nil {
22+
return err
23+
}
24+
return nil
25+
}
26+
1627
func (ic *ContainerEngine) NetworkList(ctx context.Context, options entities.NetworkListOptions) ([]types.Network, error) {
1728
// dangling filter is not provided by netutil
1829
var wantDangling bool

pkg/domain/infra/tunnel/network.go

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import (
1212
"github.com/containers/podman/v4/pkg/errorhandling"
1313
)
1414

15+
func (ic *ContainerEngine) NetworkUpdate(ctx context.Context, netName string, opts entities.NetworkUpdateOptions) error {
16+
options := new(network.UpdateOptions).WithAddDNSServers(opts.AddDNSServers).WithRemoveDNSServers(opts.RemoveDNSServers)
17+
return network.Update(ic.ClientCtx, netName, options)
18+
}
19+
1520
func (ic *ContainerEngine) NetworkList(ctx context.Context, opts entities.NetworkListOptions) ([]types.Network, error) {
1621
options := new(network.ListOptions).WithFilters(opts.Filters)
1722
return network.List(ic.ClientCtx, options)

0 commit comments

Comments
 (0)