Skip to content

Commit 6d77e1d

Browse files
committed
fix: set the default schema for databases
this commit fix the postgres user creation/deletion. when a user is created a schema named <user>_schema is created and made the default for the user. This resolves apache/openserverless#151
1 parent 233449a commit 6d77e1d

File tree

6 files changed

+75
-13
lines changed

6 files changed

+75
-13
lines changed

nuvolaris/kube.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
# default output is text
3838
# if you specify jsonpath it will filter and parse the json output
3939
# returns exceptions if errors
40-
def kubectl(*args, namespace="nuvolaris", input=None, jsonpath=None, debugresult=True):
40+
def kubectl(*args, namespace="nuvolaris", input=None, jsonpath=None, debugresult=True, timeout=None):
4141
# support for mocked requests
4242
mres = mocker.invoke(*args)
4343
if mres:
@@ -55,7 +55,7 @@ def kubectl(*args, namespace="nuvolaris", input=None, jsonpath=None, debugresult
5555

5656
# executing
5757
logging.debug(cmd)
58-
res = subprocess.run(cmd, capture_output=True, input=input)
58+
res = subprocess.run(cmd, capture_output=True, input=input, timeout=timeout)
5959

6060
global returncode, output, error
6161
returncode = res.returncode

nuvolaris/postgres_operator.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,16 @@ def render_postgres_script(namespace,template,data):
178178
file = ntp.spool_template(template, out, data)
179179
return os.path.abspath(file)
180180

181-
def exec_psql_command(pod_name,path_to_psql_script,path_to_pgpass):
181+
def exec_psql_command(pod_name,path_to_psql_script,path_to_pgpass,additional_psql_args=''):
182182
logging.info(f"passing script {path_to_psql_script} to pod {pod_name}")
183183
res = kube.kubectl("cp",path_to_psql_script,f"{pod_name}:{path_to_psql_script}")
184184
res = kube.kubectl("cp",path_to_pgpass,f"{pod_name}:/tmp/.pgpass")
185185
res = kube.kubectl("exec","-it",pod_name,"--","/bin/bash","-c",f"chmod 600 /tmp/.pgpass")
186-
res = kube.kubectl("exec","-it",pod_name,"--","/bin/bash","-c",f"PGPASSFILE='/tmp/.pgpass' psql --username postgres --dbname postgres -f {path_to_psql_script}")
186+
187+
cmd = f"PGPASSFILE='/tmp/.pgpass' psql --username postgres --dbname postgres {additional_psql_args} -f {path_to_psql_script}"
188+
logging.info(f"executing command: {cmd}")
189+
res = kube.kubectl("exec","-it",pod_name,"--","/bin/bash","-c",cmd)
190+
187191
os.remove(path_to_psql_script)
188192
os.remove(path_to_pgpass)
189193
return res
@@ -209,6 +213,10 @@ def create_db_user(ucfg: UserConfig, user_metadata: UserMetadata):
209213
if res:
210214
_add_pdb_user_metadata(ucfg, user_metadata)
211215

216+
path_to_pgpass = render_postgres_script(ucfg.get('namespace'),"dbname_pgpass_tpl.properties",data)
217+
path_to_schema_script = render_postgres_script(ucfg.get('namespace'),"postgres_manage_user_schema_tpl.sql",data)
218+
res = exec_psql_command_in_db(database,pod_name,path_to_schema_script,path_to_pgpass)
219+
212220
data["extensions"]=["vector"]
213221
path_to_pgpass = render_postgres_script(ucfg.get('namespace'),"dbname_pgpass_tpl.properties",data)
214222
path_to_extensions_script = render_postgres_script(ucfg.get('namespace'),"postgres_manage_user_extension_tpl.sql",data)
@@ -232,12 +240,17 @@ def delete_db_user(namespace, database):
232240
data["database"]=database
233241
data["mode"]="delete"
234242

235-
path_to_pgpass = render_postgres_script(namespace,"pgpass_tpl.properties",data)
236-
path_to_mdb_script = render_postgres_script(namespace,"postgres_manage_user_tpl.sql",data)
243+
237244
pod_name = util.get_pod_name_by_selector("app=nuvolaris-postgres","{.items[?(@.metadata.labels.replicationRole == 'primary')].metadata.name}")
238245

239246
if(pod_name):
240-
res = exec_psql_command(pod_name,path_to_mdb_script,path_to_pgpass)
247+
path_to_pgpass = render_postgres_script(namespace,"pgpass_tpl.properties",data)
248+
path_to_ter_script = render_postgres_script(namespace,"postgres_terminate_tpl.sql",data)
249+
res = exec_psql_command(pod_name,path_to_ter_script,path_to_pgpass,' -q -t ')
250+
251+
path_to_pgpass = render_postgres_script(namespace,"pgpass_tpl.properties",data)
252+
path_to_mdb_script = render_postgres_script(namespace,"postgres_manage_user_tpl.sql",data)
253+
res += exec_psql_command(pod_name,path_to_mdb_script,path_to_pgpass)
241254
return res
242255

243256
return None

nuvolaris/templates/postgres_manage_user_extension_tpl.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
{% if mode == 'create' %}
2121
{% for extension in extensions -%}
22-
CREATE EXTENSION IF NOT EXISTS {{extension}};
22+
CREATE EXTENSION IF NOT EXISTS {{extension}} WITH SCHEMA {{username}}_schema;
2323
{% endfor %}
2424
{% endif %}
2525

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
{% if mode == 'create' %}
21+
-- Create schema only if not exists and owned by correct user
22+
CREATE SCHEMA IF NOT EXISTS {{username}}_schema;
23+
ALTER SCHEMA {{username}}_schema OWNER TO {{username}};
24+
ALTER DATABASE {{database}} SET search_path TO {{username}}_schema, pg_catalog;
25+
{% endif %}

nuvolaris/templates/postgres_manage_user_tpl.sql

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ REVOKE CONNECT ON DATABASE {{database}} from public;
2525
{% endif %}
2626

2727
{% if mode == 'delete' %}
28-
SELECT pg_terminate_backend(pg_stat_activity.pid)
29-
FROM pg_stat_activity
30-
WHERE pg_stat_activity.datname = '{{database}}';
31-
3228
DROP DATABASE {{database}};
33-
3429
DROP OWNED BY {{username}};
3530
DROP USER {{username}};
3631
{% endif %}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
DO $$
21+
BEGIN
22+
PERFORM pg_catalog.pg_terminate_backend(pid)
23+
FROM pg_catalog.pg_stat_activity
24+
WHERE pg_stat_activity.datname = '{{database}}'
25+
AND pg_stat_activity.pid <> pg_catalog.pg_backend_pid();
26+
END;
27+
$$;
28+
29+

0 commit comments

Comments
 (0)