diff --git a/cmd/climc/shell/compute/networks.go b/cmd/climc/shell/compute/networks.go index b00f60d0eb..4b8c32de2a 100644 --- a/cmd/climc/shell/compute/networks.go +++ b/cmd/climc/shell/compute/networks.go @@ -40,6 +40,9 @@ func init() { ServerType string `help:"search networks belongs to a ServerType" choices:"baremetal|container|eip|guest|ipmi|pxe"` Schedtag string `help:"filter networks by schedtag"` + IsAutoAlloc *bool `help:"search network with is_auto_alloc"` + IsClassic *bool `help:"search classic on-premise network"` + Status string `help:"filter by network status"` } R(&NetworkListOptions{}, "network-list", "List networks", func(s *mcclient.ClientSession, opts *NetworkListOptions) error { @@ -79,6 +82,7 @@ func init() { VlanId int64 `help:"Vlan ID" default:"1"` ExternalId string `help:"External ID"` AllocPolicy string `help:"Address allocation policy" choices:"none|stepdown|stepup|random"` + IsAutoAlloc *bool `help:"Add network into auto-allocation pool" negative:"no_auto_alloc"` } R(&NetworkUpdateOptions{}, "network-update", "Update network", func(s *mcclient.ClientSession, args *NetworkUpdateOptions) error { params := jsonutils.NewDict() @@ -133,6 +137,9 @@ func init() { if len(args.AllocPolicy) > 0 { params.Add(jsonutils.NewString(args.AllocPolicy), "alloc_policy") } + if args.IsAutoAlloc != nil { + params.Add(jsonutils.NewBool(*args.IsAutoAlloc), "is_auto_alloc") + } if params.Size() == 0 { return InvalidUpdateError() } @@ -228,6 +235,7 @@ func init() { IfnameHint string `help:"Hint for ifname generation"` AllocPolicy string `help:"Address allocation policy" choices:"none|stepdown|stepup|random"` ServerType string `help:"Server type" choices:"baremetal|container|eip|guest|ipmi|pxe"` + IsAutoAlloc *bool `help:"Auto allocation IP pool"` Desc string `help:"Description" metavar:"DESCRIPTION"` } R(&NetworkCreateOptions{}, "network-create", "Create a virtual network", func(s *mcclient.ClientSession, args *NetworkCreateOptions) error { @@ -254,6 +262,9 @@ func init() { if len(args.Desc) > 0 { params.Add(jsonutils.NewString(args.Desc), "description") } + if args.IsAutoAlloc != nil { + params.Add(jsonutils.NewBool(*args.IsAutoAlloc), "is_auto_alloc") + } net, e := modules.Networks.CreateInContext(s, params, &modules.Wires, args.WIRE) if e != nil { return e diff --git a/pkg/apis/compute/network.go b/pkg/apis/compute/network.go index 4db501921f..35637e8d29 100644 --- a/pkg/apis/compute/network.go +++ b/pkg/apis/compute/network.go @@ -110,6 +110,10 @@ type NetworkListInput struct { ServerType []string `json:"server_type"` // 分配策略 AllocPolicy []string `json:"alloc_policy"` + // 是否加入自动分配地址池 + IsAutoAlloc *bool `json:"is_auto_alloc"` + // 是否为基础网络(underlay) + IsClassic *bool `json:"is_classic"` } type NetworkResourceInfoBase struct { @@ -177,6 +181,9 @@ type NetworkCreateInput struct { // enum: guest,baremetal,pxe,ipmi // default: guest ServerType string `json:"server_type"` + + // 是否加入自动分配地址池 + IsAutoAlloc *bool `json:"is_auto_alloc"` } type NetworkDetails struct { @@ -299,4 +306,7 @@ type NetworkUpdateInput struct { // 分配策略 AllocPolicy string `json:"alloc_policy"` + + // 是否加入自动分配地址池 + IsAutoAlloc *bool `json:"is_auto_alloc"` } diff --git a/pkg/baremetal/manager.go b/pkg/baremetal/manager.go index 6a64af2d6d..0e395ec8c9 100644 --- a/pkg/baremetal/manager.go +++ b/pkg/baremetal/manager.go @@ -378,7 +378,7 @@ func (m *SBaremetalManager) checkNetworkFromIp(ip string) (string, error) { params := jsonutils.NewDict() params.Set("ip", jsonutils.NewString(ip)) params.Set("scope", jsonutils.NewString("system")) - params.Set("is_on_premise", jsonutils.JSONTrue) + params.Set("is_classic", jsonutils.JSONTrue) res, err := modules.Networks.List(m.GetClientSession(), params) if err != nil { return "", fmt.Errorf("Fetch network by ip %s failed: %s", ip, err) @@ -1041,7 +1041,7 @@ func (b *SBaremetalInstance) findAccessNetwork(accessIp string) (*types.SNetwork params := jsonutils.NewDict() params.Add(jsonutils.NewString(accessIp), "ip") params.Add(jsonutils.NewString("system"), "scope") - params.Add(jsonutils.JSONTrue, "is_on_premise") + params.Add(jsonutils.JSONTrue, "is_classic") session := b.manager.GetClientSession() ret, err := modules.Networks.List(session, params) if err != nil { diff --git a/pkg/baremetal/pxe/dhcp.go b/pkg/baremetal/pxe/dhcp.go index ffdab6d1be..e69f0bc3c6 100644 --- a/pkg/baremetal/pxe/dhcp.go +++ b/pkg/baremetal/pxe/dhcp.go @@ -271,7 +271,7 @@ func (req *dhcpRequest) findNetworkConf(session *mcclient.ClientSession, filterU "filter.3") params.Add(jsonutils.JSONTrue, "filter_any") } - params.Add(jsonutils.JSONTrue, "is_on_premise") + params.Add(jsonutils.JSONTrue, "is_classic") params.Add(jsonutils.NewString("system"), "scope") ret, err := modules.Networks.List(session, params) if err != nil { diff --git a/pkg/baremetal/tasks/baseprepare.go b/pkg/baremetal/tasks/baseprepare.go index 5d24e6c1c7..0a9a180abf 100644 --- a/pkg/baremetal/tasks/baseprepare.go +++ b/pkg/baremetal/tasks/baseprepare.go @@ -560,7 +560,7 @@ func (task *sBaremetalPrepareTask) getIPMIIPConfig(ipAddr string) (*ipmiIPConfig params := jsonutils.NewDict() params.Add(jsonutils.NewString(ipAddr), "ip") params.Add(jsonutils.NewString("system"), "scope") - params.Add(jsonutils.JSONTrue, "is_on_premise") + params.Add(jsonutils.JSONTrue, "is_classic") listRet, err := modules.Networks.List(task.getClientSession(), params) if err != nil { return nil, err diff --git a/pkg/cloudcommon/agent/agent.go b/pkg/cloudcommon/agent/agent.go index cbda818066..c8f3659f9e 100644 --- a/pkg/cloudcommon/agent/agent.go +++ b/pkg/cloudcommon/agent/agent.go @@ -201,7 +201,7 @@ func (agent *SBaseAgent) getZoneByIP(session *mcclient.ClientSession) (jsonutils return nil, err } params.Add(jsonutils.NewString(listenIP.String()), "ip") - params.Add(jsonutils.JSONTrue, "is_on_premise") + params.Add(jsonutils.JSONTrue, "is_classic") params.Add(jsonutils.NewString("system"), "scope") networks, err := modules.Networks.List(session, params) if err != nil { diff --git a/pkg/compute/guestdrivers/baremetals.go b/pkg/compute/guestdrivers/baremetals.go index 47c021a100..349a02b67c 100644 --- a/pkg/compute/guestdrivers/baremetals.go +++ b/pkg/compute/guestdrivers/baremetals.go @@ -195,7 +195,7 @@ func (self *SBaremetalGuestDriver) Attach2RandomNetwork(guest *models.SGuest, ct if netConfig.Private { net, _ = wire.GetCandidatePrivateNetwork(userCred, models.NetworkManager.AllowScope(userCred), netConfig.Exit, netTypes) } else { - net, _ = wire.GetCandidatePublicNetwork(userCred, models.NetworkManager.AllowScope(userCred), netConfig.Exit, netTypes) + net, _ = wire.GetCandidateAutoAllocNetwork(userCred, models.NetworkManager.AllowScope(userCred), netConfig.Exit, netTypes) } if net != nil { netsAvaiable = append(netsAvaiable, *net) diff --git a/pkg/compute/guestdrivers/virtualization.go b/pkg/compute/guestdrivers/virtualization.go index e2516de213..31538e33d7 100644 --- a/pkg/compute/guestdrivers/virtualization.go +++ b/pkg/compute/guestdrivers/virtualization.go @@ -123,7 +123,7 @@ func (self *SVirtualizedGuestDriver) Attach2RandomNetwork(guest *models.SGuest, if netConfig.Private { net, _ = wire.GetCandidatePrivateNetwork(userCred, models.NetworkManager.AllowScope(userCred), netConfig.Exit, netTypes) } else { - net, _ = wire.GetCandidatePublicNetwork(userCred, models.NetworkManager.AllowScope(userCred), netConfig.Exit, netTypes) + net, _ = wire.GetCandidateAutoAllocNetwork(userCred, models.NetworkManager.AllowScope(userCred), netConfig.Exit, netTypes) } if net != nil { netsAvaiable = append(netsAvaiable, *net) diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 2af62145ab..d0bf1b309a 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -4076,7 +4076,7 @@ func (self *SHost) EnableNetif(ctx context.Context, userCred mcclient.TokenCrede return fmt.Errorf("fail to find private network %s", err) } if net == nil { - net, err = wire.GetCandidatePublicNetwork(userCred, NetworkManager.AllowScope(userCred), false, netTypes) + net, err = wire.GetCandidateAutoAllocNetwork(userCred, NetworkManager.AllowScope(userCred), false, netTypes) if err != nil { return fmt.Errorf("fail to find public network %s", err) } diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index a96568bf4c..a6a32b2393 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -116,6 +116,10 @@ type SNetwork struct { AllocPolicy string `width:"16" charset:"ascii" nullable:"true" get:"user" update:"user" create:"optional"` AllocTimoutSeconds int `default:"0" nullable:"true" get:"admin"` + + // 该网段是否用于自动分配IP地址,如果为false,则用户需要明确选择该网段,才会使用该网段分配IP, + // 如果为true,则用户不指定网段时,则自动从该值为true的网络中选择一个分配地址 + IsAutoAlloc tristate.TriState `nullable:"true" list:"user" get:"user" update:"user" create:"optional"` } func (manager *SNetworkManager) GetContextManagers() [][]db.IModelManager { @@ -1957,6 +1961,26 @@ func (manager *SNetworkManager) ListItemFilter( q = q.In("alloc_policy", input.AllocPolicy) } + if input.IsAutoAlloc != nil { + if *input.IsAutoAlloc { + q = q.IsTrue("is_auto_alloc") + } else { + q = q.IsFalse("is_auto_alloc") + } + } + + if input.IsClassic != nil { + subq := manager.Query("id") + wires := WireManager.Query("id", "vpc_id").SubQuery() + subq = subq.Join(wires, sqlchemy.Equals(wires.Field("id"), subq.Field("wire_id"))) + if *input.IsClassic { + subq = subq.Filter(sqlchemy.Equals(wires.Field("vpc_id"), api.DEFAULT_VPC_ID)) + } else { + subq = subq.Filter(sqlchemy.NotEquals(wires.Field("vpc_id"), api.DEFAULT_VPC_ID)) + } + q = q.In("id", subq.SubQuery()) + } + return q, nil } @@ -2025,6 +2049,16 @@ func (manager *SNetworkManager) InitializeData() error { return nil }) } + if n.IsAutoAlloc.IsNone() { + db.Update(&n, func() error { + if n.IsPublic { + n.IsAutoAlloc = tristate.True + } else { + n.IsAutoAlloc = tristate.False + } + return nil + }) + } } return nil } @@ -2232,6 +2266,7 @@ func (self *SNetwork) PerformSplit(ctx context.Context, userCred mcclient.TokenC // network.UserId = self.UserId network.IsSystem = self.IsSystem network.Description = self.Description + network.IsAutoAlloc = self.IsAutoAlloc err = NetworkManager.TableSpec().Insert(ctx, network) if err != nil { diff --git a/pkg/compute/models/wires.go b/pkg/compute/models/wires.go index dd9aae30ab..d4c6913707 100644 --- a/pkg/compute/models/wires.go +++ b/pkg/compute/models/wires.go @@ -642,6 +642,17 @@ func (self *SWire) getGatewayNetworkQuery(ownerId mcclient.IIdentityProvider, sc return q } +func (self *SWire) getAutoAllocNetworks(ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope) ([]SNetwork, error) { + q := self.getGatewayNetworkQuery(ownerId, scope) + q = q.IsTrue("is_auto_alloc") + nets := make([]SNetwork, 0) + err := db.FetchModelObjects(NetworkManager, q, &nets) + if err != nil { + return nil, err + } + return nets, nil +} + func (self *SWire) getPublicNetworks(ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope) ([]SNetwork, error) { q := self.getGatewayNetworkQuery(ownerId, scope) q = q.IsTrue("is_public") @@ -672,8 +683,8 @@ func (self *SWire) GetCandidatePrivateNetwork(ownerId mcclient.IIdentityProvider return ChooseCandidateNetworks(nets, isExit, serverTypes), nil } -func (self *SWire) GetCandidatePublicNetwork(ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, isExit bool, serverTypes []string) (*SNetwork, error) { - nets, err := self.getPublicNetworks(ownerId, scope) +func (self *SWire) GetCandidateAutoAllocNetwork(ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, isExit bool, serverTypes []string) (*SNetwork, error) { + nets, err := self.getAutoAllocNetworks(ownerId, scope) if err != nil { return nil, err } diff --git a/pkg/hostman/hostinfo/hostinfo.go b/pkg/hostman/hostinfo/hostinfo.go index 96dffe7a02..341eccb94a 100644 --- a/pkg/hostman/hostinfo/hostinfo.go +++ b/pkg/hostman/hostinfo/hostinfo.go @@ -776,7 +776,7 @@ func (h *SHostInfo) tryCreateNetworkOnWire() { params := jsonutils.NewDict() params.Set("ip", jsonutils.NewString(masterIp)) params.Set("mask", jsonutils.NewInt(int64(mask))) - params.Set("is_on_premise", jsonutils.JSONTrue) + params.Set("is_classic", jsonutils.JSONTrue) params.Set("server_type", jsonutils.NewString(api.NETWORK_TYPE_BAREMETAL)) ret, err := modules.Networks.PerformClassAction( hostutils.GetComputeSession(context.Background()), @@ -802,7 +802,7 @@ func (h *SHostInfo) fetchAccessNetworkInfo() { log.Debugf("Master ip %s to fetch wire", masterIp) params := jsonutils.NewDict() params.Set("ip", jsonutils.NewString(masterIp)) - params.Set("is_on_premise", jsonutils.JSONTrue) + params.Set("is_classic", jsonutils.JSONTrue) params.Set("scope", jsonutils.NewString("system")) params.Set("limit", jsonutils.NewInt(0)) // use default vpc @@ -1142,7 +1142,7 @@ func (h *SHostInfo) uploadNetworkInfo() { if len(nic.Network) == 0 { kwargs := jsonutils.NewDict() kwargs.Set("ip", jsonutils.NewString(nic.Ip)) - kwargs.Set("is_on_premise", jsonutils.JSONTrue) + kwargs.Set("is_classic", jsonutils.JSONTrue) kwargs.Set("scope", jsonutils.NewString("system")) kwargs.Set("limit", jsonutils.NewInt(0)) diff --git a/pkg/mcclient/modules/mod_networks.go b/pkg/mcclient/modules/mod_networks.go index 8cc81c997f..12912876f7 100644 --- a/pkg/mcclient/modules/mod_networks.go +++ b/pkg/mcclient/modules/mod_networks.go @@ -27,7 +27,7 @@ func init() { "wire_id", "wire", "is_public", "public_scope", "exit", "Ports", "vnics", "guest_gateway", "group_vnics", "bm_vnics", "reserve_vnics", "lb_vnics", - "server_type", "Status"}, + "server_type", "Status", "is_auto_alloc"}, []string{}) registerCompute(&Networks) diff --git a/pkg/scheduler/algorithm/predicates/network_predicate.go b/pkg/scheduler/algorithm/predicates/network_predicate.go index 4887767264..8af8d9c8bc 100644 --- a/pkg/scheduler/algorithm/predicates/network_predicate.go +++ b/pkg/scheduler/algorithm/predicates/network_predicate.go @@ -192,8 +192,8 @@ func (p *NetworkPredicate) Execute(u *core.Unit, c core.Candidater) (bool, []cor }) }*/ } else { - if !n.IsPublic { - appendError(FailReason{Reason: fmt.Sprintf("Network %s is private", n.Name), Type: NetworkPrivate}) + if n.IsAutoAlloc.IsFalse() { + appendError(FailReason{Reason: fmt.Sprintf("Network %s is not auto alloc", n.Name), Type: NetworkPrivate}) } /*else if rbacutils.TRbacScope(n.PublicScope) == rbacutils.ScopeDomain { netDomain := n.DomainId reqDomain := domain