Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Make getIPChains more precise and less failure-prone
Browse files Browse the repository at this point in the history
move fmt.Sprintf out of loop
access stat.Options through rawStat[9] with hard-coded index
only parse for IPNet when we are working with the proper ignite CNI rules
  ^ avoids coreos/go-iptables#70
  • Loading branch information
stealthybox committed Sep 13, 2019
1 parent 67ae546 commit eec6d68
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pkg/network/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,28 @@ func getIPChains(containerID string) (result []*ipChain, err error) {
return
}

stats, err := ipt.StructuredStats("nat", "POSTROUTING")
rawStats, err := ipt.Stats("nat", "POSTROUTING")
if err != nil {
return
}

for _, stat := range stats {
/* name: "ignite-containerd-default" id: "ignite-9a10b07d7c0d4ce9" */
for _, field := range strings.Split(stat.Options, " ") {
if fmt.Sprintf("%q", containerID) == field {
result = append(result, &ipChain{
ip: stat.Source,
chain: stat.Target,
})
break
quotedContainerID := fmt.Sprintf("id: %q", containerID)
const statOptionsIndex = 9
for _, rawStat := range rawStats {
// stat.Options has a comment that looks like:
// /* name: "ignite-containerd-default" id: "ignite-9a10b07d7c0d4ce9" */
if strings.Contains(rawStat[statOptionsIndex], quotedContainerID) {
// only parse the IP's for the rules we need
var stat iptables.Stat
stat, err = ipt.ParseStat(rawStat)
if err != nil {
return
}

result = append(result, &ipChain{
ip: stat.Source,
chain: stat.Target,
})
}
}

Expand Down

0 comments on commit eec6d68

Please # to comment.