compose.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/pkg/errors"
  21. "github.com/docker/compose-cli/api/compose"
  22. apicontext "github.com/docker/compose-cli/api/context"
  23. "github.com/docker/compose-cli/api/context/store"
  24. "github.com/docker/compose-cli/api/errdefs"
  25. "github.com/docker/compose-cli/api/progress"
  26. "github.com/docker/compose-cli/kube/client"
  27. "github.com/docker/compose-cli/kube/helm"
  28. "github.com/docker/compose-cli/kube/resources"
  29. "github.com/docker/compose-cli/utils"
  30. )
  31. type composeService struct {
  32. sdk *helm.Actions
  33. client *client.KubeClient
  34. }
  35. // NewComposeService create a kubernetes implementation of the compose.Service API
  36. func NewComposeService() (compose.Service, error) {
  37. contextStore := store.Instance()
  38. currentContext := apicontext.Current()
  39. var kubeContext store.KubeContext
  40. if err := contextStore.GetEndpoint(currentContext, &kubeContext); err != nil {
  41. return nil, err
  42. }
  43. config, err := resources.LoadConfig(kubeContext)
  44. if err != nil {
  45. return nil, err
  46. }
  47. actions, err := helm.NewActions(config)
  48. if err != nil {
  49. return nil, err
  50. }
  51. apiClient, err := client.NewKubeClient(config)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return &composeService{
  56. sdk: actions,
  57. client: apiClient,
  58. }, nil
  59. }
  60. // Up executes the equivalent to a `compose up`
  61. func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
  62. w := progress.ContextWriter(ctx)
  63. eventName := "Convert Compose file to Helm charts"
  64. w.Event(progress.CreatingEvent(eventName))
  65. chart, err := helm.GetChartInMemory(project)
  66. if err != nil {
  67. return err
  68. }
  69. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  70. stack, err := s.sdk.Get(project.Name)
  71. if err != nil || stack == nil {
  72. // install stack
  73. eventName = "Install Compose stack"
  74. w.Event(progress.CreatingEvent(eventName))
  75. err = s.sdk.InstallChart(project.Name, chart, func(format string, v ...interface{}) {
  76. message := fmt.Sprintf(format, v...)
  77. w.Event(progress.NewEvent(eventName, progress.Done, message))
  78. })
  79. } else {
  80. //update stack
  81. eventName = "Updating Compose stack"
  82. w.Event(progress.CreatingEvent(eventName))
  83. err = s.sdk.UpdateChart(project.Name, chart, func(format string, v ...interface{}) {
  84. message := fmt.Sprintf(format, v...)
  85. w.Event(progress.NewEvent(eventName, progress.Done, message))
  86. })
  87. }
  88. if err != nil {
  89. return err
  90. }
  91. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  92. return s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
  93. ProjectName: project.Name,
  94. Services: project.ServiceNames(),
  95. Status: compose.RUNNING,
  96. Log: func(pod string, stateReached bool, message string) {
  97. state := progress.Done
  98. if !stateReached {
  99. state = progress.Working
  100. }
  101. w.Event(progress.NewEvent(pod, state, message))
  102. },
  103. })
  104. }
  105. // Down executes the equivalent to a `compose down`
  106. func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
  107. if options.Volumes {
  108. return errors.Wrap(errdefs.ErrNotImplemented, "--volumes option is not supported on Kubernetes")
  109. }
  110. if options.Images != "" {
  111. return errors.Wrap(errdefs.ErrNotImplemented, "--rmi option is not supported on Kubernetes")
  112. }
  113. return progress.Run(ctx, func(ctx context.Context) error {
  114. return s.down(ctx, projectName, options)
  115. })
  116. }
  117. func (s *composeService) down(ctx context.Context, projectName string, options compose.DownOptions) error {
  118. w := progress.ContextWriter(ctx)
  119. eventName := fmt.Sprintf("Remove %s", projectName)
  120. w.Event(progress.CreatingEvent(eventName))
  121. logger := func(format string, v ...interface{}) {
  122. message := fmt.Sprintf(format, v...)
  123. if strings.Contains(message, "Starting delete") {
  124. action := strings.Replace(message, "Starting delete for", "Delete", 1)
  125. w.Event(progress.CreatingEvent(action))
  126. w.Event(progress.NewEvent(action, progress.Done, ""))
  127. return
  128. }
  129. w.Event(progress.NewEvent(eventName, progress.Working, message))
  130. }
  131. err := s.sdk.Uninstall(projectName, logger)
  132. if err != nil {
  133. return err
  134. }
  135. events := []string{}
  136. err = s.client.WaitForPodState(ctx, client.WaitForStatusOptions{
  137. ProjectName: projectName,
  138. Services: nil,
  139. Status: compose.REMOVING,
  140. Timeout: options.Timeout,
  141. Log: func(pod string, stateReached bool, message string) {
  142. state := progress.Done
  143. if !stateReached {
  144. state = progress.Working
  145. }
  146. w.Event(progress.NewEvent(pod, state, message))
  147. if !utils.StringContains(events, pod) {
  148. events = append(events, pod)
  149. }
  150. },
  151. })
  152. if err != nil {
  153. return err
  154. }
  155. for _, e := range events {
  156. w.Event(progress.NewEvent(e, progress.Done, ""))
  157. }
  158. w.Event(progress.NewEvent(eventName, progress.Done, ""))
  159. return nil
  160. }
  161. // List executes the equivalent to a `docker stack ls`
  162. func (s *composeService) List(ctx context.Context, opts compose.ListOptions) ([]compose.Stack, error) {
  163. return s.sdk.ListReleases()
  164. }
  165. // Build executes the equivalent to a `compose build`
  166. func (s *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error {
  167. return errdefs.ErrNotImplemented
  168. }
  169. // Push executes the equivalent ot a `compose push`
  170. func (s *composeService) Push(ctx context.Context, project *types.Project, options compose.PushOptions) error {
  171. return errdefs.ErrNotImplemented
  172. }
  173. // Pull executes the equivalent of a `compose pull`
  174. func (s *composeService) Pull(ctx context.Context, project *types.Project, options compose.PullOptions) error {
  175. return errdefs.ErrNotImplemented
  176. }
  177. // Create executes the equivalent to a `compose create`
  178. func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
  179. return errdefs.ErrNotImplemented
  180. }
  181. // Start executes the equivalent to a `compose start`
  182. func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
  183. return errdefs.ErrNotImplemented
  184. }
  185. // Restart executes the equivalent to a `compose restart`
  186. func (s *composeService) Restart(ctx context.Context, project *types.Project, options compose.RestartOptions) error {
  187. return errdefs.ErrNotImplemented
  188. }
  189. // Stop executes the equivalent to a `compose stop`
  190. func (s *composeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
  191. return errdefs.ErrNotImplemented
  192. }
  193. // Copy copies a file/folder between a service container and the local filesystem
  194. func (s *composeService) Copy(ctx context.Context, project *types.Project, options compose.CopyOptions) error {
  195. return errdefs.ErrNotImplemented
  196. }
  197. // Logs executes the equivalent to a `compose logs`
  198. func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
  199. if len(options.Services) > 0 {
  200. consumer = utils.FilteredLogConsumer(consumer, options.Services)
  201. }
  202. return s.client.GetLogs(ctx, projectName, consumer, options.Follow)
  203. }
  204. // Ps executes the equivalent to a `compose ps`
  205. func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
  206. return s.client.GetContainers(ctx, projectName, options.All)
  207. }
  208. // Convert translate compose model into backend's native format
  209. func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
  210. chart, err := helm.GetChartInMemory(project)
  211. if err != nil {
  212. return nil, err
  213. }
  214. if options.Output != "" {
  215. _, err := helm.SaveChart(chart, options.Output)
  216. return nil, err
  217. }
  218. buff := []byte{}
  219. for _, f := range chart.Raw {
  220. header := "\n" + f.Name + "\n" + strings.Repeat("-", len(f.Name)) + "\n"
  221. buff = append(buff, []byte(header)...)
  222. buff = append(buff, f.Data...)
  223. buff = append(buff, []byte("\n")...)
  224. }
  225. return buff, nil
  226. }
  227. func (s *composeService) Kill(ctx context.Context, project *types.Project, options compose.KillOptions) error {
  228. return errdefs.ErrNotImplemented
  229. }
  230. // RunOneOffContainer creates a service oneoff container and starts its dependencies
  231. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
  232. return 0, errdefs.ErrNotImplemented
  233. }
  234. func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) error {
  235. return errdefs.ErrNotImplemented
  236. }
  237. // Exec executes a command in a running service container
  238. func (s *composeService) Exec(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
  239. return 0, errdefs.ErrNotImplemented
  240. }
  241. func (s *composeService) Pause(ctx context.Context, project string, options compose.PauseOptions) error {
  242. return errdefs.ErrNotImplemented
  243. }
  244. func (s *composeService) UnPause(ctx context.Context, project string, options compose.PauseOptions) error {
  245. return errdefs.ErrNotImplemented
  246. }
  247. func (s *composeService) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) {
  248. return nil, errdefs.ErrNotImplemented
  249. }
  250. func (s *composeService) Events(ctx context.Context, project string, options compose.EventsOptions) error {
  251. return errdefs.ErrNotImplemented
  252. }
  253. func (s *composeService) Port(ctx context.Context, project string, service string, port int, options compose.PortOptions) (string, int, error) {
  254. return "", 0, errdefs.ErrNotImplemented
  255. }
  256. func (s *composeService) Images(ctx context.Context, projectName string, options compose.ImagesOptions) ([]compose.ImageSummary, error) {
  257. return nil, errdefs.ErrNotImplemented
  258. }