mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 02:37:24 +08:00
Merge pull request #11641 from swordqiu/feature/qj-server-migrate-network
feature: allow server switch network without service interruption
This commit is contained in:
@@ -87,6 +87,7 @@ func init() {
|
||||
cmd.Perform("remote-update", new(options.ServerRemoteUpdateOptions))
|
||||
cmd.Perform("create-eip", &options.ServerCreateEipOptions{})
|
||||
cmd.Perform("make-sshable", &options.ServerMakeSshableOptions{})
|
||||
cmd.Perform("migrate-network", &options.ServerMigrateNetworkOptions{})
|
||||
|
||||
cmd.Get("vnc", new(options.ServerIdOptions))
|
||||
cmd.Get("desc", new(options.ServerIdOptions))
|
||||
|
||||
@@ -549,3 +549,10 @@ type ServerResizeDiskInput struct {
|
||||
|
||||
DiskResizeInput
|
||||
}
|
||||
|
||||
type ServerMigrateNetworkInput struct {
|
||||
// Source network Id
|
||||
Src string `json:"src"`
|
||||
// Destination network Id
|
||||
Dest string `json:"dest"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/util/netutils"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
func (guest *SGuest) AllowPerformMigrateNetwork(
|
||||
ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject,
|
||||
input api.ServerMigrateNetworkInput,
|
||||
) bool {
|
||||
return db.IsAdminAllowPerform(userCred, guest, "migrate-network")
|
||||
}
|
||||
|
||||
/*
|
||||
* Migrate a server from one network to another network, without change IP address
|
||||
* Scenerio: the server used to be in a VPC, migrate it to a underlay network without network interruption
|
||||
*/
|
||||
func (guest *SGuest) PerformMigrateNetwork(ctx context.Context, userCred mcclient.TokenCredential,
|
||||
query jsonutils.JSONObject,
|
||||
input api.ServerMigrateNetworkInput,
|
||||
) (jsonutils.JSONObject, error) {
|
||||
if guest.Hypervisor != api.HYPERVISOR_KVM {
|
||||
return nil, errors.Wrap(httperrors.ErrNotSupported, "operation not supported for this hypervisor")
|
||||
}
|
||||
|
||||
// first validate it against the source network, ensure the following:
|
||||
// 1. the network is attach to this guest
|
||||
srcModel, err := NetworkManager.FetchByIdOrName(userCred, input.Src)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == sql.ErrNoRows {
|
||||
return nil, errors.Wrapf(httperrors.ErrResourceNotFound, "source network %s not found", input.Src)
|
||||
} else {
|
||||
return nil, errors.Wrap(err, "NetworkManager.FetchByIdOrName")
|
||||
}
|
||||
}
|
||||
srcNics, err := guest.GetNetworks(srcModel.GetId())
|
||||
if err != nil {
|
||||
if errors.Cause(err) == sql.ErrNoRows {
|
||||
return nil, errors.Wrapf(httperrors.ErrBadRequest, "server not attach to network %s", input.Src)
|
||||
} else {
|
||||
return nil, errors.Wrap(err, "GetNetworks")
|
||||
}
|
||||
}
|
||||
if len(srcNics) == 0 {
|
||||
return nil, errors.Wrapf(httperrors.ErrBadRequest, "server not attach to network %s", input.Src)
|
||||
} else if len(srcNics) > 1 {
|
||||
return nil, errors.Wrapf(httperrors.ErrNotSupported, "not support to migrate multiple interfaces")
|
||||
}
|
||||
srcNic := srcNics[0]
|
||||
ipAddr, err := netutils.NewIPV4Addr(srcNic.IpAddr)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "NewIPV4Addr")
|
||||
}
|
||||
// next validate against the destination network, ensure the following:
|
||||
// 1. the network is reachable to this server
|
||||
// 1. the IP address is availalble in the new network
|
||||
destModel, err := NetworkManager.FetchByIdOrName(userCred, input.Dest)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == sql.ErrNoRows {
|
||||
return nil, errors.Wrapf(httperrors.ErrResourceNotFound, "destination network %s not found", input.Src)
|
||||
} else {
|
||||
return nil, errors.Wrap(err, "NetworkManager.FetchByIdOrName")
|
||||
}
|
||||
}
|
||||
destNet := destModel.(*SNetwork)
|
||||
|
||||
host := guest.GetHost()
|
||||
if host == nil {
|
||||
return nil, errors.Wrap(httperrors.ErrInvalidStatus, "guest is not allocated!")
|
||||
}
|
||||
if destNet.isOneCloudVpcNetwork() {
|
||||
// vpc network should be in the same Zone
|
||||
destZone := destNet.GetZone()
|
||||
if destZone == nil || destZone.Id != host.ZoneId {
|
||||
return nil, errors.Wrap(httperrors.ErrBadRequest, "destination overlay network not in same zone as server")
|
||||
}
|
||||
} else {
|
||||
// underlay network should be reachable in wire
|
||||
var destWire *SWire
|
||||
wires := host.getAttachedWires()
|
||||
for i := range wires {
|
||||
if wires[i].Id == destNet.WireId {
|
||||
// reachable
|
||||
destWire = &wires[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if destWire == nil {
|
||||
return nil, errors.Wrap(httperrors.ErrBadRequest, "destination underlay network not reachable")
|
||||
}
|
||||
}
|
||||
|
||||
lockman.LockObject(ctx, destNet)
|
||||
defer lockman.ReleaseObject(ctx, destNet)
|
||||
|
||||
if !destNet.IsAddressInRange(ipAddr) {
|
||||
return nil, errors.Wrapf(httperrors.ErrBadRequest, "ip %s not in range of destination network", ipAddr.String())
|
||||
}
|
||||
used, err := destNet.isAddressUsed(ipAddr.String())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "isAddressUsed")
|
||||
}
|
||||
if used {
|
||||
return nil, errors.Wrapf(httperrors.ErrBadRequest, "ip %s has been allocated in destination network", ipAddr.String())
|
||||
}
|
||||
|
||||
// perform the database change
|
||||
_, err = db.Update(&srcNic, func() error {
|
||||
srcNic.NetworkId = destNet.Id
|
||||
srcNic.MappedIpAddr = "" // reset MappedIpAddr anyway
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fail to update nic network_id")
|
||||
}
|
||||
|
||||
// synchronize the change to host, and wait it to be effective
|
||||
err = guest.StartSyncTask(ctx, userCred, false, "")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fail to SyncTask")
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
@@ -790,12 +790,10 @@ func (s *SKVMGuestInstance) SaveDesc(desc jsonutils.JSONObject) error {
|
||||
{
|
||||
// fill in ovn vpc nic bridge field
|
||||
nics, _ := s.Desc.GetArray("nics")
|
||||
ovnBridge := options.HostOptions.OvnIntegrationBridge
|
||||
for _, nic := range nics {
|
||||
vpcProvider, _ := nic.GetString("vpc", "provider")
|
||||
if vpcProvider == compute.VPC_PROVIDER_OVN {
|
||||
if !nic.Contains("bridge") {
|
||||
nicjd := nic.(*jsonutils.JSONDict)
|
||||
nicjd.Set("bridge", jsonutils.NewString(ovnBridge))
|
||||
nicjd.Set("bridge", jsonutils.NewString(getNicBridge(nic)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1099,7 +1097,7 @@ func (s *SKVMGuestInstance) compareDescCdrom(newDesc jsonutils.JSONObject) *stri
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SKVMGuestInstance) compareDescNetworks(newDesc jsonutils.JSONObject) ([]jsonutils.JSONObject, []jsonutils.JSONObject) {
|
||||
func (s *SKVMGuestInstance) compareDescNetworks(newDesc jsonutils.JSONObject) ([]jsonutils.JSONObject, []jsonutils.JSONObject, [][]jsonutils.JSONObject) {
|
||||
var isValid = func(net jsonutils.JSONObject) bool {
|
||||
driver, _ := net.GetString("driver")
|
||||
return driver == "virtio"
|
||||
@@ -1117,9 +1115,11 @@ func (s *SKVMGuestInstance) compareDescNetworks(newDesc jsonutils.JSONObject) ([
|
||||
}
|
||||
|
||||
var delNics, addNics = []jsonutils.JSONObject{}, []jsonutils.JSONObject{}
|
||||
var changedNics = [][]jsonutils.JSONObject{}
|
||||
nics, _ := newDesc.GetArray("nics")
|
||||
for _, n := range nics {
|
||||
if isValid(n) {
|
||||
// assume all nics in new desc are new
|
||||
addNics = append(addNics, n)
|
||||
}
|
||||
}
|
||||
@@ -1129,25 +1129,77 @@ func (s *SKVMGuestInstance) compareDescNetworks(newDesc jsonutils.JSONObject) ([
|
||||
if isValid(n) {
|
||||
idx := findNet(addNics, n)
|
||||
if idx >= 0 {
|
||||
// remove n
|
||||
// check if bridge changed
|
||||
changedNics = append(changedNics, []jsonutils.JSONObject{
|
||||
n, // old
|
||||
addNics[idx], // new
|
||||
})
|
||||
// remove existing nic from new
|
||||
addNics = append(addNics[:idx], addNics[idx+1:]...)
|
||||
} else {
|
||||
// not found, remove the nic
|
||||
delNics = append(delNics, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
return delNics, addNics
|
||||
return delNics, addNics, changedNics
|
||||
}
|
||||
|
||||
func getNicBridge(nic jsonutils.JSONObject) string {
|
||||
bridge, _ := nic.GetString("bridge")
|
||||
if len(bridge) == 0 {
|
||||
vpcProvider, _ := nic.GetString("vpc", "provider")
|
||||
if vpcProvider == compute.VPC_PROVIDER_OVN {
|
||||
bridge = options.HostOptions.OvnIntegrationBridge
|
||||
}
|
||||
}
|
||||
return bridge
|
||||
}
|
||||
|
||||
func onNicChange(oldNic, newNic jsonutils.JSONObject) error {
|
||||
oldbr := getNicBridge(oldNic)
|
||||
oldifname, _ := oldNic.GetString("ifname")
|
||||
newbr := getNicBridge(newNic)
|
||||
newifname, _ := newNic.GetString("ifname")
|
||||
if oldbr != newbr {
|
||||
// bridge changed
|
||||
if oldifname == newifname {
|
||||
output, err := procutils.NewRemoteCommandAsFarAsPossible("ovs-vsctl",
|
||||
"--", "del-port", oldbr, oldifname,
|
||||
"--", "add-port", newbr, newifname,
|
||||
).Output()
|
||||
log.Infof("ovs-vsctl del-port %s %s add-port %s %s: %s", oldbr, oldifname, newbr, newifname, output)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "NewRemoteCommandAsFarAsPossible")
|
||||
}
|
||||
} else {
|
||||
log.Errorf("cannot change both bridge(%s!=%s) and ifname(%s!=%s)!!!!!", oldbr, newbr, oldifname, newifname)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SKVMGuestInstance) SyncConfig(ctx context.Context, desc jsonutils.JSONObject, fwOnly bool) (jsonutils.JSONObject, error) {
|
||||
var delDisks, addDisks, delNetworks, addNetworks []jsonutils.JSONObject
|
||||
var changedNetworks [][]jsonutils.JSONObject
|
||||
var cdrom *string
|
||||
|
||||
if !fwOnly {
|
||||
delDisks, addDisks = s.compareDescDisks(desc)
|
||||
cdrom = s.compareDescCdrom(desc)
|
||||
delNetworks, addNetworks = s.compareDescNetworks(desc)
|
||||
delNetworks, addNetworks, changedNetworks = s.compareDescNetworks(desc)
|
||||
}
|
||||
|
||||
if len(changedNetworks) > 0 && s.IsRunning() {
|
||||
// process changed networks
|
||||
for i := range changedNetworks {
|
||||
err := onNicChange(changedNetworks[i][0], changedNetworks[i][1])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "onNicChange")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.SaveDesc(desc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1122,3 +1122,13 @@ func (opts *ServerMakeSshableOptions) Params() (jsonutils.JSONObject, error) {
|
||||
}
|
||||
return jsonutils.Marshal(opts), nil
|
||||
}
|
||||
|
||||
type ServerMigrateNetworkOptions struct {
|
||||
BaseIdOptions
|
||||
|
||||
computeapi.ServerMigrateNetworkInput
|
||||
}
|
||||
|
||||
func (opts *ServerMigrateNetworkOptions) Params() (jsonutils.JSONObject, error) {
|
||||
return jsonutils.Marshal(opts), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user