index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import * as awsx from '@pulumi/awsx'
  2. import * as eks from '@pulumi/eks'
  3. import * as docker from '@pulumi/docker'
  4. import * as pulumi from '@pulumi/pulumi'
  5. import { ConfigMapFromFile } from './configMap'
  6. import * as k8s from '@pulumi/kubernetes'
  7. import { IndexerServiceDeployment } from './indexerDeployment'
  8. import { ProcessorServiceDeployment } from './processorDeployment'
  9. import { CaddyServiceDeployment } from 'pulumi-common'
  10. const config = new pulumi.Config()
  11. const awsConfig = new pulumi.Config('aws')
  12. const isMinikube = config.getBoolean('isMinikube')
  13. const externalIndexerUrl = config.get('externalIndexerUrl')
  14. const appsImage = config.get('appsImage') || `joystream/apps:latest`
  15. const skipProcessor = config.getBoolean('skipProcessor')
  16. const useLocalRepo = config.getBoolean('useLocalRepo')
  17. export let kubeconfig: pulumi.Output<any>
  18. export let joystreamAppsImage: pulumi.Output<string> = pulumi.interpolate`${appsImage}`
  19. let provider: k8s.Provider
  20. if (skipProcessor && externalIndexerUrl) {
  21. pulumi.log.info('No Indexer or Processor will be deployed only the cluster')
  22. }
  23. if (isMinikube) {
  24. provider = new k8s.Provider('local', {})
  25. if (useLocalRepo) {
  26. // Use already existing image in minikube environment
  27. joystreamAppsImage = pulumi.interpolate`${appsImage}`
  28. } else {
  29. // Access image from docker hub
  30. joystreamAppsImage = new docker.RemoteImage('apps', {
  31. name: appsImage!,
  32. }).name
  33. }
  34. } else {
  35. // Create a VPC for our cluster.
  36. const vpc = new awsx.ec2.Vpc('query-node-vpc', { numberOfAvailabilityZones: 2, numberOfNatGateways: 1 })
  37. // Create an EKS cluster with the default configuration.
  38. const cluster = new eks.Cluster('eksctl-query-node', {
  39. vpcId: vpc.id,
  40. subnetIds: vpc.publicSubnetIds,
  41. desiredCapacity: 3,
  42. maxSize: 3,
  43. instanceType: 't2.large',
  44. providerCredentialOpts: {
  45. profileName: awsConfig.get('profile'),
  46. },
  47. })
  48. provider = cluster.provider
  49. // Export the cluster's kubeconfig.
  50. kubeconfig = cluster.kubeconfig
  51. // Only deploy ECR and push image if we need to deploy processor from
  52. // local image build.
  53. if (!skipProcessor && useLocalRepo) {
  54. // Create a repository
  55. const repo = new awsx.ecr.Repository('joystream/apps')
  56. // Build an image from an existing local/docker hub image and push to ECR
  57. joystreamAppsImage = repo.buildAndPushImage({
  58. context: './docker_dummy',
  59. dockerfile: './docker_dummy/Dockerfile',
  60. args: { SOURCE_IMAGE: appsImage },
  61. })
  62. }
  63. }
  64. const resourceOptions = { provider: provider }
  65. const name = 'query-node'
  66. // Create a Kubernetes Namespace
  67. const ns = new k8s.core.v1.Namespace(name, {}, resourceOptions)
  68. // Export the Namespace name
  69. export const namespaceName = ns.metadata.name
  70. const defsConfig = new ConfigMapFromFile(
  71. 'defs-config',
  72. {
  73. filePath: '../../../types/augment/all/defs.json',
  74. namespaceName: namespaceName,
  75. },
  76. resourceOptions
  77. ).configName
  78. if (!externalIndexerUrl) {
  79. const indexer = new IndexerServiceDeployment(
  80. 'indexer',
  81. { namespaceName, storage: 10, defsConfig, joystreamAppsImage },
  82. resourceOptions
  83. )
  84. }
  85. if (!skipProcessor) {
  86. const processor = new ProcessorServiceDeployment(
  87. 'processor',
  88. { namespaceName, storage: 10, defsConfig, joystreamAppsImage, externalIndexerUrl },
  89. resourceOptions
  90. )
  91. }
  92. const caddyEndpoints = [
  93. `/indexer* {
  94. uri strip_prefix /indexer
  95. reverse_proxy indexer:4000
  96. }`,
  97. `/server* {
  98. uri strip_prefix /server
  99. reverse_proxy graphql-server:8081
  100. }`,
  101. `/@apollographql/* {
  102. reverse_proxy graphql-server:8081
  103. }`,
  104. ]
  105. const lbReady = config.get('isLoadBalancerReady') === 'true'
  106. export let endpoint1: pulumi.Output<string>
  107. export let endpoint2: pulumi.Output<string>
  108. if (!isMinikube) {
  109. const caddy = new CaddyServiceDeployment(
  110. 'caddy-proxy',
  111. { lbReady, namespaceName: namespaceName, isMinikube, caddyEndpoints },
  112. resourceOptions
  113. )
  114. endpoint1 = pulumi.interpolate`${caddy.primaryEndpoint}`
  115. endpoint2 = pulumi.interpolate`${caddy.secondaryEndpoint}`
  116. }