blib/lib/Kubernetes/Comb/Endpoint.pm

filter: Statementstmt 100% 34 / 34 Branchbran 100% 10 / 10 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 10 / 10 Podpod 80.0% 4 / 5 totaltotal 92.9% 66 / 71 CC 9 SCAR 9.9
CC
9
Cov
96%
name_problem
5.1
to_crd
3.0
BUILD
2.0
CRAP 2.7
« blib/lib/Kubernetes/Comb/Role/Static.pm ↑ Summary blib/lib/Kubernetes/Comb/Upstream/Static.pm »
1 package Kubernetes::Comb::Endpoint;
2 # ABSTRACT: Value object for an endpoint a Comb offers
3 17 our $VERSION = '0.001';
4  
5 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Types::Standard qw( Int Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Kubernetes::Comb::CRD::CombEndpoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 17 has name => ( is => 'ro', isa => Str, required => 1 );
13  
14  
15 17 has protocol => ( is => 'ro', isa => Str, default => 'tcp' );
16  
17  
18 17 has port => ( is => 'ro', isa => Int, required => 1 );
19  
20  
21 17 has cluster => ( is => 'ro', isa => Str, predicate => 1 );
22  
23  
24 17 has external => ( is => 'ro', isa => Str, predicate => 1 );
25  
26  
27 198 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
28 198 my ( $self ) = @_;
29 198 my $problem = $self->name_problem( $self->name );
30 198 ▶ croak ref($self).': '.$problem if defined $problem;
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 7 191 198
31 }
32  
33 405 ▶sub name_problem {
Subroutine name_problem: called CC=5 SCAR=16.3
Pod documented
34 405 my ( $self, $name ) = @_;
35 405 ▶ return if defined $name && length $name <= 63 && $name =~ /\A[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and length $name <= 63 and $name =~ /\A[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\z/ 395 10 405
Condition83%5/6
defined $name and length $name <= 63 and $name =~ /\A[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\z/
!ll&&!rl&&r
19395
defined $name and length $name <= 63
!ll&&!rl&&r
01404
MC/DC66%2/3
defined $name and length $name <= 63 and $name =~ /\A[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\z/
defined $name length $name <= 63 $name =~ /\A[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\z/
Truth table75%3/4
A and B and C
A= defined $nameB= length $name <= 63C= $name =~ /\A[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\z/
ABCresult
0XX0
10X0
1100
1111
36 10 ▶ return "the name '".( $name // '' )."' is not a DNS-1123 label (lowercase letters, digits and -, "
Condition50%1/2
$name // ""
l!l
100
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
37 .'alphanumeric at both ends, at most 63 characters), as the name of a Service port must be';
38 }
39  
40  
41 71 ▶sub crd_endpoint_class { 'Kubernetes::Comb::CRD::CombEndpoint' }
Subroutine crd_endpoint_class: called CC=1 SCAR=0.0
Pod documented
42  
43  
44 2 ▶sub from_crd {
Subroutine from_crd: called CC=2 SCAR=6.9
Pod documented
45 2 my ( $class, $entry ) = @_;
46 return $class->new(
47 2 ▶ map { defined $entry->$_ ? ( $_ => $entry->$_ ) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $entry->$_ ? : 7 3 10
48 qw( name protocol port cluster external )
49 );
50 }
51  
52  
53 71 ▶sub to_crd {
Subroutine to_crd: called CC=3 SCAR=11.0
Pod documented
54 71 my ( $self ) = @_;
55 71 ▶ return $self->crd_endpoint_class->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_cluster ? : 69 2 71
$self->has_external ? : 19 52 71
56 name => $self->name,
57 protocol => $self->protocol,
58 port => $self->port,
59 ( $self->has_cluster ? ( cluster => $self->cluster ) : () ),
60 ( $self->has_external ? ( external => $self->external ) : () )
61 );
62 }
63  
64  
65 17 1;
66  
67 __END__
68  
69 =pod
70  
71 =encoding UTF-8
72  
73 =head1 NAME
74  
75 Kubernetes::Comb::Endpoint - Value object for an endpoint a Comb offers
76  
77 =head1 VERSION
78  
79 version 0.001
80  
81 =head1 SYNOPSIS
82  
83 my $ep = Kubernetes::Comb::Endpoint->new(
84 name => 'client',
85 port => 4222,
86 cluster => 'nats.platform.svc:4222',
87 external => 'nats.example.com:4222'
88 );
89 $ep->protocol; # tcp
90  
91 my $status_entry = $ep->to_crd; # CombEndpoint
92 my $again = Kubernetes::Comb::Endpoint->from_crd($status_entry);
93  
94 =head1 DESCRIPTION
95  
96 What a Comb offers under one name: protocol and port as the class declares
97 them, and the addresses it is reached at once resolved -- C<cluster> from
98 inside the cluster, C<external> from outside. With an active upstream both
99 point at the upstream. Immutable; a changed address is a new object.
100  
101 =head2 name
102  
103 Required. The name the Comb class declares the endpoint under, e.g. C<client>.
104 It names the port of the Service the bridge makes, so it must be a DNS-1123
105 label (see L</name_problem>); construction dies on one that is not.
106  
107 =head2 protocol
108  
109 Defaults to C<tcp>.
110  
111 =head2 port
112  
113 Required. The port the endpoint is offered on.
114  
115 =head2 cluster
116  
117 Optional. Address inside the cluster as C<host:port>; C<has_cluster> tells
118 whether there is one.
119  
120 =head2 external
121  
122 Optional. Address from outside the cluster as C<host:port>; C<has_external>
123 tells whether there is one.
124  
125 =head2 name_problem
126  
127 my $problem = Kubernetes::Comb::Endpoint->name_problem($name);
128  
129 Why C<$name> cannot be the name of an endpoint, or nothing when it can: it
130 must be a DNS-1123 label -- lowercase letters, digits and C<->,
131 alphanumeric at both ends, at most 63 characters -- because the bridge uses
132 it as the name of a Service port. Works as class and as instance method.
133  
134 =head2 crd_endpoint_class
135  
136 The status-entry class L</to_crd> builds, L<Kubernetes::Comb::CRD::CombEndpoint>.
137 Override it in a subclass to build another.
138  
139 =head2 from_crd
140  
141 my $ep = Kubernetes::Comb::Endpoint->from_crd($combendpoint);
142  
143 Builds an endpoint from a L<Kubernetes::Comb::CRD::CombEndpoint>, as found in
144 C<status.endpoints> of a Comb custom resource. A missing C<protocol> becomes
145 C<tcp>. Dies when C<name> or C<port> is missing.
146  
147 =head2 to_crd
148  
149 $status->endpoints([ map { $_->to_crd } @endpoints ]);
150  
151 Returns the endpoint as a L<Kubernetes::Comb::CRD::CombEndpoint> for
152 C<status.endpoints>.
153  
154 =head1 SEE ALSO
155  
156 =over
157  
158 =item * L<Kubernetes::Comb::CRD::CombEndpoint>
159  
160 =back
161  
162 =head1 SUPPORT
163  
164 =head2 Issues
165  
166 Please report bugs and feature requests on GitHub at
167 L<https://github.com/Getty/p5-kubernetes-comb/issues>.
168  
169 =head2 IRC
170  
171 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
172  
173 =head1 CONTRIBUTING
174  
175 Contributions are welcome! Please fork the repository and submit a pull request.
176  
177 =head1 AUTHOR
178  
179 Torsten Raudssus <getty@cpan.org>
180  
181 =head1 COPYRIGHT AND LICENSE
182  
183 This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
184  
185 This is free software; you can redistribute it and/or modify it under
186 the same terms as the Perl 5 programming language system itself.
187  
188 =cut
« blib/lib/Kubernetes/Comb/Role/Static.pm ↑ Summary blib/lib/Kubernetes/Comb/Upstream/Static.pm »