|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as kx from '@pulumi/kubernetesx'; |
| 4 | +import * as pulumi from '@pulumi/pulumi'; |
| 5 | +import { serviceLocalEndpoint } from '../utils/local-endpoint'; |
| 6 | +import { ServiceSecret } from '../utils/secrets'; |
| 7 | +import { ServiceDeployment } from '../utils/service-deployment'; |
| 8 | +import { type Docker } from './docker'; |
| 9 | +import { type Environment } from './environment'; |
| 10 | +import { type GraphQL } from './graphql'; |
| 11 | +import { type Observability } from './observability'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Hive Gateway Docker Image Version |
| 15 | + * Bump this to update the used gateway version. |
| 16 | + */ |
| 17 | +const dockerImage = 'ghcr.io/graphql-hive/gateway:1.13.6'; |
| 18 | + |
| 19 | +const gatewayConfigDirectory = path.resolve( |
| 20 | + __dirname, |
| 21 | + '..', |
| 22 | + 'config', |
| 23 | + 'public-graphql-api-gateway', |
| 24 | +); |
| 25 | +const gatewayConfigPath = path.join(gatewayConfigDirectory, 'gateway.config.ts'); |
| 26 | +// On global scope to fail early in case of a read error |
| 27 | +const gwConfigFile = fs.readFileSync(gatewayConfigPath, 'utf-8'); |
| 28 | + |
| 29 | +export function deployPublicGraphQLAPIGateway(args: { |
| 30 | + environment: Environment; |
| 31 | + graphql: GraphQL; |
| 32 | + docker: Docker; |
| 33 | + observability: Observability; |
| 34 | +}) { |
| 35 | + const apiConfig = new pulumi.Config('api'); |
| 36 | + |
| 37 | + // Note: The persisted documents cdn endpoint can also be used for reading the contract schema |
| 38 | + const cdnEndpoint = |
| 39 | + apiConfig.requireObject<Record<string, string>>('env')['HIVE_PERSISTED_DOCUMENTS_CDN_ENDPOINT']; |
| 40 | + |
| 41 | + if (!cdnEndpoint) { |
| 42 | + throw new Error("Missing cdn endpoint variable 'HIVE_PERSISTED_DOCUMENTS_CDN_ENDPOINT'."); |
| 43 | + } |
| 44 | + |
| 45 | + const supergraphEndpoint = cdnEndpoint + '/contracts/public'; |
| 46 | + |
| 47 | + // Note: The persisted documents access key is also valid for reading the supergraph |
| 48 | + const publicGraphQLAPISecret = new ServiceSecret('public-graphql-api-secret', { |
| 49 | + cdnAccessKeyId: apiConfig.requireSecret('hivePersistedDocumentsCdnAccessKeyId'), |
| 50 | + }); |
| 51 | + |
| 52 | + const configMap = new kx.ConfigMap('public-graphql-api-gateway-config', { |
| 53 | + data: { |
| 54 | + 'gateway.config.ts': gwConfigFile, |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + return new ServiceDeployment( |
| 59 | + 'public-graphql-api-gateway', |
| 60 | + { |
| 61 | + imagePullSecret: args.docker.secret, |
| 62 | + image: dockerImage, |
| 63 | + replicas: args.environment.isProduction ? 3 : 1, |
| 64 | + availabilityOnEveryNode: true, |
| 65 | + env: { |
| 66 | + GRAPHQL_SERVICE_ENDPOINT: serviceLocalEndpoint(args.graphql.service).apply( |
| 67 | + value => `${value}/graphql-public`, |
| 68 | + ), |
| 69 | + SUPERGRAPH_ENDPOINT: supergraphEndpoint, |
| 70 | + OPENTELEMETRY_COLLECTOR_ENDPOINT: args.observability.tracingEndpoint ?? '', |
| 71 | + }, |
| 72 | + port: 4000, |
| 73 | + args: ['-c', '/config/gateway.config.ts', 'supergraph'], |
| 74 | + volumes: [ |
| 75 | + { |
| 76 | + name: 'gateway-config', |
| 77 | + configMap: { |
| 78 | + name: configMap.metadata.name, |
| 79 | + }, |
| 80 | + }, |
| 81 | + ], |
| 82 | + volumeMounts: [ |
| 83 | + { |
| 84 | + mountPath: '/config/', |
| 85 | + name: 'gateway-config', |
| 86 | + readOnly: true, |
| 87 | + }, |
| 88 | + ], |
| 89 | + readinessProbe: '/readiness', |
| 90 | + livenessProbe: '/healthcheck', |
| 91 | + startupProbe: { |
| 92 | + endpoint: '/healthcheck', |
| 93 | + initialDelaySeconds: 60, |
| 94 | + failureThreshold: 10, |
| 95 | + periodSeconds: 15, |
| 96 | + timeoutSeconds: 15, |
| 97 | + }, |
| 98 | + }, |
| 99 | + [args.graphql.deployment, args.graphql.service], |
| 100 | + ) |
| 101 | + .withSecret('HIVE_CDN_ACCESS_TOKEN', publicGraphQLAPISecret, 'cdnAccessKeyId') |
| 102 | + .deploy(); |
| 103 | +} |
| 104 | + |
| 105 | +export type PublicGraphQLAPIGateway = ReturnType<typeof deployPublicGraphQLAPIGateway>; |
0 commit comments