From 29c4858c1f04be5a052297182b906334566e67f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E5=81=A5=E4=BF=9E?= Date: Tue, 20 Jun 2023 15:34:45 +0800 Subject: [PATCH] feat(qrm): network plugin deal with sidecars --- .../network/staticpolicy/policy.go | 47 ++++++------------- .../qrm-plugins/network/staticpolicy/util.go | 37 +++++++++++++++ 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/pkg/agent/qrm-plugins/network/staticpolicy/policy.go b/pkg/agent/qrm-plugins/network/staticpolicy/policy.go index ae2866bbc..79d1afcca 100644 --- a/pkg/agent/qrm-plugins/network/staticpolicy/policy.go +++ b/pkg/agent/qrm-plugins/network/staticpolicy/policy.go @@ -299,7 +299,8 @@ func (p *StaticPolicy) GetTopologyHints(_ context.Context, } }() - if req.ContainerType == pluginapi.ContainerType_INIT { + if req.ContainerType == pluginapi.ContainerType_INIT || + req.ContainerType == pluginapi.ContainerType_SIDECAR { return util.PackResourceHintsResponse(req, p.ResourceName(), map[string]*pluginapi.ListOfTopologyHints{ p.ResourceName(): nil, // indicates that there is no numa preference }) @@ -377,13 +378,18 @@ func (p *StaticPolicy) getResourceAllocationAnnotations(podAnnotations map[strin return nil, fmt.Errorf("getNetClassID failed with error: %v", err) } - return map[string]string{ + resourceAllocationAnnotations := map[string]string{ p.ipv4ResourceAllocationAnnotationKey: strings.Join(selectedNIC.GetNICIPs(machine.IPVersionV4), IPsSeparator), p.ipv6ResourceAllocationAnnotationKey: strings.Join(selectedNIC.GetNICIPs(machine.IPVersionV6), IPsSeparator), - p.netNSPathResourceAllocationAnnotationKey: selectedNIC.NSAbsolutePath, p.netInterfaceNameResourceAllocationAnnotationKey: selectedNIC.Iface, p.netClassIDResourceAllocationAnnotationKey: fmt.Sprintf("%d", netClsID), - }, nil + } + + if len(selectedNIC.NSAbsolutePath) > 0 { + resourceAllocationAnnotations[p.netNSPathResourceAllocationAnnotationKey] = selectedNIC.NSAbsolutePath + } + + return resourceAllocationAnnotations, nil } // Allocate is called during pod admit so that the resource @@ -438,6 +444,9 @@ func (p *StaticPolicy) Allocate(_ context.Context, Labels: general.DeepCopyMap(req.Labels), Annotations: general.DeepCopyMap(req.Annotations), }, nil + } else if req.ContainerType == pluginapi.ContainerType_SIDECAR { + // not to deal with sidcars, and return a trivial allocationResult to avoid re-allocating + return packAllocationResponse(req, p.ResourceName(), 0, nil) } selectedNIC, err := p.selectNICByReq(req) @@ -456,34 +465,8 @@ func (p *StaticPolicy) Allocate(_ context.Context, return nil, err } - return &pluginapi.ResourceAllocationResponse{ - PodUid: req.PodUid, - PodNamespace: req.PodNamespace, - PodName: req.PodName, - ContainerName: req.ContainerName, - ContainerType: req.ContainerType, - ContainerIndex: req.ContainerIndex, - PodRole: req.PodRole, - PodType: req.PodType, - ResourceName: p.ResourceName(), - AllocationResult: &pluginapi.ResourceAllocation{ - ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{ - p.ResourceName(): { - IsNodeResource: false, - IsScalarResource: true, // to avoid re-allocating - AllocatedQuantity: 0, // TODO fill it with allocated bandwidth quantity - Annotations: resourceAllocationAnnotations, - ResourceHints: &pluginapi.ListOfTopologyHints{ - Hints: []*pluginapi.TopologyHint{ - req.Hint, - }, - }, - }, - }, - }, - Labels: general.DeepCopyMap(req.Labels), - Annotations: general.DeepCopyMap(req.Annotations), - }, nil + // TODO fill it with allocated bandwidth quantity + return packAllocationResponse(req, p.ResourceName(), 0, resourceAllocationAnnotations) } // PreStartContainer is called, if indicated by resource plugin during registration phase, diff --git a/pkg/agent/qrm-plugins/network/staticpolicy/util.go b/pkg/agent/qrm-plugins/network/staticpolicy/util.go index a4ca2f11b..71e5bc87d 100644 --- a/pkg/agent/qrm-plugins/network/staticpolicy/util.go +++ b/pkg/agent/qrm-plugins/network/staticpolicy/util.go @@ -213,3 +213,40 @@ func getRandomNICs(nics []machine.InterfaceInfo) machine.InterfaceInfo { rand.Seed(time.Now().UnixNano()) return nics[rand.Intn(len(nics))] } + +// packAllocationResponse fills pluginapi.ResourceAllocationResponse with information from AllocationInfo and pluginapi.ResourceRequest +func packAllocationResponse(req *pluginapi.ResourceRequest, resourceName string, + allocatedQuantity float64, resourceAllocationAnnotations map[string]string) (*pluginapi.ResourceAllocationResponse, error) { + if req == nil { + return nil, fmt.Errorf("packAllocationResponse got nil request") + } + + return &pluginapi.ResourceAllocationResponse{ + PodUid: req.PodUid, + PodNamespace: req.PodNamespace, + PodName: req.PodName, + ContainerName: req.ContainerName, + ContainerType: req.ContainerType, + ContainerIndex: req.ContainerIndex, + PodRole: req.PodRole, + PodType: req.PodType, + ResourceName: resourceName, + AllocationResult: &pluginapi.ResourceAllocation{ + ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{ + resourceName: { + IsNodeResource: false, + IsScalarResource: true, // to avoid re-allocating + AllocatedQuantity: allocatedQuantity, + Annotations: resourceAllocationAnnotations, + ResourceHints: &pluginapi.ListOfTopologyHints{ + Hints: []*pluginapi.TopologyHint{ + req.Hint, + }, + }, + }, + }, + }, + Labels: general.DeepCopyMap(req.Labels), + Annotations: general.DeepCopyMap(req.Annotations), + }, nil +}