build.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright 2017 The Kubernetes 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 build
  14. import (
  15. "io"
  16. "github.com/pkg/errors"
  17. "github.com/spf13/cobra"
  18. "sigs.k8s.io/kustomize/pkg/constants"
  19. "sigs.k8s.io/kustomize/pkg/fs"
  20. "sigs.k8s.io/kustomize/pkg/ifc/transformer"
  21. "sigs.k8s.io/kustomize/pkg/loader"
  22. "sigs.k8s.io/kustomize/pkg/resmap"
  23. "sigs.k8s.io/kustomize/pkg/target"
  24. )
  25. // Options contain the options for running a build
  26. type Options struct {
  27. kustomizationPath string
  28. outputPath string
  29. }
  30. // NewOptions creates a Options object
  31. func NewOptions(p, o string) *Options {
  32. return &Options{
  33. kustomizationPath: p,
  34. outputPath: o,
  35. }
  36. }
  37. var examples = `
  38. Use the file somedir/kustomization.yaml to generate a set of api resources:
  39. build somedir
  40. Use a url pointing to a remote directory/kustomization.yaml to generate a set of api resources:
  41. build url
  42. The url should follow hashicorp/go-getter URL format described in
  43. https://github.com/hashicorp/go-getter#url-format
  44. url examples:
  45. sigs.k8s.io/kustomize//examples/multibases?ref=v1.0.6
  46. github.com/Liujingfang1/mysql
  47. github.com/Liujingfang1/kustomize//examples/helloWorld?ref=repoUrl2
  48. `
  49. // NewCmdBuild creates a new build command.
  50. func NewCmdBuild(
  51. out io.Writer, fs fs.FileSystem,
  52. rf *resmap.Factory,
  53. ptf transformer.Factory) *cobra.Command {
  54. var o Options
  55. cmd := &cobra.Command{
  56. Use: "build [path]",
  57. Short: "Print current configuration per contents of " + constants.KustomizationFileNames[0],
  58. Example: examples,
  59. SilenceUsage: true,
  60. RunE: func(cmd *cobra.Command, args []string) error {
  61. err := o.Validate(args)
  62. if err != nil {
  63. return err
  64. }
  65. return o.RunBuild(out, fs, rf, ptf)
  66. },
  67. }
  68. cmd.Flags().StringVarP(
  69. &o.outputPath,
  70. "output", "o", "",
  71. "If specified, write the build output to this path.")
  72. return cmd
  73. }
  74. // Validate validates build command.
  75. func (o *Options) Validate(args []string) error {
  76. if len(args) > 1 {
  77. return errors.New("specify one path to " + constants.KustomizationFileNames[0])
  78. }
  79. if len(args) == 0 {
  80. o.kustomizationPath = "./"
  81. } else {
  82. o.kustomizationPath = args[0]
  83. }
  84. return nil
  85. }
  86. // RunBuild runs build command.
  87. func (o *Options) RunBuild(
  88. out io.Writer, fSys fs.FileSystem,
  89. rf *resmap.Factory, ptf transformer.Factory) error {
  90. ldr, err := loader.NewLoader(o.kustomizationPath, fSys)
  91. if err != nil {
  92. return err
  93. }
  94. defer ldr.Cleanup()
  95. kt, err := target.NewKustTarget(ldr, rf, ptf)
  96. if err != nil {
  97. return err
  98. }
  99. allResources, err := kt.MakeCustomizedResMap()
  100. if err != nil {
  101. return err
  102. }
  103. // Output the objects.
  104. res, err := allResources.EncodeAsYaml()
  105. if err != nil {
  106. return err
  107. }
  108. if o.outputPath != "" {
  109. return fSys.WriteFile(o.outputPath, res)
  110. }
  111. _, err = out.Write(res)
  112. return err
  113. }