bridge.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/v2/cmd/formatter"
  25. "github.com/docker/compose/v2/pkg/bridge"
  26. )
  27. func bridgeCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
  28. cmd := &cobra.Command{
  29. Use: "bridge CMD [OPTIONS]",
  30. Short: "Convert compose files into another model",
  31. TraverseChildren: true,
  32. }
  33. cmd.AddCommand(
  34. convertCommand(p, dockerCli),
  35. transformersCommand(dockerCli),
  36. )
  37. return cmd
  38. }
  39. func convertCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
  40. convertOpts := bridge.ConvertOptions{}
  41. cmd := &cobra.Command{
  42. Use: "convert",
  43. Short: "Convert compose files to Kubernetes manifests, Helm charts, or another model",
  44. RunE: Adapt(func(ctx context.Context, args []string) error {
  45. return runConvert(ctx, dockerCli, p, convertOpts)
  46. }),
  47. }
  48. flags := cmd.Flags()
  49. flags.StringVarP(&convertOpts.Output, "output", "o", "out", "The output directory for the Kubernetes resources")
  50. flags.StringArrayVarP(&convertOpts.Transformations, "transformation", "t", nil, "Transformation to apply to compose model (default: docker/compose-bridge-kubernetes)")
  51. flags.StringVar(&convertOpts.Templates, "templates", "", "Directory containing transformation templates")
  52. return cmd
  53. }
  54. func runConvert(ctx context.Context, dockerCli command.Cli, p *ProjectOptions, opts bridge.ConvertOptions) error {
  55. project, _, err := p.ToProject(ctx, dockerCli, nil)
  56. if err != nil {
  57. return err
  58. }
  59. return bridge.Convert(ctx, dockerCli, project, opts)
  60. }
  61. func transformersCommand(dockerCli command.Cli) *cobra.Command {
  62. cmd := &cobra.Command{
  63. Use: "transformations CMD [OPTIONS]",
  64. Short: "Manage transformation images",
  65. }
  66. cmd.AddCommand(
  67. listTransformersCommand(dockerCli),
  68. createTransformerCommand(dockerCli),
  69. )
  70. return cmd
  71. }
  72. func listTransformersCommand(dockerCli command.Cli) *cobra.Command {
  73. options := lsOptions{}
  74. cmd := &cobra.Command{
  75. Use: "list",
  76. Aliases: []string{"ls"},
  77. Short: "List available transformations",
  78. RunE: Adapt(func(ctx context.Context, args []string) error {
  79. transformers, err := bridge.ListTransformers(ctx, dockerCli)
  80. if err != nil {
  81. return err
  82. }
  83. return displayTransformer(dockerCli, transformers, options)
  84. }),
  85. }
  86. cmd.Flags().StringVar(&options.Format, "format", "table", "Format the output. Values: [table | json]")
  87. cmd.Flags().BoolVarP(&options.Quiet, "quiet", "q", false, "Only display transformer names")
  88. return cmd
  89. }
  90. func displayTransformer(dockerCli command.Cli, transformers []image.Summary, options lsOptions) error {
  91. if options.Quiet {
  92. for _, t := range transformers {
  93. if len(t.RepoTags) > 0 {
  94. _, _ = fmt.Fprintln(dockerCli.Out(), t.RepoTags[0])
  95. } else {
  96. _, _ = fmt.Fprintln(dockerCli.Out(), t.ID)
  97. }
  98. }
  99. return nil
  100. }
  101. return formatter.Print(transformers, options.Format, dockerCli.Out(),
  102. func(w io.Writer) {
  103. for _, img := range transformers {
  104. id := stringid.TruncateID(img.ID)
  105. size := units.HumanSizeWithPrecision(float64(img.Size), 3)
  106. repo, tag := "<none>", "<none>"
  107. if len(img.RepoTags) > 0 {
  108. ref, err := reference.ParseDockerRef(img.RepoTags[0])
  109. if err == nil {
  110. // ParseDockerRef will reject a local image ID
  111. repo = reference.FamiliarName(ref)
  112. if tagged, ok := ref.(reference.Tagged); ok {
  113. tag = tagged.Tag()
  114. }
  115. }
  116. }
  117. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", id, repo, tag, size)
  118. }
  119. },
  120. "IMAGE ID", "REPO", "TAGS", "SIZE")
  121. }
  122. func createTransformerCommand(dockerCli command.Cli) *cobra.Command {
  123. var opts bridge.CreateTransformerOptions
  124. cmd := &cobra.Command{
  125. Use: "create [OPTION] PATH",
  126. Short: "Create a new transformation",
  127. RunE: Adapt(func(ctx context.Context, args []string) error {
  128. opts.Dest = args[0]
  129. return bridge.CreateTransformer(ctx, dockerCli, opts)
  130. }),
  131. }
  132. cmd.Flags().StringVarP(&opts.From, "from", "f", "", "Existing transformation to copy (default: docker/compose-bridge-kubernetes)")
  133. return cmd
  134. }