Skip to content

Commit

Permalink
extract namespace from network annotation when cheking for kubevirt r…
Browse files Browse the repository at this point in the history
…elated pod instance

Signed-off-by: Ram Lavi <ralavi@redhat.com>
  • Loading branch information
RamLavi committed May 11, 2020
1 parent 97e9201 commit e5fb594
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
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) {
// 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 {
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 := p.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")
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 All @@ -348,6 +358,18 @@ func (p *PoolManager) isRelatedToKubevirt(pod *corev1.Pod) bool {
return false
}

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

return firstNamespace, true
}

func (p *PoolManager) releaseMacAddressesFromInterfaceMap(allocations map[string]string) {
for _, value := range allocations {
delete(p.macPoolMap, value)
Expand Down

0 comments on commit e5fb594

Please sign in to comment.