compose.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // +build kube
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package kube
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/docker/compose-cli/api/compose"
  21. apicontext "github.com/docker/compose-cli/api/context"
  22. "github.com/docker/compose-cli/api/context/store"
  23. "github.com/docker/compose-cli/api/errdefs"
  24. "github.com/docker/compose-cli/api/progress"
  25. "github.com/docker/compose-cli/kube/client"
  26. "github.com/docker/compose-cli/kube/helm"
  27. "github.com/docker/compose-cli/kube/resources"
  28. "github.com/docker/compose-cli/utils"
  29. )
  30. type composeService struct {
  31. sdk *helm.Actions
  32. client *client.KubeClient
  33. }
  34. // NewComposeService create a kubernetes implementation of the compose.Service API
  35. func NewComposeService(ctx context.Context) (compose.Service, error) {
  36. contextStore := store.ContextStore(ctx)
  37. currentContext := apicontext.CurrentContext(ctx)
  38. var kubeContext store.KubeContext
  39. if err := contextStore.GetEndpoint(currentContext, &kubeContext); err != nil {
  40. return nil, err
  41. }
  42. config, err := resources.LoadConfig(kubeContext)
  43. if err != nil {
  44. return nil, err
  45. }
  46. actions, err := helm.NewActions(config)
  47. if err != nil {
  48. return nil, err
  49. }
  50. apiClient, err := client.NewKubeClient(config)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return &composeService{
  55. sdk: actions,
  56. client: apiClient,
  57. }, nil
  58. }
  59. // Up executes the equivalent to a `compose up`
  60. func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
  61. w := progress.ContextWriter(ctx)
  62. eventName := "Convert to Helm charts"
  63. w.Event(progress.CreatingEvent(eventName))
  64. chart, err := helm.GetChartInMemory(project)
  65. if err != nil {
  66. return err
  67. }
  68. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  69. eventName = "Install Helm charts"
  70. w.Event(progress.CreatingEvent(eventName))
  71. err = s.sdk.InstallChart(project.Name, chart, func(format string, v ...interface{}) {
  72. message := fmt.Sprintf(format, v...)
  73. w.Event(progress.NewEvent(eventName, progress.Done, message))
  74. })
  75. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  76. return s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
  77. ProjectName: project.Name,
  78. Services: project.ServiceNames(),
  79. Status: compose.RUNNING,
  80. Log: func(pod string, stateReached bool, message string) {
  81. state := progress.Done
  82. if !stateReached {
  83. state = progress.Working
  84. }
  85. w.Event(progress.NewEvent(pod, state, message))
  86. },
  87. })
  88. }
  89. // Down executes the equivalent to a `compose down`
  90. func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
  91. w := progress.ContextWriter(ctx)
  92. eventName := fmt.Sprintf("Remove %s", projectName)
  93. w.Event(progress.CreatingEvent(eventName))
  94. logger := func(format string, v ...interface{}) {
  95. message := fmt.Sprintf(format, v...)
  96. if strings.Contains(message, "Starting delete") {
  97. action := strings.Replace(message, "Starting delete for", "Delete", 1)
  98. w.Event(progress.CreatingEvent(action))
  99. w.Event(progress.NewEvent(action, progress.Done, ""))
  100. return
  101. }
  102. w.Event(progress.NewEvent(eventName, progress.Working, message))
  103. }
  104. err := s.sdk.Uninstall(projectName, logger)
  105. if err != nil {
  106. return err
  107. }
  108. events := []string{}
  109. err = s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
  110. ProjectName: projectName,
  111. Services: nil,
  112. Status: compose.REMOVING,
  113. Timeout: options.Timeout,
  114. Log: func(pod string, stateReached bool, message string) {
  115. state := progress.Done
  116. if !stateReached {
  117. state = progress.Working
  118. }
  119. w.Event(progress.NewEvent(pod, state, message))
  120. if !utils.StringContains(events, pod) {
  121. events = append(events, pod)
  122. }
  123. },
  124. })
  125. if err != nil {
  126. return err
  127. }
  128. for _, e := range events {
  129. w.Event(progress.NewEvent(e, progress.Done, ""))
  130. }
  131. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  132. return nil
  133. }
  134. // List executes the equivalent to a `docker stack ls`
  135. func (s *composeService) List(ctx context.Context, opts compose.ListOptions) ([]compose.Stack, error) {
  136. return s.sdk.ListReleases()
  137. }
  138. // Build executes the equivalent to a `compose build`
  139. func (s *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
  140. return errdefs.ErrNotImplemented
  141. }
  142. // Push executes the equivalent ot a `compose push`
  143. func (s *composeService) Push(ctx context.Context, project *types.Project) error {
  144. return errdefs.ErrNotImplemented
  145. }
  146. // Pull executes the equivalent of a `compose pull`
  147. func (s *composeService) Pull(ctx context.Context, project *types.Project) error {
  148. return errdefs.ErrNotImplemented
  149. }
  150. // Create executes the equivalent to a `compose create`
  151. func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
  152. return errdefs.ErrNotImplemented
  153. }
  154. // Start executes the equivalent to a `compose start`
  155. func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
  156. return errdefs.ErrNotImplemented
  157. }
  158. // Stop executes the equivalent to a `compose stop`
  159. func (s *composeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
  160. return errdefs.ErrNotImplemented
  161. }
  162. // Logs executes the equivalent to a `compose logs`
  163. func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  164. if len(options.Services) > 0 {
  165. consumer = utils.FilteredLogConsumer(consumer, options.Services)
  166. }
  167. return s.client.GetLogs(ctx, projectName, consumer, options.Follow)
  168. }
  169. // Ps executes the equivalent to a `compose ps`
  170. func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
  171. return s.client.GetContainers(ctx, projectName, options.All)
  172. }
  173. // Convert translate compose model into backend's native format
  174. func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
  175. chart, err := helm.GetChartInMemory(project)
  176. if err != nil {
  177. return nil, err
  178. }
  179. if options.Output != "" {
  180. _, err := helm.SaveChart(chart, options.Output)
  181. return nil, err
  182. }
  183. buff := []byte{}
  184. for _, f := range chart.Raw {
  185. header := "\n" + f.Name + "\n" + strings.Repeat("-", len(f.Name)) + "\n"
  186. buff = append(buff, []byte(header)...)
  187. buff = append(buff, f.Data...)
  188. buff = append(buff, []byte("\n")...)
  189. }
  190. return buff, nil
  191. }
  192. func (s *composeService) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error {
  193. return errdefs.ErrNotImplemented
  194. }
  195. // RunOneOffContainer creates a service oneoff container and starts its dependencies
  196. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
  197. return 0, errdefs.ErrNotImplemented
  198. }
  199. func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
  200. return nil, errdefs.ErrNotImplemented
  201. }
  202. // Exec executes a command in a running service container
  203. func (s *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
  204. return errdefs.ErrNotImplemented
  205. }
  206. func (s *composeService) Pause(ctx context.Context, project *types.Project) error {
  207. return errdefs.ErrNotImplemented
  208. }
  209. func (s *composeService) UnPause(ctx context.Context, project *types.Project) error {
  210. return errdefs.ErrNotImplemented
  211. }