-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:intel/multus-cni into dev/defaultnet
- Loading branch information
Showing
15 changed files
with
509 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package checkpoint | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
|
||
"github.com/intel/multus-cni/logging" | ||
"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 | ||
} | ||
|
||
type Checkpoint interface { | ||
// GetComputeDeviceMap returns an instance of a map of ResourceInfo for a PodID | ||
GetComputeDeviceMap(string) (map[string]*types.ResourceInfo, error) | ||
} | ||
type checkpoint struct { | ||
fileName string | ||
podEntires []PodDevicesEntry | ||
} | ||
|
||
// GetCheckpoint returns an instance of Checkpoint | ||
func GetCheckpoint() (Checkpoint, error) { | ||
logging.Debugf("GetCheckpoint(): invoked") | ||
return getCheckpoint(checkPointfile) | ||
} | ||
|
||
func getCheckpoint(filePath string) (Checkpoint, error) { | ||
cp := &checkpoint{fileName: filePath} | ||
err := cp.getPodEntries() | ||
if err != nil { | ||
return nil, err | ||
} | ||
logging.Debugf("getCheckpoint(): created checkpoint instance with file: %s", filePath) | ||
return cp, nil | ||
} | ||
|
||
// getPodEntries gets all Pod device allocation entries from checkpoint file | ||
func (cp *checkpoint) getPodEntries() error { | ||
|
||
cpd := &Data{} | ||
rawBytes, err := ioutil.ReadFile(cp.fileName) | ||
if err != nil { | ||
return logging.Errorf("getPodEntries(): error reading file %s\n%v\n", checkPointfile, err) | ||
} | ||
|
||
if err = json.Unmarshal(rawBytes, cpd); err != nil { | ||
return logging.Errorf("getPodEntries(): error unmarshalling raw bytes %v", err) | ||
} | ||
|
||
cp.podEntires = cpd.Data.PodDeviceEntries | ||
logging.Debugf("getPodEntries(): podEntires %+v", cp.podEntires) | ||
return nil | ||
} | ||
|
||
// GetComputeDeviceMap returns an instance of a map of ResourceInfo | ||
func (cp *checkpoint) GetComputeDeviceMap(podID string) (map[string]*types.ResourceInfo, error) { | ||
|
||
resourceMap := make(map[string]*types.ResourceInfo) | ||
|
||
if podID == "" { | ||
return nil, logging.Errorf("GetComputeDeviceMap(): invalid Pod cannot be empty") | ||
} | ||
|
||
for _, pod := range cp.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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package checkpoint | ||
|
||
import ( | ||
"os" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/intel/multus-cni/types" | ||
) | ||
|
||
const ( | ||
fakeTempFile = "/tmp/kubelet_internal_checkpoint" | ||
) | ||
|
||
type fakeCheckpoint struct { | ||
fileName string | ||
} | ||
|
||
func (fc *fakeCheckpoint) WriteToFile(inBytes []byte) error { | ||
return ioutil.WriteFile(fc.fileName, inBytes, 0600) | ||
} | ||
|
||
func (fc *fakeCheckpoint) DeleteFile() error { | ||
return os.Remove(fc.fileName) | ||
} | ||
|
||
func TestCheckpoint(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Checkpoint") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
sampleData := `{ | ||
"Data": { | ||
"PodDeviceEntries": [ | ||
{ | ||
"PodUID": "970a395d-bb3b-11e8-89df-408d5c537d23", | ||
"ContainerName": "appcntr1", | ||
"ResourceName": "intel.com/sriov_net_A", | ||
"DeviceIDs": [ | ||
"0000:03:02.3", | ||
"0000:03:02.0" | ||
], | ||
"AllocResp": "CikKC3NyaW92X25ldF9BEhogMDAwMDowMzowMi4zIDAwMDA6MDM6MDIuMA==" | ||
} | ||
], | ||
"RegisteredDevices": { | ||
"intel.com/sriov_net_A": [ | ||
"0000:03:02.1", | ||
"0000:03:02.2", | ||
"0000:03:02.3", | ||
"0000:03:02.0" | ||
], | ||
"intel.com/sriov_net_B": [ | ||
"0000:03:06.3", | ||
"0000:03:06.0", | ||
"0000:03:06.1", | ||
"0000:03:06.2" | ||
] | ||
} | ||
}, | ||
"Checksum": 229855270 | ||
}` | ||
|
||
fakeCheckpoint := &fakeCheckpoint{fileName: fakeTempFile} | ||
err := fakeCheckpoint.WriteToFile([]byte(sampleData)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
var _ = Describe("Kubelet checkpoint data read operations", func() { | ||
Context("Using /tmp/kubelet_internal_checkpoint file", func() { | ||
var ( | ||
cp Checkpoint | ||
err error | ||
resourceMap map[string]*types.ResourceInfo | ||
resourceInfo *types.ResourceInfo | ||
resourceAnnot = "intel.com/sriov_net_A" | ||
) | ||
|
||
It("should get a Checkpoint instance from file", func() { | ||
cp, err = getCheckpoint(fakeTempFile) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should return a ResourceMap instance", func() { | ||
rmap, err := cp.GetComputeDeviceMap("970a395d-bb3b-11e8-89df-408d5c537d23") | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(rmap).NotTo(BeEmpty()) | ||
resourceMap = rmap | ||
}) | ||
|
||
It("resourceMap should have value for \"intel.com/sriov_net_A\"", func() { | ||
rInfo, ok := resourceMap[resourceAnnot] | ||
Expect(ok).To(BeTrue()) | ||
resourceInfo = rInfo | ||
}) | ||
|
||
It("should have 2 deviceIDs", func() { | ||
Expect(len(resourceInfo.DeviceIDs)).To(BeEquivalentTo(2)) | ||
}) | ||
|
||
It("should have \"0000:03:02.3\" in deviceIDs[0]", func() { | ||
Expect(resourceInfo.DeviceIDs[0]).To(BeEquivalentTo("0000:03:02.3")) | ||
}) | ||
|
||
It("should have \"0000:03:02.0\" in deviceIDs[1]", func() { | ||
Expect(resourceInfo.DeviceIDs[1]).To(BeEquivalentTo("0000:03:02.0")) | ||
}) | ||
}) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
fakeCheckpoint := &fakeCheckpoint{fileName: fakeTempFile} | ||
err := fakeCheckpoint.DeleteFile() | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: testpod1 | ||
labels: | ||
env: test | ||
annotations: | ||
k8s.v1.cni.cncf.io/networks: sriov-net-a | ||
spec: | ||
containers: | ||
- name: appcntr1 | ||
image: centos/tools | ||
imagePullPolicy: IfNotPresent | ||
command: [ "/bin/bash", "-c", "--" ] | ||
args: [ "while true; do sleep 300000; done;" ] | ||
resources: | ||
requests: | ||
intel.com/sriov: '1' | ||
limits: | ||
intel.com/sriov: '1' | ||
restartPolicy: "Never" |
Oops, something went wrong.