| 1 | package Kubernetes::Comb::Role::Static; | ||||||||||||||
| 2 | # ABSTRACT: Comb manifests loaded from .pk8s and YAML files | ||||||||||||||
| 3 | 2 | our $VERSION = '0.001'; | |||||||||||||
| 4 | |||||||||||||||
| 5 | 2 | ▶ | use Moo::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | ▶ | use Carp qw( croak ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use Path::Tiny qw( path ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 2 | requires qw( io_k8s manifest_files ); | |||||||||||||
| 12 | |||||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | has _static_manifests => ( | |||||||||||||
| 15 | is => 'lazy', | ||||||||||||||
| 16 | init_arg => undef | ||||||||||||||
| 17 | ); | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 24 | ▶ | sub _build__static_manifests { | ||||||||||||
|
Subroutine
_build__static_manifests: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 24 | my ( $self ) = @_; | |||||||||||||
| 21 | 24 | return [ map { $self->_load_manifest_file($_) } $self->manifest_files ]; | |||||||||||||
| 22 | } | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 29 | ▶ | sub manifests { @{ shift->_static_manifests } } | ||||||||||||
|
Subroutine
manifests: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | sub manifest_dir { return } | ||||||||||||
|
Subroutine
manifest_dir: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 28 | |||||||||||||||
| 29 | |||||||||||||||
| 30 | 36 | ▶ | sub _load_manifest_file { | ||||||||||||
|
Subroutine
_load_manifest_file: called CC=7 SCAR=19.5
| |||||||||||||||
| 31 | 36 | my ( $self, $file ) = @_; | |||||||||||||
| 32 | 36 | my $path = path($file)->absolute( $self->manifest_dir ); | |||||||||||||
| 33 | 36 | ▶ | my $format = $path =~ /\.pk8s\z/ ? 'pk8s' : $path =~ /\.ya?ml\z/ ? 'yaml' : undef; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 34 | 36 | ▶ | croak ref($self).': manifest file '.$path.' is neither .pk8s nor .yaml/.yml' unless $format; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 34 | ▶ | croak ref($self).': manifest file '.$path.' does not exist' unless $path->is_file; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 30 | my $io = $self->io_k8s; | |||||||||||||
| 37 | # load_yaml gets the text: given a path it reads bytes, and takes a path | ||||||||||||||
| 38 | # that is not a file for YAML. | ||||||||||||||
| 39 | 30 | ▶ | my $objects = eval { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 30 | ▶ | $format eq 'pk8s' ? $io->load( $path->stringify ) : $io->load_yaml( $path->slurp_utf8 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | } or croak ref($self).': cannot load manifest file '.$path.': '.( $@ =~ s/\s+\z//r ); | ||||||||||||||
| 42 | 24 | return @$objects; | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | |||||||||||||||
| 46 | 2 | 1; | |||||||||||||
| 47 | |||||||||||||||
| 48 | __END__ | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =pod | ||||||||||||||
| 51 | |||||||||||||||
| 52 | =encoding UTF-8 | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head1 NAME | ||||||||||||||
| 55 | |||||||||||||||
| 56 | Kubernetes::Comb::Role::Static - Comb manifests loaded from .pk8s and YAML files | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =head1 VERSION | ||||||||||||||
| 59 | |||||||||||||||
| 60 | version 0.001 | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =head1 SYNOPSIS | ||||||||||||||
| 63 | |||||||||||||||
| 64 | # A stub that is just a file: it inherits the contract of the Mailer and | ||||||||||||||
| 65 | # swaps the implementation. | ||||||||||||||
| 66 | package MyApp::Comb::Mailer::Stub; | ||||||||||||||
| 67 | use Moo; | ||||||||||||||
| 68 | extends 'MyApp::Comb::Mailer'; | ||||||||||||||
| 69 | with 'Kubernetes::Comb::Role::Static'; | ||||||||||||||
| 70 | |||||||||||||||
| 71 | use File::ShareDir qw( dist_dir ); | ||||||||||||||
| 72 | |||||||||||||||
| 73 | sub manifest_dir { dist_dir('MyApp-Combs') } | ||||||||||||||
| 74 | sub manifest_files { 'mailer-stub.pk8s' } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | # mailer-stub.pk8s, in the share dir of MyApp-Combs | ||||||||||||||
| 77 | Deployment { | ||||||||||||||
| 78 | name => 'mailer', | ||||||||||||||
| 79 | spec => { ... } | ||||||||||||||
| 80 | }; | ||||||||||||||
| 81 | Service { name => 'mailer', spec => { ... } }; | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||||||
| 84 | |||||||||||||||
| 85 | Makes L<Kubernetes::Comb/manifests> of the consuming class the resources | ||||||||||||||
| 86 | declared in files: C<.pk8s> files (the Perl DSL of L<IO::K8s/load>) and | ||||||||||||||
| 87 | C<.yaml>/C<.yml> files, multi-document YAML included (L<IO::K8s/load_yaml>). | ||||||||||||||
| 88 | The class names the files with L</manifest_files>, the directory relative | ||||||||||||||
| 89 | names are resolved against with L</manifest_dir>. | ||||||||||||||
| 90 | |||||||||||||||
| 91 | The role goes into any L<Kubernetes::Comb> subclass, the stub of an existing | ||||||||||||||
| 92 | Comb class included: its C<manifests> replaces the inherited one. A class that | ||||||||||||||
| 93 | is only files extends L<Kubernetes::Comb::Static> instead. | ||||||||||||||
| 94 | |||||||||||||||
| 95 | The files are parsed with the Comb's L<Kubernetes::Comb/io_k8s>, so its CRD | ||||||||||||||
| 96 | providers (or C<< unknown_kinds => 'unstructured' >>) decide which custom | ||||||||||||||
| 97 | resource Kinds they may contain. They are read once per instance, on the | ||||||||||||||
| 98 | first call of C<manifests>; a failed read is tried again on the next call. | ||||||||||||||
| 99 | They are static: namespace and Comb labels come from | ||||||||||||||
| 100 | L<Kubernetes::Comb/deploy>, anything that depends on L<Kubernetes::Comb/config> | ||||||||||||||
| 101 | belongs into a C<manifests> of Perl code (an C<around> works). | ||||||||||||||
| 102 | |||||||||||||||
| 103 | A C<.pk8s> file is Perl code, run in-process: name only files that ship with | ||||||||||||||
| 104 | your code, never a path from a custom resource or its C<config>. YAML files are | ||||||||||||||
| 105 | read as UTF-8; a C<.pk8s> file is Perl source, so one with non-ASCII text | ||||||||||||||
| 106 | starts with C<use utf8;>. | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head2 manifests | ||||||||||||||
| 109 | |||||||||||||||
| 110 | The objects of all L</manifest_files>, in the order of the files and, within | ||||||||||||||
| 111 | a file, in the order they are declared. Dies naming the file when one does | ||||||||||||||
| 112 | not exist, has neither extension or does not parse. | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head2 manifest_files | ||||||||||||||
| 115 | |||||||||||||||
| 116 | Required from the consuming class: the list of the files, each a C<.pk8s>, | ||||||||||||||
| 117 | C<.yaml> or C<.yml> file, absolute or relative to L</manifest_dir>. | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =head2 manifest_dir | ||||||||||||||
| 120 | |||||||||||||||
| 121 | The directory relative L</manifest_files> are resolved against, for example | ||||||||||||||
| 122 | the share dir of the distribution with the Comb classes. Default: none, they | ||||||||||||||
| 123 | are taken from the current directory. | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =head1 SEE ALSO | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =over | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =item * L<Kubernetes::Comb::Static> -- a Comb class that is only files | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =item * L<IO::K8s/load>, L<IO::K8s/load_yaml> | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =back | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =head1 SUPPORT | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =head2 Issues | ||||||||||||||
| 138 | |||||||||||||||
| 139 | Please report bugs and feature requests on GitHub at | ||||||||||||||
| 140 | L<https://github.com/Getty/p5-kubernetes-comb/issues>. | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =head2 IRC | ||||||||||||||
| 143 | |||||||||||||||
| 144 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =head1 CONTRIBUTING | ||||||||||||||
| 147 | |||||||||||||||
| 148 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =head1 AUTHOR | ||||||||||||||
| 151 | |||||||||||||||
| 152 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||
| 153 | |||||||||||||||
| 154 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 155 | |||||||||||||||
| 156 | This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>. | ||||||||||||||
| 157 | |||||||||||||||
| 158 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 159 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 160 | |||||||||||||||
| 161 | =cut | ||||||||||||||