prevent unpredictable results with network create|remove

due to a lack of "locking" on cni operations, we could get ourselves in trouble when doing rapid creation or removal of networks.  added a simple file lock to deal with the collision and because it is not considered a performent path, use of the file lock should be ok.  if proven otherwise in the future, some generic shared memory lock should be implemented for libpod and also used here.

moved pkog/network to libpod/network because libpod is now being pulled into the package and it has therefore lost its generic nature. this will make it easier to absorb into libpod as we try to make the network closer to core operations.

Fixes: #7807

Signed-off-by: baude <bbaude@redhat.com>
This commit is contained in:
baude
2020-10-06 12:24:21 -05:00
parent defb754945
commit fe3faa517e
17 changed files with 249 additions and 185 deletions

View File

@ -0,0 +1,35 @@
package network
import (
"net"
"testing"
)
func parseCIDR(n string) *net.IPNet {
_, parsedNet, _ := net.ParseCIDR(n)
return parsedNet
}
func Test_networkIntersect(t *testing.T) {
type args struct {
n1 *net.IPNet
n2 *net.IPNet
}
tests := []struct {
name string
args args
want bool
}{
{"16 and 24 intersects", args{n1: parseCIDR("192.168.0.0/16"), n2: parseCIDR("192.168.1.0/24")}, true},
{"24 and 25 intersects", args{n1: parseCIDR("192.168.1.0/24"), n2: parseCIDR("192.168.1.0/25")}, true},
{"Two 24s", args{n1: parseCIDR("192.168.1.0/24"), n2: parseCIDR("192.168.2.0/24")}, false},
}
for _, tt := range tests {
test := tt
t.Run(tt.name, func(t *testing.T) {
if got := networkIntersect(test.args.n1, test.args.n2); got != test.want {
t.Errorf("networkIntersect() = %v, want %v", got, test.want)
}
})
}
}