-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls-kube
213 lines (166 loc) · 4.23 KB
/
ls-kube
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env perl
use strict;
use Getopt::Long qw(:config gnu_getopt);
use constant DEFAULT_SERVER_PORTS => (8001, 9001);
use constant EXCLUDED_NODE => {
type => [
'nodes',
],
};
use constant EXCLUDED_NAMESPACE => {
type => [
'namespaces',
],
};
use constant EXCLUDED_SYSTEM => {
type => [
'events',
'events.events.k8s.io',
'componentstatuses',
],
name => [
],
instance => [
qr(^default:(services|endpoints)/kubernetes\z),
],
};
use constant EXCLUDED_DEFAULT => {
type => [
'apiservices.apiregistration.k8s.io',
'customresourcedefinitions.apiextensions.k8s.io',
],
name => [
'serviceaccounts/default',
'storageclasses.storage.k8s.io/hostpath',
qr(/kube-proxy\b),
qr(/kube-dns\b),
qr(^secrets/default-token-),
],
instance => [
],
};
sub usage {
print <<END;
List Kubernetes cluster resources.
Usage:
$0 [options] <api server url>
Options:
--all, -a
List all system resources
--namespaces, -n
List namespaces
--nodes, -d
List nodes
--uid, -u
Output resource UID
--json, -j
Output as JSON data
--help, -h
Display this help
END
exit 2;
}
sub error {
print STDERR "Error: ", @_, "\n";
exit 1;
}
sub quote { local $_ = shift; s/'/'\\''/g; "'$_'" }
my $server_url;
sub fetch {
my @args = (qw(curl -sS -kL -g --fail), quote(join('/', $server_url, @{shift()})));
push @args, '|', qw(jq -r), quote(shift) if @_;
my @output = map { chomp; $_ } qx(@args);
error("Command failed") if $?;
return @output if wantarray;
return $output[0];
}
my $ls_all;
my $ls_ns;
my $ls_node;
my $ls_uid;
my $out_json;
GetOptions(
"help|h" => \&usage,
"all|a" => \$ls_all,
"namespaces|n" => \$ls_ns,
"nodes|d" => \$ls_node,
"uid|u" => \$ls_uid,
"json|j" => \$out_json,
)
or error("Invalid argument");
require JSON if $out_json;
if (@ARGV) {
$server_url = shift;
}
else {
foreach (DEFAULT_SERVER_PORTS) {
system('nc', '-z', '-w', 1, 'localhost', $_) and next;
$server_url = "localhost:$_";
last;
}
error("Default local API server unreachable") unless defined($server_url);
}
error("Unexpected argument: @ARGV") if @ARGV;
error("Failed reaching API server") unless fetch(['version'], '.major') eq '1';
my @filters;
push @filters, EXCLUDED_SYSTEM, EXCLUDED_DEFAULT unless $ls_all;
push @filters, EXCLUDED_NAMESPACE unless $ls_ns;
push @filters, EXCLUDED_NODE unless $ls_node;
sub excluded {
my ($type, $name) = @_;
foreach (@filters) {
foreach (@{$_->{$type}}) {
if (ref eq 'Regexp') {
return 1 if $name =~ $_;
}
else {
return 1 if $name eq $_;
}
}
}
return 0;
}
my @entries;
my %index;
foreach my $group ('', fetch(['apis'], '.groups[].name')) {
my $api_path = length($group) ? ['apis', $group] : ['api'];
my $versions_expr = length($group) ? '.versions[].version' : '.versions[]';
my $preferred_version_expr = length($group) ? '.preferredVersion.version // empty' : '.preferredVersion // empty';
my @api_versions = fetch($api_path, $versions_expr);
my $preferred_version = fetch($api_path, $preferred_version_expr);
if (length($preferred_version)) {
@api_versions = grep($_ ne $preferred_version, @api_versions);
unshift @api_versions, $preferred_version;
}
foreach my $version (@api_versions) {
my $version_path = [@$api_path, $version];
my @api_resources = fetch($version_path, '.resources[] | select(.verbs[] | select(. == "list")) | .name');
foreach my $resource (@api_resources) {
my $type = length($group) ? $resource . '.' . $group : $resource;
next if excluded(type => $type);
my @resource_instances = fetch([@$version_path, $resource], '.items[].metadata | .namespace + ";" + .name + ";" + .uid');
foreach (@resource_instances) {
my ($namespace, $name, $uid) = split /;/;
next if $index{$uid};
my $ref_name = "$type/$name";
my $ref_instance = "$namespace:$name";
next if excluded(name => $ref_name);
next if excluded(instance => $ref_instance);
$index{$uid} = 1;
my %entry = (type => $type, namespace => $namespace, name => $name);
$entry{uid} = $uid if $ls_uid;
push @entries, \%entry;
}
}
}
}
if ($out_json) {
print JSON::to_json(\@entries), "\n";
}
else {
foreach (@entries) {
print "$_->{namespace}:$_->{type}/$_->{name}";
print "#$_->{uid}" if exists($_->{uid});
print "\n";
}
}