Skip to content

Commit

Permalink
fix: add the missing indexer VMByNetworkIndex back
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mingshuoqiu committed Jan 9, 2025
1 parent 6017634 commit 880fba0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
7 changes: 5 additions & 2 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions pkg/utils/vmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <networkName> or <namespace>/<networkName>
// 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
}
Expand All @@ -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
}

0 comments on commit 880fba0

Please sign in to comment.