Skip to content

Commit

Permalink
Merge pull request #74 from steadybit/fix/check-ipv6-module
Browse files Browse the repository at this point in the history
fix: check if ipv6 module is enabled
  • Loading branch information
joshiste authored Dec 20, 2023
2 parents f7cf225 + c0084f7 commit f813334
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 33 deletions.
18 changes: 13 additions & 5 deletions go/action_kit_commons/network/bandwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (

func TestLimitBandwidthOpts_TcCommands(t *testing.T) {
tests := []struct {
name string
opts LimitBandwidthOpts
wantAdd []byte
wantDel []byte
wantErr bool
name string
opts LimitBandwidthOpts
ipv6Disabled bool
wantAdd []byte
wantDel []byte
wantErr bool
}{
{
name: "bandwidth less then 8bit not supported",
Expand Down Expand Up @@ -94,6 +95,13 @@ qdisc del dev eth0 root handle 1: htb default 30
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ipv6Supported = func() bool {
return !tt.ipv6Disabled
}
defer func() {
ipv6Supported = defaultIpv6Supported
}()

gotAdd, err := tt.opts.TcCommands(ModeAdd)
if (err != nil) != tt.wantErr {
t.Errorf("TcCommands() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
22 changes: 15 additions & 7 deletions go/action_kit_commons/network/blackhole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (

func TestBlackholeOpts_IpCommands(t *testing.T) {
tests := []struct {
name string
opts BlackholeOpts
wantAddV4 []byte
wantDelV4 []byte
wantAddV6 []byte
wantDelV6 []byte
wantErr bool
name string
opts BlackholeOpts
ipv6Disabled bool
wantAddV4 []byte
wantDelV4 []byte
wantAddV6 []byte
wantDelV6 []byte
wantErr bool
}{
{
name: "blackhole",
Expand Down Expand Up @@ -91,6 +92,13 @@ rule del blackhole to ::/0 ipproto udp dport 123
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ipv6Supported = func() bool {
return !tt.ipv6Disabled
}
defer func() {
ipv6Supported = defaultIpv6Supported
}()

gotAddV4, err := tt.opts.IpCommands(FamilyV4, ModeAdd)
if (err != nil) != tt.wantErr {
t.Errorf("TcCommands() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
18 changes: 13 additions & 5 deletions go/action_kit_commons/network/delay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import (

func TestDelayOpts_TcCommands(t *testing.T) {
tests := []struct {
name string
opts DelayOpts
wantAdd []byte
wantDel []byte
wantErr bool
name string
opts DelayOpts
ipv6Disabled bool
wantAdd []byte
wantDel []byte
wantErr bool
}{
{
name: "delay",
Expand Down Expand Up @@ -86,6 +87,13 @@ qdisc del dev eth0 root handle 1: prio
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ipv6Supported = func() bool {
return !tt.ipv6Disabled
}
defer func() {
ipv6Supported = defaultIpv6Supported
}()

gotAdd, err := tt.opts.TcCommands(ModeAdd)
if (err != nil) != tt.wantErr {
t.Errorf("TcCommands() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
30 changes: 24 additions & 6 deletions go/action_kit_commons/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/rs/zerolog/log"
"github.com/steadybit/action-kit/go/action_kit_commons/runc"
"github.com/steadybit/action-kit/go/action_kit_commons/utils"
"os"
"runtime/trace"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -42,9 +44,12 @@ func generateAndRunCommands(ctx context.Context, r runc.Runc, sidecar SidecarOpt
return err
}

ipCommandsV6, err := opts.IpCommands(FamilyV6, mode)
if err != nil {
return err
var ipCommandsV6 []string
if ipv6Supported() {
ipCommandsV6, err = opts.IpCommands(FamilyV6, mode)
if err != nil {
return err
}
}

tcCommands, err := opts.TcCommands(mode)
Expand All @@ -56,19 +61,19 @@ func generateAndRunCommands(ctx context.Context, r runc.Runc, sidecar SidecarOpt
runLock.LockKey(netNsID)
defer func() { _ = runLock.UnlockKey(netNsID) }()

if ipCommandsV4 != nil {
if len(ipCommandsV4) > 0 {
if ipErr := executeIpCommands(ctx, r, sidecar, FamilyV4, ipCommandsV4); ipErr != nil {
err = errors.Join(err, FilterBatchErrors(ipErr, mode, ipCommandsV4))
}
}

if ipCommandsV6 != nil {
if len(ipCommandsV6) > 0 {
if ipErr := executeIpCommands(ctx, r, sidecar, FamilyV6, ipCommandsV6); ipErr != nil {
err = errors.Join(err, FilterBatchErrors(ipErr, mode, ipCommandsV6))
}
}

if tcCommands != nil {
if len(tcCommands) > 0 {
if tcErr := executeTcCommands(ctx, r, sidecar, tcCommands); tcErr != nil {
err = errors.Join(err, FilterBatchErrors(tcErr, mode, tcCommands))
}
Expand All @@ -77,6 +82,19 @@ func generateAndRunCommands(ctx context.Context, r runc.Runc, sidecar SidecarOpt
return err
}

var ipv6Supported = defaultIpv6Supported

func defaultIpv6Supported() bool {
if content, err := os.ReadFile("/sys/module/ipv6/parameters/disable"); err == nil {
disabled := strings.TrimSpace(string(content)) == "1"
log.Trace().Bool("disabled", disabled).Msg("read ipv6 module parameters")
return !disabled
} else {
log.Warn().Err(err).Msg("Failed to read /sys/module/ipv6/parameters/disable. Assuming ipv6 is disabled.")
return false
}
}

func executeIpCommands(ctx context.Context, r runc.Runc, sidecar SidecarOpts, family Family, cmds []string) error {
defer trace.StartRegion(ctx, "network.executeIpCommands").End()
if len(cmds) == 0 {
Expand Down
18 changes: 13 additions & 5 deletions go/action_kit_commons/network/packageCorruption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (

func TestCorruptPackagesOpts_TcCommands(t *testing.T) {
tests := []struct {
name string
opts CorruptPackagesOpts
wantAdd []byte
wantDel []byte
wantErr bool
name string
opts CorruptPackagesOpts
ipv6Disabled bool
wantAdd []byte
wantDel []byte
wantErr bool
}{
{
name: "corrupt",
Expand Down Expand Up @@ -82,6 +83,13 @@ qdisc del dev eth0 root handle 1: prio
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ipv6Supported = func() bool {
return !tt.ipv6Disabled
}
defer func() {
ipv6Supported = defaultIpv6Supported
}()

gotAdd, err := tt.opts.TcCommands(ModeAdd)
if (err != nil) != tt.wantErr {
t.Errorf("TcCommands() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
18 changes: 13 additions & 5 deletions go/action_kit_commons/network/packageLoss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (

func TestPackageLossOpts_TcCommands(t *testing.T) {
tests := []struct {
name string
opts PackageLossOpts
wantAdd []byte
wantDel []byte
wantErr bool
name string
opts PackageLossOpts
ipv6Disabled bool
wantAdd []byte
wantDel []byte
wantErr bool
}{
{
name: "loss",
Expand Down Expand Up @@ -82,6 +83,13 @@ qdisc del dev eth0 root handle 1: prio
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ipv6Supported = func() bool {
return !tt.ipv6Disabled
}
defer func() {
ipv6Supported = defaultIpv6Supported
}()

gotAdd, err := tt.opts.TcCommands(ModeAdd)
if (err != nil) != tt.wantErr {
t.Errorf("TcCommands() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit f813334

Please sign in to comment.