@@ -953,26 +953,31 @@ func migrateWorkerNodeIP(nodeName, fromIP, targetIP string, invertOrder bool) (e
953953
954954 // Define a function to change the IP address for later use.
955955 changeIPAddress := func () error {
956- // Add new IP first - this will preserve the default route.
957956 newIPMask := targetIP + "/" + mask
958- framework .Logf ("Adding new IP address %s to node %s" , newIPMask , nodeName )
959- // Add cleanup command.
960- cleanupCmd := []string {"ip" , "address" , "del" , newIPMask , "dev" , iface }
957+
958+ // Delete current IP address. If you add a second ip from the same subnet to an interface, it will
959+ // be considered a secondary IP address and will be deleted together with the primary (aka old) IP.
960+ framework .Logf ("Deleting current IP address %s from node %s" , parsedNetIPMask .String (), nodeName )
961+ // Add cleanup command to add original IP back to the end of the cleanupCommands list.
962+ // This way, we preserve first delete then add new IP sequence.
963+ cleanupCmd := []string {"ip" , "address" , "add" , parsedNetIPMask .String (), "dev" , iface }
961964 cleanupCommands = append (cleanupCommands , cleanupCmd )
962965 // Run command.
963- _ , err = infraprovider .Get ().ExecK8NodeCommand (nodeName , []string {"ip" , "address" , "add " , newIPMask , "dev" , iface })
966+ _ , err = infraprovider .Get ().ExecK8NodeCommand (nodeName , []string {"ip" , "address" , "del " , parsedNetIPMask . String () , "dev" , iface })
964967 if err != nil {
965- return fmt . Errorf ( "failed to add new IP %s to interface %s on node %s: %v" , newIPMask , iface , nodeName , err )
968+ return err
966969 }
967- // Delete current IP address. On rollback, first add the old IP and then delete the new one.
968- framework .Logf ("Deleting current IP address %s from node %s" , parsedNetIPMask .String (), nodeName )
969- // Add cleanup command.
970- cleanupCmd = []string {"ip" , "address" , "add" , parsedNetIPMask .String (), "dev" , iface }
970+
971+ // Now add new IP.
972+ framework .Logf ("Adding new IP address %s to node %s" , newIPMask , nodeName )
973+ // Add cleanup command to remove the new IP address to the beginning of the cleanupCommands list.
974+ cleanupCmd = []string {"ip" , "address" , "del" , newIPMask , "dev" , iface }
971975 cleanupCommands = append ([][]string {cleanupCmd }, cleanupCommands ... )
976+
972977 // Run command.
973- _ , err = infraprovider .Get ().ExecK8NodeCommand (nodeName , []string {"ip" , "address" , "del " , parsedNetIPMask . String () , "dev" , iface })
978+ _ , err = infraprovider .Get ().ExecK8NodeCommand (nodeName , []string {"ip" , "address" , "add " , newIPMask , "dev" , iface })
974979 if err != nil {
975- return err
980+ return fmt . Errorf ( "failed to add new IP %s to interface %s on node %s: %v" , newIPMask , iface , nodeName , err )
976981 }
977982 return nil
978983 }
0 commit comments