MCPcopy Create free account
hub / github.com/containerd/nerdctl / ForwardExists

Function ForwardExists

pkg/testutil/iptables/iptables_linux.go:29–56  ·  view source on GitHub ↗

ForwardExists check that at least 2 rules are present in the CNI-HOSTPORT-DNAT chain and checks for regex matches in the list of rules

(t *testing.T, ipt *iptables.IPTables, chain, containerIP string, port int)

Source from the content-addressed store, hash-verified

27// ForwardExists check that at least 2 rules are present in the CNI-HOSTPORT-DNAT chain
28// and checks for regex matches in the list of rules
29func ForwardExists(t *testing.T, ipt *iptables.IPTables, chain, containerIP string, port int) bool {
30 rules, err := ipt.List("nat", chain)
31 if err != nil {
32 t.Logf("error listing rules in chain: %q\n", err)
33 return false
34 }
35
36 if len(rules) < 1 {
37 t.Logf("not enough rules: %d", len(rules))
38 return false
39 }
40
41 // here we check if at least one of the rules in the chain
42 // matches the required string to identify that the rule was applied
43 found := false
44 matchRule := `--dport ` + fmt.Sprintf("%d", port) + ` .+ --to-destination ` + containerIP
45 for _, rule := range rules {
46 foundInRule, err := regexp.MatchString(matchRule, rule)
47 if err != nil {
48 t.Logf("error in match string: %q\n", err)
49 return false
50 }
51 if foundInRule {
52 found = foundInRule
53 }
54 }
55 return found
56}
57
58// GetRedirectedChain returns the chain where the traffic is being redirected.
59// This is how libcni manage its port maps.

Callers

nothing calls this directly

Calls 1

ListMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…