-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hotunplug, unpluggedPodNic: implement reverse phase1
The commit includes removal of bridge, tap and dummy nic if they exist. Follow up commits will take care of the caches files deletion. Notice: 1. The cleanup is general and not binding specific. Any link created by whatecer binding should be removed (although we curretnly support only bridge binding hotunplug). 2. Error removing one of the links doesn't block removal on the others. Signed-off-by: Alona Paz <alkaplan@redhat.com>
- Loading branch information
1 parent
2282f81
commit 946e040
Showing
6 changed files
with
191 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package network | ||
|
||
import ( | ||
"errors" | ||
|
||
v1 "kubevirt.io/api/core/v1" | ||
|
||
"kubevirt.io/kubevirt/pkg/network/namescheme" | ||
|
||
"github.com/vishvananda/netlink" | ||
k8serrors "k8s.io/apimachinery/pkg/util/errors" | ||
|
||
netdriver "kubevirt.io/kubevirt/pkg/network/driver" | ||
virtnetlink "kubevirt.io/kubevirt/pkg/network/link" | ||
) | ||
|
||
type Unpluggedpodnic struct { | ||
networkName string | ||
handler netdriver.NetworkHandler | ||
} | ||
|
||
func NewUnpluggedpodnic(network string, handler netdriver.NetworkHandler) Unpluggedpodnic { | ||
return Unpluggedpodnic{networkName: network, handler: handler} | ||
} | ||
|
||
func (c Unpluggedpodnic) UnplugPhase1() error { | ||
var unplugErrors []error | ||
|
||
dummyNet := v1.Network{ | ||
Name: c.networkName, | ||
NetworkSource: v1.NetworkSource{ | ||
Multus: &v1.MultusNetwork{}, | ||
}, | ||
} // TODO - remove this dummy network once we will have the absent network object | ||
podInterfaceName := namescheme.HashedPodInterfaceName(dummyNet) | ||
bridgeName := virtnetlink.GenerateBridgeName(podInterfaceName) | ||
err := c.delLinkIfExists(bridgeName) | ||
if err != nil { | ||
unplugErrors = append(unplugErrors, err) | ||
} | ||
|
||
// remove extra nic | ||
dummyIfaceName := virtnetlink.GenerateNewBridgedVmiInterfaceName(podInterfaceName) | ||
err = c.delLinkIfExists(dummyIfaceName) | ||
if err != nil { | ||
unplugErrors = append(unplugErrors, err) | ||
} | ||
|
||
// remove tap if exists | ||
tapDeviceName := virtnetlink.GenerateTapDeviceName(podInterfaceName) | ||
err = c.delLinkIfExists(tapDeviceName) | ||
if err != nil { | ||
unplugErrors = append(unplugErrors, err) | ||
} | ||
|
||
// clean caches | ||
// TODO remove all three cache files | ||
|
||
return k8serrors.NewAggregate(unplugErrors) | ||
|
||
} | ||
|
||
func (c Unpluggedpodnic) delLinkIfExists(linkName string) error { | ||
link, err := c.handler.LinkByName(linkName) | ||
if err != nil { | ||
var linkNotFoundErr netlink.LinkNotFoundError | ||
if errors.As(err, &linkNotFoundErr) { | ||
return nil | ||
} | ||
return err | ||
} | ||
return c.handler.LinkDel(link) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2023 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package network_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/golang/mock/gomock" | ||
|
||
"github.com/vishvananda/netlink" | ||
|
||
netdriver "kubevirt.io/kubevirt/pkg/network/driver" | ||
network "kubevirt.io/kubevirt/pkg/network/setup" | ||
) | ||
|
||
var _ = Describe("Unpluggedpodnic", func() { | ||
var ( | ||
mockHandler *netdriver.MockNetworkHandler | ||
ctrl *gomock.Controller | ||
unpluggedpodnic network.Unpluggedpodnic | ||
tapLink *netlink.GenericLink | ||
dummyIfaceLink *netlink.GenericLink | ||
bridgeLink *netlink.GenericLink | ||
) | ||
const ( | ||
tapName = "tap16477688c0e" | ||
dummyIfaceName = "16477688c0e-nic" | ||
bridgeName = "k6t-16477688c0e" | ||
networkName = "blue" | ||
) | ||
|
||
BeforeEach(func() { | ||
ctrl = gomock.NewController(GinkgoT()) | ||
mockHandler = netdriver.NewMockNetworkHandler(ctrl) | ||
|
||
unpluggedpodnic = network.NewUnpluggedpodnic(networkName, mockHandler) | ||
|
||
tapLink = &netlink.GenericLink{} | ||
dummyIfaceLink = &netlink.GenericLink{} | ||
bridgeLink = &netlink.GenericLink{} | ||
}) | ||
|
||
Context("UnplugPhase1", func() { | ||
It("successfully when bridge, tap and dummy nic exist", func() { | ||
mockHandler.EXPECT().LinkByName(tapName).Return(tapLink, nil) | ||
mockHandler.EXPECT().LinkByName(dummyIfaceName).Return(dummyIfaceLink, nil) | ||
mockHandler.EXPECT().LinkByName(bridgeName).Return(bridgeLink, nil) | ||
mockHandler.EXPECT().LinkDel(tapLink).Return(nil) | ||
mockHandler.EXPECT().LinkDel(dummyIfaceLink).Return(nil) | ||
mockHandler.EXPECT().LinkDel(bridgeLink).Return(nil) | ||
|
||
Expect(unpluggedpodnic.UnplugPhase1()).To(Succeed()) | ||
}) | ||
It("successfully when dummy nic doesn't exist", func() { | ||
mockHandler.EXPECT().LinkByName(tapName).Return(tapLink, nil) | ||
mockHandler.EXPECT().LinkByName(dummyIfaceName).Return(nil, netlink.LinkNotFoundError{}) | ||
mockHandler.EXPECT().LinkByName(bridgeName).Return(bridgeLink, nil) | ||
mockHandler.EXPECT().LinkDel(tapLink).Return(nil) | ||
mockHandler.EXPECT().LinkDel(bridgeLink).Return(nil) | ||
|
||
Expect(unpluggedpodnic.UnplugPhase1()).To(Succeed()) | ||
}) | ||
It("partially fails when some of the devices fail to be removed", func() { | ||
const ( | ||
linkByNameErr1 = "link by name error1" | ||
linkByNameErr2 = "link by name error2" | ||
) | ||
|
||
mockHandler.EXPECT().LinkByName(tapName).Return(tapLink, nil) | ||
mockHandler.EXPECT().LinkByName(dummyIfaceName).Return(nil, fmt.Errorf(linkByNameErr1)) | ||
mockHandler.EXPECT().LinkByName(bridgeName).Return(bridgeLink, fmt.Errorf(linkByNameErr2)) | ||
mockHandler.EXPECT().LinkDel(tapLink).Return(nil) | ||
|
||
err := unpluggedpodnic.UnplugPhase1() | ||
Expect(err.Error()).To(ContainSubstring(linkByNameErr1)) | ||
Expect(err.Error()).To(ContainSubstring(linkByNameErr1)) | ||
}) | ||
}) | ||
}) |