Skip to content

Commit c5ce533

Browse files
committed
some adjust by review
1 parent 8914b1d commit c5ce533

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

platforms/kubernetes/postgres-operator/deploy/postgres-operator.yaml.template

+7-4
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,6 @@ spec:
161161
type: string
162162
PATH:
163163
type: string
164-
BACKUP_NAME:
165-
type: string
166-
RESTORE_NAME:
167-
type: string
168164
services:
169165
type: array
170166
items:
@@ -361,6 +357,11 @@ spec:
361357
type: object
362358
x-kubernetes-preserve-unknown-fields: true
363359
properties:
360+
backups3:
361+
type: object
362+
properties:
363+
name:
364+
type: string
364365
manual:
365366
type: object
366367
properties:
@@ -414,6 +415,8 @@ spec:
414415
froms3:
415416
type: object
416417
properties:
418+
name:
419+
type: string
417420
recovery:
418421
type: string
419422

platforms/kubernetes/postgres-operator/deploy/postgresql.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ spec:
164164
ENDPOINT: "http://s3.pek3.qingstor.com/"
165165
BUCKET: "postgresql"
166166
PATH: "test/backup"
167-
# BACKUP_NAME: "postgresql-backup"
168-
# RESTORE_NAME: "postgresql-backup"
169167
# backupCluster:
168+
# backups3:
169+
# name: postgresql-backup
170170
# manual:
171171
# trigger-id: 2
172172
# cron:
@@ -188,6 +188,7 @@ spec:
188188
# RECOVERY WINDOW OF value {DAYS | WEEKS | MONTHS}: keep backup time. eg: RECOVERY WINDOW OF 60 DAYS
189189
# restore:
190190
# froms3:
191+
## name: postgresql-backup
191192
# recovery: latest # backupid or recovery_time or macro variable
192193
# eg:
193194
# recovery: 20221011T143943 means recovering full data from 20221011T143943 backup

platforms/kubernetes/postgres-operator/postgres/constants.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
SPEC_POSTGRESQL_USERS_USER_PASSWORD = "password"
5151
SPEC_POSTGRESQL_USERS_USER_NAME = "name"
5252
SPEC_BACKUP = "backupCluster"
53+
SPEC_BACKUP_BACKUPS3 = "backups3"
54+
SPEC_BACKUP_BACKUPS3_NAME = "name"
5355
SPEC_BACKUP_MANUAL = "manual"
5456
SPEC_BACKUP_TRIGGER_ID = "trigger-id"
5557
SPEC_BACKUP_CRON = "cron"
@@ -70,6 +72,7 @@
7072
RESTORE_FROMSSH_ADDRESS = "address"
7173
RESTORE_FROMSSH_LOCAL = "local"
7274
RESTORE_FROMS3 = "froms3"
75+
RESTORE_FROMS3_NAME = "name"
7376
RESTORE_FROMS3_RECOVERY = "recovery"
7477
RESTORE_FROMS3_RECOVERY_LATEST = "latest"
7578
RESTORE_FROMS3_RECOVERY_LATEST_FULL = "latest-full"
@@ -153,8 +156,6 @@
153156
SPEC_S3_ENDPOINT = "ENDPOINT"
154157
SPEC_S3_BUCKET = "BUCKET"
155158
SPEC_S3_PATH = "PATH"
156-
SPEC_S3_BACKUP_NAME = "BACKUP_NAME"
157-
SPEC_S3_RESTORE_NAME = "RESTORE_NAME"
158159

159160
# pod PriorityClass
160161
SPEC_POD_PRIORITY_CLASS = "priorityClassName"

platforms/kubernetes/postgres-operator/postgres/handle.py

+26-16
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@
152152
SPEC_S3_ENDPOINT,
153153
SPEC_S3_BUCKET,
154154
SPEC_S3_PATH,
155-
SPEC_S3_BACKUP_NAME,
156-
SPEC_S3_RESTORE_NAME,
157155
SPEC_BACKUP,
156+
SPEC_BACKUP_BACKUPS3,
157+
SPEC_BACKUP_BACKUPS3_NAME,
158158
SPEC_BACKUP_MANUAL,
159159
SPEC_BACKUP_TRIGGER_ID,
160160
SPEC_BACKUP_CRON,
@@ -170,6 +170,7 @@
170170
SPEC_BACKUP_POLICY_RETENTION,
171171
SPEC_BACKUP_POLICY_RETENTION_DEFAULT_VALUE,
172172
RESTORE_FROMS3,
173+
RESTORE_FROMS3_NAME,
173174
RESTORE_FROMS3_RECOVERY,
174175
CLUSTER_STATUS_BACKUP,
175176
CLUSTER_STATUS_ARCHIVE,
@@ -1613,16 +1614,21 @@ def get_oldest_backupid(backup_info: TypedDict) -> str:
16131614
return backupid
16141615

16151616

1616-
def get_s3_env(meta: kopf.Meta, s3: TypedDict) -> List:
1617+
def get_backup_name_env(meta: kopf.Meta, name: str = None) -> List:
16171618
res = list()
16181619

1619-
name = meta["name"]
1620-
if SPEC_S3_BACKUP_NAME in s3:
1621-
name = s3.pop(SPEC_S3_BACKUP_NAME)
1622-
if SPEC_S3_RESTORE_NAME in s3:
1623-
name = s3.pop(SPEC_S3_RESTORE_NAME)
1624-
res.append("-e")
1625-
res.append(BARMAN_BACKUP_NAME + '="' + name + '"')
1620+
if name is None:
1621+
name = meta['name']
1622+
1623+
env = BARMAN_BACKUP_NAME + '="' + name + '"'
1624+
res.append('-e')
1625+
res.append(env)
1626+
1627+
return res
1628+
1629+
1630+
def get_s3_env(s3: TypedDict) -> List:
1631+
res = list()
16261632

16271633
# use SPEC_S3 prefix replace S3 key (must use S3_ prefix)
16281634
for k in list(s3.keys()):
@@ -1676,11 +1682,13 @@ def restore_postgresql_froms3(
16761682
recovery_time = None
16771683

16781684
recovery = spec[RESTORE][RESTORE_FROMS3].get(RESTORE_FROMS3_RECOVERY, None)
1685+
name = spec[RESTORE][RESTORE_FROMS3].get(RESTORE_FROMS3_NAME, None)
16791686

16801687
# add s3 env by pgtools -e
16811688
s3 = spec[SPEC_S3].copy()
1682-
s3.pop(SPEC_S3_BACKUP_NAME, None)
1683-
s3_info = get_s3_env(meta, s3)
1689+
s3_list = get_s3_env(s3)
1690+
name_list = get_backup_name_env(meta, name)
1691+
s3_info = [*s3_list, *name_list]
16841692

16851693
tmpconns: InstanceConnections = InstanceConnections()
16861694
tmpconns.add(conn)
@@ -1913,13 +1921,15 @@ def backup_postgresql_to_s3(
19131921

19141922
# add s3 env by pgtools
19151923
s3 = spec[SPEC_S3].copy()
1916-
s3.pop(SPEC_S3_RESTORE_NAME, None)
1917-
s3_list = get_s3_env(meta, s3)
1924+
s3_list = get_s3_env(s3)
19181925

19191926
backup_policy = spec.get(SPEC_BACKUP, {}).get(SPEC_BACKUP_POLICY, {})
19201927
policy_list = get_policy_env(backup_policy)
19211928

1922-
s3_info = [*s3_list, *policy_list]
1929+
name = spec[SPEC_BACKUP].get(SPEC_BACKUP_BACKUPS3, {}).get(SPEC_BACKUP_BACKUPS3_NAME, None)
1930+
name_list = get_backup_name_env(meta, name)
1931+
1932+
s3_info = [*s3_list, *policy_list, *name_list]
19231933

19241934
cmd = ["pgtools", "-b"] + s3_info
19251935
logging.warning(f"backup_postgresql_to_s3 execute {cmd} to backup cluster on readwrite node")
@@ -3592,7 +3602,7 @@ def correct_s3_profile(
35923602
) -> None:
35933603
if is_s3_manual_backup_mode(meta, spec, patch, status, logger) or is_s3_cron_backup_mode(meta, spec, patch, status, logger):
35943604
s3 = spec[SPEC_S3].copy()
3595-
s3_info = get_s3_env(meta, s3)
3605+
s3_info = get_s3_env(s3)
35963606
readwrite_conns = connections(spec, meta, patch,
35973607
get_field(POSTGRESQL, READWRITEINSTANCE),
35983608
False, None, logger, None, status, False)

0 commit comments

Comments
 (0)