| 1 | package Net::Kubernetes; | ||||||||||||||||||||||||||||
| 2 | 2 | $Net::Kubernetes::VERSION = '0.05'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | require Net::Kubernetes::Namespace; | |||||||||||||||||||||||||||
| 6 | 2 | require LWP::UserAgent; | |||||||||||||||||||||||||||
| 7 | 2 | require HTTP::Request; | |||||||||||||||||||||||||||
| 8 | 2 | require JSON; | |||||||||||||||||||||||||||
| 9 | 2 | require URI;; | |||||||||||||||||||||||||||
| 10 | 2 | require Throwable::Error; | |||||||||||||||||||||||||||
| 11 | 2 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | require Net::Kubernetes::Exception; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # ABSTRACT: Perl interface to kubernetes | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Net::Kubernetes | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 VERSION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | version 0.05 | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | my $kube = Net::Kubernets->new(url=>'http://127.0.0.1:8080', username=>'dave', password=>'davespassword'); | ||||||||||||||||||||||||||||
| 27 | my $pod_list = $kube->list_pods(); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | my $nginx_pod = $kube->create_from_file('kubernetes/examples/pod.yaml'); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $ns = $kube->get_namespace('default'); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | my $services = $ns->list_services; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | my $pod = $ns->get_pod('my-pod'); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $pod->delete; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | my $other_pod = $ns->create_from_file('./my-pod.yaml'); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 Description | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | This package allows programatic access to the L<Kubernetes|http://kubernetes.io> rest api. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Please note this package is still a BETA release (as is kubernetes itself), and the methods | ||||||||||||||||||||||||||||
| 46 | and API are still subject to change. Use at your own risk. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | with 'Net::Kubernetes::Role::APIAccess'; | |||||||||||||||||||||||||||
| 52 | 2 | with 'Net::Kubernetes::Role::ResourceLister'; | |||||||||||||||||||||||||||
| 53 | 2 | with 'Net::Kubernetes::Role::ResourceCreator'; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 Methods | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | By convention, methods will throw exceptions if kubernetes api server returns a non-successful status code. | ||||||||||||||||||||||||||||
| 58 | Unless otherwise noted, assume this behavoir through out. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =over 1 | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =item new - Create a new $kube object | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | All parameters are optional and have some basic default values (where appropriate). | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =over 1 | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =item url ['http://localhost:8080'] | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | The base url for the kubernetes. This should include the protocal (http or https) but not "/api/v1beta3" (see base_path). | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =item base_path ['/api/v1beta3'] | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | The entry point for api calls, this may be used to set the api version with which to interact. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =item username | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Username to use with basic authentication. If either username or password are not provided, basic authentication will not | ||||||||||||||||||||||||||||
| 79 | be used. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =item password | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Password to use with basic authentication. If either username or password are not provided, basic authentication will not | ||||||||||||||||||||||||||||
| 84 | be used. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =item token | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | An authentication token to be used to access the apiserver. This may be provided as a plain string, a path to a file | ||||||||||||||||||||||||||||
| 89 | from which to read the token (like /var/run/secrets/kubernetes.io/serviceaccount/token from within a pod), or a reference | ||||||||||||||||||||||||||||
| 90 | to a file handle (from which to read the token). | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =back | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =back | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head2 List resources | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | These methods retrieve lists (optionally limited by field or label selector) of the various resources types | ||||||||||||||||||||||||||||
| 99 | kubernetes makes available via the API servers rest interface. These methods may also be called on a | ||||||||||||||||||||||||||||
| 100 | "Namespace" object, which will implicitly limit the result set by namespace. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | All of these methods will return an array (or and array ref denpending on context) of the approriate | ||||||||||||||||||||||||||||
| 103 | resource type (Net::Kubernetes::Resource::Pod for example). | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =over 1 | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =item $kube->list_pods([label=>{label=>value}], [fields=>{field=>value}]) | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =item $kube->list_rc([label=>{label=>value}], [fields=>{field=>value}]) | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =item $kube->list_replication_controllers([label=>{label=>value}], [fields=>{field=>value}]) (alias to list_rc) | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =item $kube->list_secrets([label=>{label=>value}], [fields=>{field=>value}]) | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =item $kube->list_services([label=>{label=>value}], [fields=>{field=>value}]) | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =back | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 Create Methods | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | These methods may be called either globally or on a namespace object. The return value is is an object of the | ||||||||||||||||||||||||||||
| 122 | approriate type (determined by the "Kind" field) | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =over 1 | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =item my $resource = $kube->create({OBJECT}) | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item my $resource = $kube->create_from_file(PATH_TO_FILE) (accepts either JSON or YAML files) | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Create from file is really just a short cut around something like: | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | my $object = YAML::LoadFile(PATH_TO_FILE); | ||||||||||||||||||||||||||||
| 133 | $kube->create($object); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =back | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head2 Global scoped methods | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | These methods are only available at the top level (i.e. not available via a namespace object) | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =over 1 | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =item $kube->get_namespace("myNamespace"); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | This method returns a "Namespace" object on which many methods can be called implicitly | ||||||||||||||||||||||||||||
| 146 | limited to the specified namespace. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =back | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 Namespace methods | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | These methods are accessible only from a name-space object. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =over 1 | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =item $ns->get_pod('my-pod-name') | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =item $ns->get_repllcation_controller('my-rc-name') (aliased as $ns->get_rc('my-rc-name')) | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item $ns->get_service('my-servce-name') | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =item $ns->get_secret('my-secret-name') | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =back | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 Resource Methods | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | These methods can be called directly on a resource object. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =over 1 | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item $resource->delete | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =item $resource->refresh (update object from api server) | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =item $resource->update (send local changes to api server) | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | This method is only available for resources which have a status (currently everything | ||||||||||||||||||||||||||||
| 179 | other than secretes). | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | sub get_namespace { | ||||||||||||||||||||||||||||
| 184 | 4 | ▶ | my($self, $namespace) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_namespace: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 185 | 4 | ▶ | if (! defined $namespace || ! length $namespace) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $namespace and length $namespace
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
| |||||||||||||||||||||||||||||
| 186 | 1 | Throwable::Error->throw(message=>'$namespace cannot be null'); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 3 | print "Getting from ".$self->path.'/namespaces/'.$namespace."\n"; | |||||||||||||||||||||||||||
| 189 | 3 | my $res = $self->ua->request($self->create_request(GET => $self->path.'/namespaces/'.$namespace)); | |||||||||||||||||||||||||||
| 190 | 3 | ▶ | if ($res->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 2 | my $ns = $self->json->decode($res->content); | |||||||||||||||||||||||||||
| 192 | 2 | print Dumper($ns)."\n"; | |||||||||||||||||||||||||||
| 193 | 2 | my(%create_args) = (url => $self->url, base_path=>$ns->{metadata}{selfLink}, namespace=> $namespace, _namespace_data=>$ns); | |||||||||||||||||||||||||||
| 194 | 2 | ▶ | $create_args{username} = $self->username if(defined $self->username); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 2 | ▶ | $create_args{password} = $self->password if(defined $self->password); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 2 | return Net::Kubernetes::Namespace->new(%create_args); | |||||||||||||||||||||||||||
| 197 | }else{ | ||||||||||||||||||||||||||||
| 198 | 1 | Net::Kubernetes::Exception->throw(code=>$res->code, message=>"Error getting namespace $namespace:\n".$res->message); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 2 | return 42; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =back | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Dave Mueller <dave@perljedi.com> | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | This software is copyright (c) 2015 by Dave Mueller. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | This is free software; you can redistribute it and/or modify it under the | ||||||||||||||||||||||||||||
| 215 | same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||