migrate 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. 'dnsConfig', 'environmentVariables', 'service', 'enableResourceLimits',
  20. 'memLimit', 'cpuLimit', 'extraAppVolumeMounts', 'appVolumeMounts',
  21. 'runAsGroup', 'runAsUser',
  22. ]
  23. values.update({
  24. # Migrate Network
  25. 'netdataNetwork': {
  26. 'webPort': values['service']['nodePort'],
  27. },
  28. # Migrate Resources
  29. 'resources': {
  30. 'limits': {
  31. 'cpu': values.get('cpuLimit', '4000m'),
  32. 'memory': values.get('memLimit', '8Gi'),
  33. }
  34. },
  35. # Migrate DNS
  36. 'podOptions': {
  37. 'dnsConfig': {
  38. 'options': [
  39. {'name': opt['name'], 'value': opt['value']}
  40. for opt in values.get('dnsConfig', {}).get('options', [])
  41. ]
  42. }
  43. },
  44. # Migrate Config
  45. 'netdataConfig': {
  46. 'additionalEnvs': values.get('environmentVariables', []),
  47. },
  48. # Migrate Storage
  49. 'netdataStorage': {
  50. 'config': migrate_volume(values['appVolumeMounts']['netdataconfig']),
  51. 'cache': migrate_volume(values['appVolumeMounts']['netdatacache']),
  52. 'lib': migrate_volume(values['appVolumeMounts']['netdatalib']),
  53. 'additionalStorages': [
  54. {
  55. 'type': 'hostPath',
  56. 'hostPathConfig': {'hostPath': e['hostPath']},
  57. 'mountPath': e['mountPath'],
  58. 'readOnly': e.get('readOnly', False),
  59. }
  60. for e in values.get('extraAppVolumeMounts', [])
  61. ],
  62. },
  63. })
  64. for k in delete_keys:
  65. values.pop(k, None)
  66. return values
  67. def migrate(values):
  68. # If this missing, we have already migrated
  69. if not 'appVolumeMounts' in values.keys():
  70. return values
  71. return migrate_common_lib(values)
  72. if __name__ == '__main__':
  73. if len(sys.argv) != 2:
  74. exit(1)
  75. if os.path.exists(sys.argv[1]):
  76. with open(sys.argv[1], 'r') as f:
  77. print(json.dumps(migrate(json.loads(f.read()))))