File Coverage

blib/lib/IO/K8s/Role/Loadbalanced.pm
Criterion Covered Total %
statement 23 23 100.0
branch 5 6 83.3
condition 7 10 70.0
subroutine 3 3 100.0
pod 0 2 0.0
total 38 44 86.3


line stmt bran cond sub pod time code
1             package IO::K8s::Role::Loadbalanced;
2             # ABSTRACT: Role for traffic distribution (weighted backends, mirroring)
3             our $VERSION = '1.009';
4 3     3   3834 use Moo::Role;
  3         8  
  3         24  
5              
6             sub set_weighted {
7 4     4 0 18215 my ($self, $name, $weight) = @_;
8 4   100     123 my $spec = $self->spec // {};
9 4   100     56 my $weighted = $spec->{weighted} //= { services => [] };
10 4         11 my $services = $weighted->{services};
11              
12             # Update existing or add new
13 4         10 my $found = 0;
14 4         11 for my $svc (@$services) {
15 2 100       9 if ($svc->{name} eq $name) {
16 1         3 $svc->{weight} = $weight;
17 1         3 $found = 1;
18 1         2 last;
19             }
20             }
21 4 100       22 push @$services, { name => $name, weight => $weight } unless $found;
22 4         92 $self->spec($spec);
23 4         135 return $self;
24             }
25              
26             sub mirror_to {
27 1     1 0 6269 my ($self, $name, %opts) = @_;
28 1   50     27 my $spec = $self->spec // {};
29 1   50     96 my $mirroring = $spec->{mirroring} //= {};
30 1   50     12 my $mirrors = $mirroring->{mirrors} //= [];
31             push @$mirrors, {
32             name => $name,
33 1 50       7 $opts{percent} ? (percent => $opts{percent}) : (),
34             };
35 1         30 $self->spec($spec);
36 1         37 return $self;
37             }
38              
39             1;
40              
41             __END__