Skip to content

Commit

Permalink
virt-controller: Introduce multus annotation generator
Browse files Browse the repository at this point in the history
Generating multus network selection annotation code
is done in few places at virt-controller's VMIController
sync flow.
There few cases where each require different arguments:
1. On VM creation and migration, requires network
   name-scheme and cluster config
2. On legacy VM migration (who's pod interface names are
   in ordinal form net1, net2 ..), requires ordinal
   network name-scheme and cluster config.
3. When VM is updated (pod already exist) it requires
   network name-scheme only.

To enable more granularity in generating multus annotation w/o
having multiple functions that calls it with awkward names or
passing nil arguments, the multus annotation generator is
introduced.

Signed-off-by: Or Mergi <ormergi@redhat.com>
  • Loading branch information
ormergi committed Oct 16, 2023
1 parent f7b1847 commit 7215840
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
46 changes: 39 additions & 7 deletions pkg/virt-controller/services/multus_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,50 @@ func (mnap multusNetworkAnnotationPool) toString() (string, error) {
return string(multusNetworksAnnotation), nil
}

func GenerateMultusCNIAnnotation(namespace string, interfaces []v1.Interface, networks []v1.Network, config *virtconfig.ClusterConfig) (string, error) {
return GenerateMultusCNIAnnotationFromNameScheme(namespace, interfaces, networks, namescheme.CreateHashedNetworkNameScheme(networks), config)
type multusNetworkAnnotationGenerator struct {
config *virtconfig.ClusterConfig
networkNameScheme map[string]string
}

type option func(*multusNetworkAnnotationGenerator)

func NewMultusNetworkAnnotationGenerator(opts ...option) *multusNetworkAnnotationGenerator {
gen := &multusNetworkAnnotationGenerator{}

for _, opt := range opts {
opt(gen)
}

return gen
}

func WithNetworkNameScheme(networkNameScheme map[string]string) option {
return func(gen *multusNetworkAnnotationGenerator) {
gen.networkNameScheme = networkNameScheme
}
}

func WithClusterConfig(config *virtconfig.ClusterConfig) option {
return func(gen *multusNetworkAnnotationGenerator) {
gen.config = config
}
}

func GenerateMultusCNIAnnotationFromNameScheme(namespace string, interfaces []v1.Interface, networks []v1.Network, networkNameScheme map[string]string, config *virtconfig.ClusterConfig) (string, error) {
func (g *multusNetworkAnnotationGenerator) Generate(namespace string, interfaces []v1.Interface, networks []v1.Network) (string, error) {
multusNetworkAnnotationPool := multusNetworkAnnotationPool{}

for _, network := range networks {
if vmispec.IsSecondaryMultusNetwork(network) {
podInterfaceName := networkNameScheme[network.Name]
podInterfaceName := g.networkNameScheme[network.Name]
multusNetworkAnnotationPool.add(
newMultusAnnotationData(namespace, interfaces, network, podInterfaceName))
}

if config != nil && config.NetworkBindingPlugingsEnabled() {
if g.config != nil && g.config.NetworkBindingPlugingsEnabled() {
if iface := vmispec.LookupInterfaceByName(interfaces, network.Name); iface.Binding != nil {
podIfaceName := networkNameScheme[network.Name]
podIfaceName := g.networkNameScheme[network.Name]
bindingPluginAnnotationData, err := newBindingPluginMultusAnnotationData(
config.GetConfig(), iface.Binding.Name, namespace, podIfaceName)
g.config.GetConfig(), iface.Binding.Name, namespace, podIfaceName)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -120,6 +145,13 @@ func newMultusAnnotationData(namespace string, interfaces []v1.Interface, networ
}
}

func GenerateMultusCNIAnnotation(namespace string, interfaces []v1.Interface, networks []v1.Network, config *virtconfig.ClusterConfig) (string, error) {
g := NewMultusNetworkAnnotationGenerator(
WithNetworkNameScheme(namescheme.CreateHashedNetworkNameScheme(networks)),
WithClusterConfig(config))
return g.Generate(namespace, interfaces, networks)
}

func NonDefaultMultusNetworksIndexedByIfaceName(pod *k8sv1.Pod) map[string]networkv1.NetworkStatus {
indexedNetworkStatus := map[string]networkv1.NetworkStatus{}
podNetworkStatus, found := pod.Annotations[networkv1.NetworkStatusAnnot]
Expand Down
4 changes: 2 additions & 2 deletions pkg/virt-controller/services/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ func (t *templateService) RenderMigrationManifest(vmi *v1.VirtualMachineInstance

if namescheme.PodHasOrdinalInterfaceName(NonDefaultMultusNetworksIndexedByIfaceName(pod)) {
ordinalNameScheme := namescheme.CreateOrdinalNetworkNameScheme(vmi.Spec.Networks)
multusNetworksAnnotation, err := GenerateMultusCNIAnnotationFromNameScheme(
vmi.Namespace, vmi.Spec.Domain.Devices.Interfaces, vmi.Spec.Networks, ordinalNameScheme, t.clusterConfig)
g := NewMultusNetworkAnnotationGenerator(WithNetworkNameScheme(ordinalNameScheme), WithClusterConfig(t.clusterConfig))
multusNetworksAnnotation, err := g.Generate(vmi.Namespace, vmi.Spec.Domain.Devices.Interfaces, vmi.Spec.Networks)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/virt-controller/watch/vmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2292,7 +2292,8 @@ func (c *VMIController) updateMultusAnnotation(namespace string, interfaces []vi

indexedMultusStatusIfaces := services.NonDefaultMultusNetworksIndexedByIfaceName(pod)
networkToPodIfaceMap := namescheme.CreateNetworkNameSchemeByPodNetworkStatus(networks, indexedMultusStatusIfaces)
multusAnnotations, err := services.GenerateMultusCNIAnnotationFromNameScheme(namespace, interfaces, networks, networkToPodIfaceMap, nil)
g := services.NewMultusNetworkAnnotationGenerator(services.WithNetworkNameScheme(networkToPodIfaceMap))
multusAnnotations, err := g.Generate(namespace, interfaces, networks)
if err != nil {
return err
}
Expand Down

0 comments on commit 7215840

Please sign in to comment.