Skip to content

Commit

Permalink
new life
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <[email protected]>

Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram committed Sep 11, 2024
1 parent 89f63b8 commit f3a4bd3
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions hooks/convert_bd_names_to_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# 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.
import datetime
import os
import time
from typing import Any, List
Expand All @@ -36,6 +37,7 @@
secret_name = 'lvg-migration'

migration_completed_label = 'migration-completed'
migration_condition_type = 'MigrationStatus'


# This webhook ensures the migration of LVMVolumeGroup resources from the old CRD version to the new one:
Expand All @@ -51,30 +53,16 @@ def main(ctx: hook.Context):
api_extension = kubernetes.client.ApiextensionsV1Api()

print(f"{migrate_script} tries to check if LvmVolumeGroup migration has been completed")
try:
kubernetes.client.CoreV1Api().read_namespaced_secret(secret_name, 'd8-sds-node-configurator')
print(f"{migrate_script} secret {secret_name} was found, no need to run the migration")
return
except kubernetes.client.exceptions.ApiException as ae:
if ae.status == 404:
pass
else:
print(f"{migrate_script} unable to get the secret {secret_name}, error: {ae}")
raise ae

print(
f"{migrate_script} no migration has been completed, starts to migrate LvmVolumeGroup kind to LVMVolumeGroup new version")

print(f"{migrate_script} tries to scale down the sds-node-configurator daemon set")
try:
api_v1.delete_namespaced_daemon_set(name=ds_name, namespace=ds_ns)
except kubernetes.client.exceptions.ApiException as e:
# if we are retrying the ds is already deleted
if e.status == 404:
pass
except Exception as e:
raise e
print(f"{migrate_script} daemon set has been successfully scaled down")
# try:
# kubernetes.client.CoreV1Api().read_namespaced_secret(secret_name, 'd8-sds-node-configurator')
# print(f"{migrate_script} secret {secret_name} was found, no need to run the migration")
# return
# except kubernetes.client.exceptions.ApiException as ae:
# if ae.status == 404:
# pass
# else:
# print(f"{migrate_script} unable to get the secret {secret_name}, error: {ae}")
# raise ae

print(f"{migrate_script} tries to find lvmvolumegroup CRD")
try:
Expand Down Expand Up @@ -147,6 +135,25 @@ def main(ctx: hook.Context):
print(f"{migrate_script} error occurred, error: {e}")
raise e

for condition in lvg_crd.status.conditions:
if condition.type == migration_condition_type:
print(f"{migrate_script} LvmVolumeGroup CRD has been already migrated to LVMVolumeGroup one")
return

print(
f"{migrate_script} no migration has been completed, starts to migrate LvmVolumeGroup kind to LVMVolumeGroup new version")

print(f"{migrate_script} tries to scale down the sds-node-configurator daemon set")
try:
api_v1.delete_namespaced_daemon_set(name=ds_name, namespace=ds_ns)
except kubernetes.client.exceptions.ApiException as e:
# if we are retrying the ds is already deleted
if e.status == 404:
pass
except Exception as e:
raise e
print(f"{migrate_script} daemon set has been successfully scaled down")

# LvmVolumeGroup CRD flow
if lvg_crd.spec.names.kind == 'LvmVolumeGroup':
print(f"{migrate_script} found LvmVolumeGroup CRD")
Expand Down Expand Up @@ -457,9 +464,28 @@ def create_migration_secret():
# 'kind': 'Secret',
# 'metadata': {
# 'name': secret_name}})
print(f"{migrate_script} created")
# print(f"{migrate_script} created")
# lvg_crd.status.conditions.append({
# 'lastTransitionTime': datetime.datetime.now(),
# 'message': 'LvmVolumeGroup CRD has been migrated to LVMVolumeGroup one',
# 'reason': 'SuccessfulMigration',
# 'type': migration_condition_type,
# })

kubernetes.client.ApiextensionsV1Api().patch_custom_resource_definition_status(name=lvg_crd_name,
body={
'status':
{
'conditions': [{
'lastTransitionTime': datetime.datetime.now(),
'message': 'LvmVolumeGroup CRD has been migrated to LVMVolumeGroup one',
'reason': 'MigrationCompleted',
'type': migration_condition_type,
}]
}
})
except kubernetes.client.api_client as ae:
print(f"{migrate_script} unable to create migration secret, error: {ae}")
print(f"{migrate_script} unable to patch LVMVolumeGroup CRD, error: {ae}")
raise ae


Expand Down

0 comments on commit f3a4bd3

Please sign in to comment.