kubehandler.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package kubehandlers
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/onecloud/pkg/appsrv"
  21. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  22. "yunion.io/x/onecloud/pkg/hostman/system_service"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. )
  26. type sKubeConf struct {
  27. DockerdConf map[string]interface{}
  28. AgentConfig map[string]interface{}
  29. }
  30. var keyWords = []string{"kubeagent"}
  31. func AddKubeAgentHandler(prefix string, app *appsrv.Application) {
  32. for _, keyword := range keyWords {
  33. app.AddHandler("POST", fmt.Sprintf("%s/%s/<action>", prefix, keyword),
  34. auth.Authenticate(dispatcher))
  35. }
  36. }
  37. func dispatcher(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  38. var (
  39. params, _, body = appsrv.FetchEnv(ctx, w, r)
  40. action = params["<action>"]
  41. )
  42. switch action {
  43. case "start":
  44. dockerdConf, _ := body.Get("dockerdConfig")
  45. agentConfig, _ := body.Get("agentConfig")
  46. err := prepareAgentStart(dockerdConf, agentConfig)
  47. if err != nil {
  48. hostutils.Response(ctx, w, err)
  49. return
  50. }
  51. var dm = map[string]interface{}{}
  52. if err := dockerdConf.Unmarshal(&dm); err != nil {
  53. hostutils.Response(ctx, w, err)
  54. return
  55. }
  56. var am = map[string]interface{}{}
  57. if err := agentConfig.Unmarshal(&am); err != nil {
  58. hostutils.Response(ctx, w, err)
  59. return
  60. }
  61. hostutils.DelayKubeTask(ctx, startAgent, &sKubeConf{dm, am})
  62. case "restart":
  63. hostutils.DelayKubeTask(ctx, restartAgent, nil)
  64. case "stop":
  65. hostutils.DelayKubeTask(ctx, stopAgent, nil)
  66. default:
  67. hostutils.Response(ctx, w, httperrors.NewNotFoundError("Not found"))
  68. return
  69. }
  70. hostutils.ResponseOk(ctx, w)
  71. }
  72. func prepareAgentStart(dockerdConf, agentConfig jsonutils.JSONObject) error {
  73. if !agentConfig.Contains("serverUrl") {
  74. return httperrors.NewBadRequestError("Kube server url empty")
  75. }
  76. if !agentConfig.Contains("nodeId") {
  77. return httperrors.NewBadRequestError("NodeId empty")
  78. }
  79. if !agentConfig.Contains("token") {
  80. return httperrors.NewBadRequestError("Register token empty")
  81. }
  82. return nil
  83. }
  84. func startAgent(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  85. sp, ok := params.(*sKubeConf)
  86. if !ok {
  87. return nil, hostutils.ParamsError
  88. }
  89. lxcfs := system_service.GetService("lxcfs")
  90. if !lxcfs.IsInstalled() {
  91. return nil, fmt.Errorf("Service lxcfs not installed")
  92. } else if !lxcfs.IsActive() {
  93. if err := lxcfs.Start(false); err != nil {
  94. return nil, err
  95. }
  96. }
  97. if err := lxcfs.Enable(); err != nil {
  98. return nil, err
  99. }
  100. if err := serviceReloadStart("docker", sp.DockerdConf); err != nil {
  101. return nil, err
  102. }
  103. if err := serviceReloadStart("kube_agent", sp.AgentConfig); err != nil {
  104. return nil, err
  105. }
  106. return nil, nil
  107. }
  108. func serviceReloadStart(srv string, conf map[string]interface{}) error {
  109. srvinst := system_service.GetService(srv)
  110. if srvinst == nil {
  111. return fmt.Errorf("srv %s not found", srv)
  112. }
  113. if !srvinst.IsInstalled() {
  114. return fmt.Errorf("Service %s nout found", srv)
  115. }
  116. if err := srvinst.Reload(conf); err != nil {
  117. return err
  118. }
  119. if !srvinst.IsActive() {
  120. if err := srvinst.Start(false); err != nil {
  121. return err
  122. }
  123. }
  124. return srvinst.Enable()
  125. }
  126. func restartAgent(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  127. srvinst := system_service.GetService("kube_agent")
  128. if !srvinst.IsInstalled() {
  129. return nil, fmt.Errorf("Service kube_agent not installed")
  130. }
  131. return nil, srvinst.Start(true)
  132. }
  133. func stopAgent(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  134. srvinst := system_service.GetService("kube_agent")
  135. if !srvinst.IsInstalled() {
  136. return nil, fmt.Errorf("Service kube_agent not installed")
  137. }
  138. return nil, srvinst.Stop(true)
  139. }