GetRedirectedChain returns the chain where the traffic is being redirected. This is how libcni manage its port maps. Suppose you have the following rule: -A CNI-HOSTPORT-DNAT -p tcp -m comment --comment "dnat name: \"bridge\" id: \"default-YYYYYY\"" -m multiport --dports 9999 -j CNI-DN-XXXXXX So the
(t *testing.T, ipt *iptables.IPTables, chain, namespace, containerID string)
| 62 | // So the chain where the traffic is redirected is CNI-DN-XXXXXX |
| 63 | // Returns an empty string in case nothing was found. |
| 64 | func GetRedirectedChain(t *testing.T, ipt *iptables.IPTables, chain, namespace, containerID string) string { |
| 65 | rules, err := ipt.List("nat", chain) |
| 66 | if err != nil { |
| 67 | t.Logf("error listing rules in chain: %q\n", err) |
| 68 | return "" |
| 69 | } |
| 70 | |
| 71 | if len(rules) < 1 { |
| 72 | t.Logf("not enough rules: %d", len(rules)) |
| 73 | return "" |
| 74 | } |
| 75 | |
| 76 | var redirectedChain string |
| 77 | re := regexp.MustCompile(`-j\s+([^ ]+)`) |
| 78 | for _, rule := range rules { |
| 79 | // first we verify the comment section is present: "dnat name: \"bridge\" id: \"default-YYYYYY\"" |
| 80 | matchesContainer, err := regexp.MatchString(namespace+"-"+containerID, rule) |
| 81 | if err != nil { |
| 82 | t.Logf("error in match string: %q\n", err) |
| 83 | return "" |
| 84 | } |
| 85 | if matchesContainer { |
| 86 | // then we find the appropriate chain in the rule |
| 87 | matches := re.FindStringSubmatch(rule) |
| 88 | fmt.Println(matches) |
| 89 | if len(matches) >= 2 { |
| 90 | redirectedChain = matches[1] |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | if redirectedChain == "" { |
| 95 | t.Logf("no redirectced chain found") |
| 96 | return "" |
| 97 | } |
| 98 | return redirectedChain |
| 99 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…