compose.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. )
  29. type composeService struct {
  30. sdk *helm.Actions
  31. client *client.KubeClient
  32. }
  33. // NewComposeService create a kubernetes implementation of the compose.Service API
  34. func NewComposeService(ctx context.Context) (compose.Service, error) {
  35. contextStore := store.ContextStore(ctx)
  36. currentContext := apicontext.CurrentContext(ctx)
  37. var kubeContext store.KubeContext
  38. if err := contextStore.GetEndpoint(currentContext, &kubeContext); err != nil {
  39. return nil, err
  40. }
  41. config, err := resources.LoadConfig(kubeContext)
  42. if err != nil {
  43. return nil, err
  44. }
  45. actions, err := helm.NewActions(config)
  46. if err != nil {
  47. return nil, err
  48. }
  49. apiClient, err := client.NewKubeClient(config)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &composeService{
  54. sdk: actions,
  55. client: apiClient,
  56. }, nil
  57. }
  58. // Up executes the equivalent to a `compose up`
  59. func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
  60. w := progress.ContextWriter(ctx)
  61. eventName := "Convert to Helm charts"
  62. w.Event(progress.CreatingEvent(eventName))
  63. chart, err := helm.GetChartInMemory(project)
  64. if err != nil {
  65. return err
  66. }
  67. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  68. eventName = "Install Helm charts"
  69. w.Event(progress.CreatingEvent(eventName))
  70. err = s.sdk.InstallChart(project.Name, chart, func(format string, v ...interface{}) {
  71. message := fmt.Sprintf(format, v...)
  72. w.Event(progress.NewEvent(eventName, progress.Done, message))
  73. })
  74. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  75. return err
  76. }
  77. // Down executes the equivalent to a `compose down`
  78. func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
  79. w := progress.ContextWriter(ctx)
  80. eventName := fmt.Sprintf("Remove %s", projectName)
  81. w.Event(progress.CreatingEvent(eventName))
  82. logger := func(format string, v ...interface{}) {
  83. message := fmt.Sprintf(format, v...)
  84. if strings.Contains(message, "Starting delete") {
  85. action := strings.Replace(message, "Starting delete for", "Delete", 1)
  86. w.Event(progress.CreatingEvent(action))
  87. w.Event(progress.NewEvent(action, progress.Done, ""))
  88. return
  89. }
  90. w.Event(progress.NewEvent(eventName, progress.Working, message))
  91. }
  92. err := s.sdk.Uninstall(projectName, logger)
  93. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  94. return err
  95. }
  96. // List executes the equivalent to a `docker stack ls`
  97. func (s *composeService) List(ctx context.Context) ([]compose.Stack, error) {
  98. return s.sdk.ListReleases()
  99. }
  100. // Build executes the equivalent to a `compose build`
  101. func (s *composeService) Build(ctx context.Context, project *types.Project) error {
  102. return errdefs.ErrNotImplemented
  103. }
  104. // Push executes the equivalent ot a `compose push`
  105. func (s *composeService) Push(ctx context.Context, project *types.Project) error {
  106. return errdefs.ErrNotImplemented
  107. }
  108. // Pull executes the equivalent of a `compose pull`
  109. func (s *composeService) Pull(ctx context.Context, project *types.Project) error {
  110. return errdefs.ErrNotImplemented
  111. }
  112. // Create executes the equivalent to a `compose create`
  113. func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
  114. return errdefs.ErrNotImplemented
  115. }
  116. // Start executes the equivalent to a `compose start`
  117. func (s *composeService) Start(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
  118. return errdefs.ErrNotImplemented
  119. }
  120. // Stop executes the equivalent to a `compose stop`
  121. func (s *composeService) Stop(ctx context.Context, project *types.Project) error {
  122. return errdefs.ErrNotImplemented
  123. }
  124. // Logs executes the equivalent to a `compose logs`
  125. func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  126. return errdefs.ErrNotImplemented
  127. }
  128. // Ps executes the equivalent to a `compose ps`
  129. func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
  130. return s.client.GetContainers(ctx, projectName, options.All)
  131. }
  132. // Convert translate compose model into backend's native format
  133. func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
  134. chart, err := helm.GetChartInMemory(project)
  135. if err != nil {
  136. return nil, err
  137. }
  138. buff := []byte{}
  139. for _, f := range chart.Raw {
  140. header := "\n" + f.Name + "\n" + strings.Repeat("-", len(f.Name)) + "\n"
  141. buff = append(buff, []byte(header)...)
  142. buff = append(buff, f.Data...)
  143. buff = append(buff, []byte("\n")...)
  144. }
  145. return buff, nil
  146. }
  147. func (s *composeService) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error {
  148. return errdefs.ErrNotImplemented
  149. }
  150. // RunOneOffContainer creates a service oneoff container and starts its dependencies
  151. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
  152. return errdefs.ErrNotImplemented
  153. }