Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Ensure deviceID is set when a resource is requested #1311

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/k8sclient/k8sclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ func getKubernetesDelegate(client *ClientInfo, net *types.NetworkSelectionElemen
entry.Index++ // increment Index for next delegate
}
}

if deviceID == "" {
return nil, resourceMap, logging.Errorf("getKubernetesDelegate: requested resource not found in resourceMap %+v", resourceMap)
}
}

configBytes, err := netutils.GetCNIConfig(customResource, confdir)
Expand Down
61 changes: 61 additions & 0 deletions pkg/k8sclient/k8sclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,67 @@ users:
_, err = GetNetworkDelegates(clientInfo, fakePod, networks, netConf, nil)
Expect(err).To(HaveOccurred())
})
It("fails when the requested resource is not available", func() {
fakePod := testutils.NewFakePod(fakePodName, "net1", "")
net1 := `{
"name": "net1",
"type": "mynet",
"cniVersion": "0.2.0"
}`

clientInfo := NewFakeClientInfo()
_, err := clientInfo.AddPod(fakePod)
Expect(err).NotTo(HaveOccurred())
_, err = clientInfo.AddNetAttachDef(
testutils.NewFakeNetAttachDefAnnotation(fakePod.ObjectMeta.Namespace, "net1", net1))
Expect(err).NotTo(HaveOccurred())

networks, err := GetPodNetwork(fakePod)
Expect(err).NotTo(HaveOccurred())

netConf, err := types.LoadNetConf([]byte(genericConf))
netConf.ConfDir = tmpDir

resourceMap := make(map[string]*types.ResourceInfo)
resourceMap["some-other-resource"] = &types.ResourceInfo{
Index: 0,
DeviceIDs: []string{"id-1", "id-2", "id-3"},
}
_, err = GetNetworkDelegates(clientInfo, fakePod, networks, netConf, resourceMap)
Expect(err).To(HaveOccurred())
})
It("provides deviceID when the requested resource is available", func() {
fakePod := testutils.NewFakePod(fakePodName, "net1", "")
net1 := `{
"name": "net1",
"type": "mynet",
"cniVersion": "0.2.0"
}`

clientInfo := NewFakeClientInfo()
_, err := clientInfo.AddPod(fakePod)
Expect(err).NotTo(HaveOccurred())
_, err = clientInfo.AddNetAttachDef(
testutils.NewFakeNetAttachDefAnnotation(fakePod.ObjectMeta.Namespace, "net1", net1))
Expect(err).NotTo(HaveOccurred())

networks, err := GetPodNetwork(fakePod)
Expect(err).NotTo(HaveOccurred())

netConf, err := types.LoadNetConf([]byte(genericConf))
netConf.ConfDir = tmpDir

resourceMap := make(map[string]*types.ResourceInfo)
// This annotation is set by default in the testutils.NewFakeNetAttachDefAnnotation()
resourceMap["intel.com/sriov"] = &types.ResourceInfo{
Index: 0,
DeviceIDs: []string{"id-1", "id-2", "id-3"},
}
delegates, err := GetNetworkDelegates(clientInfo, fakePod, networks, netConf, resourceMap)
Expect(err).ToNot(HaveOccurred())
Expect(delegates).To(HaveLen(1))
Expect(string(delegates[0].Bytes)).To(ContainSubstring("id-1"))
})
})

Context("parsePodNetworkObjectName", func() {
Expand Down
Loading