Skip to content

Commit 184cf82

Browse files
authored
Adds support for iptables ingress packet loss (#264)
* Adds support for iptables ingress packet loss * Fixes arguments for dport and sport * Fixes review comments * Fixes typo * Resolves some linting errros * Fixes the test case * Adds test case, fixes issue where iptables is called with an invalid parameter, i.e. `-dport` and `-sport` instead of `--dport` and `--sport`. * Pins mockery version, reverts go version back to 1.21 * Improves code and fixes linting errors * Reverts some changes made to Dockerfile
1 parent 036f8e8 commit 184cf82

19 files changed

+1892
-75
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ setup-gocov-xml:
7575
setup-go-junit-report:
7676
$(GO) install github.com/jstemmer/go-junit-report/v2@latest
7777
setup-mockery:
78-
$(GO) get github.com/vektra/mockery/v2@latest
78+
$(GO) get github.com/vektra/mockery/v2@v2.40.3
7979

8080
# Tests
8181

README.md

+146-43
Large diffs are not rendered by default.

cmd/main.go

+50
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/alexei-led/pumba/pkg/chaos"
1515
"github.com/alexei-led/pumba/pkg/chaos/docker/cmd"
16+
ipTablesCmd "github.com/alexei-led/pumba/pkg/chaos/iptables/cmd"
1617
netemCmd "github.com/alexei-led/pumba/pkg/chaos/netem/cmd"
1718
stressCmd "github.com/alexei-led/pumba/pkg/chaos/stress/cmd"
1819
"github.com/alexei-led/pumba/pkg/container"
@@ -327,5 +328,54 @@ func initializeCLICommands() []cli.Command {
327328
*netemCmd.NewCorruptCLICommand(topContext),
328329
},
329330
},
331+
{
332+
Name: "iptables",
333+
Flags: []cli.Flag{
334+
cli.DurationFlag{
335+
Name: "duration, d",
336+
Usage: "network emulation duration; should be smaller than recurrent interval; use with optional unit suffix: 'ms/s/m/h'",
337+
},
338+
cli.StringFlag{
339+
Name: "interface, i",
340+
Usage: "network interface to apply input rules on",
341+
Value: defaultInterface,
342+
},
343+
cli.StringFlag{
344+
Name: "protocol, p",
345+
Usage: "protocol to apply input rules on (any, udp, tcp or icmp)",
346+
Value: "any",
347+
},
348+
cli.StringSliceFlag{
349+
Name: "source, src, s",
350+
Usage: "source IP filter; supports multiple IPs; supports CIDR notation",
351+
},
352+
cli.StringSliceFlag{
353+
Name: "destination, dest",
354+
Usage: "destination IP filter; supports multiple IPs; supports CIDR notation",
355+
},
356+
cli.StringFlag{
357+
Name: "src-port, sport",
358+
Usage: "source port filter; supports multiple ports (comma-separated)",
359+
},
360+
cli.StringFlag{
361+
Name: "dst-port, dport",
362+
Usage: "destination port filter; supports multiple ports (comma-separated)",
363+
},
364+
cli.StringFlag{
365+
Name: "iptables-image",
366+
Usage: "Docker image with iptables (iptables package); try 'rancher/mirrored-kube-vip-kube-vip-iptables:v0.8.9'",
367+
},
368+
cli.BoolTFlag{
369+
Name: "pull-image",
370+
Usage: "force pull iptables-image",
371+
},
372+
},
373+
Usage: "apply IPv4 packet filter on incoming IP packets",
374+
ArgsUsage: fmt.Sprintf("containers (name, list of names, or RE2 regex if prefixed with %q", re2Prefix),
375+
Description: "emulate loss of incoming packets, all ports and address arguments will result in separate rules",
376+
Subcommands: []cli.Command{
377+
*ipTablesCmd.NewLossCLICommand(topContext),
378+
},
379+
},
330380
}
331381
}

docker/Dockerfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1010
bash \
1111
tzdata \
1212
ca-certificates \
13-
&& rm -rf /var/lib/apt/lists/*
13+
&& rm -rf /var/lib/apt/lists/*
1414

1515
#
1616
# ----- Build and Test Image -----
1717
#
18-
FROM builder as build-and-test
18+
FROM builder AS build-and-test
1919

2020
# set working directory
2121
RUN mkdir -p /go/src/pumba
@@ -60,7 +60,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build make build TARGETOS=${TARGET
6060
#
6161
# ------ Pumba Integration Tests ------
6262
#
63-
FROM --platform=$TARGETPLATFORM bats/bats:v1.10.0 as integration-tests
63+
FROM --platform=$TARGETPLATFORM bats/bats:v1.10.0 AS integration-tests
6464

6565
# install required packages
6666
RUN apk add --no-cache docker iproute2
@@ -89,4 +89,4 @@ COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
8989
# this is the last command since it's never cached
9090
COPY --from=build-and-test /go/src/pumba/.bin/github.com/alexei-led/pumba /pumba
9191

92-
ENTRYPOINT ["/pumba"]
92+
ENTRYPOINT ["/pumba"]

go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/sirupsen/logrus v1.9.3
1010
github.com/stretchr/testify v1.8.4
1111
github.com/urfave/cli v1.22.12
12-
golang.org/x/sync v0.3.0
12+
golang.org/x/sync v0.6.0
1313
)
1414

1515
require (
@@ -29,11 +29,11 @@ require (
2929
github.com/pmezard/go-difflib v1.0.0 // indirect
3030
github.com/russross/blackfriday/v2 v2.1.0 // indirect
3131
github.com/stretchr/objx v0.5.0 // indirect
32-
golang.org/x/mod v0.12.0 // indirect
33-
golang.org/x/net v0.15.0 // indirect
34-
golang.org/x/sys v0.12.0 // indirect
32+
golang.org/x/mod v0.14.0 // indirect
33+
golang.org/x/net v0.20.0 // indirect
34+
golang.org/x/sys v0.16.0 // indirect
3535
golang.org/x/time v0.1.0 // indirect
36-
golang.org/x/tools v0.13.0 // indirect
36+
golang.org/x/tools v0.17.0 // indirect
3737
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
3838
gopkg.in/yaml.v3 v3.0.1 // indirect
3939
gotest.tools/v3 v3.0.2 // indirect

go.sum

+10-12
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ
1010
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1111
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1212
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13-
github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68=
14-
github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
1513
github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=
1614
github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
1715
github.com/docker/docker v23.0.3+incompatible h1:9GhVsShNWz1hO//9BNg/dpMnZW25KydO4wtVxWAIbho=
@@ -75,27 +73,27 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
7573
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
7674
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
7775
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
78-
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
79-
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
76+
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
77+
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
8078
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
8179
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
8280
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
8381
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
8482
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
85-
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
86-
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
83+
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
84+
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
8785
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
8886
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
8987
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
90-
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
91-
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
88+
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
89+
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
9290
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
9391
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9492
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9593
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9694
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
97-
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
98-
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
95+
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
96+
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
9997
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
10098
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
10199
golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=
@@ -105,8 +103,8 @@ golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgw
105103
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
106104
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
107105
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
108-
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
109-
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
106+
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
107+
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
110108
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
111109
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
112110
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)