|
| 1 | +import * as gcp from '@pulumi/gcp' |
| 2 | +import * as k8s from '@pulumi/kubernetes' |
| 3 | +import * as pulumi from '@pulumi/pulumi' |
| 4 | + |
| 5 | +import * as os from 'os' |
| 6 | +import { URL } from 'url' |
| 7 | + |
| 8 | +import { clusterConfig, gcloudConfig } from './config' |
| 9 | + |
| 10 | +const name = clusterConfig.name || `${os.userInfo().username}-sourcegraph-test` |
| 11 | + |
| 12 | +const cluster = new gcp.container.Cluster(name, { |
| 13 | + // Don't auto-generate a name iff the user explicitly defined one. |
| 14 | + name: clusterConfig.name, |
| 15 | + |
| 16 | + description: 'Scratch cluster used for testing sourcegraph/deploy-sourcegraph', |
| 17 | + |
| 18 | + location: gcloudConfig.location, |
| 19 | + project: gcloudConfig.project, |
| 20 | + |
| 21 | + initialNodeCount: clusterConfig.nodeCount, |
| 22 | + removeDefaultNodePool: true, |
| 23 | + |
| 24 | + nodeConfig: { |
| 25 | + diskType: 'pd-ssd', |
| 26 | + localSsdCount: 1, |
| 27 | + machineType: clusterConfig.machineType, |
| 28 | + |
| 29 | + oauthScopes: [ |
| 30 | + 'https://www.googleapis.com/auth/compute', |
| 31 | + 'https://www.googleapis.com/auth/devstorage.read_only', |
| 32 | + 'https://www.googleapis.com/auth/logging.write', |
| 33 | + 'https://www.googleapis.com/auth/monitoring', |
| 34 | + ], |
| 35 | + }, |
| 36 | +}) |
| 37 | + |
| 38 | +export const clusterContext = pulumi |
| 39 | + .all([cluster.name, cluster.location, cluster.project]) |
| 40 | + .apply(([name, location, project]) => `gke_${project}_${location}_${name}`) |
| 41 | + |
| 42 | +export const kubeconfig = pulumi |
| 43 | + .all([clusterContext, cluster.endpoint, cluster.masterAuth]) |
| 44 | + .apply(([context, endpoint, masterAuth]) => { |
| 45 | + return `apiVersion: v1 |
| 46 | +clusters: |
| 47 | +- cluster: |
| 48 | + certificate-authority-data: ${masterAuth.clusterCaCertificate} |
| 49 | + server: https://${endpoint} |
| 50 | + name: ${context} |
| 51 | +contexts: |
| 52 | +- context: |
| 53 | + cluster: ${context} |
| 54 | + user: ${context} |
| 55 | + name: ${context} |
| 56 | +current-context: ${context} |
| 57 | +kind: Config |
| 58 | +preferences: {} |
| 59 | +users: |
| 60 | +- name: ${context} |
| 61 | + user: |
| 62 | + auth-provider: |
| 63 | + config: |
| 64 | + cmd-args: config config-helper --format=json |
| 65 | + cmd-path: gcloud |
| 66 | + expiry-key: '{.credential.token_expiry}' |
| 67 | + token-key: '{.credential.access_token}' |
| 68 | + name: gcp |
| 69 | +` |
| 70 | + }) |
| 71 | + |
| 72 | +export const gcloudAuthCommand = pulumi |
| 73 | + .all([cluster.name, cluster.location, cluster.project]) |
| 74 | + .apply( |
| 75 | + ([name, location, project]) => |
| 76 | + `gcloud container clusters get-credentials ${name} --location ${location} --project ${project}` |
| 77 | + ) |
| 78 | + |
| 79 | +export const gcpURL = pulumi |
| 80 | + .all([cluster.name, cluster.location, cluster.project]) |
| 81 | + .apply(([name, location, project]) => { |
| 82 | + const url = new URL(`${location}/${name}`, 'https://console.cloud.google.com/kubernetes/clusters/details/') |
| 83 | + url.searchParams.set('project', project) |
| 84 | + return url.toString() |
| 85 | + }) |
| 86 | + |
| 87 | +export const k8sProvider = new k8s.Provider(name, { |
| 88 | + kubeconfig, |
| 89 | +}) |
0 commit comments