migrate 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. 'web_port', 'tcp_port', 'udp_port', 'hostNetwork', 'dnsConfig',
  20. 'ownerUID', 'ownerGID', 'environmentVariables', 'cpuLimit', 'memLimit',
  21. 'enableResourceLimits', 'extraAppVolumeMounts', 'appVolumeMounts',
  22. ]
  23. values.update({
  24. # Migrate Network
  25. 'syncthingNetwork': {
  26. 'webPort': values['web_port'],
  27. 'tcpPort': values['tcp_port'],
  28. 'udpPort': values['udp_port'],
  29. 'hostNetwork': values['hostNetwork'],
  30. },
  31. # Migrate Resources
  32. 'resources': {
  33. 'limits': {
  34. 'cpu': values.get('cpuLimit', '4000m'),
  35. 'memory': values.get('memLimit', '8Gi'),
  36. }
  37. },
  38. # Migrate DNS
  39. 'podOptions': {
  40. 'dnsConfig': {
  41. 'options': [
  42. {'name': opt['name'], 'value': opt['value']}
  43. for opt in values.get('dnsConfig', {}).get('options', [])
  44. ]
  45. }
  46. },
  47. # Migrate ID
  48. 'syncthingID': {
  49. 'user': values['ownerUID'],
  50. 'group': values['ownerGID'],
  51. },
  52. # Migrate Config
  53. 'syncthingConfig': {
  54. 'additionalEnvs': values.get('environmentVariables', []),
  55. },
  56. # Migrate Storage
  57. 'syncthingStorage': {
  58. 'config': migrate_volume(values['appVolumeMounts']['config']),
  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 this missing, we have already migrated
  74. if not 'appVolumeMounts' in values.keys():
  75. return values
  76. return migrate_common_lib(values)
  77. if __name__ == '__main__':
  78. if len(sys.argv) != 2:
  79. exit(1)
  80. if os.path.exists(sys.argv[1]):
  81. with open(sys.argv[1], 'r') as f:
  82. print(json.dumps(migrate(json.loads(f.read()))))