简介
在 pod 启动后,pod 只有一块 eth0 网卡,bridge 的情况下,虚拟机启动后则有三个网卡加一个 bridge。分析一下实现的过程。
virt-handler
processVmUpdate
| -- vmUpdateHelperDefault
| -- setupNetwork
| -- Setup
| -- netConfigurator.SetupPodNetworkPhase1
| -- getPhase1NICs
err = configState.Run(
nics,
preConfigStateRun,
func(nic *podNIC) error {
return nic.discoverAndStoreCache()
},
func(nic *podNIC) error {
if nic.infraConfigurator == nil {
return nil
}
return nic.infraConfigurator.PreparePodNetworkInterface()
})
if err != nil {
return fmt.Errorf("failed setup pod network phase1: %w", err)
}
getPhase1NICs []podNIC
podNIC.vmiSpecIface v1.Interface
podNIC.vmiSpecNetwork v1.Network
nics[idx].podInterfaceName == eth0 (关联的网卡)
configState.Run 在 netns 依次执行 preConfigStateRun,discoverFunc 和 configFunc
针对 podNIC
discoverFunc
| -- nic.discoverAndStoreCache
| -- setPodInterfaceCache // 从 eth0 读取 ipv4/ipv6,存到 cache
| -- l.infraConfigurator.DiscoverPodNetworkInterface // 获取 pod 内路由,生成 bridgeInterfaceName 和 tapDeviceName;获取 mac (如果 yaml 写了 mac,则使用此 mac ,如果没写,则使用 eth0 mac)
| -- l.infraConfigurator.GenerateNonRecoverableDHCPConfig() // 使用 mac,IP,路由 配置 dhcp 配置
| -- l.infraConfigurator.GenerateNonRecoverableDomainIfaceSpec() // 使用 mac 生成 domainInterface 然后存到 cache
configFunc
| -- nic.infraConfigurator.PreparePodNetworkInterface()
| -- LinkSetDown // 网卡down
| -- AddrDel // 删 ip
| -- switchPodInterfaceWithDummy // 创建 dummy 口,将 ip 配上,修改 eth0 名称
| -- createBridge // 创建网桥,设置 mac
| -- createAndBindTapToBridge // 创建 tap 绑定网桥
phase1 结束
virt-launcher
virt-handler 通过 grpc 通知 virt-launcher 进行 SyncVMI,进行虚拟机创建,创建时生成 xml 信息。
SyncVMI
| -- generateConverterContext // 关于网络方面可能涉及 sriov 网卡
| -- Convert_v1_VirtualMachineInstance_To_api_Domain // 主要的 xml 生成方法,根据 vmi
的 yaml 和信息生成 interface 段的 xml。
| -- preStartHook // 预创建,提前检查或准备
| -- SetupPodNetworkPhase2
| -- getPhase2NICs
| -- PlugPhase2 // phase2 步骤 设置 xml 中网卡 mac