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

Function GetRedirectedChain

pkg/testutil/iptables/iptables_linux.go:64–99  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

62// So the chain where the traffic is redirected is CNI-DN-XXXXXX
63// Returns an empty string in case nothing was found.
64func 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}

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…