Skip to content

Commit

Permalink
network, vmispec: Move multus network filtering helper
Browse files Browse the repository at this point in the history
Signed-off-by: Ram Lavi <[email protected]>
  • Loading branch information
RamLavi committed Aug 18, 2022
1 parent 417fce4 commit 83a55bc
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/network/vmispec/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_test(
name = "go_default_test",
srcs = [
"interface_test.go",
"network_test.go",
"vmispec_suite_test.go",
],
deps = [
Expand Down
13 changes: 13 additions & 0 deletions pkg/network/vmispec/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ func LookupPodNetwork(networks []v1.Network) *v1.Network {
}
return nil
}

func FilterMultusNonDefaultNetworks(networks []v1.Network) []v1.Network {
var multusNetworks []v1.Network
for _, network := range networks {
if network.Multus != nil {
if network.Multus.Default {
continue
}
multusNetworks = append(multusNetworks, network)
}
}
return multusNetworks
}
88 changes: 88 additions & 0 deletions pkg/network/vmispec/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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 2022 Red Hat, Inc.
*
*/

package vmispec_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

v1 "kubevirt.io/api/core/v1"

"kubevirt.io/kubevirt/pkg/network/vmispec"
)

var _ = Describe("Network", func() {
podNetwork := createPodNetwork("default")
multusDefaultNetwork := createMultusDefaultNetwork("network0", "default/nad0")
multusSecondaryNetwork1 := createMultusSecondaryNetwork("network1", "default/nad1")
multusSecondaryNetwork2 := createMultusSecondaryNetwork("network2", "default/nad2")
multusSecondaryNetwork3 := createMultusSecondaryNetwork("network3", "default/nad3")
DescribeTable("should return only Multus non-default networks", func(inputNetworks, expectFilteredNetworks []v1.Network) {
filteredNetworks := vmispec.FilterMultusNonDefaultNetworks(inputNetworks)

Expect(filteredNetworks).To(Equal(expectFilteredNetworks))
},
Entry("when there are no networks", []v1.Network{}, nil),
Entry("when there is only the pod network", []v1.Network{podNetwork}, nil),
Entry("when there is only a default Multus network", []v1.Network{multusDefaultNetwork}, nil),
Entry("when there are default and non-default Multus networks",
[]v1.Network{
multusDefaultNetwork,
multusSecondaryNetwork1,
multusSecondaryNetwork2,
multusSecondaryNetwork3,
},
[]v1.Network{
multusSecondaryNetwork1,
multusSecondaryNetwork2,
multusSecondaryNetwork3,
}),
)
})

func createMultusSecondaryNetwork(name, networkName string) v1.Network {
return createMultusNetwork(name, networkName)
}

func createMultusDefaultNetwork(name, networkName string) v1.Network {
multusNetwork := createMultusNetwork(name, networkName)
multusNetwork.Multus.Default = true
return multusNetwork
}

func createMultusNetwork(name, networkName string) v1.Network {
return v1.Network{
Name: name,
NetworkSource: v1.NetworkSource{
Multus: &v1.MultusNetwork{
NetworkName: networkName,
},
},
}
}

func createPodNetwork(name string) v1.Network {
return v1.Network{
Name: name,
NetworkSource: v1.NetworkSource{
Pod: &v1.PodNetwork{},
},
}
}
19 changes: 19 additions & 0 deletions pkg/network/vmispec/vmispec_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* 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 2022 Red Hat, Inc.
*
*/

package vmispec_test

import (
Expand Down
1 change: 1 addition & 0 deletions pkg/virt-controller/services/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ go_library(
"//pkg/hooks:go_default_library",
"//pkg/host-disk:go_default_library",
"//pkg/network/istio:go_default_library",
"//pkg/network/vmispec:go_default_library",
"//pkg/storage/types:go_default_library",
"//pkg/util:go_default_library",
"//pkg/util/hardware:go_default_library",
Expand Down
17 changes: 3 additions & 14 deletions pkg/virt-controller/services/multus_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"fmt"

v1 "kubevirt.io/api/core/v1"

"kubevirt.io/kubevirt/pkg/network/vmispec"
)

type multusNetworkAnnotation struct {
Expand Down Expand Up @@ -56,7 +58,7 @@ func (mnap multusNetworkAnnotationPool) toString() (string, error) {
func generateMultusCNIAnnotation(vmi *v1.VirtualMachineInstance) (string, error) {
multusNetworkAnnotationPool := multusNetworkAnnotationPool{}

multusNonDefaultNetworks := filterMultusNonDefaultNetworks(vmi.Spec.Networks)
multusNonDefaultNetworks := vmispec.FilterMultusNonDefaultNetworks(vmi.Spec.Networks)
for i, network := range multusNonDefaultNetworks {
multusNetworkAnnotationPool.add(
newMultusAnnotationData(vmi, network, fmt.Sprintf("net%d", i+1)))
Expand All @@ -68,19 +70,6 @@ func generateMultusCNIAnnotation(vmi *v1.VirtualMachineInstance) (string, error)
return "", nil
}

func filterMultusNonDefaultNetworks(networks []v1.Network) []v1.Network {
var multusNetworks []v1.Network
for _, network := range networks {
if network.Multus != nil {
if network.Multus.Default {
continue
}
multusNetworks = append(multusNetworks, network)
}
}
return multusNetworks
}

func newMultusAnnotationData(vmi *v1.VirtualMachineInstance, network v1.Network, podInterfaceName string) multusNetworkAnnotation {
multusIface := getIfaceByName(vmi, network.Name)
namespace, networkName := getNamespaceAndNetworkName(vmi, network.Multus.NetworkName)
Expand Down

0 comments on commit 83a55bc

Please sign in to comment.