| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package commands |
| 5 |
|
| 6 |
import ( |
| 7 |
"path/filepath" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"sigs.k8s.io/kustomize/cmd/config/ext" |
| 11 |
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands" |
| 12 |
"sigs.k8s.io/kustomize/cmd/config/runner" |
| 13 |
"sigs.k8s.io/kustomize/kyaml/kio/filters" |
| 14 |
|
| 15 |
"github.com/spf13/cobra" |
| 16 |
"sigs.k8s.io/kustomize/kyaml/kio" |
| 17 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 18 |
) |
| 19 |
|
| 20 |
func GetTreeRunner(name string) *TreeRunner { |
| 21 |
r := &TreeRunner{} |
| 22 |
c := &cobra.Command{ |
| 23 |
Use: "tree [DIR]", |
| 24 |
Short: commands.TreeShort, |
| 25 |
Long: commands.TreeLong, |
| 26 |
Example: commands.TreeExamples, |
| 27 |
RunE: r.runE, |
| 28 |
Args: cobra.MaximumNArgs(1), |
| 29 |
} |
| 30 |
runner.FixDocs(name, c) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
c.Flags().BoolVar(&r.name, "name", false, "print name field") |
| 35 |
c.Flags().BoolVar(&r.resources, "resources", false, "print resources field") |
| 36 |
c.Flags().BoolVar(&r.ports, "ports", false, "print ports field") |
| 37 |
c.Flags().BoolVar(&r.images, "image", false, "print image field") |
| 38 |
c.Flags().BoolVar(&r.replicas, "replicas", false, "print replicas field") |
| 39 |
c.Flags().BoolVar(&r.args, "args", false, "print args field") |
| 40 |
c.Flags().BoolVar(&r.cmd, "command", false, "print command field") |
| 41 |
c.Flags().BoolVar(&r.env, "env", false, "print env field") |
| 42 |
c.Flags().BoolVar(&r.all, "all", false, "print all field infos") |
| 43 |
c.Flags().StringSliceVar(&r.fields, "field", []string{}, "print field") |
| 44 |
c.Flags().BoolVar(&r.includeLocal, "include-local", false, |
| 45 |
"if true, include local-config in the output.") |
| 46 |
c.Flags().BoolVar(&r.excludeNonLocal, "exclude-non-local", false, |
| 47 |
"if true, exclude non-local-config in the output.") |
| 48 |
c.Flags().StringVar(&r.structure, "graph-structure", "", |
| 49 |
"Graph structure to use for printing the tree. may be any of: "+ |
| 50 |
strings.Join(kio.GraphStructures, ",")) |
| 51 |
|
| 52 |
r.Command = c |
| 53 |
return r |
| 54 |
} |
| 55 |
|
| 56 |
func TreeCommand(name string) *cobra.Command { |
| 57 |
return GetTreeRunner(name).Command |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
type TreeRunner struct { |
| 62 |
Command *cobra.Command |
| 63 |
name bool |
| 64 |
resources bool |
| 65 |
ports bool |
| 66 |
images bool |
| 67 |
replicas bool |
| 68 |
all bool |
| 69 |
env bool |
| 70 |
args bool |
| 71 |
cmd bool |
| 72 |
fields []string |
| 73 |
includeLocal bool |
| 74 |
excludeNonLocal bool |
| 75 |
structure string |
| 76 |
} |
| 77 |
|
| 78 |
func (r *TreeRunner) runE(c *cobra.Command, args []string) error { |
| 79 |
var input kio.Reader |
| 80 |
var root = "." |
| 81 |
if len(args) == 0 { |
| 82 |
args = append(args, root) |
| 83 |
} |
| 84 |
if args[0] == "-" { |
| 85 |
input = &kio.ByteReader{Reader: c.InOrStdin()} |
| 86 |
} else { |
| 87 |
root = filepath.Clean(args[0]) |
| 88 |
input = kio.LocalPackageReader{PackagePath: args[0]} |
| 89 |
} |
| 90 |
|
| 91 |
var fields []kio.TreeWriterField |
| 92 |
for _, field := range r.fields { |
| 93 |
path, err := runner.ParseFieldPath(field) |
| 94 |
if err != nil { |
| 95 |
return err |
| 96 |
} |
| 97 |
fields = append(fields, newField(path...)) |
| 98 |
} |
| 99 |
|
| 100 |
if r.name || (r.all && !c.Flag("name").Changed) { |
| 101 |
fields = append(fields, |
| 102 |
newField("spec", "containers", "[name=.*]", "name"), |
| 103 |
newField("spec", "template", "spec", "containers", "[name=.*]", "name"), |
| 104 |
newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "name"), |
| 105 |
) |
| 106 |
} |
| 107 |
if r.images || (r.all && !c.Flag("image").Changed) { |
| 108 |
fields = append(fields, |
| 109 |
newField("spec", "containers", "[name=.*]", "image"), |
| 110 |
newField("spec", "template", "spec", "containers", "[name=.*]", "image"), |
| 111 |
newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "image"), |
| 112 |
) |
| 113 |
} |
| 114 |
|
| 115 |
if r.cmd || (r.all && !c.Flag("command").Changed) { |
| 116 |
fields = append(fields, |
| 117 |
newField("spec", "containers", "[name=.*]", "command"), |
| 118 |
newField("spec", "template", "spec", "containers", "[name=.*]", "command"), |
| 119 |
newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "command"), |
| 120 |
) |
| 121 |
} |
| 122 |
if r.args || (r.all && !c.Flag("args").Changed) { |
| 123 |
fields = append(fields, |
| 124 |
newField("spec", "containers", "[name=.*]", "args"), |
| 125 |
newField("spec", "template", "spec", "containers", "[name=.*]", "args"), |
| 126 |
newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "args"), |
| 127 |
) |
| 128 |
} |
| 129 |
if r.env || (r.all && !c.Flag("env").Changed) { |
| 130 |
fields = append(fields, |
| 131 |
newField("spec", "containers", "[name=.*]", "env"), |
| 132 |
newField("spec", "template", "spec", "containers", "[name=.*]", "env"), |
| 133 |
newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "env"), |
| 134 |
) |
| 135 |
} |
| 136 |
|
| 137 |
if r.replicas || (r.all && !c.Flag("replicas").Changed) { |
| 138 |
fields = append(fields, |
| 139 |
newField("spec", "replicas"), |
| 140 |
) |
| 141 |
} |
| 142 |
if r.resources || (r.all && !c.Flag("resources").Changed) { |
| 143 |
fields = append(fields, |
| 144 |
newField("spec", "containers", "[name=.*]", "resources"), |
| 145 |
newField("spec", "template", "spec", "containers", "[name=.*]", "resources"), |
| 146 |
newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "resources"), |
| 147 |
) |
| 148 |
} |
| 149 |
if r.ports || (r.all && !c.Flag("ports").Changed) { |
| 150 |
fields = append(fields, |
| 151 |
newField("spec", "containers", "[name=.*]", "ports"), |
| 152 |
newField("spec", "template", "spec", "containers", "[name=.*]", "ports"), |
| 153 |
newField("spec", "jobTemplate", "spec", "template", "spec", "containers", "[name=.*]", "ports"), |
| 154 |
newField("spec", "ports"), |
| 155 |
) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
fltrs := []kio.Filter{&filters.IsLocalConfig{ |
| 160 |
IncludeLocalConfig: r.includeLocal, |
| 161 |
ExcludeNonLocalConfig: r.excludeNonLocal, |
| 162 |
}} |
| 163 |
|
| 164 |
return runner.HandleError(c, kio.Pipeline{ |
| 165 |
Inputs: []kio.Reader{input}, |
| 166 |
Filters: fltrs, |
| 167 |
Outputs: []kio.Writer{kio.TreeWriter{ |
| 168 |
Root: root, |
| 169 |
Writer: c.OutOrStdout(), |
| 170 |
Fields: fields, |
| 171 |
Structure: kio.TreeStructure(r.structure), |
| 172 |
OpenAPIFileName: ext.KRMFileName(), |
| 173 |
}}, |
| 174 |
}.Execute()) |
| 175 |
} |
| 176 |
|
| 177 |
func newField(val ...string) kio.TreeWriterField { |
| 178 |
if strings.HasPrefix(strings.Join(val, "."), "spec.template.spec.containers") { |
| 179 |
return kio.TreeWriterField{ |
| 180 |
Name: "spec.template.spec.containers", |
| 181 |
PathMatcher: yaml.PathMatcher{Path: val, StripComments: true}, |
| 182 |
SubName: val[len(val)-1], |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if strings.HasPrefix(strings.Join(val, "."), "spec.jobTemplate.spec.template.spec.containers") { |
| 187 |
return kio.TreeWriterField{ |
| 188 |
Name: "spec.jobTemplate.spec.template.spec.containers", |
| 189 |
PathMatcher: yaml.PathMatcher{Path: val, StripComments: true}, |
| 190 |
SubName: val[len(val)-1], |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if strings.HasPrefix(strings.Join(val, "."), "spec.containers") { |
| 195 |
return kio.TreeWriterField{ |
| 196 |
Name: "spec.containers", |
| 197 |
PathMatcher: yaml.PathMatcher{Path: val, StripComments: true}, |
| 198 |
SubName: val[len(val)-1], |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return kio.TreeWriterField{ |
| 203 |
Name: strings.Join(val, "."), |
| 204 |
PathMatcher: yaml.PathMatcher{Path: val, StripComments: true}, |
| 205 |
} |
| 206 |
} |