sampledeviceplugin.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "time"
  19. "k8s.io/klog"
  20. pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
  21. dm "k8s.io/kubernetes/pkg/kubelet/cm/devicemanager"
  22. )
  23. const (
  24. resourceName = "example.com/resource"
  25. )
  26. // stubAllocFunc creates and returns allocation response for the input allocate request
  27. func stubAllocFunc(r *pluginapi.AllocateRequest, devs map[string]pluginapi.Device) (*pluginapi.AllocateResponse, error) {
  28. var responses pluginapi.AllocateResponse
  29. for _, req := range r.ContainerRequests {
  30. response := &pluginapi.ContainerAllocateResponse{}
  31. for _, requestID := range req.DevicesIDs {
  32. dev, ok := devs[requestID]
  33. if !ok {
  34. return nil, fmt.Errorf("invalid allocation request with non-existing device %s", requestID)
  35. }
  36. if dev.Health != pluginapi.Healthy {
  37. return nil, fmt.Errorf("invalid allocation request with unhealthy device: %s", requestID)
  38. }
  39. // create fake device file
  40. fpath := filepath.Join("/tmp", dev.ID)
  41. // clean first
  42. if err := os.RemoveAll(fpath); err != nil {
  43. return nil, fmt.Errorf("failed to clean fake device file from previous run: %s", err)
  44. }
  45. f, err := os.Create(fpath)
  46. if err != nil && !os.IsExist(err) {
  47. return nil, fmt.Errorf("failed to create fake device file: %s", err)
  48. }
  49. f.Close()
  50. response.Mounts = append(response.Mounts, &pluginapi.Mount{
  51. ContainerPath: fpath,
  52. HostPath: fpath,
  53. })
  54. }
  55. responses.ContainerResponses = append(responses.ContainerResponses, response)
  56. }
  57. return &responses, nil
  58. }
  59. func main() {
  60. devs := []*pluginapi.Device{
  61. {ID: "Dev-1", Health: pluginapi.Healthy},
  62. {ID: "Dev-2", Health: pluginapi.Healthy},
  63. }
  64. pluginSocksDir := os.Getenv("PLUGIN_SOCK_DIR")
  65. klog.Infof("pluginSocksDir: %s", pluginSocksDir)
  66. if pluginSocksDir == "" {
  67. klog.Errorf("Empty pluginSocksDir")
  68. return
  69. }
  70. socketPath := pluginSocksDir + "/dp." + fmt.Sprintf("%d", time.Now().Unix())
  71. dp1 := dm.NewDevicePluginStub(devs, socketPath, resourceName, false)
  72. if err := dp1.Start(); err != nil {
  73. panic(err)
  74. }
  75. dp1.SetAllocFunc(stubAllocFunc)
  76. if err := dp1.Register(pluginapi.KubeletSocket, resourceName, pluginapi.DevicePluginPath); err != nil {
  77. panic(err)
  78. }
  79. select {}
  80. }