DBIO

 view release on metacpan or  search on metacpan

lib/DBIO/Test/Kubernetes.pm  view on Meta::CPAN

        run_id          => _generate_suffix(),
        databases       => $databases,
        api             => $api,
        port_forward_pids => [],
        local_ports     => {},
        _created        => [],
        _deployed       => 0,
        _ready          => 0,
    }, $class;
}


sub namespace  { $_[0]->{namespace} }


sub databases  { $_[0]->{databases} }


sub api        { $_[0]->{api} }


sub kubeconfig { $_[0]->{kubeconfig} }


sub uses_existing_namespace { $_[0]->{existing_namespace} ? 1 : 0 }


sub created_objects { @{ $_[0]->{_created} // [] } }

sub _generate_suffix {
    my @chars = ('a'..'z', '0'..'9');
    return join '', map { $chars[rand @chars] } 1..8;
}

sub _generate_namespace { 'dbio-test-' . _generate_suffix() }

# ============================================================================
# OBJECT NAMES AND LABELS
# ============================================================================

# A namespace of our own is the isolation, so the names in it are the plain
# ones. In somebody else's namespace a plain name is a collision waiting to
# happen (a Pod `mysql` and a Service `mysql-svc` are exactly what a hand-run
# test database is called), so every name carries this run's id.
sub _object_name {
    my ($self, $base) = @_;

    return $base unless $self->{existing_namespace};
    return "dbio-test-$self->{run_id}-$base";
}

sub _job_name {
    my ($self) = @_;

    return 'dbio-test-runner' unless $self->{existing_namespace};
    return $self->_object_name('runner');
}

# Labels are the sharper edge. A Service selects by label, not by name: a pod
# of ours labelled app=mysql in a namespace that already has a Service
# selecting app=mysql becomes one of that Service's endpoints, and a share of
# its traffic lands on our throwaway pod. So in an existing namespace a pod
# carries only label keys that belong to this module, one of them the run id
# -- nothing a selector written by anybody else can match.
sub _pod_labels {
    my ($self, $db) = @_;

    return { app => $db, role => 'dbio-test-db' }
        unless $self->{existing_namespace};
    return { 'dbio-test-run' => $self->{run_id}, 'dbio-test-db' => $db };
}

# Service and Job are not selected by anything of ours, but they are left
# behind just as well by a run that dies. In an existing namespace they carry
# the run id as a label, so that one label finds every object of a run
# (maint/live-preflight). Metadata only: no selector reads a Service's or a
# Job's own labels. Empty list in a namespace of our own.
sub _run_labels {
    my ($self) = @_;

    return () unless $self->{existing_namespace};
    return ( labels => { 'dbio-test-run' => $self->{run_id} } );
}

# What the Service of a database selects: its own pod and no other -- in an
# existing namespace that takes the run id, or two concurrent runs would
# serve each other's connections.
sub _pod_selector {
    my ($self, $db) = @_;

    return { app => $db } unless $self->{existing_namespace};
    return $self->_pod_labels($db);
}

# Every object goes through here, and is written down the moment the API has
# accepted it. cleanup works from that list alone, so a deploy that dies
# halfway leaves nothing behind, and nothing is ever deleted that this run did
# not create: a create that fails (the name is taken, the quota is full) is
# not recorded.
sub _create {
    my ($self, $kind, %args) = @_;

    my $api = $self->api;
    $api->create($api->new_object($kind => %args));
    push @{ $self->{_created} }, { kind => $kind, name => $args{metadata}{name} };

    return;
}

# ============================================================================
# RESOURCE LIMITS
# ============================================================================

# The ONE place the default container resource envelope lives. Both the
# database pods and the in-cluster test Job resolve through here, so the call
# sites cannot drift apart. The values match the checked-in manifests of the
# driver distributions (maint/k8s/*.yaml): at most one core, because these
# suites run on small shared machines next to other databases and services.
#
# Returns a fresh hashref on every call -- the result is handed to the k8s
# object builder, which must not end up aliasing a shared structure.



( run in 0.748 second using v1.01-cache-2.11-cpan-eb134014566 )