Skip to content

Commit

Permalink
feat(qrm): network plugin deal with sidecars (kubewharf#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
csfldf authored and luomingmeng committed Oct 11, 2024
1 parent d8b9d78 commit 7e2527a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
47 changes: 15 additions & 32 deletions pkg/agent/qrm-plugins/network/staticpolicy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions pkg/agent/qrm-plugins/network/staticpolicy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ func TestAllocate(t *testing.T) {
Annotations: map[string]string{
testIPv4ResourceAllocationAnnotationKey: testEth0IPv4,
testIPv6ResourceAllocationAnnotationKey: "",
testNetNSPathResourceAllocationAnnotationKey: testEth0NSAbsolutePath,
testNetInterfaceNameResourceAllocationAnnotationKey: testEth0Name,
testNetClassIDResourceAllocationAnnotationKey: testSharedNetClsId,
},
Expand Down Expand Up @@ -403,7 +402,6 @@ func TestAllocate(t *testing.T) {
Annotations: map[string]string{
testIPv4ResourceAllocationAnnotationKey: testEth0IPv4,
testIPv6ResourceAllocationAnnotationKey: "",
testNetNSPathResourceAllocationAnnotationKey: testEth0NSAbsolutePath,
testNetInterfaceNameResourceAllocationAnnotationKey: testEth0Name,
testNetClassIDResourceAllocationAnnotationKey: fmt.Sprintf("%d", testDefaultDedicatedNetClsId),
},
Expand Down
37 changes: 37 additions & 0 deletions pkg/agent/qrm-plugins/network/staticpolicy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7e2527a

Please sign in to comment.