Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow configuring hairpinning on default nomad bridge #13834

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/allocrunner/network_manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func newNetworkConfigurator(log hclog.Logger, alloc *structs.Allocation, config

switch {
case netMode == "bridge":
c, err := newBridgeNetworkConfigurator(log, config.BridgeNetworkName, config.BridgeNetworkAllocSubnet, config.CNIPath, ignorePortMappingHostIP)
c, err := newBridgeNetworkConfigurator(log, config.BridgeNetworkName, config.BridgeNetworkAllocSubnet, config.CNIPath, ignorePortMappingHostIP, config.BridgeNetworkHairpin)
if err != nil {
return nil, err
}
Expand Down
11 changes: 7 additions & 4 deletions client/allocrunner/networking_bridge_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ type bridgeNetworkConfigurator struct {
cni *cniNetworkConfigurator
allocSubnet string
bridgeName string
hairpin bool

logger hclog.Logger
}

func newBridgeNetworkConfigurator(log hclog.Logger, bridgeName, ipRange, cniPath string, ignorePortMappingHostIP bool) (*bridgeNetworkConfigurator, error) {
func newBridgeNetworkConfigurator(log hclog.Logger, bridgeName, ipRange, cniPath string, ignorePortMappingHostIP bool, hairpin bool) (*bridgeNetworkConfigurator, error) {
b := &bridgeNetworkConfigurator{
bridgeName: bridgeName,
allocSubnet: ipRange,
logger: log,
hairpin: hairpin,
}

if b.bridgeName == "" {
Expand All @@ -54,7 +56,7 @@ func newBridgeNetworkConfigurator(log hclog.Logger, bridgeName, ipRange, cniPath
b.allocSubnet = defaultNomadAllocSubnet
}

c, err := newCNINetworkConfiguratorWithConf(log, cniPath, bridgeNetworkAllocIfPrefix, ignorePortMappingHostIP, buildNomadBridgeNetConfig(b.bridgeName, b.allocSubnet))
c, err := newCNINetworkConfiguratorWithConf(log, cniPath, bridgeNetworkAllocIfPrefix, ignorePortMappingHostIP, buildNomadBridgeNetConfig(b.bridgeName, b.allocSubnet, b.hairpin))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -134,8 +136,8 @@ func (b *bridgeNetworkConfigurator) Teardown(ctx context.Context, alloc *structs
return b.cni.Teardown(ctx, alloc, spec)
}

func buildNomadBridgeNetConfig(bridgeName, subnet string) []byte {
return []byte(fmt.Sprintf(nomadCNIConfigTemplate, bridgeName, subnet, cniAdminChainName))
func buildNomadBridgeNetConfig(bridgeName, subnet string, hairpin bool) []byte {
return []byte(fmt.Sprintf(nomadCNIConfigTemplate, bridgeName, hairpin, subnet, cniAdminChainName))
}

const nomadCNIConfigTemplate = `{
Expand All @@ -151,6 +153,7 @@ const nomadCNIConfigTemplate = `{
"ipMasq": true,
"isGateway": true,
"forceAddress": true,
"hairpinMode": %t,
"ipam": {
"type": "host-local",
"ranges": [
Expand Down
4 changes: 4 additions & 0 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ type Config struct {
// networking mode. This defaults to 'nomad' if not set
BridgeNetworkName string

// BridgeNetworkHairpin sets wether the bridge allows hairpining, that is allow clients
// to connect to their own IP. This defaults to false if not set
BridgeNetworkHairpin bool

// BridgeNetworkAllocSubnet is the IP subnet to use for address allocation
// for allocations in bridge networking mode. Subnet must be in CIDR
// notation
Expand Down
1 change: 1 addition & 0 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ func convertClientConfig(agentConfig *Config) (*clientconfig.Config, error) {
conf.CNIPath = agentConfig.Client.CNIPath
conf.CNIConfigDir = agentConfig.Client.CNIConfigDir
conf.BridgeNetworkName = agentConfig.Client.BridgeNetworkName
conf.BridgeNetworkHairpin = agentConfig.Client.BridgeNetworkHairpin
conf.BridgeNetworkAllocSubnet = agentConfig.Client.BridgeNetworkSubnet

for _, hn := range agentConfig.Client.HostNetworks {
Expand Down
8 changes: 8 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ type ClientConfig struct {
// bridge network mode
BridgeNetworkName string `hcl:"bridge_network_name"`

// BridgeNetworkHairpin sets wether to allow hairpinning on the bridge
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// BridgeNetworkHairpin sets wether to allow hairpinning on the bridge
// BridgeNetworkHairpin sets whether to allow hairpinning on the bridge

BridgeNetworkHairpin bool `hcl:"bridge_network_hairpin"`

// BridgeNetworkSubnet is the subnet to allocate IP addresses from when
// creating allocations with bridge networking mode. This range is local to
// the host
Expand Down Expand Up @@ -988,6 +991,7 @@ func DevConfig(mode *devModeConfig) *Config {
DisableSandbox: false,
}
conf.Client.BindWildcardDefaultHostNetwork = true
conf.Client.BridgeNetworkHairpin = false
conf.Client.NomadServiceDiscovery = helper.BoolToPtr(true)
conf.Telemetry.PrometheusMetrics = true
conf.Telemetry.PublishAllocationMetrics = true
Expand Down Expand Up @@ -1038,6 +1042,7 @@ func DefaultConfig() *Config {
DisableSandbox: false,
},
BindWildcardDefaultHostNetwork: true,
BridgeNetworkHairpin: false,
CNIPath: "/opt/cni/bin",
CNIConfigDir: "/opt/cni/config",
NomadServiceDiscovery: helper.BoolToPtr(true),
Expand Down Expand Up @@ -1841,6 +1846,9 @@ func (a *ClientConfig) Merge(b *ClientConfig) *ClientConfig {
if b.BridgeNetworkName != "" {
result.BridgeNetworkName = b.BridgeNetworkName
}
if b.BridgeNetworkHairpin {
result.BridgeNetworkHairpin = true
}
if b.BridgeNetworkSubnet != "" {
result.BridgeNetworkSubnet = b.BridgeNetworkSubnet
}
Expand Down
3 changes: 3 additions & 0 deletions website/content/docs/configuration/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ client {
- `bridge_network_subnet` `(string: "172.26.64.0/20")` - Specifies the subnet
which the client will use to allocate IP addresses from.

- `bridge_network_hairpin` `(bool: false)` - Specifies wether the bridge should
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `bridge_network_hairpin` `(bool: false)` - Specifies wether the bridge should
- `bridge_network_hairpin` `(bool: false)` - Specifies whether the bridge should

allow hairpinning.

- `artifact` <code>([Artifact](#artifact-parameters): varied)</code> -
Specifies controls on the behavior of task
[`artifact`](/docs/job-specification/artifact) stanzas.
Expand Down