compose.go 6.3 KB

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