Merge pull request #8540 from swordqiu/hotfix/qj-set-customized-private-prefixes

fix: allow set customized private prefixes
This commit is contained in:
yunion-ci-robot
2020-10-29 17:00:45 +08:00
committed by GitHub
8 changed files with 70 additions and 17 deletions
+1 -1
View File
@@ -144,7 +144,7 @@ require (
yunion.io/x/jsonutils v0.0.0-20201022101715-4e3add1ac4aa
yunion.io/x/log v0.0.0-20200313080802-57a4ce5966b3
yunion.io/x/ovsdb v0.0.0-20200526071744-27bf0940cbc7
yunion.io/x/pkg v0.0.0-20200814072949-4f1b541857d6
yunion.io/x/pkg v0.0.0-20201028134817-3ed15ee169bc
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
yunion.io/x/sqlchemy v0.0.0-20201014101037-8fe75542e6d8
yunion.io/x/structarg v0.0.0-20200720093445-9f850fa222ce
+2
View File
@@ -1136,6 +1136,8 @@ yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZV
yunion.io/x/pkg v0.0.0-20190628082551-f4033ba2ea30/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/pkg v0.0.0-20200814072949-4f1b541857d6 h1:UarEDTBGkgcgc+nc+PZ75uo9M9+jiOGd5P2B90TxDNw=
yunion.io/x/pkg v0.0.0-20200814072949-4f1b541857d6/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/pkg v0.0.0-20201028134817-3ed15ee169bc h1:cgwWI6k9LsXpq6yiOuEIprsXmPyPGUa60sOZu57e8Jo=
yunion.io/x/pkg v0.0.0-20201028134817-3ed15ee169bc/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo=
yunion.io/x/sqlchemy v0.0.0-20201014101037-8fe75542e6d8 h1:AkPrqO37Yi3aICys9qDjTlSdVVGh2pWQ8ET+dSJmhoI=
+1
View File
@@ -105,6 +105,7 @@ var (
"time_zone",
"domainized_namespace",
"api_server",
"customized_private_prefixes",
},
}
+27
View File
@@ -15,6 +15,11 @@
package options
import (
"sort"
"yunion.io/x/log"
"yunion.io/x/pkg/util/netutils"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
)
@@ -24,6 +29,7 @@ func OnBaseOptionsChange(oOpts, nOpts interface{}) bool {
changed := false
if oldOpts.RequestWorkerCount != newOpts.RequestWorkerCount {
log.Debugf("RequestWorkerCount changed from %d to %d", oldOpts.RequestWorkerCount, newOpts.RequestWorkerCount)
changed = true
}
if oldOpts.TimeZone != newOpts.TimeZone {
@@ -40,9 +46,30 @@ func OnBaseOptionsChange(oOpts, nOpts interface{}) bool {
consts.SetDomainizedNamespace(newOpts.DomainizedNamespace)
changed = true
}
if privatePrrefixesChanged(oldOpts.CustomizedPrivatePrefixes, newOpts.CustomizedPrivatePrefixes) {
netutils.SetPrivatePrefixes(newOpts.CustomizedPrivatePrefixes)
log.Debugf("Customized private prefixes: %s", netutils.GetPrivateIPRanges())
}
return changed
}
func privatePrrefixesChanged(oldprefs, newprefs []string) bool {
if len(oldprefs) != len(newprefs) {
return true
}
if len(oldprefs) == 0 {
return false
}
sort.Strings(oldprefs)
sort.Strings(newprefs)
for i := range newprefs {
if oldprefs[i] != newprefs[i] {
return true
}
}
return false
}
func OnCommonOptionsChange(oOpts, nOpts interface{}) bool {
oldOpts := oOpts.(*CommonOptions)
newOpts := nOpts.(*CommonOptions)
+2
View File
@@ -90,6 +90,8 @@ type BaseOptions struct {
ApiServer string `help:"URL to access frontend webconsole"`
CustomizedPrivatePrefixes []string `help:"customized private prefixes"`
structarg.BaseOptions
}
+1 -1
View File
@@ -1077,7 +1077,7 @@ yunion.io/x/log/hooks
yunion.io/x/ovsdb/cli_util
yunion.io/x/ovsdb/schema/ovn_nb
yunion.io/x/ovsdb/types
# yunion.io/x/pkg v0.0.0-20200814072949-4f1b541857d6
# yunion.io/x/pkg v0.0.0-20201028134817-3ed15ee169bc
yunion.io/x/pkg/errors
yunion.io/x/pkg/gotypes
yunion.io/x/pkg/prettytable
+8 -4
View File
@@ -97,8 +97,10 @@ func CompareSets(dbSet interface{}, extSet interface{}, removed interface{}, com
i += 1
j += 1
} else if cmp < 0 {
newVal := reflect.Append(removedValue, dbSetArray[i].value)
removedValue.Set(newVal)
if len(dbSetArray[i].key) > 0 {
newVal := reflect.Append(removedValue, dbSetArray[i].value)
removedValue.Set(newVal)
}
i += 1
} else {
newVal := reflect.Append(addedValue, extSetArray[j].value)
@@ -110,8 +112,10 @@ func CompareSets(dbSet interface{}, extSet interface{}, removed interface{}, com
addedValue.Set(newVal)
j += 1
} else if j >= len(extSetArray) {
newVal := reflect.Append(removedValue, dbSetArray[i].value)
removedValue.Set(newVal)
if len(dbSetArray[i].key) > 0 {
newVal := reflect.Append(removedValue, dbSetArray[i].value)
removedValue.Set(newVal)
}
i += 1
}
}
+28 -11
View File
@@ -416,23 +416,14 @@ const (
multicastPrefix = "224.0.0.0/4"
)
var privatePrefixes = []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
}
var privateIPRanges []IPV4AddrRange
var hostLocalIPRange IPV4AddrRange
var linkLocalIPRange IPV4AddrRange
var multicastIPRange IPV4AddrRange
func init() {
privateIPRanges = make([]IPV4AddrRange, len(privatePrefixes))
for i, prefix := range privatePrefixes {
prefix, _ := NewIPV4Prefix(prefix)
privateIPRanges[i] = prefix.ToIPRange()
}
updatePrivateIPRanges(nil)
prefix, _ := NewIPV4Prefix(hostlocalPrefix)
hostLocalIPRange = prefix.ToIPRange()
prefix, _ = NewIPV4Prefix(linklocalPrefix)
@@ -441,6 +432,32 @@ func init() {
multicastIPRange = prefix.ToIPRange()
}
func updatePrivateIPRanges(prefs []string) {
if len(prefs) == 0 {
prefs = []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
}
}
privateIPRanges = make([]IPV4AddrRange, len(prefs))
for i, prefix := range prefs {
prefix, err := NewIPV4Prefix(prefix)
if err != nil {
continue
}
privateIPRanges[i] = prefix.ToIPRange()
}
}
func SetPrivatePrefixes(pref []string) {
updatePrivateIPRanges(pref)
}
func GetPrivateIPRanges() []IPV4AddrRange {
return privateIPRanges
}
func IsPrivate(addr IPV4Addr) bool {
for _, ipRange := range privateIPRanges {
if ipRange.Contains(addr) {