index.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import * as awsx from '@pulumi/awsx'
  2. import * as eks from '@pulumi/eks'
  3. import * as k8s from '@pulumi/kubernetes'
  4. import * as pulumi from '@pulumi/pulumi'
  5. import { CaddyServiceDeployment } from 'pulumi-common'
  6. import { MongoDBServiceDeployment } from './mongo'
  7. const awsConfig = new pulumi.Config('aws')
  8. const config = new pulumi.Config()
  9. const name = 'orion'
  10. const queryNodeHost = config.require('queryNodeEndpoint')
  11. const lbReady = config.get('isLoadBalancerReady') === 'true'
  12. const orionImage = config.get('orionImage') || `joystream/orion:latest`
  13. const contentSecret = config.require('contentSecret')
  14. const storage = parseInt(config.get('storage') || '40')
  15. const isMinikube = config.getBoolean('isMinikube')
  16. export let kubeconfig: pulumi.Output<any>
  17. let provider: k8s.Provider
  18. if (isMinikube) {
  19. provider = new k8s.Provider('local', {})
  20. } else {
  21. // Create a VPC for our cluster.
  22. const vpc = new awsx.ec2.Vpc('orion-vpc', { numberOfAvailabilityZones: 2, numberOfNatGateways: 1 })
  23. // Create an EKS cluster with the default configuration.
  24. const cluster = new eks.Cluster('eksctl-orion-node', {
  25. vpcId: vpc.id,
  26. subnetIds: vpc.publicSubnetIds,
  27. instanceType: 't2.medium',
  28. providerCredentialOpts: {
  29. profileName: awsConfig.get('profile'),
  30. },
  31. })
  32. provider = cluster.provider
  33. // Export the cluster's kubeconfig.
  34. kubeconfig = cluster.kubeconfig
  35. }
  36. const resourceOptions = { provider: provider }
  37. // Create a Kubernetes Namespace
  38. const ns = new k8s.core.v1.Namespace(name, {}, resourceOptions)
  39. // Export the Namespace name
  40. export const namespaceName = ns.metadata.name
  41. const appLabels = { appClass: name }
  42. const mongoDb = new MongoDBServiceDeployment(
  43. 'mongo-db',
  44. {
  45. namespaceName: namespaceName,
  46. storage: storage,
  47. },
  48. resourceOptions
  49. )
  50. // Create a Deployment
  51. const deployment = new k8s.apps.v1.Deployment(
  52. name,
  53. {
  54. metadata: {
  55. namespace: namespaceName,
  56. labels: appLabels,
  57. },
  58. spec: {
  59. replicas: 1,
  60. selector: { matchLabels: appLabels },
  61. template: {
  62. metadata: {
  63. labels: appLabels,
  64. },
  65. spec: {
  66. containers: [
  67. {
  68. name: 'orion',
  69. image: orionImage,
  70. imagePullPolicy: 'IfNotPresent',
  71. env: [
  72. {
  73. name: 'ORION_PORT',
  74. value: '6116',
  75. },
  76. {
  77. name: 'ORION_MONGO_HOSTNAME',
  78. value: mongoDb.service.metadata.name,
  79. },
  80. {
  81. name: 'ORION_FEATURED_CONTENT_SECRET',
  82. value: contentSecret,
  83. },
  84. {
  85. name: 'ORION_QUERY_NODE_URL',
  86. value: queryNodeHost,
  87. },
  88. ],
  89. ports: [{ containerPort: 6116 }],
  90. },
  91. ],
  92. },
  93. },
  94. },
  95. },
  96. resourceOptions
  97. )
  98. // Create a LoadBalancer Service for the Deployment
  99. const service = new k8s.core.v1.Service(
  100. name,
  101. {
  102. metadata: {
  103. labels: appLabels,
  104. namespace: namespaceName,
  105. name: 'orion-node',
  106. },
  107. spec: {
  108. type: isMinikube ? 'NodePort' : 'ClusterIP',
  109. ports: [{ name: 'port-1', port: 6116 }],
  110. selector: appLabels,
  111. },
  112. },
  113. resourceOptions
  114. )
  115. // Export the Service name
  116. export const serviceName = service.metadata.name
  117. // Export the Deployment name
  118. export const deploymentName = deployment.metadata.name
  119. const caddyEndpoints = [
  120. ` {
  121. reverse_proxy orion-node:6116
  122. }`,
  123. ]
  124. export let endpoint1: pulumi.Output<string> = pulumi.interpolate``
  125. export let endpoint2: pulumi.Output<string> = pulumi.interpolate``
  126. if (!isMinikube) {
  127. const caddy = new CaddyServiceDeployment(
  128. 'caddy-proxy',
  129. { lbReady, namespaceName: namespaceName, caddyEndpoints },
  130. resourceOptions
  131. )
  132. endpoint1 = pulumi.interpolate`${caddy.primaryEndpoint}`
  133. endpoint2 = pulumi.interpolate`${caddy.secondaryEndpoint}`
  134. }