@@ -641,6 +641,16 @@ func (c *Controller) reconcileAllocateSubnets(cachedPod, pod *v1.Pod, needAlloca
641
641
podName := c .getNameByPod (pod )
642
642
// todo: isVmPod, getPodType, getNameByPod has duplicated logic
643
643
644
+ var err error
645
+ var isMigrating bool
646
+ var vmKey , srcNodeName , targetNodeName string
647
+ if isVMPod && c .config .EnableKeepVMIP {
648
+ vmKey = fmt .Sprintf ("%s/%s" , namespace , vmName )
649
+ if isMigrating , srcNodeName , targetNodeName , err = c .migrateVM (pod , vmKey ); err != nil {
650
+ klog .Error (err )
651
+ return nil , err
652
+ }
653
+ }
644
654
// Avoid create lsp for already running pod in ovn-nb when controller restart
645
655
for _ , podNet := range needAllocatePodNets {
646
656
// the subnet may changed when alloc static ip from the latter subnet after ns supports multi subnets
@@ -669,10 +679,11 @@ func (c *Controller) reconcileAllocateSubnets(cachedPod, pod *v1.Pod, needAlloca
669
679
delete (pod .Annotations , fmt .Sprintf (util .PodNicAnnotationTemplate , podNet .ProviderName ))
670
680
}
671
681
pod .Annotations [fmt .Sprintf (util .AllocatedAnnotationTemplate , podNet .ProviderName )] = "true"
672
- if isVMPod && c .config .EnableKeepVMIP {
682
+
683
+ if vmKey != "" {
673
684
pod .Annotations [fmt .Sprintf (util .VMTemplate , podNet .ProviderName )] = vmName
674
685
if err := c .changeVMSubnet (vmName , namespace , podNet .ProviderName , subnet .Name ); err != nil {
675
- klog .Errorf ("change subnet of pod %s/%s to %s failed: %v" , namespace , name , subnet .Name , err )
686
+ klog .Errorf ("vm %s change subnet to %s failed: %v" , vmKey , subnet .Name , err )
676
687
return nil , err
677
688
}
678
689
}
@@ -720,12 +731,31 @@ func (c *Controller) reconcileAllocateSubnets(cachedPod, pod *v1.Pod, needAlloca
720
731
DHCPv6OptionsUUID : subnet .Status .DHCPv6OptionsUUID ,
721
732
}
722
733
723
- if err := c .OVNNbClient .CreateLogicalSwitchPort (subnet .Name , portName , ipStr , mac , podName , pod .Namespace , portSecurity , securityGroupAnnotation , vips , podNet .Subnet .Spec .EnableDHCP , dhcpOptions , subnet .Spec .Vpc ); err != nil {
734
+ if err := c .OVNNbClient .CreateLogicalSwitchPort (subnet .Name , portName , ipStr , mac , podName , pod .Namespace ,
735
+ portSecurity , securityGroupAnnotation , vips , podNet .Subnet .Spec .EnableDHCP , dhcpOptions , subnet .Spec .Vpc ); err != nil {
724
736
c .recorder .Eventf (pod , v1 .EventTypeWarning , "CreateOVNPortFailed" , err .Error ())
725
737
klog .Errorf ("%v" , err )
726
738
return nil , err
727
739
}
728
740
741
+ if vmKey != "" {
742
+ if isMigrating {
743
+ if err = c .setVMLSPMigrationOptions (portName , srcNodeName , targetNodeName ); err != nil {
744
+ klog .Error (err )
745
+ return nil , err
746
+ }
747
+ pod .Annotations [util .OVNMigrationAnnotation ] = "true"
748
+ } else {
749
+ if migrate , ok := pod .Annotations [util .OVNMigrationAnnotation ]; ok && migrate == "true" {
750
+ if err = c .cleanVMLSPMigrationOptions (portName ); err != nil {
751
+ klog .Error (err )
752
+ return nil , err
753
+ }
754
+ pod .Annotations [util .OVNMigratedAnnotation ] = "true"
755
+ }
756
+ }
757
+ }
758
+
729
759
if pod .Annotations [fmt .Sprintf (util .Layer2ForwardAnnotationTemplate , podNet .ProviderName )] == "true" {
730
760
if err := c .OVNNbClient .EnablePortLayer2forward (portName ); err != nil {
731
761
c .recorder .Eventf (pod , v1 .EventTypeWarning , "SetOVNPortL2ForwardFailed" , err .Error ())
@@ -2089,3 +2119,72 @@ func (c *Controller) getVirtualIPs(pod *v1.Pod, podNets []*kubeovnNet) map[strin
2089
2119
}
2090
2120
return vipsMap
2091
2121
}
2122
+
2123
+ // migrate vm return migrate, src node, target node, err
2124
+ func (c * Controller ) migrateVM (pod * v1.Pod , vmKey string ) (bool , string , string , error ) {
2125
+ if migrated , ok := pod .Annotations [util .OVNMigratedAnnotation ]; ok && migrated == "true" {
2126
+ klog .Infof ("vm %s is migrated" , vmKey )
2127
+ return false , "" , "" , nil
2128
+ }
2129
+
2130
+ migratePhase , ok := pod .Annotations [util .MigrationPhaseAnnotation ]
2131
+ if ! ok {
2132
+ // not migrate vm
2133
+ return false , "" , "" , nil
2134
+ }
2135
+ // check migrate phase
2136
+ if migratePhase == "" {
2137
+ err := fmt .Errorf ("vm %s migration phase is empty" , vmKey )
2138
+ klog .Error (err )
2139
+ return false , "" , "" , err
2140
+ }
2141
+ if migratePhase == util .MigrationStateSucceeded {
2142
+ klog .Infof ("vm %s migration is succeeded" , vmKey )
2143
+ return false , "" , "" , nil
2144
+ }
2145
+ if migratePhase == util .MigrationStateFailed {
2146
+ klog .Warningf ("vm %s migration is failed" , vmKey )
2147
+ return false , "" , "" , nil
2148
+ }
2149
+ if migratePhase == util .MigrationStateStarted {
2150
+ if isTarget , ok := pod .Annotations [util .MigrationTargetAnnotation ]; ok && isTarget == "true" {
2151
+ klog .Infof ("start to migrate target vm %s" , vmKey )
2152
+ // this pod is target vm pod
2153
+ srcNode , ok := pod .Annotations [util .MigrationSourceNodeAnnotation ]
2154
+ if ! ok || srcNode == "" {
2155
+ err := fmt .Errorf ("vm %s migration source node is not set" , vmKey )
2156
+ klog .Error (err )
2157
+ return false , "" , "" , err
2158
+ }
2159
+ targetNode := pod .Spec .NodeName
2160
+ if targetNode == "" {
2161
+ err := fmt .Errorf ("vm %s migration target node is not set" , vmKey )
2162
+ klog .Error (err )
2163
+ return false , "" , "" , err
2164
+ }
2165
+ return true , srcNode , targetNode , nil
2166
+ }
2167
+ klog .Infof ("start to migrate src vm %s" , vmKey )
2168
+ }
2169
+ return false , "" , "" , nil
2170
+ }
2171
+
2172
+ func (c * Controller ) setVMLSPMigrationOptions (portName , srcNodeName , targetNodeName string ) error {
2173
+ klog .Infof ("set migrate options for lsp %s" , portName )
2174
+ if err := c .OVNNbClient .SetLogicalSwitchPortMigrateOptions (portName , srcNodeName , targetNodeName ); err != nil {
2175
+ err = fmt .Errorf ("failed to set migrate options for lsp %s, %v" , portName , err )
2176
+ klog .Error (err )
2177
+ return err
2178
+ }
2179
+ return nil
2180
+ }
2181
+
2182
+ func (c * Controller ) cleanVMLSPMigrationOptions (portName string ) error {
2183
+ klog .Infof ("clean migrate options for lsp %s" , portName )
2184
+ if err := c .OVNNbClient .CleanLogicalSwitchPortMigrateOptions (portName ); err != nil {
2185
+ err = fmt .Errorf ("failed to clean migrate options for lsp %s, %v" , portName , err )
2186
+ klog .Error (err )
2187
+ return err
2188
+ }
2189
+ return nil
2190
+ }
0 commit comments