bridge.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "github.com/distribution/reference"
  19. "github.com/docker/cli/cli/command"
  20. "github.com/docker/docker/api/types/image"
  21. "github.com/docker/docker/pkg/stringid"
  22. "github.com/docker/go-units"
  23. "github.com/spf13/cobra"
  24. "github.com/docker/compose/v5/cmd/formatter"
  25. "github.com/docker/compose/v5/pkg/bridge"
  26. "github.com/docker/compose/v5/pkg/compose"
  27. )
  28. func bridgeCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
  29. cmd := &cobra.Command{
  30. Use: "bridge CMD [OPTIONS]",
  31. Short: "Convert compose files into another model",
  32. TraverseChildren: true,
  33. }
  34. cmd.AddCommand(
  35. convertCommand(p, dockerCli),
  36. transformersCommand(dockerCli),
  37. )
  38. return cmd
  39. }
  40. func convertCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
  41. convertOpts := bridge.ConvertOptions{}
  42. cmd := &cobra.Command{
  43. Use: "convert",
  44. Short: "Convert compose files to Kubernetes manifests, Helm charts, or another model",
  45. RunE: Adapt(func(ctx context.Context, args []string) error {
  46. return runConvert(ctx, dockerCli, p, convertOpts)
  47. }),
  48. }
  49. flags := cmd.Flags()
  50. flags.StringVarP(&convertOpts.Output, "output", "o", "out", "The output directory for the Kubernetes resources")
  51. flags.StringArrayVarP(&convertOpts.Transformations, "transformation", "t", nil, "Transformation to apply to compose model (default: docker/compose-bridge-kubernetes)")
  52. flags.StringVar(&convertOpts.Templates, "templates", "", "Directory containing transformation templates")
  53. return cmd
  54. }
  55. func runConvert(ctx context.Context, dockerCli command.Cli, p *ProjectOptions, opts bridge.ConvertOptions) error {
  56. backend, err := compose.NewComposeService(dockerCli)
  57. if err != nil {
  58. return err
  59. }
  60. project, _, err := p.ToProject(ctx, dockerCli, backend, nil)
  61. if err != nil {
  62. return err
  63. }
  64. return bridge.Convert(ctx, dockerCli, project, opts)
  65. }
  66. func transformersCommand(dockerCli command.Cli) *cobra.Command {
  67. cmd := &cobra.Command{
  68. Use: "transformations CMD [OPTIONS]",
  69. Short: "Manage transformation images",
  70. }
  71. cmd.AddCommand(
  72. listTransformersCommand(dockerCli),
  73. createTransformerCommand(dockerCli),
  74. )
  75. return cmd
  76. }
  77. func listTransformersCommand(dockerCli command.Cli) *cobra.Command {
  78. options := lsOptions{}
  79. cmd := &cobra.Command{
  80. Use: "list",
  81. Aliases: []string{"ls"},
  82. Short: "List available transformations",
  83. RunE: Adapt(func(ctx context.Context, args []string) error {
  84. transformers, err := bridge.ListTransformers(ctx, dockerCli)
  85. if err != nil {
  86. return err
  87. }
  88. return displayTransformer(dockerCli, transformers, options)
  89. }),
  90. }
  91. cmd.Flags().StringVar(&options.Format, "format", "table", "Format the output. Values: [table | json]")
  92. cmd.Flags().BoolVarP(&options.Quiet, "quiet", "q", false, "Only display transformer names")
  93. return cmd
  94. }
  95. func displayTransformer(dockerCli command.Cli, transformers []image.Summary, options lsOptions) error {
  96. if options.Quiet {
  97. for _, t := range transformers {
  98. if len(t.RepoTags) > 0 {
  99. _, _ = fmt.Fprintln(dockerCli.Out(), t.RepoTags[0])
  100. } else {
  101. _, _ = fmt.Fprintln(dockerCli.Out(), t.ID)
  102. }
  103. }
  104. return nil
  105. }
  106. return formatter.Print(transformers, options.Format, dockerCli.Out(),
  107. func(w io.Writer) {
  108. for _, img := range transformers {
  109. id := stringid.TruncateID(img.ID)
  110. size := units.HumanSizeWithPrecision(float64(img.Size), 3)
  111. repo, tag := "<none>", "<none>"
  112. if len(img.RepoTags) > 0 {
  113. ref, err := reference.ParseDockerRef(img.RepoTags[0])
  114. if err == nil {
  115. // ParseDockerRef will reject a local image ID
  116. repo = reference.FamiliarName(ref)
  117. if tagged, ok := ref.(reference.Tagged); ok {
  118. tag = tagged.Tag()
  119. }
  120. }
  121. }
  122. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", id, repo, tag, size)
  123. }
  124. },
  125. "IMAGE ID", "REPO", "TAGS", "SIZE")
  126. }
  127. func createTransformerCommand(dockerCli command.Cli) *cobra.Command {
  128. var opts bridge.CreateTransformerOptions
  129. cmd := &cobra.Command{
  130. Use: "create [OPTION] PATH",
  131. Short: "Create a new transformation",
  132. RunE: Adapt(func(ctx context.Context, args []string) error {
  133. opts.Dest = args[0]
  134. return bridge.CreateTransformer(ctx, dockerCli, opts)
  135. }),
  136. }
  137. cmd.Flags().StringVarP(&opts.From, "from", "f", "", "Existing transformation to copy (default: docker/compose-bridge-kubernetes)")
  138. return cmd
  139. }