migrate 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python3
  2. import json
  3. import os
  4. import sys
  5. def migrate_volume(volume):
  6. return {
  7. 'type': 'hostPath',
  8. 'hostPathConfig': {
  9. 'hostPath': volume['hostPath']
  10. },
  11. } if volume.get('hostPathEnabled', False) else {
  12. 'type': 'ixVolume',
  13. 'ixVolumeConfig': {
  14. 'datasetName': volume['datasetName'],
  15. },
  16. }
  17. def migrate_common_lib(values):
  18. delete_keys = [
  19. 'enableResourceLimits', 'memLimit', 'cpuLimit', 'dnsConfig',
  20. 'web_port', 'environmentVariables', 'timezone', 'password',
  21. 'extraAppVolumeMounts', 'appVolumeMounts', 'dhcp', 'dhcp_start',
  22. 'dhcp_end', 'dhcp_gateway', 'ownerUID', 'ownerGID',
  23. ]
  24. values.update({
  25. # Migrate Network
  26. 'piholeNetwork': {
  27. 'webPort': values['web_port'],
  28. 'dhcp': {
  29. 'enabled': values['dhcp'],
  30. 'start': values.get('dhcp_start', ''),
  31. 'end': values.get('dhcp_end', ''),
  32. 'gateway': values.get('dhcp_gateway', ''),
  33. }
  34. },
  35. # Migrate Resources
  36. 'resources': {
  37. 'limits': {
  38. 'cpu': values.get('cpuLimit', '4000m'),
  39. 'memory': values.get('memLimit', '8Gi'),
  40. }
  41. },
  42. # Migrate DNS
  43. 'podOptions': {
  44. 'dnsConfig': {
  45. 'options': [
  46. {'name': opt['name'], 'value': opt['value']}
  47. for opt in values.get('dnsConfig', {}).get('options', [])
  48. ]
  49. }
  50. },
  51. # Migrate Config
  52. 'TZ': values['timezone'],
  53. 'piholeConfig': {
  54. 'webPassword': values['password'],
  55. 'additionalEnvs': values.get('environmentVariables', []),
  56. },
  57. # Migrate Storage
  58. 'piholeStorage': {
  59. 'config': migrate_volume(values['appVolumeMounts']['config']),
  60. 'cache': migrate_volume(values['appVolumeMounts']['dnsmasq']),
  61. 'additionalStorages': [
  62. {
  63. 'type': 'hostPath',
  64. 'hostPathConfig': {'hostPath': e['hostPath']},
  65. 'mountPath': e['mountPath'],
  66. 'readOnly': e.get('readOnly', False),
  67. }
  68. for e in values.get('extraAppVolumeMounts', [])
  69. ],
  70. },
  71. })
  72. for k in delete_keys:
  73. values.pop(k, None)
  74. return values
  75. def migrate(values):
  76. # If this missing, we have already migrated
  77. if not 'appVolumeMounts' in values.keys():
  78. return values
  79. return migrate_common_lib(values)
  80. if __name__ == '__main__':
  81. if len(sys.argv) != 2:
  82. exit(1)
  83. if os.path.exists(sys.argv[1]):
  84. with open(sys.argv[1], 'r') as f:
  85. print(json.dumps(migrate(json.loads(f.read()))))