index.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import * as awsx from '@pulumi/awsx'
  2. import * as aws from '@pulumi/aws'
  3. import * as eks from '@pulumi/eks'
  4. import * as docker from '@pulumi/docker'
  5. import * as k8s from '@pulumi/kubernetes'
  6. import * as pulumi from '@pulumi/pulumi'
  7. import { CaddyServiceDeployment, CustomPersistentVolume } from 'pulumi-common'
  8. const awsConfig = new pulumi.Config('aws')
  9. const config = new pulumi.Config()
  10. const queryNodeHost = config.require('queryNodeHost')
  11. const wsProviderEndpointURI = config.require('wsProviderEndpointURI')
  12. const configArgusImage = config.require('argusImage')
  13. const lbReady = config.get('isLoadBalancerReady') === 'true'
  14. const keys = config.require('keys')
  15. const buckets = config.require('buckets')
  16. const workerId = config.require('workerId')
  17. const name = 'argus-node'
  18. const isMinikube = config.getBoolean('isMinikube')
  19. const dataStorage = config.getNumber('dataStorage') || 10
  20. const logStorage = config.getNumber('logStorage') || 2
  21. const cacheStorage = config.getNumber('cacheStorage') || 10
  22. export let kubeconfig: pulumi.Output<any>
  23. export let argusImage: pulumi.Output<string> = pulumi.interpolate`${configArgusImage}`
  24. let provider: k8s.Provider
  25. if (isMinikube) {
  26. provider = new k8s.Provider('local', {})
  27. } else {
  28. // Create a VPC for our cluster.
  29. const vpc = new awsx.ec2.Vpc('argus-vpc', { numberOfAvailabilityZones: 2, numberOfNatGateways: 1 })
  30. // Create an EKS cluster with the default configuration.
  31. const cluster = new eks.Cluster('eksctl-argus-node', {
  32. vpcId: vpc.id,
  33. subnetIds: vpc.publicSubnetIds,
  34. desiredCapacity: 2,
  35. maxSize: 2,
  36. instanceType: 't2.medium',
  37. providerCredentialOpts: {
  38. profileName: awsConfig.get('profile'),
  39. },
  40. })
  41. provider = cluster.provider
  42. // Export the cluster's kubeconfig.
  43. kubeconfig = cluster.kubeconfig
  44. // Create a repository
  45. const repo = new awsx.ecr.Repository('distributor-node')
  46. // Build an image and publish it to our ECR repository.
  47. argusImage = repo.buildAndPushImage({
  48. context: './docker_dummy',
  49. dockerfile: './docker_dummy/Dockerfile',
  50. args: { SOURCE_IMAGE: argusImage! },
  51. })
  52. // Uncomment the below line to use an existing image
  53. // argusImage = pulumi.interpolate`ahhda/distributor-node:latest`
  54. }
  55. const resourceOptions = { provider: provider }
  56. // Create a Kubernetes Namespace
  57. const ns = new k8s.core.v1.Namespace(name, {}, resourceOptions)
  58. // Export the Namespace name
  59. export const namespaceName = ns.metadata.name
  60. const appLabels = { appClass: name }
  61. const dataPVC = new CustomPersistentVolume(
  62. 'data',
  63. { namespaceName: namespaceName, storage: dataStorage },
  64. resourceOptions
  65. )
  66. const logsPVC = new CustomPersistentVolume(
  67. 'logs',
  68. { namespaceName: namespaceName, storage: logStorage },
  69. resourceOptions
  70. )
  71. const cachePVC = new CustomPersistentVolume(
  72. 'cache',
  73. { namespaceName: namespaceName, storage: cacheStorage },
  74. resourceOptions
  75. )
  76. // Create a Deployment
  77. const deployment = new k8s.apps.v1.Deployment(
  78. name,
  79. {
  80. metadata: {
  81. namespace: namespaceName,
  82. labels: appLabels,
  83. },
  84. spec: {
  85. replicas: 1,
  86. selector: { matchLabels: appLabels },
  87. template: {
  88. metadata: {
  89. labels: appLabels,
  90. },
  91. spec: {
  92. containers: [
  93. {
  94. name: 'argus',
  95. image: argusImage,
  96. imagePullPolicy: 'IfNotPresent',
  97. workingDir: '/joystream/distributor-node',
  98. env: [
  99. {
  100. name: 'JOYSTREAM_DISTRIBUTOR__ENDPOINTS__QUERY_NODE',
  101. value: queryNodeHost,
  102. },
  103. {
  104. name: 'JOYSTREAM_DISTRIBUTOR__ENDPOINTS__JOYSTREAM_NODE_WS',
  105. value: wsProviderEndpointURI,
  106. },
  107. {
  108. name: 'JOYSTREAM_DISTRIBUTOR__KEYS',
  109. value: keys,
  110. },
  111. {
  112. name: 'JOYSTREAM_DISTRIBUTOR__BUCKETS',
  113. value: buckets,
  114. },
  115. {
  116. name: 'JOYSTREAM_DISTRIBUTOR__WORKER_ID',
  117. value: workerId,
  118. },
  119. {
  120. name: 'JOYSTREAM_DISTRIBUTOR__PORT',
  121. value: '3334',
  122. },
  123. ],
  124. args: ['start'],
  125. ports: [{ containerPort: 3334 }],
  126. volumeMounts: [
  127. {
  128. name: 'data',
  129. mountPath: '/data',
  130. subPath: 'data',
  131. },
  132. {
  133. name: 'logs',
  134. mountPath: '/logs',
  135. subPath: 'logs',
  136. },
  137. {
  138. name: 'cache',
  139. mountPath: '/cache',
  140. subPath: 'cache',
  141. },
  142. ],
  143. },
  144. ],
  145. volumes: [
  146. {
  147. name: 'data',
  148. persistentVolumeClaim: {
  149. claimName: dataPVC.pvc.metadata.name,
  150. },
  151. },
  152. {
  153. name: 'logs',
  154. persistentVolumeClaim: {
  155. claimName: logsPVC.pvc.metadata.name,
  156. },
  157. },
  158. {
  159. name: 'cache',
  160. persistentVolumeClaim: {
  161. claimName: cachePVC.pvc.metadata.name,
  162. },
  163. },
  164. ],
  165. },
  166. },
  167. },
  168. },
  169. resourceOptions
  170. )
  171. // Create a LoadBalancer Service for the Deployment
  172. const service = new k8s.core.v1.Service(
  173. name,
  174. {
  175. metadata: {
  176. labels: appLabels,
  177. namespace: namespaceName,
  178. name: name,
  179. },
  180. spec: {
  181. type: isMinikube ? 'NodePort' : 'ClusterIP',
  182. ports: [{ name: 'port-1', port: 3334 }],
  183. selector: appLabels,
  184. },
  185. },
  186. resourceOptions
  187. )
  188. // Export the Service name
  189. export const serviceName = service.metadata.name
  190. // Export the Deployment name
  191. export const deploymentName = deployment.metadata.name
  192. export let endpoint1: pulumi.Output<string> = pulumi.interpolate``
  193. export let endpoint2: pulumi.Output<string> = pulumi.interpolate``
  194. const caddyEndpoints = [
  195. ` {
  196. reverse_proxy ${name}:3334
  197. }`,
  198. ]
  199. if (!isMinikube) {
  200. const caddy = new CaddyServiceDeployment(
  201. 'caddy-proxy',
  202. { lbReady, namespaceName: namespaceName, caddyEndpoints },
  203. resourceOptions
  204. )
  205. endpoint1 = pulumi.interpolate`${caddy.primaryEndpoint}`
  206. endpoint2 = pulumi.interpolate`${caddy.secondaryEndpoint}`
  207. }