106
106
LVS_REAL_MAIN_SERVER ,
107
107
LVS_REAL_READ_SERVER ,
108
108
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 ,
109
116
)
110
117
111
118
FIELD_DELIMITER = "-"
171
178
STATUS_KEEPALIVED = "systemctl status keepalived.service"
172
179
173
180
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 )
176
206
177
207
178
208
def set_password (patch : kopf .Patch , status : kopf .Status ) -> None :
@@ -1863,6 +1893,42 @@ def create_services(
1863
1893
conns .free_conns ()
1864
1894
1865
1895
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
+
1866
1932
async def create_postgresql_cluster (
1867
1933
meta : kopf .Meta ,
1868
1934
spec : kopf .Spec ,
@@ -1878,7 +1944,7 @@ async def create_postgresql_cluster(
1878
1944
create_services (meta , spec , patch , status , logger )
1879
1945
1880
1946
# create autofailover
1881
- set_create_cluster (patch , CLUSTER_CREATE_ADD_FAILOVER )
1947
+ # set_create_cluster(patch, CLUSTER_CREATE_ADD_FAILOVER)
1882
1948
create_autofailover (meta , spec , patch , status , logger ,
1883
1949
get_autofailover_labels (meta ))
1884
1950
conns = connections (spec , meta , patch , get_field (AUTOFAILOVER ), False ,
@@ -1887,7 +1953,7 @@ async def create_postgresql_cluster(
1887
1953
conns .free_conns ()
1888
1954
1889
1955
# create postgresql & readwrite node
1890
- set_create_cluster (patch , CLUSTER_CREATE_ADD_READWRITE )
1956
+ # set_create_cluster(patch, CLUSTER_CREATE_ADD_READWRITE)
1891
1957
create_postgresql_readwrite (meta , spec , patch , status , logger ,
1892
1958
get_readwrite_labels (meta ), 0 , True )
1893
1959
#conns = connections(spec, meta, patch,
@@ -1897,12 +1963,12 @@ async def create_postgresql_cluster(
1897
1963
#conns.free_conns()
1898
1964
1899
1965
# create postgresql & readonly node
1900
- set_create_cluster (patch , CLUSTER_CREATE_ADD_READONLY )
1966
+ # set_create_cluster(patch, CLUSTER_CREATE_ADD_READONLY)
1901
1967
create_postgresql_readonly (meta , spec , patch , status , logger ,
1902
1968
get_readonly_labels (meta ), 0 )
1903
1969
1904
1970
# finish
1905
- set_create_cluster (patch , CLUSTER_CREATE_FINISH )
1971
+ # set_create_cluster(patch, CLUSTER_CREATE_FINISH)
1906
1972
1907
1973
1908
1974
async def create_cluster (
@@ -1912,39 +1978,23 @@ async def create_cluster(
1912
1978
status : kopf .Status ,
1913
1979
logger : logging .Logger ,
1914
1980
) -> None :
1915
- set_create_cluster (patch , CLUSTER_CREATE_BEGIN )
1981
+ try :
1982
+ set_cluster_status (meta , CLUSTER_CREATE_CLUSTER , CLUSTER_STATUS_CREATE , logger )
1916
1983
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 )
1926
1987
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 )
1945
1990
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 )
1948
1998
1949
1999
1950
2000
async def delete_cluster (
@@ -1954,7 +2004,7 @@ async def delete_cluster(
1954
2004
status : kopf .Status ,
1955
2005
logger : logging .Logger ,
1956
2006
) -> None :
1957
-
2007
+ set_cluster_status ( meta , CLUSTER_CREATE_CLUSTER , CLUSTER_STATUS_TERMINATE , logger )
1958
2008
await delete_postgresql_cluster (meta , spec , patch , status , logger )
1959
2009
1960
2010
@@ -2740,20 +2790,36 @@ async def update_cluster(
2740
2790
logger : logging .Logger ,
2741
2791
diffs : kopf .Diff ,
2742
2792
) -> 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