forked from projectcalico/calicoctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
195 lines (166 loc) · 6.8 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) 2016-2017 Tigera, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"os"
"github.com/docopt/docopt-go"
"fmt"
"strings"
"github.com/projectcalico/calicoctl/calicoctl/commands/argutils"
"github.com/projectcalico/calicoctl/calicoctl/commands/constants"
log "github.com/sirupsen/logrus"
)
func Get(args []string) {
doc := constants.DatastoreIntro + `Usage:
calicoctl get ( (<KIND> [<NAME>]) |
--filename=<FILENAME>)
[--output=<OUTPUT>] [--config=<CONFIG>] [--namespace=<NS>] [--all-namespaces] [--export]
Examples:
# List all policy in default output format.
calicoctl get policy
# List a specific policy in YAML format
calicoctl get -o yaml policy my-policy-1
Options:
-h --help Show this screen.
-f --filename=<FILENAME> Filename to use to get the resource. If set to
"-" loads from stdin.
-o --output=<OUTPUT FORMAT> Output format. One of: yaml, json, ps, wide,
custom-columns=..., go-template=...,
go-template-file=... [Default: ps]
-c --config=<CONFIG> Path to the file containing connection
configuration in YAML or JSON format.
[default: ` + constants.DefaultConfigPath + `]
-n --namespace=<NS> Namespace of the resource.
Only applicable to NetworkPolicy and WorkloadEndpoint.
Uses the default namespace if not specified.
-a --all-namespaces If present, list the requested object(s) across all namespaces.
--export If present, returns the requested object(s) stripped of
cluster-specific information. This flag will be ignored
if <NAME> is not specified.
Description:
The get command is used to display a set of resources by filename or stdin,
or by type and identifiers. JSON and YAML formats are accepted for file and
stdin format.
Valid resource types are:
* bgpConfiguration
* bgpPeer
* felixConfiguration
* globalNetworkPolicy
* hostEndpoint
* ipPool
* networkPolicy
* node
* profile
* workloadEndpoint
The resource type is case insensitive and may be pluralized.
Attempting to get resources that do not exist will simply return no results.
When getting resources by type, only a single type may be specified at a
time. The name and other identifiers (hostname, scope) are optional, and are
wildcarded when omitted. Thus if you specify no identifiers at all (other
than type), then all configured resources of the requested type will be
returned.
By default the results are output in a ps-style table output. There are
alternative ways to display the data using the --output option:
ps Display the results in ps-style output.
wide As per the ps option, but includes more headings.
custom-columns As per the ps option, but only display the columns
that are requested in the comma-separated list.
golang-template Display the results using the specified golang
template. This can be used to filter results, for
example to return a specific value.
golang-template-file Display the results using the golang template that is
contained in the specified file.
yaml Display the results in YAML output format.
json Display the results in JSON output format.
Note that the data output using YAML or JSON format is always valid to use as
input to all of the resource management commands (create, apply, replace,
delete, get).
Please refer to the docs at http://docs.projectcalico.org for more details on
the output formats, including example outputs, resource structure (required
for the golang template definitions) and the valid column names (required for
the custom-columns option).
`
parsedArgs, err := docopt.Parse(doc, args, true, "", false, false)
if err != nil {
fmt.Printf("Invalid option: 'calicoctl %s'. Use flag '--help' to read about a specific subcommand.\n", strings.Join(args, " "))
os.Exit(1)
}
if len(parsedArgs) == 0 {
return
}
printNamespace := false
if argutils.ArgBoolOrFalse(parsedArgs, "--all-namespaces") || argutils.ArgStringOrBlank(parsedArgs, "--namespace") != "" {
printNamespace = true
}
var rp resourcePrinter
output := parsedArgs["--output"].(string)
switch output {
case "yaml", "yml":
rp = resourcePrinterYAML{}
case "json":
rp = resourcePrinterJSON{}
case "ps":
rp = resourcePrinterTable{wide: false, printNamespace: printNamespace}
case "wide":
rp = resourcePrinterTable{wide: true, printNamespace: printNamespace}
default:
// Output format may be a key=value pair, so split on "=" to find out. Pull
// out the key and value, and split the value by "," as some options allow
// a multiple-valued value.
outputParms := strings.SplitN(output, "=", 2)
outputKey := outputParms[0]
outputValue := ""
outputValues := []string{}
if len(outputParms) == 2 {
outputValue = outputParms[1]
outputValues = strings.Split(outputValue, ",")
}
switch outputKey {
case "go-template":
if outputValue == "" {
fmt.Printf("need to specify a template\n")
os.Exit(1)
}
rp = resourcePrinterTemplate{template: outputValue}
case "go-template-file":
if outputValue == "" {
fmt.Printf("need to specify a template file\n")
os.Exit(1)
}
rp = resourcePrinterTemplateFile{templateFile: outputValue}
case "custom-columns":
if outputValue == "" {
fmt.Printf("need to specify at least one column\n")
os.Exit(1)
}
rp = resourcePrinterTable{headings: outputValues}
}
}
if rp == nil {
fmt.Printf("unrecognized output format '%s'\n", output)
os.Exit(1)
}
results := executeConfigCommand(parsedArgs, actionGetOrList)
log.Infof("results: %+v", results)
if results.fileInvalid {
fmt.Printf("Failed to execute command: %v\n", results.err)
os.Exit(1)
} else if results.err != nil {
fmt.Printf("Failed to get resources: %v\n", results.err)
os.Exit(1)
}
err = rp.print(results.client, results.resources)
if err != nil {
fmt.Println(err)
}
}