Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract namespace from network annotation when checking for kubevirt related pod instance #141

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/pool-manager/pod_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (p *PoolManager) AllocatePodMac(pod *corev1.Pod) error {
}

// validate if the pod is related to kubevirt
if p.isRelatedToKubevirt(pod) {
if p.isRelatedToKubevirt(pod, networks) {
RamLavi marked this conversation as resolved.
Show resolved Hide resolved
// nothing to do here. the mac is already by allocated by the virtual machine webhook
log.V(1).Info("This pod have ownerReferences from kubevirt skipping")
return nil
Expand Down Expand Up @@ -220,7 +220,7 @@ func (p *PoolManager) initPodMap() error {
}

// validate if the pod is related to kubevirt
if p.isRelatedToKubevirt(&pod) {
if p.isRelatedToKubevirt(&pod, networks) {
// nothing to do here. the mac is already by allocated by the virtual machine webhook
log.V(1).Info("This pod have ownerReferences from kubevirt skipping")
continue
Expand Down
26 changes: 24 additions & 2 deletions pkg/pool-manager/virtualmachine_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

multus "github.com/intel/multus-cni/types"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -323,14 +324,23 @@ func (p *PoolManager) IsKubevirtEnabled() bool {
return p.isKubevirt
}

func (p *PoolManager) isRelatedToKubevirt(pod *corev1.Pod) bool {
func (p *PoolManager) isRelatedToKubevirt(pod *corev1.Pod, networks []*multus.NetworkSelectionElement) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Network namespaces are in no way related kubevirt. If you want to check if a Pod is owned by KubeVirt, you should check its labels/annotations/owner. I'm sure one of those will provide you what you seek, and reliably.

if pod.ObjectMeta.OwnerReferences == nil {
log.V(1).Info("this pod has no OwnerReferences, so will be considered as a regular pod")
return false
}

// since the pod request may not have owned the vm namespace in this early state of vm making,
// we need to receive the namespace from the network annotation
namespace, ok := selectNamespaceFromNetworks(networks)
if !ok {
log.V(1).Info("this pod has more than 1 namespace in its secondary networks, so will be considered as a regular pod")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work? Any pod or VM can request networks from any number of namespaces.

return false
}

for _, ref := range pod.OwnerReferences {
if ref.Kind == kubevirt.VirtualMachineInstanceGroupVersionKind.Kind {
requestUrl := fmt.Sprintf("apis/kubevirt.io/v1alpha3/namespaces/%s/virtualmachines/%s", pod.Namespace, ref.Name)
requestUrl := fmt.Sprintf("apis/kubevirt.io/v1alpha3/namespaces/%s/virtualmachines/%s", namespace, ref.Name)
log.V(1).Info("test", "requestURI", requestUrl)
result := p.kubeClient.ExtensionsV1beta1().RESTClient().Get().RequestURI(requestUrl).Do()

Expand Down Expand Up @@ -482,6 +492,18 @@ func (p *PoolManager) IsVmInstanceOptedIn(namespaceName string) (bool, error) {
return p.isInstanceOptedIn(namespaceName, "kubemacpool-mutator", "mutatevirtualmachines.kubemacpool.io")
}

//making sure that all networks have the same namespace, otherwise returning error.
func selectNamespaceFromNetworks(networks []*multus.NetworkSelectionElement) (string, bool) {
firstNamespace := networks[0].Namespace
for _, network := range networks {
if network.Namespace != firstNamespace {
return "", false
}
}

return firstNamespace, true
}

func vmNamespaced(machine *kubevirt.VirtualMachine) string {
return fmt.Sprintf("%s/%s", machine.Namespace, machine.Name)
}