Repository: moby/moby
File: libnetwork/controller_linux.go (Go)

Code before:
package libnetwork

import (
	"context"
	"fmt"
	"sync"

	"github.com/containerd/log"
	"github.com/docker/docker/api/types/system"
	"github.com/docker/docker/libnetwork/iptables"
	"github.com/docker/docker/libnetwork/netlabel"
	"github.com/docker/docker/libnetwork/options"
	"github.com/docker/docker/libnetwork/osl"
)

// FirewallBackend returns the name of the firewall backend for "docker info".
func (c *Controller) FirewallBackend() *system.FirewallInfo {
	usingFirewalld, err := iptables.UsingFirewalld()
	if err != nil {
		return nil
	}
	if usingFirewalld {
		info := &system.FirewallInfo{Driver: "iptables+firewalld"}
		reloadedAt := iptables.FirewalldReloadedAt()
		if !reloadedAt.IsZero() {
			info.Info = append(info.Info, [2]string{"ReloadedAt", reloadedAt.String()})
		}
		return info
	}
	return &system.FirewallInfo{Driver: "iptables"}
}

// enabledIptablesVersions returns the iptables versions that are enabled
// for the controller.
func (c *Controller) enabledIptablesVersions() []iptables.IPVersion {
	c.mu.Lock()
	defer c.mu.Unlock()
	if c.cfg == nil {
		return nil
	}
	// parse map cfg["bridge"]["generic"]["EnableIPTable"]
	cfgBridge := c.cfg.DriverConfig("bridge")
	cfgGeneric, ok := cfgBridge[netlabel.GenericData].(options.Generic)
	if !ok {
		return nil
	}

	var versions []iptables.IPVersion
	if enabled, ok := cfgGeneric["EnableIPTables"].(bool); enabled || !ok {
		// iptables is enabled unless user explicitly disabled it
		versions = append(versions, iptables.IPv4)

Review comment (suggestion):
We should probably use `RFC3339` here, as we tend to (should!) do for values returned by the API;

For systemtime, we include nano-seconds, but perhaps that's a bit too precise?

```suggestion
			info.Info = append(info.Info, [2]string{"ReloadedAt", reloadedAt.Format(time.RFC3339Nano)})
```

If so, we could just use without;

```suggestion
			info.Info = append(info.Info, [2]string{"ReloadedAt", reloadedAt.Format(time.RFC3339)})
```

Code after:
package libnetwork

import (
	"context"
	"fmt"
	"sync"
	"time"

	"github.com/containerd/log"
	"github.com/docker/docker/api/types/system"
	"github.com/docker/docker/libnetwork/iptables"
	"github.com/docker/docker/libnetwork/netlabel"
	"github.com/docker/docker/libnetwork/options"
	"github.com/docker/docker/libnetwork/osl"
)

// FirewallBackend returns the name of the firewall backend for "docker info".
func (c *Controller) FirewallBackend() *system.FirewallInfo {
	usingFirewalld, err := iptables.UsingFirewalld()
	if err != nil {
		return nil
	}
	if usingFirewalld {
		info := &system.FirewallInfo{Driver: "iptables+firewalld"}
		reloadedAt := iptables.FirewalldReloadedAt()
		if !reloadedAt.IsZero() {
			info.Info = append(info.Info, [2]string{"ReloadedAt", reloadedAt.Format(time.RFC3339)})
		}
		return info
	}
	return &system.FirewallInfo{Driver: "iptables"}
}

// enabledIptablesVersions returns the iptables versions that are enabled
// for the controller.
func (c *Controller) enabledIptablesVersions() []iptables.IPVersion {
	c.mu.Lock()
	defer c.mu.Unlock()
	if c.cfg == nil {
		return nil
	}
	// parse map cfg["bridge"]["generic"]["EnableIPTable"]
	cfgBridge := c.cfg.DriverConfig("bridge")
	cfgGeneric, ok := cfgBridge[netlabel.GenericData].(options.Generic)
	if !ok {
		return nil
	}

	var versions []iptables.IPVersion
	if enabled, ok := cfgGeneric["EnableIPTables"].(bool); enabled || !ok {
		// iptables is enabled unless user explicitly disabled it