Kubernetes.pm

filter: Statementstmt 92.1% 70 / 76 Branchbran 83.3% 25 / 30 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 85.3% 105 / 123 CC 20 SCAR 17.5
CC
20
Cov
88%
list_nodes
7.0
list_service_accounts
7.0
get_namespace
6.1
CRAP 5.8
« Kubernetes/Role/ResourceCreator.pm ↑ Summary Kubernetes/Role/APIAccess.pm »
1 package Net::Kubernetes;
2 # ABSTRACT: An object oriented interface to the REST API's provided by kubernetes
3 4 $Net::Kubernetes::VERSION = '0.19';
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 require Net::Kubernetes::Namespace;
6 4 require LWP::UserAgent;
7 4 require HTTP::Request;
8 4 require URI;;
9 4 require Throwable::Error;
10 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 require Net::Kubernetes::Exception;
12  
13  
14 4 with 'Net::Kubernetes::Role::APIAccess';
15 4 with 'Net::Kubernetes::Role::ResourceLister';
16 4 with 'Net::Kubernetes::Role::ResourceFetcher';
17  
18  
19 4 has 'default_namespace' => (
20 is => 'rw',
21 isa => 'Net::Kubernetes::Namespace',
22 required => 0,
23 lazy => 1,
24 handles => [qw(get_pod get_rc get_replication_controller get_secret get_service create create_from_file build_secret)],
25 builder => '_get_default_namespace',
26 );
27  
28 sub get_namespace {
29 4 ▶ my($self, $namespace) = @_;
Subroutine get_namespace: called CC=6 SCAR=18.1
30 4 ▶ if (! defined $namespace || ! length $namespace) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $namespace and length $namespace) 1 3 4
Condition66%2/3
defined $namespace and length $namespace
!ll&&!rl&&r
103
MC/DC50%1/2
defined $namespace and length $namespace
defined $namespace length $namespace
Truth table66%2/3
A and B
A= defined $namespaceB= length $namespace
ABresult
0X0
100
111
31 1 Throwable::Error->throw(message=>'$namespace cannot be null');
32 }
33 3 my $res = $self->ua->request($self->create_request(GET => $self->path.'/namespaces/'.$namespace));
34 3 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 2 1 3
35 2 my $ns = $self->json->decode($res->content);
36 2 my(%create_args) = (url => $self->url, base_path=>$ns->{metadata}{selfLink}, api_version=>$self->api_version, namespace=> $namespace, _namespace_data=>$ns);
37 2 ▶ $create_args{username} = $self->username if(defined $self->username);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->username 0 2 2
38 2 ▶ $create_args{password} = $self->password if(defined $self->password);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->password 0 2 2
39 2 return Net::Kubernetes::Namespace->new(%create_args);
40 }else{
41 1 Net::Kubernetes::Exception->throw(code=>$res->code, message=>"Error getting namespace $namespace:\n".$res->message);
42 }
43 }
44  
45  
46 sub list_nodes {
47 5 ▶ my $self = shift;
Subroutine list_nodes: called CC=7 SCAR=19.5
48 5 my(%options);
49 5 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 5 5
50 0 %options = %{ $_[0] };
51 }else{
52 5 %options = @_;
53 }
54  
55 5 my $uri = URI->new($self->path.'/nodes');
56 5 my(%form) = ();
57 5 ▶ $form{labelSelector}=$self->_build_selector_from_hash($options{labels}) if (exists $options{labels});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"labels"} 1 4 5
58 5 ▶ $form{fieldSelector}=$self->_build_selector_from_hash($options{fields}) if (exists $options{fields});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"fields"} 1 4 5
59 5 $uri->query_form(%form);
60  
61 5 my $res = $self->ua->request($self->create_request(GET => $uri));
62 5 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 4 1 5
63 4 my $node_list = $self->json->decode($res->content);
64 4 my(@nodes)=();
65 4 foreach my $node (@{ $node_list->{items}}){
66 1 $node->{apiVersion} = $node_list->{apiVersion};
67 1 push @nodes, $self->create_resource_object($node, 'Node');
68 }
69 4 ▶ return wantarray ? @nodes : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
70 }else{
71 1 Net::Kubernetes::Exception->throw(code=>$res->code, message=>$res->message);
72 }
73 }
74  
75 sub get_node {
76 2 ▶ my($self, $name) = @_;
Subroutine get_node: called CC=3 SCAR=12.5
77 2 ▶ Net::Kubernetes::Exception->throw(message=>"Missing required parameter 'name'") if(! defined $name || ! length $name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 2 2
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
002
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
78 2 return $self->get_resource_by_name($name, 'nodes');
79 }
80  
81  
82 sub list_service_accounts {
83 5 ▶ my $self = shift;
Subroutine list_service_accounts: called CC=7 SCAR=19.5
84 5 my(%options);
85 5 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 5 5
86 0 %options = %{ $_[0] };
87 }else{
88 5 %options = @_;
89 }
90  
91 5 my $uri = URI->new($self->path.'/serviceaccounts');
92 5 my(%form) = ();
93 5 ▶ $form{labelSelector}=$self->_build_selector_from_hash($options{labels}) if (exists $options{labels});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"labels"} 1 4 5
94 5 ▶ $form{fieldSelector}=$self->_build_selector_from_hash($options{fields}) if (exists $options{fields});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"fields"} 1 4 5
95 5 $uri->query_form(%form);
96  
97 5 my $res = $self->ua->request($self->create_request(GET => $uri));
98 5 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 4 1 5
99 4 my $sa_list = $self->json->decode($res->content);
100 4 my(@saccs)=();
101 4 foreach my $sacc (@{ $sa_list->{items}}){
102 1 $sacc->{apiVersion} = $sa_list->{apiVersion};
103 1 push @saccs, $self->create_resource_object($sacc, 'ServiceAccount');
104 }
105 4 ▶ return wantarray ? @saccs : \@saccs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
106 }else{
107 1 Net::Kubernetes::Exception->throw(code=>$res->code, message=>$res->message);
108 }
109 }
110  
111  
112 sub _get_default_namespace {
113 0 ▶ my($self) = @_;
Subroutine _get_default_namespace: not called CC=1 SCAR=6.9
114 0 return $self->get_namespace('default');
115 }
116  
117 # SEEALSO: Net::Kubernetes::Namespace, Net::Kubernetes::Resource
118  
119 4 return 42;
120  
121 __END__
122  
123 =pod
124  
125 =encoding UTF-8
126  
127 =head1 NAME
128  
129 Net::Kubernetes - An object oriented interface to the REST API's provided by kubernetes
130  
131 =head1 VERSION
132  
133 version 0.19
134  
135 =head1 SYNOPSIS
136  
137 my $kube = Net::Kubernetes->new(url=>'http://127.0.0.1:8080', username=>'dave', password=>'davespassword');
138 my $pod_list = $kube->list_pods();
139  
140 my $nginx_pod = $kube->create_from_file('kubernetes/examples/pod.yaml');
141  
142 my $ns = $kube->get_namespace('default');
143  
144 my $services = $ns->list_services;
145  
146 my $pod = $ns->get_pod('my-pod');
147  
148 $pod->delete;
149  
150 my $other_pod = $ns->create_from_file('./my-pod.yaml');
151  
152 =head1 METHODS
153  
154 =head2 new - Create a new $kube object
155  
156 All parameters are optional and have some basic default values (where appropriate).
157  
158 =over 1
159  
160 =item url ['http://localhost:8080']
161  
162 The base url for the kubernetes. This should include the protocal (http or https) but not "/api/v1beta3" (see base_path).
163  
164 =item base_path ['/api/v1beta3']
165  
166 The entry point for api calls, this may be used to set the api version with which to interact.
167  
168 =item username
169  
170 Username to use with basic authentication. If either username or password are not provided, basic authentication will not
171 be used.
172  
173 =item password
174  
175 Password to use with basic authentication. If either username or password are not provided, basic authentication will not
176 be used.
177  
178 =item token
179  
180 An authentication token to be used to access the apiserver. This may be provided as a plain string, a path to a file
181 from which to read the token (like /var/run/secrets/kubernetes.io/serviceaccount/token from within a pod), or a reference
182 to a file handle (from which to read the token).
183  
184 =back
185  
186 =head2 get_namespace("myNamespace");
187  
188 This method returns a "Namespace" object on which many methods can be called implicitly
189 limited to the specified namespace.
190  
191 =head2 get_pod('my-pod-name')
192  
193 Delegates automatically to L<Net::Kubernetes::Namespace> via $self->get_namespace('default')
194  
195 =head2 get_repllcation_controller('my-rc-name') (aliased as $ns->get_rc('my-rc-name'))
196  
197 Delegates automatically to L<Net::Kubernetes::Namespace> via $self->get_namespace('default')
198  
199 =head2 get_service('my-servce-name')
200  
201 Delegates automatically to L<Net::Kubernetes::Namespace> via $self->get_namespace('default')
202  
203 =head2 get_secret('my-secret-name')
204  
205 Delegates automatically to L<Net::Kubernetes::Namespace> via $self->get_namespace('default')
206  
207 =head2 list_nodes([label=>{label=>value}], [fields=>{field=>value}])
208  
209 returns a list of L<Net::Kubernetes::Resource::Node>s
210  
211 =head2 list_service_accounts([label=>{label=>value}], [fields=>{field=>value}])
212  
213 returns a list of L<Net::Kubernetes::Resource::Service>s
214  
215 =begin html
216  
217 <h2>Build Status</h2>
218  
219 <img src="https://travis-ci.org/perljedi/net-kubernetes.svg?branch=release-0.19" />
220  
221 =end html
222  
223 =head1 AUTHOR
224  
225 Dave Mueller <dave@perljedi.com>
226  
227 =head1 COPYRIGHT AND LICENSE
228  
229 This software is Copyright (c) 2015 by Dave Mueller.
230  
231 This is free software, licensed under:
232  
233 The MIT (X11) License
234  
235 =head1 SEE ALSO
236  
237 Please see those modules/websites for more information related to this module.
238  
239 =over 4
240  
241 =item *
242  
243 L<Net::Kubernetes::Namespace|Net::Kubernetes::Namespace>
244  
245 =item *
246  
247 L<Net::Kubernetes::Resource|Net::Kubernetes::Resource>
248  
249 =back
250  
251 =head1 CONSUMES
252  
253 =over 4
254  
255 =item * L<Net::Kubernetes::Role::APIAccess>
256  
257 =item * L<Net::Kubernetes::Role::ResourceFactory>
258  
259 =item * L<Net::Kubernetes::Role::ResourceFetcher>
260  
261 =item * L<Net::Kubernetes::Role::ResourceLister>
262  
263 =back
264  
265 =head1 CONTRIBUTORS
266  
267 =for stopwords Christopher Pruden Dave Mueller
268  
269 =over 4
270  
271 =item *
272  
273 Christopher Pruden <cdpruden@liquidweb.com>
274  
275 =item *
276  
277 Dave <dave@perljedi.com>
278  
279 =item *
280  
281 Dave Mueller <dmueller@liquidweb.com>
282  
283 =back
284  
285 =cut
« Kubernetes/Role/ResourceCreator.pm ↑ Summary Kubernetes/Role/APIAccess.pm »