compose.go 7.1 KB

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