compose.go 5.5 KB

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