compose.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. if err != nil {
  76. return err
  77. }
  78. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  79. return s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
  80. ProjectName: project.Name,
  81. Services: project.ServiceNames(),
  82. Status: compose.RUNNING,
  83. Log: func(pod string, stateReached bool, message string) {
  84. state := progress.Done
  85. if !stateReached {
  86. state = progress.Working
  87. }
  88. w.Event(progress.NewEvent(pod, state, message))
  89. },
  90. })
  91. }
  92. // Down executes the equivalent to a `compose down`
  93. func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
  94. w := progress.ContextWriter(ctx)
  95. eventName := fmt.Sprintf("Remove %s", projectName)
  96. w.Event(progress.CreatingEvent(eventName))
  97. logger := func(format string, v ...interface{}) {
  98. message := fmt.Sprintf(format, v...)
  99. if strings.Contains(message, "Starting delete") {
  100. action := strings.Replace(message, "Starting delete for", "Delete", 1)
  101. w.Event(progress.CreatingEvent(action))
  102. w.Event(progress.NewEvent(action, progress.Done, ""))
  103. return
  104. }
  105. w.Event(progress.NewEvent(eventName, progress.Working, message))
  106. }
  107. err := s.sdk.Uninstall(projectName, logger)
  108. if err != nil {
  109. return err
  110. }
  111. events := []string{}
  112. err = s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
  113. ProjectName: projectName,
  114. Services: nil,
  115. Status: compose.REMOVING,
  116. Timeout: options.Timeout,
  117. Log: func(pod string, stateReached bool, message string) {
  118. state := progress.Done
  119. if !stateReached {
  120. state = progress.Working
  121. }
  122. w.Event(progress.NewEvent(pod, state, message))
  123. if !utils.StringContains(events, pod) {
  124. events = append(events, pod)
  125. }
  126. },
  127. })
  128. if err != nil {
  129. return err
  130. }
  131. for _, e := range events {
  132. w.Event(progress.NewEvent(e, progress.Done, ""))
  133. }
  134. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  135. return nil
  136. }
  137. // List executes the equivalent to a `docker stack ls`
  138. func (s *composeService) List(ctx context.Context, opts compose.ListOptions) ([]compose.Stack, error) {
  139. return s.sdk.ListReleases()
  140. }
  141. // Build executes the equivalent to a `compose build`
  142. func (s *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
  143. return errdefs.ErrNotImplemented
  144. }
  145. // Push executes the equivalent ot a `compose push`
  146. func (s *composeService) Push(ctx context.Context, project *types.Project, options compose.PushOptions) error {
  147. return errdefs.ErrNotImplemented
  148. }
  149. // Pull executes the equivalent of a `compose pull`
  150. func (s *composeService) Pull(ctx context.Context, project *types.Project) error {
  151. return errdefs.ErrNotImplemented
  152. }
  153. // Create executes the equivalent to a `compose create`
  154. func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
  155. return errdefs.ErrNotImplemented
  156. }
  157. // Start executes the equivalent to a `compose start`
  158. func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
  159. return errdefs.ErrNotImplemented
  160. }
  161. // Stop executes the equivalent to a `compose stop`
  162. func (s *composeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
  163. return errdefs.ErrNotImplemented
  164. }
  165. // Logs executes the equivalent to a `compose logs`
  166. func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  167. if len(options.Services) > 0 {
  168. consumer = utils.FilteredLogConsumer(consumer, options.Services)
  169. }
  170. return s.client.GetLogs(ctx, projectName, consumer, options.Follow)
  171. }
  172. // Ps executes the equivalent to a `compose ps`
  173. func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
  174. return s.client.GetContainers(ctx, projectName, options.All)
  175. }
  176. // Convert translate compose model into backend's native format
  177. func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
  178. chart, err := helm.GetChartInMemory(project)
  179. if err != nil {
  180. return nil, err
  181. }
  182. if options.Output != "" {
  183. _, err := helm.SaveChart(chart, options.Output)
  184. return nil, err
  185. }
  186. buff := []byte{}
  187. for _, f := range chart.Raw {
  188. header := "\n" + f.Name + "\n" + strings.Repeat("-", len(f.Name)) + "\n"
  189. buff = append(buff, []byte(header)...)
  190. buff = append(buff, f.Data...)
  191. buff = append(buff, []byte("\n")...)
  192. }
  193. return buff, nil
  194. }
  195. func (s *composeService) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error {
  196. return errdefs.ErrNotImplemented
  197. }
  198. // RunOneOffContainer creates a service oneoff container and starts its dependencies
  199. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
  200. return 0, errdefs.ErrNotImplemented
  201. }
  202. func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
  203. return nil, errdefs.ErrNotImplemented
  204. }
  205. // Exec executes a command in a running service container
  206. func (s *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
  207. return errdefs.ErrNotImplemented
  208. }
  209. func (s *composeService) Pause(ctx context.Context, project *types.Project) error {
  210. return errdefs.ErrNotImplemented
  211. }
  212. func (s *composeService) UnPause(ctx context.Context, project *types.Project) error {
  213. return errdefs.ErrNotImplemented
  214. }