Skip to content

Commit

Permalink
hook improves
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram committed Sep 6, 2024
1 parent cf5df67 commit 6062d0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
33 changes: 18 additions & 15 deletions hooks/convert_bd_names_to_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def main(ctx: hook.Context):
try:
api_v1.delete_namespaced_daemon_set(name=ds_name, namespace=ds_ns)
except kubernetes.client.exceptions.ApiException as e:
if e.status == '404':
if e.status == 404:
pass
except Exception as e:
raise e
Expand All @@ -78,7 +78,8 @@ def main(ctx: hook.Context):
version=version)
print(f"{migrate_script} successfully listed lvmvolumegroup resources")
except kubernetes.client.exceptions.ApiException as e:
if e.status == '404':
# means no lvmvolumegroup resources found
if e.status == 404:
print(f"{migrate_script} no lvmvolumegroup resources found, tries to delete LvmVolumeGroup CRD")
try:
api_extension.delete_custom_resource_definition(crd_name)
Expand All @@ -105,10 +106,10 @@ def main(ctx: hook.Context):
lvg_backup['metadata']['labels']['kubernetes.io/hostname'] = lvg_backup['status']['nodes'][0]['name']
lvg_backup['metadata']['labels'][migration_completed_label] = 'false'
try:
create_or_update_custom_resource(group,
'lvmvolumegroupbackups',
version,
lvg_backup)
create_or_update_custom_resource(group=group,
plural='lvmvolumegroupbackups',
version=version,
resource=lvg_backup)

except Exception as e:
print(f"{migrate_script} unable to create backup, error: {e}")
Expand All @@ -134,7 +135,7 @@ def main(ctx: hook.Context):
try:
api_extension.delete_custom_resource_definition(crd_name)
except kubernetes.client.exceptions.ApiException as e:
if e.status == '404':
if e.status == 404:
print(f"{migrate_script} the LvmVolumeGroup CRD has been already deleted")
pass
except Exception as e:
Expand All @@ -160,10 +161,10 @@ def main(ctx: hook.Context):
for lvg_backup in lvg_backup_list.get('items', []):
lvg = configure_new_lvg(lvg_backup)
try:
create_or_update_custom_resource(group,
'lvmvolumegroups',
version,
lvg)
create_or_update_custom_resource(group=group,
plural='lvmvolumegroups',
version=version,
resource=lvg)
print(f"{migrate_script} LVMVolumeGroup {lvg['metadata']['name']} was created")
except Exception as e:
print(f"{migrate_script} unable to create LVMVolumeGroup {lvg['metadata']['name']}, error: {e}")
Expand Down Expand Up @@ -255,7 +256,7 @@ def main(ctx: hook.Context):
### End of LVMVolumeGroup CRD flow
except kubernetes.client.exceptions.ApiException as e:
### If we do not find any lvmvolumegroup CRD flow
if e.status == '404':
if e.status == 404:
# ничего нет, просто создаем новую CRD и создаем по бэкапам
print(f"{migrate_script} no lvmvolumegroup CRD was found")

Expand Down Expand Up @@ -379,10 +380,11 @@ def create_or_update_custom_resource(group, plural, version, resource):
'finalizers': resource['metadata'][
'finalizers'],
'spec': resource['spec']})
print(f"{migrate_script} {resource['kind']} {resource['metadata']['name']} created")
print(f"{migrate_script} {resource['kind']} {resource['metadata']['name']} updated")
return True
except kubernetes.client.exceptions.ApiException as e:
if e.status == '404':
print(f"{migrate_script} {resource['kind']} {resource['metadata']['name']} was not found, try to create it")
if e.status == 404:
try:
kubernetes.client.CustomObjectsApi().create_cluster_custom_object(group=group,
plural=plural,
Expand All @@ -401,10 +403,11 @@ def create_or_update_custom_resource(group, plural, version, resource):
resource['metadata'][
'finalizers']},
'spec': resource['spec']})
print(f"{migrate_script} {resource['kind']} {resource['metadata']['name']} created")
except Exception as e:
print(
f"{migrate_script} failed to create {resource['kind']} {resource['metadata']['name']} after {max_attempts} attempts, error: {e}")
return False
return False
except Exception as e:
print(
f"{migrate_script} attempt {attempt + 1} failed for {resource['kind']} {resource['metadata']['name']} with message: {e}")
Expand Down
7 changes: 4 additions & 3 deletions images/agent/src/pkg/controller/lvm_volume_group_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,11 @@ func ReconcileUnhealthyLVMVolumeGroups(
}

// take thin-pools from status instead of spec to prevent miss never-created ones
for _, thinPool := range lvg.Status.ThinPools {
for i, thinPool := range lvg.Status.ThinPools {
if candidateTp, exist := candidateTPs[thinPool.Name]; !exist {
log.Warning(fmt.Sprintf("[ReconcileUnhealthyLVMVolumeGroups] the LVMVolumeGroup %s misses its ThinPool %s", lvg.Name, thinPool.Name))
messageBldr.WriteString(fmt.Sprintf("Unable to find ThinPool %s. ", thinPool.Name))
lvg.Status.ThinPools = append(lvg.Status.ThinPools[:i], lvg.Status.ThinPools[i+1:]...)
} else if !utils.AreSizesEqualWithinDelta(candidate.VGSize, thinPool.ActualSize, internal.ResizeDelta) &&
candidateTp.ActualSize.Value()+internal.ResizeDelta.Value() < thinPool.ActualSize.Value() {
// that means thin-pool is not 100%VG space
Expand Down Expand Up @@ -980,9 +981,9 @@ func convertSpecThinPools(thinPools map[string]resource.Quantity) []v1alpha1.LVM
result := make([]v1alpha1.LVMVolumeGroupThinPoolSpec, 0, len(thinPools))
for name, size := range thinPools {
result = append(result, v1alpha1.LVMVolumeGroupThinPoolSpec{
Name: name,
Name: name,
AllocationLimit: "150%",
Size: size.String(),
Size: size.String(),
})
}

Expand Down

0 comments on commit 6062d0d

Please sign in to comment.