| 1 | package Net::Kubernetes::Role::SecretBuilder; | ||||||||||||||
| 2 | # ABSTRACT: Role to allow creation of resources from either objects or files. | ||||||||||||||
| 3 | 4 | $Net::Kubernetes::Role::SecretBuilder::VERSION = '1.03'; | |||||||||||||
| 4 | 4 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 4 | ▶ | use MooseX::Aliases; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 4 | require Net::Kubernetes::Resource::Secret; | |||||||||||||
| 7 | 4 | require Net::Kubernetes::Exception; | |||||||||||||
| 8 | 4 | ▶ | use File::Slurp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 4 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 4 | ▶ | use syntax 'try'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | 0 | ▶ | sub build_secret { | ||||||||||||
|
Subroutine
build_secret: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 14 | 0 | my($self, $name, $data) = @_; | |||||||||||||
| 15 | 0 | return $self->create($self->_assemble_secret($name, $data)); | |||||||||||||
| 16 | } | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 0 | ▶ | sub _assemble_secret { | ||||||||||||
|
Subroutine
_assemble_secret: not called CC=6 SCAR=37.4
| |||||||||||||||
| 19 | 0 | my($self, $name, $data) = @_; | |||||||||||||
| 20 | 0 | my($secret) = { | |||||||||||||
| 21 | kind => 'Secret', | ||||||||||||||
| 22 | apiVersion => $self->api_version, | ||||||||||||||
| 23 | metadata=>{ | ||||||||||||||
| 24 | name => $name, | ||||||||||||||
| 25 | }, | ||||||||||||||
| 26 | type => 'Opaque', | ||||||||||||||
| 27 | id => $name, | ||||||||||||||
| 28 | data => {}, | ||||||||||||||
| 29 | namespace=>'default', | ||||||||||||||
| 30 | }; | ||||||||||||||
| 31 | 0 | foreach my $key (keys %$data){ | |||||||||||||
| 32 | 0 | ▶ | if (ref $data->{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 0 | my $value; | |||||||||||||
| 34 | # Handle serializing data | ||||||||||||||
| 35 | 0 | ▶ | if (uc($data->{$key}{type}) eq 'JSON') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 36 | 0 | $value = $self->json->encode($data->{$key}{value}); | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | elsif (uc($data->{$key}{type}) eq 'YAML') { | ||||||||||||||
| 39 | 0 | $value = YAML::XS::Dump($data->{$key}{value}); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | else{ | ||||||||||||||
| 42 | 0 | $value = $data->{$key}{value}; | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | 0 | $secret->{data}{$key} = encode_base64($value, ""); | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | else { | ||||||||||||||
| 47 | # if passed a string, it should be a filename | ||||||||||||||
| 48 | 0 | ▶ | if (! -f $data->{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 0 | Net::Kubernetes::Exception->throw(message => "Failed to build secret: $data->{$key} - Not Such file."); | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | 0 | $secret->{data}{$key} = encode_base64(read_file($data->{$key}), ""); | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | } | ||||||||||||||
| 55 | 0 | return $secret; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 4 | return 42; | |||||||||||||
| 59 | |||||||||||||||
| 60 | __END__ | ||||||||||||||