import * as pulumi from "@pulumi/pulumi" ; import * as gcp from "@pulumi/gcp" ; import * as k8s from "@pulumi/kubernetes" ; import * as random from "@pulumi/random" ; import * as config from "./config" ; const name = config . envName ; const defaultClusterOptions = { nodeCount : 3 , nodeVersion : "latest" , nodeMachineType : "n1-standard-1" , minMasterVersion : "latest" , masterUsername : "admin" , masterPassword : new random . RandomString ( "password" , { length : 16 , special : true , } ) . result , } ; const network = new gcp . compute . Network ( name , { project : config . project , autoCreateSubnetworks : false , } ) ; const subnet = new gcp . compute . Subnetwork ( name , { project : config . project , region : pulumi . output ( config . zone ) . apply ( zone => ( < string > zone ) . split ( "-" ) . slice ( 0 , 2 ) . join ( "-" ) ) , ipCidrRange : "10.0.0.0/24" , network : network . name , secondaryIpRanges : [ { rangeName : "pods" , ipCidrRange : "10.1.0.0/16" } ] , } ) ; const cluster = new gcp . container . Cluster ( name , { project : config . project , zone : config . zone , initialNodeCount : config . nodeCount || defaultClusterOptions . nodeCount , nodeVersion : defaultClusterOptions . nodeVersion , minMasterVersion : defaultClusterOptions . minMasterVersion , masterAuth : { username : defaultClusterOptions . masterUsername , password : defaultClusterOptions . masterPassword , } , network : network . name , subnetwork : subnet . name , nodeConfig : { machineType : config . nodeMachineType || defaultClusterOptions . nodeMachineType , oauthScopes : [ "https://www.googleapis.com/auth/compute" , "https://www.googleapis.com/auth/devstorage.read_only" , "https://www.googleapis.com/auth/logging.write" , "https://www.googleapis.com/auth/monitoring" , ] , } , } ) ; function createKubeconfig ( gkeCluster ) { return pulumi . all ( [ gkeCluster . name , gkeCluster . endpoint , gkeCluster . masterAuth ] ) . apply ( ( [ name , endpoint , auth ] ) => { const context = `${ gcp . config . project }_${ gcp . config . zone }_${ name }` ; return `apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: ${ auth . clusterCaCertificate }
    server: https://${ endpoint }
  name: ${ context }
contexts:
- context:
    cluster: ${ context }
    user: ${ context }
  name: ${ context }
current-context: ${ context }
kind: Config
preferences: {}
users:
- name: ${ context }
  user:
    auth-provider:
      config:
        cmd-args: config config-helper --format=json
        cmd-path: gcloud
        expiry-key: '{.credential.token_expiry}'
        token-key: '{.credential.access_token}'
      name: gcp
` ; } ) ; } function createProvider ( name , k8sConfig ) { return new k8s . Provider ( name , { kubeconfig : k8sConfig , } ) ; } export const kubeconfig = createKubeconfig ( cluster ) ;