example_handler.go 4.9 KB

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