From 880fba0ca8c01eb6ea3905acccb35375ebb34f8d Mon Sep 17 00:00:00 2001 From: Chris Chiu Date: Wed, 1 Jan 2025 16:46:12 +0800 Subject: [PATCH] fix: add the missing indexer VMByNetworkIndex back fix the compile error since the indexeres.VMByNetworkIndex is deprecated since harvester 1.4.0 pkg/utils/vmi.go:23:34: undefined: indexeres.VMByNetworkIndex pkg/utils/vmi.go:27:47: undefined: indexeres.VMByNetworkIndex pkg/utils/vmi.go:32:50: undefined: indexeres.VMByNetworkIndex FATA[0126] exit status 1 make: *** [Makefile:11: ci] Error 1 Signed-off-by: Chris Chiu --- cmd/webhook/main.go | 7 +++++-- pkg/utils/vmi.go | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cmd/webhook/main.go b/cmd/webhook/main.go index 94de69de..3dccbae7 100644 --- a/cmd/webhook/main.go +++ b/cmd/webhook/main.go @@ -9,7 +9,6 @@ import ( ctlcniv1 "github.com/harvester/harvester/pkg/generated/controllers/k8s.cni.cncf.io/v1" ctlkubevirt "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io" ctlkubevirtv1 "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io/v1" - "github.com/harvester/harvester/pkg/indexeres" "github.com/harvester/webhook/pkg/config" "github.com/harvester/webhook/pkg/server" ctlcore "github.com/rancher/wrangler/pkg/generated/controllers/core" @@ -133,6 +132,10 @@ func run(ctx context.Context, cfg *rest.Config, options *config.Options) error { return nil } +const ( + VMIByNetworkIndex = "vm.harvesterhci.io/vmi-by-network" +) + type caches struct { nadCache ctlcniv1.NetworkAttachmentDefinitionCache vmiCache ctlkubevirtv1.VirtualMachineInstanceCache @@ -163,7 +166,7 @@ func newCaches(ctx context.Context, cfg *rest.Config, threadiness int) (*caches, nodeCache: coreFactory.Core().V1().Node().Cache(), } // Indexer must be added before starting the informer, otherwise panic `cannot add indexers to running index` happens - c.vmiCache.AddIndexer(indexeres.VMByNetworkIndex, vmiByNetwork) + c.vmiCache.AddIndexer(VMIByNetworkIndex, vmiByNetwork) if err := start.All(ctx, threadiness, starters...); err != nil { return nil, err diff --git a/pkg/utils/vmi.go b/pkg/utils/vmi.go index 3595b439..0256a4d5 100644 --- a/pkg/utils/vmi.go +++ b/pkg/utils/vmi.go @@ -5,26 +5,30 @@ import ( mapset "github.com/deckarep/golang-set/v2" ctlkubevirtv1 "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io/v1" - "github.com/harvester/harvester/pkg/indexeres" nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" kubevirtv1 "kubevirt.io/api/core/v1" ) +const ( + VMIByNetworkIndex = "vm.harvesterhci.io/vmi-by-network" +) + type VmiGetter struct { VmiCache ctlkubevirtv1.VirtualMachineInstanceCache } // WhoUseNad requires adding network indexer to the vmi cache before invoking it func (v *VmiGetter) WhoUseNad(nad *nadv1.NetworkAttachmentDefinition, nodesFilter mapset.Set[string]) ([]*kubevirtv1.VirtualMachineInstance, error) { + v.VmiCache.AddIndexer(VMIByNetworkIndex, vmiByNetwork) // multus network name can be or / // ref: https://github.com/kubevirt/client-go/blob/148fa0d1c7e83b7a56606a7ca92394ba6768c9ac/api/v1/schema.go#L1436-L1439 networkName := fmt.Sprintf("%s/%s", nad.Namespace, nad.Name) - vmis, err := v.VmiCache.GetByIndex(indexeres.VMByNetworkIndex, networkName) + vmis, err := v.VmiCache.GetByIndex(VMIByNetworkIndex, networkName) if err != nil { return nil, err } - vmisTmp, err := v.VmiCache.GetByIndex(indexeres.VMByNetworkIndex, nad.Name) + vmisTmp, err := v.VmiCache.GetByIndex(VMIByNetworkIndex, nad.Name) if err != nil { return nil, err } @@ -49,3 +53,15 @@ func (v *VmiGetter) WhoUseNad(nad *nadv1.NetworkAttachmentDefinition, nodesFilte return afterFilter, nil } + +func vmiByNetwork(obj *kubevirtv1.VirtualMachineInstance) ([]string, error) { + networks := obj.Spec.Networks + networkNameList := make([]string, 0, len(networks)) + for _, network := range networks { + if network.NetworkSource.Multus == nil { + continue + } + networkNameList = append(networkNameList, network.NetworkSource.Multus.NetworkName) + } + return networkNameList, nil +}