Skip to content

Commit ff03ee1

Browse files
author
yb
committed
[add]add cluster status
1 parent 20cbd11 commit ff03ee1

File tree

3 files changed

+143
-54
lines changed

3 files changed

+143
-54
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,57 @@ spec:
2828
description: The cluster action
2929
name: Action
3030
type: string
31+
priority: 0 # show in standard view
3132
- jsonPath: .spec.deletepvc
3233
description: delete the pvc when cluster is deleted.
3334
name: DeletePvc
3435
type: boolean
36+
priority: 0 # show in standard view
37+
- jsonPath: .status.cluster_create
38+
description: The postgresql cluster status
39+
name: Clusterstatus
40+
type: string
41+
priority: 0 # show in standard view
3542
- jsonPath: .spec.autofailover.podspec.containers[0].image
3643
description: The autofailover image
3744
name: FailoverImage
3845
type: string
46+
priority: 1 # show in standard view
3947
- jsonPath: .spec.postgresql.readwriteinstance.podspec.containers[0].image
4048
description: The postgresql image
4149
name: PostgresqlImage
4250
type: string
51+
priority: 1 # show in standard view
4352
- jsonPath: .spec.postgresql.readwriteinstance.replicas
4453
description: The readwriteinstance nodes
4554
name: RWnodes
4655
type: integer
56+
priority: 1 # show in wide view
4757
- jsonPath: .spec.postgresql.readwriteinstance.podspec.containers[0].resources.limits.cpu
4858
description: The readwriteinstance cpu
4959
name: RWcpu
5060
type: string
61+
priority: 1 # show in wide view
5162
- jsonPath: .spec.postgresql.readwriteinstance.podspec.containers[0].resources.limits.memory
5263
description: The readwriteinstance memory
5364
name: RWmemory
5465
type: string
66+
priority: 1 # show in wide view
5567
- jsonPath: .spec.postgresql.readonlyinstance.replicas
5668
description: The readonlyinstance nodes
5769
name: ROnodes
5870
type: integer
71+
priority: 1 # show in wide view
5972
- jsonPath: .spec.postgresql.readonlyinstance.podspec.containers[0].resources.limits.cpu
6073
description: The readonlyinstance cpu
6174
name: ROcpu
6275
type: string
76+
priority: 1 # show in wide view
6377
- jsonPath: .spec.postgresql.readonlyinstance.podspec.containers[0].resources.limits.memory
6478
description: The readonlyinstance memory
6579
name: ROmemory
6680
type: string
81+
priority: 1 # show in wide view
6782
schema:
6883
openAPIV3Schema:
6984
type: object

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

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@
6565
CLUSTER_CREATE_ADD_READONLY = "addition readonly"
6666
CLUSTER_CREATE_FINISH = "finish"
6767

68+
CLUSTER_STATUS = "status"
69+
CLUSTER_STATUS_CREATE = "Creating"
70+
CLUSTER_STATUS_UPDATE = "Updating"
71+
CLUSTER_STATUS_RUN = "Running"
72+
CLUSTER_STATUS_CREATE_FAILED = "CreateFailed"
73+
CLUSTER_STATUS_UPDATE_FAILED = "UpdateFailed"
74+
CLUSTER_STATUS_TERMINATE = "Terminating"
75+
6876
# base label
6977
BASE_LABEL_PART_OF = "part-of"
7078
BASE_LABEL_MANAGED_BY = "managed-by"

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

+120-54
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@
106106
LVS_REAL_MAIN_SERVER,
107107
LVS_REAL_READ_SERVER,
108108
LVS_SET_NET,
109+
CLUSTER_STATUS_CREATE,
110+
CLUSTER_STATUS_UPDATE,
111+
CLUSTER_STATUS_RUN,
112+
CLUSTER_STATUS_CREATE_FAILED,
113+
CLUSTER_STATUS_UPDATE_FAILED,
114+
CLUSTER_STATUS_TERMINATE,
115+
CLUSTER_STATUS,
109116
)
110117

111118
FIELD_DELIMITER = "-"
@@ -171,8 +178,31 @@
171178
STATUS_KEEPALIVED = "systemctl status keepalived.service"
172179

173180

174-
def set_create_cluster(patch, state: str) -> None:
175-
patch.status[CLUSTER_CREATE_CLUSTER] = state
181+
def set_cluster_status(meta: kopf.Meta,
182+
statefield: str,
183+
state: str,
184+
logger: logging.Logger) -> None:
185+
customer_obj_api = client.CustomObjectsApi()
186+
name = meta['name']
187+
namespace = meta['namespace']
188+
189+
# get customer definition
190+
body = customer_obj_api.get_namespaced_custom_object(group=API_GROUP, version=API_VERSION_V1,
191+
namespace=namespace, plural=RESOURCE_POSTGRESQL,
192+
name=name)
193+
if CLUSTER_STATUS not in body:
194+
cluster_create = {CLUSTER_STATUS: {statefield: state}}
195+
body = {**body, **cluster_create}
196+
else:
197+
body[CLUSTER_STATUS][statefield] = state
198+
199+
logger.info(f"update {API_GROUP + API_VERSION_V1} crd {name} field .status.{statefield} = {state}, set_cluster_status body = {body}")
200+
customer_obj_api.patch_namespaced_custom_object(group=API_GROUP,
201+
version=API_VERSION_V1,
202+
namespace=namespace,
203+
plural=RESOURCE_POSTGRESQL,
204+
name=name,
205+
body=body)
176206

177207

178208
def set_password(patch: kopf.Patch, status: kopf.Status) -> None:
@@ -1863,6 +1893,42 @@ def create_services(
18631893
conns.free_conns()
18641894

18651895

1896+
def check_param(spec: kopf.Spec, logger: logging.Logger,) -> None:
1897+
autofailover_machines = spec.get(AUTOFAILOVER).get(MACHINES)
1898+
readwrite_machines = spec.get(POSTGRESQL).get(READWRITEINSTANCE).get(
1899+
MACHINES)
1900+
# *_replicas will be replace in machine mode
1901+
readwrite_replicas = spec.get(POSTGRESQL).get(READWRITEINSTANCE).get(
1902+
REPLICAS)
1903+
readonly_machines = spec.get(POSTGRESQL).get(READONLYINSTANCE).get(
1904+
MACHINES)
1905+
readonly_replicas = spec.get(POSTGRESQL).get(READONLYINSTANCE).get(
1906+
REPLICAS)
1907+
1908+
if autofailover_machines != None or readwrite_machines != None or readonly_machines != None:
1909+
logger.info("running on machines mode")
1910+
readwrite_replicas = len(readwrite_machines)
1911+
readonly_replicas = len(readonly_machines)
1912+
1913+
if autofailover_machines == None:
1914+
raise kopf.PermanentError("autofailover machines not set")
1915+
if readwrite_machines == None:
1916+
raise kopf.PermanentError("readwrite machines not set")
1917+
if len(autofailover_machines) != 1:
1918+
raise kopf.PermanentError("autofailover only support one machine.")
1919+
if len(readwrite_machines) < 1:
1920+
raise kopf.PermanentError(
1921+
"readwrite machines must set at lease one machine")
1922+
if spec[ACTION] == ACTION_STOP:
1923+
raise kopf.PermanentError("can't set stop at init cluster.")
1924+
if readwrite_replicas < 1:
1925+
raise kopf.PermanentError("readwrite replicas must set at lease one")
1926+
if readonly_replicas < 0:
1927+
raise kopf.PermanentError("readonly replicas must large than zero")
1928+
1929+
logger.info("parameters are correct")
1930+
1931+
18661932
async def create_postgresql_cluster(
18671933
meta: kopf.Meta,
18681934
spec: kopf.Spec,
@@ -1878,7 +1944,7 @@ async def create_postgresql_cluster(
18781944
create_services(meta, spec, patch, status, logger)
18791945

18801946
# create autofailover
1881-
set_create_cluster(patch, CLUSTER_CREATE_ADD_FAILOVER)
1947+
# set_create_cluster(patch, CLUSTER_CREATE_ADD_FAILOVER)
18821948
create_autofailover(meta, spec, patch, status, logger,
18831949
get_autofailover_labels(meta))
18841950
conns = connections(spec, meta, patch, get_field(AUTOFAILOVER), False,
@@ -1887,7 +1953,7 @@ async def create_postgresql_cluster(
18871953
conns.free_conns()
18881954

18891955
# create postgresql & readwrite node
1890-
set_create_cluster(patch, CLUSTER_CREATE_ADD_READWRITE)
1956+
# set_create_cluster(patch, CLUSTER_CREATE_ADD_READWRITE)
18911957
create_postgresql_readwrite(meta, spec, patch, status, logger,
18921958
get_readwrite_labels(meta), 0, True)
18931959
#conns = connections(spec, meta, patch,
@@ -1897,12 +1963,12 @@ async def create_postgresql_cluster(
18971963
#conns.free_conns()
18981964

18991965
# create postgresql & readonly node
1900-
set_create_cluster(patch, CLUSTER_CREATE_ADD_READONLY)
1966+
# set_create_cluster(patch, CLUSTER_CREATE_ADD_READONLY)
19011967
create_postgresql_readonly(meta, spec, patch, status, logger,
19021968
get_readonly_labels(meta), 0)
19031969

19041970
# finish
1905-
set_create_cluster(patch, CLUSTER_CREATE_FINISH)
1971+
# set_create_cluster(patch, CLUSTER_CREATE_FINISH)
19061972

19071973

19081974
async def create_cluster(
@@ -1912,39 +1978,23 @@ async def create_cluster(
19121978
status: kopf.Status,
19131979
logger: logging.Logger,
19141980
) -> None:
1915-
set_create_cluster(patch, CLUSTER_CREATE_BEGIN)
1981+
try:
1982+
set_cluster_status(meta, CLUSTER_CREATE_CLUSTER, CLUSTER_STATUS_CREATE, logger)
19161983

1917-
autofailover_machines = spec.get(AUTOFAILOVER).get(MACHINES)
1918-
readwrite_machines = spec.get(POSTGRESQL).get(READWRITEINSTANCE).get(
1919-
MACHINES)
1920-
readwrite_replicas = spec.get(POSTGRESQL).get(READWRITEINSTANCE).get(
1921-
REPLICAS)
1922-
readonly_machines = spec.get(POSTGRESQL).get(READONLYINSTANCE).get(
1923-
MACHINES)
1924-
readonly_replicas = spec.get(POSTGRESQL).get(READONLYINSTANCE).get(
1925-
REPLICAS)
1984+
logging.info("check create_cluster params")
1985+
check_param(spec, logger)
1986+
await create_postgresql_cluster(meta, spec, patch, status, logger)
19261987

1927-
if autofailover_machines != None or readwrite_machines != None or readonly_machines != None:
1928-
logger.info("running on machines mode")
1929-
if autofailover_machines == None:
1930-
raise kopf.PermanentError("autofailover machines not set")
1931-
if readwrite_machines == None:
1932-
raise kopf.PermanentError("readwrite machines not set")
1933-
if len(autofailover_machines) != 1:
1934-
raise kopf.PermanentError("autofailover only support one machine.")
1935-
if len(readwrite_machines) < 1:
1936-
raise kopf.PermanentError(
1937-
"readwrite machines must set at lease one machine")
1938-
1939-
if spec[ACTION] == ACTION_STOP:
1940-
raise kopf.PermanentError("can't set stop at init cluster.")
1941-
if readwrite_replicas < 1:
1942-
raise kopf.PermanentError("readwrite replicas must set at lease one")
1943-
if readonly_replicas < 0:
1944-
raise kopf.PermanentError("readonly replicas must large than zero")
1988+
logger.info("waiting for create_cluster success")
1989+
waiting_cluster_correct_status(meta, spec, patch, status, logger)
19451990

1946-
logger.info("parameters are correct")
1947-
await create_postgresql_cluster(meta, spec, patch, status, logger)
1991+
# wait a few seconds to prevent the pod not running
1992+
time.sleep(5)
1993+
# cluster running
1994+
set_cluster_status(meta, CLUSTER_CREATE_CLUSTER, CLUSTER_STATUS_RUN, logger)
1995+
except Exception as e:
1996+
logger.error(f"error occurs, {e.args}")
1997+
set_cluster_status(meta, CLUSTER_CREATE_CLUSTER, CLUSTER_STATUS_CREATE_FAILED, logger)
19481998

19491999

19502000
async def delete_cluster(
@@ -1954,7 +2004,7 @@ async def delete_cluster(
19542004
status: kopf.Status,
19552005
logger: logging.Logger,
19562006
) -> None:
1957-
2007+
set_cluster_status(meta, CLUSTER_CREATE_CLUSTER, CLUSTER_STATUS_TERMINATE, logger)
19582008
await delete_postgresql_cluster(meta, spec, patch, status, logger)
19592009

19602010

@@ -2740,20 +2790,36 @@ async def update_cluster(
27402790
logger: logging.Logger,
27412791
diffs: kopf.Diff,
27422792
) -> None:
2743-
for diff in diffs:
2744-
AC = diff[0]
2745-
FIELD = diff[1]
2746-
OLD = diff[2]
2747-
NEW = diff[3]
2748-
2749-
logger.info(diff)
2750-
update_replicas(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2751-
update_action(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2752-
update_service(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2753-
update_hbas(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2754-
update_users(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2755-
update_streaming(meta, spec, patch, status, logger, AC, FIELD, OLD,
2756-
NEW)
2757-
update_podspec_volume(meta, spec, patch, status, logger, AC, FIELD,
2758-
OLD, NEW)
2759-
update_configs(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2793+
try:
2794+
set_cluster_status(meta, CLUSTER_CREATE_CLUSTER, CLUSTER_STATUS_UPDATE, logger)
2795+
logger.info("check update_cluster params")
2796+
check_param(spec, logger)
2797+
2798+
for diff in diffs:
2799+
AC = diff[0]
2800+
FIELD = diff[1]
2801+
OLD = diff[2]
2802+
NEW = diff[3]
2803+
2804+
logger.info(diff)
2805+
update_replicas(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2806+
update_action(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2807+
update_service(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2808+
update_hbas(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2809+
update_users(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2810+
update_streaming(meta, spec, patch, status, logger, AC, FIELD, OLD,
2811+
NEW)
2812+
update_podspec_volume(meta, spec, patch, status, logger, AC, FIELD,
2813+
OLD, NEW)
2814+
update_configs(meta, spec, patch, status, logger, AC, FIELD, OLD, NEW)
2815+
2816+
logger.info("waiting for update_cluster success")
2817+
waiting_cluster_correct_status(meta, spec, patch, status, logger)
2818+
2819+
# wait a few seconds to prevent the pod not running
2820+
time.sleep(5)
2821+
# set Running
2822+
set_cluster_status(meta, CLUSTER_CREATE_CLUSTER, CLUSTER_STATUS_RUN, logger)
2823+
except Exception as e:
2824+
logger.error(f"error occurs, {e.args}")
2825+
set_cluster_status(meta, CLUSTER_CREATE_CLUSTER, CLUSTER_STATUS_UPDATE_FAILED, logger)

0 commit comments

Comments
 (0)