migrate 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/python3
  2. import json
  3. import os
  4. import sys
  5. def migrate_common_lib(values):
  6. delete_keys = [
  7. 'wgUDPPort', 'webUIPort', 'hostNetwork', 'cpuLimit', 'memLimit',
  8. 'dnsConfig', 'environmentVariables', 'appVolumeMounts',
  9. 'extraAppVolumeMounts', 'wgeasy', 'enableResourceLimits',
  10. ]
  11. values.update({
  12. # Migrate Network
  13. 'wgNetwork': {
  14. 'udpPort': values['wgUDPPort'],
  15. 'webPort': values['webUIPort'],
  16. 'hostNetwork': values['hostNetwork'],
  17. },
  18. # Migrate Resources
  19. 'resources': {
  20. 'limits': {
  21. 'cpu': values.get('cpuLimit', '4000m'),
  22. 'memory': values.get('memLimit', '8Gi'),
  23. }
  24. },
  25. # Migrate DNS
  26. 'podOptions': {
  27. 'dnsConfig': {
  28. 'options': [
  29. {'name': opt['name'], 'value': opt['value']}
  30. for opt in values.get('dnsConfig', {}).get('options', [])
  31. ]
  32. }
  33. },
  34. # Migrate Config
  35. 'wgConfig': {
  36. 'host': values['wgeasy']['host'],
  37. 'externalPort': values.get('wgUDPPort', 30057),
  38. 'password': values['wgeasy'].get('password', ''),
  39. 'keepAlive': values['wgeasy']['keep_alive'],
  40. 'clientMTU': values['wgeasy']['client_mtu'],
  41. 'clientAddressRange': values['wgeasy']['client_address_range'],
  42. 'clientDNSServer': values['wgeasy']['client_dns_server'],
  43. 'allowedIPs': values['wgeasy']['allowed_ips'],
  44. 'additionalEnvs': values.get('environmentVariables', []),
  45. },
  46. # Migrate Storage
  47. 'wgStorage': {
  48. 'config': {
  49. 'type': 'hostPath',
  50. 'hostPathConfig': {
  51. 'hostPath': values['appVolumeMounts']['config']['hostPath']
  52. },
  53. } if values['appVolumeMounts']['config']['hostPathEnabled'] else {
  54. 'type': 'ixVolume',
  55. 'ixVolumeConfig': {
  56. 'datasetName': values['appVolumeMounts']['config']['datasetName'],
  57. },
  58. },
  59. 'additionalStorages': [
  60. {
  61. 'type': 'hostPath',
  62. 'hostPathConfig': {'hostPath': e['hostPath']},
  63. 'mountPath': e['mountPath'],
  64. }
  65. for e in values.get('extraAppVolumeMounts', [])
  66. ],
  67. },
  68. })
  69. for k in delete_keys:
  70. values.pop(k, None)
  71. return values
  72. def migrate(values):
  73. # If we have migrated...
  74. if 'wgConfig' in values.keys():
  75. # Make sure the externalPort is not missing.
  76. if not values['wgConfig'].get('externalPort', None):
  77. values['wgConfig']['externalPort'] = values['wgNetwork'].get('udpPort', 30057)
  78. return values
  79. # If this key is missing, we have already migrated.
  80. if 'wgeasy' not in values.keys():
  81. return values
  82. return migrate_common_lib(values)
  83. if __name__ == '__main__':
  84. if len(sys.argv) != 2:
  85. exit(1)
  86. if os.path.exists(sys.argv[1]):
  87. with open(sys.argv[1], 'r') as f:
  88. print(json.dumps(migrate(json.loads(f.read()))))