Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
- uses: nolar/setup-k3d-k3s@v1
with:
version: "${{ matrix.kubernetes }}"
k3d-tag: v4.4.8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see changes like this as separate commits ⛏️

k3d-args: --no-lb
- name: Prefetch container images
run: >
Expand Down
17 changes: 15 additions & 2 deletions internal/controller/postgrescluster/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/crunchydata/postgres-operator/internal/logging"
"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/internal/pgaudit"
"github.com/crunchydata/postgres-operator/internal/postgis"
"github.com/crunchydata/postgres-operator/internal/postgres"
pgpassword "github.com/crunchydata/postgres-operator/internal/postgres/password"
"github.com/crunchydata/postgres-operator/internal/util"
Expand Down Expand Up @@ -188,7 +189,7 @@ func (r *Reconciler) reconcilePostgresDatabases(

// Calculate a hash of the SQL that should be executed in PostgreSQL.

var pgAuditOK bool
var pgAuditOK, postgisInstallOK bool
create := func(ctx context.Context, exec postgres.Executor) error {
if pgAuditOK = pgaudit.EnableInPostgreSQL(ctx, exec) == nil; !pgAuditOK {
// pgAudit can only be enabled after its shared library is loaded,
Expand All @@ -201,6 +202,18 @@ func (r *Reconciler) reconcilePostgresDatabases(
"Unable to install pgAudit; try restarting PostgreSQL")
}

// Enabling PostGIS extensions is a one-way operation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the comment(s) 👍

// e.g., you can take a PostgresCluster and turn it into a PostGISCluster,
// but you cannot reverse the process, as that would potentially remove an extension
// that is being used by some database/tables
if cluster.Spec.PostGISVersion != "" {
if postgisInstallOK = postgis.EnableInPostgreSQL(ctx, exec) == nil; !postgisInstallOK {
// TODO(benjb): Investigate under what conditions postgis would fail install
r.Recorder.Event(cluster, corev1.EventTypeWarning, "PostGISDisabled",
"Unable to install PostGIS")
}
}

return postgres.CreateDatabasesInPostgreSQL(ctx, exec, databases.List())
}

Expand Down Expand Up @@ -232,7 +245,7 @@ func (r *Reconciler) reconcilePostgresDatabases(
log := logging.FromContext(ctx).WithValues("revision", revision)
err = errors.WithStack(create(logging.NewContext(ctx, log), podExecutor))
}
if err == nil && pgAuditOK {
if err == nil && pgAuditOK && postgisInstallOK {
cluster.Status.DatabaseRevision = revision
}

Expand Down
53 changes: 53 additions & 0 deletions internal/postgis/postgis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2021 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package postgis

import (
"context"
"strings"

"github.com/crunchydata/postgres-operator/internal/logging"
"github.com/crunchydata/postgres-operator/internal/postgres"
)

// EnableInPostgreSQL installs triggers for the following extensions into every database:
// - postgis
// - postgis_topology
// - fuzzystrmatch
// - postgis_tiger_geocoder
func EnableInPostgreSQL(ctx context.Context, exec postgres.Executor) error {
log := logging.FromContext(ctx)

stdout, stderr, err := exec.ExecInAllDatabases(ctx,
strings.Join([]string{
// Quiet NOTICE messages from IF NOT EXISTS statements.
// - https://www.postgresql.org/docs/current/runtime-config-client.html
`SET client_min_messages = WARNING;`,

`CREATE EXTENSION IF NOT EXISTS postgis;`,
`CREATE EXTENSION IF NOT EXISTS postgis_topology;`,
`CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;`,
`CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder;`,
}, "\n"),
map[string]string{
"ON_ERROR_STOP": "on", // Abort when any one statement fails.
"QUIET": "on", // Do not print successful statements to stdout.
})

log.V(1).Info("enabled PostGIS and related extensions", "stdout", stdout, "stderr", stderr)

return err
}
54 changes: 54 additions & 0 deletions internal/postgis/postgis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2021 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package postgis

import (
"context"
"errors"
"io"
"io/ioutil"
"strings"
"testing"

"gotest.tools/v3/assert"
)

func TestEnableInPostgreSQL(t *testing.T) {
expected := errors.New("whoops")
exec := func(
_ context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
assert.Assert(t, stdout != nil, "should capture stdout")
assert.Assert(t, stderr != nil, "should capture stderr")

assert.Assert(t, strings.Contains(strings.Join(command, "\n"),
`SELECT datname FROM pg_catalog.pg_database`,
), "expected all databases and templates")

b, err := ioutil.ReadAll(stdin)
assert.NilError(t, err)
assert.Equal(t, string(b), `SET client_min_messages = WARNING;
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS postgis_topology;
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder;`)

return expected
}

ctx := context.Background()
assert.Equal(t, expected, EnableInPostgreSQL(ctx, exec))
}