parse kubelet checkpoint file for pod devices

Enabling kubelete checkpoint file  parsing to get Pod device info
so that these device information can be passed into CNI plugins
that need specific device information to work on.

Change-Id: I6630f56adc0a8307f575fc09ce9090c1ffca0337
This commit is contained in:
Abdul Halim
2018-09-10 16:27:07 +01:00
committed by Kuralamudhan Ramakrishnan
parent 46a0f7590c
commit e3d14b2732
7 changed files with 218 additions and 9 deletions

76
checkpoint/checkpoint.go Normal file
View File

@@ -0,0 +1,76 @@
package checkpoint
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/intel/multus-cni/types"
)
const (
checkPointfile = "/var/lib/kubelet/device-plugins/kubelet_internal_checkpoint"
)
type PodDevicesEntry struct {
PodUID string
ContainerName string
ResourceName string
DeviceIDs []string
AllocResp []byte
}
type checkpointData struct {
PodDeviceEntries []PodDevicesEntry
RegisteredDevices map[string][]string
}
type Data struct {
Data checkpointData
Checksum uint64
}
// getPodEntries gets all Pod device allocation entries from checkpoint file
func getPodEntries() ([]PodDevicesEntry, error) {
podEntries := []PodDevicesEntry{}
cpd := &Data{}
rawBytes, err := ioutil.ReadFile(checkPointfile)
if err != nil {
return podEntries, fmt.Errorf("getPodEntries(): error reading file %s\n%v\n", checkPointfile, err)
}
if err = json.Unmarshal(rawBytes, cpd); err != nil {
return podEntries, fmt.Errorf("getPodEntries(): error unmarshalling raw bytes %v", err)
}
return cpd.Data.PodDeviceEntries, nil
}
// GetComputeDeviceMap returns a map of resourceName to list of device IDs
func GetComputeDeviceMap(podID string) (map[string]*types.ResourceInfo, error) {
resourceMap := make(map[string]*types.ResourceInfo)
podEntires, err := getPodEntries()
if err != nil {
return nil, err
}
for _, pod := range podEntires {
if pod.PodUID == podID {
entry, ok := resourceMap[pod.ResourceName]
if ok {
// already exists; append to it
entry.DeviceIDs = append(entry.DeviceIDs, pod.DeviceIDs...)
} else {
// new entry
resourceMap[pod.ResourceName] = &types.ResourceInfo{DeviceIDs: pod.DeviceIDs}
}
}
}
return resourceMap, nil
}