example_handler.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 pluginwatcher
  14. import (
  15. "errors"
  16. "fmt"
  17. "net"
  18. "reflect"
  19. "sync"
  20. "time"
  21. "golang.org/x/net/context"
  22. "google.golang.org/grpc"
  23. "k8s.io/klog"
  24. registerapi "k8s.io/kubernetes/pkg/kubelet/apis/pluginregistration/v1"
  25. v1beta1 "k8s.io/kubernetes/pkg/kubelet/pluginmanager/pluginwatcher/example_plugin_apis/v1beta1"
  26. v1beta2 "k8s.io/kubernetes/pkg/kubelet/pluginmanager/pluginwatcher/example_plugin_apis/v1beta2"
  27. )
  28. type exampleHandler struct {
  29. SupportedVersions []string
  30. ExpectedNames map[string]int
  31. eventChans map[string]chan examplePluginEvent // map[pluginName]eventChan
  32. m sync.Mutex
  33. count int
  34. permitDeprecatedDir bool
  35. }
  36. type examplePluginEvent int
  37. const (
  38. exampleEventValidate examplePluginEvent = 0
  39. exampleEventRegister examplePluginEvent = 1
  40. exampleEventDeRegister examplePluginEvent = 2
  41. exampleEventError examplePluginEvent = 3
  42. )
  43. // NewExampleHandler provide a example handler
  44. func NewExampleHandler(supportedVersions []string, permitDeprecatedDir bool) *exampleHandler {
  45. return &exampleHandler{
  46. SupportedVersions: supportedVersions,
  47. ExpectedNames: make(map[string]int),
  48. eventChans: make(map[string]chan examplePluginEvent),
  49. permitDeprecatedDir: permitDeprecatedDir,
  50. }
  51. }
  52. func (p *exampleHandler) ValidatePlugin(pluginName string, endpoint string, versions []string, foundInDeprecatedDir bool) error {
  53. if foundInDeprecatedDir && !p.permitDeprecatedDir {
  54. return fmt.Errorf("device plugin socket was found in a directory that is no longer supported and this test does not permit plugins from deprecated dir")
  55. }
  56. p.SendEvent(pluginName, exampleEventValidate)
  57. n, ok := p.DecreasePluginCount(pluginName)
  58. if !ok && n > 0 {
  59. return fmt.Errorf("pluginName('%s') wasn't expected (count is %d)", pluginName, n)
  60. }
  61. if !reflect.DeepEqual(versions, p.SupportedVersions) {
  62. return fmt.Errorf("versions('%v') != supported versions('%v')", versions, p.SupportedVersions)
  63. }
  64. // this handler expects non-empty endpoint as an example
  65. if len(endpoint) == 0 {
  66. return errors.New("expecting non empty endpoint")
  67. }
  68. return nil
  69. }
  70. func (p *exampleHandler) RegisterPlugin(pluginName, endpoint string, versions []string) error {
  71. p.SendEvent(pluginName, exampleEventRegister)
  72. // Verifies the grpcServer is ready to serve services.
  73. _, conn, err := dial(endpoint, time.Second)
  74. if err != nil {
  75. return fmt.Errorf("Failed dialing endpoint (%s): %v", endpoint, err)
  76. }
  77. defer conn.Close()
  78. // The plugin handler should be able to use any listed service API version.
  79. v1beta1Client := v1beta1.NewExampleClient(conn)
  80. v1beta2Client := v1beta2.NewExampleClient(conn)
  81. // Tests v1beta1 GetExampleInfo
  82. _, err = v1beta1Client.GetExampleInfo(context.Background(), &v1beta1.ExampleRequest{})
  83. if err != nil {
  84. return fmt.Errorf("Failed GetExampleInfo for v1beta2Client(%s): %v", endpoint, err)
  85. }
  86. // Tests v1beta1 GetExampleInfo
  87. _, err = v1beta2Client.GetExampleInfo(context.Background(), &v1beta2.ExampleRequest{})
  88. if err != nil {
  89. return fmt.Errorf("Failed GetExampleInfo for v1beta2Client(%s): %v", endpoint, err)
  90. }
  91. return nil
  92. }
  93. func (p *exampleHandler) DeRegisterPlugin(pluginName string) {
  94. p.SendEvent(pluginName, exampleEventDeRegister)
  95. }
  96. func (p *exampleHandler) EventChan(pluginName string) chan examplePluginEvent {
  97. return p.eventChans[pluginName]
  98. }
  99. func (p *exampleHandler) SendEvent(pluginName string, event examplePluginEvent) {
  100. klog.V(2).Infof("Sending %v for plugin %s over chan %v", event, pluginName, p.eventChans[pluginName])
  101. p.eventChans[pluginName] <- event
  102. }
  103. func (p *exampleHandler) AddPluginName(pluginName string) {
  104. p.m.Lock()
  105. defer p.m.Unlock()
  106. v, ok := p.ExpectedNames[pluginName]
  107. if !ok {
  108. p.eventChans[pluginName] = make(chan examplePluginEvent)
  109. v = 1
  110. }
  111. p.ExpectedNames[pluginName] = v
  112. }
  113. func (p *exampleHandler) DecreasePluginCount(pluginName string) (old int, ok bool) {
  114. p.m.Lock()
  115. defer p.m.Unlock()
  116. v, ok := p.ExpectedNames[pluginName]
  117. if !ok {
  118. v = -1
  119. }
  120. return v, ok
  121. }
  122. // Dial establishes the gRPC communication with the picked up plugin socket. https://godoc.org/google.golang.org/grpc#Dial
  123. func dial(unixSocketPath string, timeout time.Duration) (registerapi.RegistrationClient, *grpc.ClientConn, error) {
  124. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  125. defer cancel()
  126. c, err := grpc.DialContext(ctx, unixSocketPath, grpc.WithInsecure(), grpc.WithBlock(),
  127. grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
  128. return net.DialTimeout("unix", addr, timeout)
  129. }),
  130. )
  131. if err != nil {
  132. return nil, nil, fmt.Errorf("failed to dial socket %s, err: %v", unixSocketPath, err)
  133. }
  134. return registerapi.NewRegistrationClient(c), c, nil
  135. }