kustomize.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2019 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 kustomize
  14. import (
  15. "errors"
  16. "github.com/spf13/cobra"
  17. "k8s.io/cli-runtime/pkg/genericclioptions"
  18. "k8s.io/cli-runtime/pkg/kustomize"
  19. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  20. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  21. "sigs.k8s.io/kustomize/pkg/fs"
  22. )
  23. type kustomizeOptions struct {
  24. kustomizationDir string
  25. }
  26. var (
  27. kustomizeLong = templates.LongDesc(i18n.T(`
  28. Print a set of API resources generated from instructions in a kustomization.yaml file.
  29. The argument must be the path to the directory containing
  30. the file, or a git repository
  31. URL with a path suffix specifying same with respect to the
  32. repository root.
  33. kubectl kustomize somedir
  34. `))
  35. kustomizeExample = templates.Examples(i18n.T(`
  36. # Use the current working directory
  37. kubectl kustomize .
  38. # Use some shared configuration directory
  39. kubectl kustomize /home/configuration/production
  40. # Use a URL
  41. kubectl kustomize github.com/kubernetes-sigs/kustomize.git/examples/helloWorld?ref=v1.0.6
  42. `))
  43. )
  44. // NewCmdKustomize returns a kustomize command
  45. func NewCmdKustomize(streams genericclioptions.IOStreams) *cobra.Command {
  46. var o kustomizeOptions
  47. cmd := &cobra.Command{
  48. Use: "kustomize <dir>",
  49. Short: i18n.T("Build a kustomization target from a directory or a remote url."),
  50. Long: kustomizeLong,
  51. Example: kustomizeExample,
  52. RunE: func(cmd *cobra.Command, args []string) error {
  53. err := o.Validate(args)
  54. if err != nil {
  55. return err
  56. }
  57. return kustomize.RunKustomizeBuild(streams.Out, fs.MakeRealFS(), o.kustomizationDir)
  58. },
  59. }
  60. return cmd
  61. }
  62. // Validate validates build command.
  63. func (o *kustomizeOptions) Validate(args []string) error {
  64. if len(args) > 1 {
  65. return errors.New("specify one path to a kustomization directory")
  66. }
  67. if len(args) == 0 {
  68. o.kustomizationDir = "./"
  69. } else {
  70. o.kustomizationDir = args[0]
  71. }
  72. return nil
  73. }