DBIO

 view release on metacpan or  search on metacpan

lib/DBIO/AccessBroker.pm  view on Meta::CPAN

    my $broker = DBIO::AccessBroker::Static->new(
        dbname => 'myapp',
        username => 'app', password => 'secret',
    );
    # Storage gets storage-native connect info
    my $info = $broker->current_connect_info_for_storage($schema->storage);

    # Vault — rotating credentials from OpenBao/Vault
    use DBIO::AccessBroker::Vault;
    my $broker = DBIO::AccessBroker::Vault->new(
        vault     => WWW::OpenBao->new(endpoint => 'http://vault:8200', token => $token),
        dsn       => 'dbi:Pg:dbname=myapp;host=db',
        cred_path => 'database/creds/myapp',
        ttl       => 3600,         # credentials valid for 1 hour
        refresh_margin => 900,     # refresh 15 min before expiry
    );
    # DBIO can now connect directly with a broker
    my $schema = MyApp::Schema->connect($broker);

    # the async mode is the connect call's, beside the broker
    my $async_schema = MyApp::Schema->connect($broker, { async => 'future_io' });

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


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 };
}

t/kubernetes/existing-namespace.t  view on Meta::CPAN

# Offline test for the two kinds of namespace DBIO::Test::Kubernetes works in
# (k223).
#
# WHY this is pinned: the module used to assume it owns its namespace -- it
# created one, used fixed object names in it (pod `mysql`, service
# `mysql-svc`), and deleted the whole namespace at the end. An account that is
# confined to one shared namespace can do neither, and the fixed names are the
# lesser problem there. The namespace this was written for already holds a pod
# `mysql` labelled app=mysql behind a LoadBalancer service selecting
# app=mysql, in use by other live suites. A second pod with that label joins
# that service's endpoints and takes a share of its connections; and a cleanup
# that deletes "the namespace", or "everything labelled role=dbio-test-db",
# takes the other databases with it.
#
# The claims, in an existing namespace:
#   1. no Namespace object is created and none is ever deleted;
#   2. names and selector labels are unique per run, and no selector that was
#      there before matches a pod of ours;
#   3. cleanup deletes exactly what this run created, by kind and name -- also
#      after a deploy that died halfway.
# And the namespace of its own keeps working as before: created, plain names,

t/kubernetes/existing-namespace.t  view on Meta::CPAN

    is(scalar @svcs, 2, 'two services');

    for my $obj (@pods, @svcs) {
        my $name = $obj->{metadata}{name};
        is($obj->{metadata}{namespace}, 'dev-dbio', "$obj->{kind} $name is in dev-dbio");
        ok(!$FOREIGN_NAMES{$name},
            "$obj->{kind} $name does not take a name that is in use there");
        like($name, qr/^[a-z][a-z0-9-]{0,62}$/, "$name is a valid object name");
    }

    # The real harm: one of our pods becoming an endpoint of their service,
    # or being swept up by a selector somebody else deletes by.
    for my $pod (@pods) {
        my $name = $pod->{metadata}{name};
        for my $who (sort keys %FOREIGN_SELECTORS) {
            ok(!selects($FOREIGN_SELECTORS{$who}, $pod->{metadata}{labels}),
                "pod $name is not matched by the selector of: $who");
        }
    }

    # And the other direction: our service selects our pod of that database,



( run in 4.065 seconds using v1.01-cache-2.11-cpan-eb134014566 )