aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/networks/reload.go
blob: 66248e9fb34195d8560ef6713e015dac93d2de10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package network

import (
	"fmt"

	"github.com/containers/podman/v4/cmd/podman/common"
	"github.com/containers/podman/v4/cmd/podman/registry"
	"github.com/containers/podman/v4/cmd/podman/utils"
	"github.com/containers/podman/v4/cmd/podman/validate"
	"github.com/containers/podman/v4/pkg/domain/entities"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

var (
	networkReloadDescription = `reload container networks, recreating firewall rules`
	networkReloadCommand     = &cobra.Command{
		Annotations: map[string]string{registry.EngineMode: registry.ABIMode},
		Use:         "reload [options] [CONTAINER...]",
		Short:       "Reload firewall rules for one or more containers",
		Long:        networkReloadDescription,
		RunE:        networkReload,
		Args: func(cmd *cobra.Command, args []string) error {
			return validate.CheckAllLatestAndIDFile(cmd, args, false, "")
		},
		ValidArgsFunction: common.AutocompleteContainers,
		Example: `podman network reload --latest
  podman network reload 3c13ef6dd843
  podman network reload test1 test2`,
	}
)

var (
	reloadOptions entities.NetworkReloadOptions
)

func reloadFlags(flags *pflag.FlagSet) {
	flags.BoolVarP(&reloadOptions.All, "all", "a", false, "Reload network configuration of all containers")
}

func init() {
	registry.Commands = append(registry.Commands, registry.CliCommand{
		Command: networkReloadCommand,
		Parent:  networkCmd,
	})
	reloadFlags(networkReloadCommand.Flags())
	validate.AddLatestFlag(networkReloadCommand, &reloadOptions.Latest)
}

func networkReload(cmd *cobra.Command, args []string) error {
	responses, err := registry.ContainerEngine().NetworkReload(registry.Context(), args, reloadOptions)
	if err != nil {
		return err
	}

	var errs utils.OutputErrors
	for _, r := range responses {
		if r.Err == nil {
			fmt.Println(r.Id)
		} else {
			errs = append(errs, r.Err)
		}
	}

	return errs.PrintErrors()
}