package Net::Kubernetes::Resource::Deployment;
$Net::Kubernetes::Resource::Deployment::VERSION = '1.08';
use Moose;

extends 'Net::Kubernetes::Resource';
with 'Net::Kubernetes::Resource::Role::State';
with 'Net::Kubernetes::Resource::Role::Spec';
with 'Net::Kubernetes::Resource::Role::HasPods';
with 'Net::Kubernetes::Resource::Role::HasReplicaSets';


sub scale {
    my ($self, $replicas, $timeout) = @_;
    $timeout //= $self->scale_timeout;
    $self->spec->{replicas} = $replicas;
    $self->update;
    my $st = time;
    while ($timeout < 0 || (time - $st) < $timeout) {
        my $pods = $self->get_pods;
        if (scalar(@$pods) == $replicas) {
            return "scaled";
        }
        sleep(0.3);
    }
    return 0;
}

return 1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Net::Kubernetes::Resource::Deployment

=head1 VERSION

version 1.08

=head2 my @pods = $deployment->get_pods()

Fetch a list of all pods belonging to this deployment.

=head2 my @sets = $deployment->get_rs()

Fetch a list of all replica sets belonging to this deployment.

=head2 $deployment->scale($replicas[, $timeout]);

Scales the deployment to the requested number of replicas. This method will
poll waiting for the replicas to reach the requested value for the duration
specified by $timeout in seconds.

On success, the string "scaled" is returned.
On a timeout, 0 is returned.

If $timeout is -1, then the method will wait indefinitely.

A default scale timeout can be specified by passing scale_timeout
to the constructor.

=head1 AUTHOR

Dave Mueller <dave@perljedi.com>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2015 by Liquid Web Inc.

This is free software, licensed under:

  The MIT (X11) License

=head1 SEE ALSO

Please see those modules/websites for more information related to this module.

=over 4

=item *

L<Net::Kubernetes|Net::Kubernetes>

=back

=head1 CONSUMES

=over 4

=item * L<Net::Kubernetes::Resource::Role::HasPods>

=item * L<Net::Kubernetes::Resource::Role::HasReplicaSets>

=item * L<Net::Kubernetes::Resource::Role::Spec>

=item * L<Net::Kubernetes::Resource::Role::State>

=item * L<Net::Kubernetes::Role::APIAccess>

=item * L<Net::Kubernetes::Role::ResourceCatalog>

=back

=cut
