You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2021 The KubeEdge 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 server
  14. import (
  15. "fmt"
  16. "net/http"
  17. "github.com/emicklei/go-restful/v3"
  18. "k8s.io/klog/v2"
  19. "github.com/kubeedge/sedna/cmd/sedna-lc/app/options"
  20. "github.com/kubeedge/sedna/pkg/localcontroller/common/constants"
  21. "github.com/kubeedge/sedna/pkg/localcontroller/managers"
  22. workertypes "github.com/kubeedge/sedna/pkg/localcontroller/worker"
  23. )
  24. // Server defines server
  25. type Server struct {
  26. Port string
  27. Resource *Resource
  28. fmm featureManagerMap
  29. }
  30. // Resource defines resource
  31. type Resource struct {
  32. Worker map[string]workertypes.MessageContent
  33. }
  34. // ResponseMessage defines send message to worker
  35. type ResponseMessage struct {
  36. Status int
  37. Message string
  38. }
  39. type featureManagerMap map[string]managers.FeatureManager
  40. // New creates a new LC server
  41. func New(options *options.LocalControllerOptions) *Server {
  42. s := Server{
  43. Port: options.BindPort,
  44. }
  45. s.fmm = featureManagerMap{}
  46. return &s
  47. }
  48. func (s *Server) AddFeatureManager(m managers.FeatureManager) {
  49. s.fmm[m.GetName()] = m
  50. }
  51. // register registers api
  52. func (s *Server) register(container *restful.Container) {
  53. ws := new(restful.WebService)
  54. ws.Path(fmt.Sprintf("/%s", constants.ServerRootPath)).
  55. Consumes(restful.MIME_XML, restful.MIME_JSON).
  56. Produces(restful.MIME_JSON, restful.MIME_XML)
  57. ws.Route(ws.POST("/workers/{worker-name}/info").
  58. To(s.messageHandler).
  59. Doc("receive worker message"))
  60. container.Add(ws)
  61. }
  62. // reply replies message to the worker
  63. func (s *Server) reply(response *restful.Response, statusCode int, msg string) error {
  64. err := response.WriteHeaderAndEntity(statusCode, ResponseMessage{
  65. Status: statusCode,
  66. Message: msg,
  67. })
  68. if err != nil {
  69. klog.Errorf("the value could not be written on the response, error: %v", err)
  70. return err
  71. }
  72. return nil
  73. }
  74. // messageHandler handles message from the worker
  75. func (s *Server) messageHandler(request *restful.Request, response *restful.Response) {
  76. var err error
  77. workerName := request.PathParameter("worker-name")
  78. workerMessage := workertypes.MessageContent{}
  79. err = request.ReadEntity(&workerMessage)
  80. if workerMessage.Name != workerName || err != nil {
  81. var msg string
  82. if workerMessage.Name != workerName {
  83. msg = fmt.Sprintf("worker name(name=%s) in the api is different from that(name=%s) in the message body",
  84. workerName, workerMessage.Name)
  85. } else {
  86. msg = fmt.Sprintf("read worker(name=%s) message body failed, error: %v", workerName, err)
  87. }
  88. klog.Errorf(msg)
  89. err = s.reply(response, http.StatusBadRequest, msg)
  90. if err != nil {
  91. klog.Errorf("reply messge to worker(name=%s) failed, error: %v", workerName, err)
  92. }
  93. return
  94. }
  95. if m, ok := s.fmm[workerMessage.OwnerKind]; ok {
  96. m.AddWorkerMessage(workerMessage)
  97. }
  98. err = s.reply(response, http.StatusOK, "OK")
  99. if err != nil {
  100. klog.Errorf("reply message to worker(name=%s) failed, error: %v", workerName, err)
  101. return
  102. }
  103. }
  104. // ListenAndServe starts server
  105. func (s *Server) ListenAndServe() {
  106. wsContainer := restful.NewContainer()
  107. resource := Resource{map[string]workertypes.MessageContent{}}
  108. s.Resource = &resource
  109. s.register(wsContainer)
  110. server := &http.Server{Addr: fmt.Sprintf(":%s", s.Port), Handler: wsContainer}
  111. klog.Infof("server binds port %s successfully", s.Port)
  112. klog.Fatal(server.ListenAndServe())
  113. }