|
|
@ -16,9 +16,11 @@ package flannel |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"math/big" |
|
|
|
"net" |
|
|
|
"net" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
|
|
|
|
"strings" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/flannel-io/flannel/pkg/backend" |
|
|
|
"github.com/flannel-io/flannel/pkg/backend" |
|
|
@ -228,34 +230,73 @@ func WriteSubnetFile(path string, nw ip.IP4Net, nwv6 ip.IP6Net, ipMasq bool, bn |
|
|
|
|
|
|
|
|
|
|
|
// ReadCIDRFromSubnetFile reads the flannel subnet file and extracts the value of IPv4 network CIDRKey
|
|
|
|
// ReadCIDRFromSubnetFile reads the flannel subnet file and extracts the value of IPv4 network CIDRKey
|
|
|
|
func ReadCIDRFromSubnetFile(path string, CIDRKey string) ip.IP4Net { |
|
|
|
func ReadCIDRFromSubnetFile(path string, CIDRKey string) ip.IP4Net { |
|
|
|
var prevCIDR ip.IP4Net |
|
|
|
prevCIDRs := ReadCIDRsFromSubnetFile(path, CIDRKey) |
|
|
|
|
|
|
|
if len(prevCIDRs) == 0 { |
|
|
|
|
|
|
|
logrus.Warningf("no subnet found for key: %s in file: %s", CIDRKey, path) |
|
|
|
|
|
|
|
return ip.IP4Net{IP: 0, PrefixLen: 0} |
|
|
|
|
|
|
|
} else if len(prevCIDRs) > 1 { |
|
|
|
|
|
|
|
logrus.Errorf("error reading subnet: more than 1 entry found for key: %s in file %s: ", CIDRKey, path) |
|
|
|
|
|
|
|
return ip.IP4Net{IP: 0, PrefixLen: 0} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return prevCIDRs[0] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func ReadCIDRsFromSubnetFile(path string, CIDRKey string) []ip.IP4Net { |
|
|
|
|
|
|
|
prevCIDRs := make([]ip.IP4Net, 0) |
|
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) { |
|
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) { |
|
|
|
prevSubnetVals, err := godotenv.Read(path) |
|
|
|
prevSubnetVals, err := godotenv.Read(path) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
logrus.Errorf("Couldn't fetch previous %s from subnet file at %s: %v", CIDRKey, path, err) |
|
|
|
logrus.Errorf("Couldn't fetch previous %s from subnet file at %s: %v", CIDRKey, path, err) |
|
|
|
} else if prevCIDRString, ok := prevSubnetVals[CIDRKey]; ok { |
|
|
|
} else if prevCIDRString, ok := prevSubnetVals[CIDRKey]; ok { |
|
|
|
err = prevCIDR.UnmarshalJSON([]byte(prevCIDRString)) |
|
|
|
cidrs := strings.Split(prevCIDRString, ",") |
|
|
|
if err != nil { |
|
|
|
prevCIDRs = make([]ip.IP4Net, 0) |
|
|
|
logrus.Errorf("Couldn't parse previous %s from subnet file at %s: %v", CIDRKey, path, err) |
|
|
|
for i := range cidrs { |
|
|
|
|
|
|
|
_, cidr, err := net.ParseCIDR(cidrs[i]) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
logrus.Errorf("Couldn't parse previous %s from subnet file at %s: %v", CIDRKey, path, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
prevCIDRs = append(prevCIDRs, ip.FromIPNet(cidr)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return prevCIDR |
|
|
|
return prevCIDRs |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ReadIP6CIDRFromSubnetFile reads the flannel subnet file and extracts the value of IPv6 network CIDRKey
|
|
|
|
// ReadIP6CIDRFromSubnetFile reads the flannel subnet file and extracts the value of IPv6 network CIDRKey
|
|
|
|
func ReadIP6CIDRFromSubnetFile(path string, CIDRKey string) ip.IP6Net { |
|
|
|
func ReadIP6CIDRFromSubnetFile(path string, CIDRKey string) ip.IP6Net { |
|
|
|
var prevCIDR ip.IP6Net |
|
|
|
prevCIDRs := ReadIP6CIDRsFromSubnetFile(path, CIDRKey) |
|
|
|
|
|
|
|
if len(prevCIDRs) == 0 { |
|
|
|
|
|
|
|
logrus.Warningf("no subnet found for key: %s in file: %s", CIDRKey, path) |
|
|
|
|
|
|
|
return ip.IP6Net{IP: (*ip.IP6)(big.NewInt(0)), PrefixLen: 0} |
|
|
|
|
|
|
|
} else if len(prevCIDRs) > 1 { |
|
|
|
|
|
|
|
logrus.Errorf("error reading subnet: more than 1 entry found for key: %s in file %s: ", CIDRKey, path) |
|
|
|
|
|
|
|
return ip.IP6Net{IP: (*ip.IP6)(big.NewInt(0)), PrefixLen: 0} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return prevCIDRs[0] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func ReadIP6CIDRsFromSubnetFile(path string, CIDRKey string) []ip.IP6Net { |
|
|
|
|
|
|
|
prevCIDRs := make([]ip.IP6Net, 0) |
|
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) { |
|
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) { |
|
|
|
prevSubnetVals, err := godotenv.Read(path) |
|
|
|
prevSubnetVals, err := godotenv.Read(path) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
logrus.Errorf("Couldn't fetch previous %s from subnet file at %s: %v", CIDRKey, path, err) |
|
|
|
logrus.Errorf("Couldn't fetch previous %s from subnet file at %s: %v", CIDRKey, path, err) |
|
|
|
} else if prevCIDRString, ok := prevSubnetVals[CIDRKey]; ok { |
|
|
|
} else if prevCIDRString, ok := prevSubnetVals[CIDRKey]; ok { |
|
|
|
err = prevCIDR.UnmarshalJSON([]byte(prevCIDRString)) |
|
|
|
cidrs := strings.Split(prevCIDRString, ",") |
|
|
|
if err != nil { |
|
|
|
prevCIDRs = make([]ip.IP6Net, 0) |
|
|
|
logrus.Errorf("Couldn't parse previous %s from subnet file at %s: %v", CIDRKey, path, err) |
|
|
|
for i := range cidrs { |
|
|
|
|
|
|
|
_, cidr, err := net.ParseCIDR(cidrs[i]) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
logrus.Errorf("Couldn't parse previous %s from subnet file at %s: %v", CIDRKey, path, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
prevCIDRs = append(prevCIDRs, ip.FromIP6Net(cidr)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return prevCIDR |
|
|
|
return prevCIDRs |
|
|
|
} |
|
|
|
} |
|
|
|