Skip to content

Commit

Permalink
feature: add is_auto_alloc flag to networks (#7473)
Browse files Browse the repository at this point in the history
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
  • Loading branch information
swordqiu and Qiu Jian committed Aug 3, 2020
1 parent 1929076 commit 1ba9715
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 16 deletions.
11 changes: 11 additions & 0 deletions cmd/climc/shell/compute/networks.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/compute/network.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -299,4 +306,7 @@ type NetworkUpdateInput struct {

// 分配策略
AllocPolicy string `json:"alloc_policy"`

// 是否加入自动分配地址池
IsAutoAlloc *bool `json:"is_auto_alloc"`
}
4 changes: 2 additions & 2 deletions pkg/baremetal/manager.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/baremetal/pxe/dhcp.go
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/baremetal/tasks/baseprepare.go
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudcommon/agent/agent.go
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/guestdrivers/baremetals.go
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/guestdrivers/virtualization.go
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/models/hosts.go
Expand Up @@ -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)
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/compute/models/networks.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 13 additions & 2 deletions pkg/compute/models/wires.go
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/hostman/hostinfo/hostinfo.go
Expand Up @@ -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()),
Expand All @@ -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
Expand Down Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion pkg/mcclient/modules/mod_networks.go
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/scheduler/algorithm/predicates/network_predicate.go
Expand Up @@ -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
Expand Down

0 comments on commit 1ba9715

Please sign in to comment.