Last active
October 7, 2018 01:43
-
-
Save geekflyer/b78adab2667d8526a1dd593bc5c844bf to your computer and use it in GitHub Desktop.
pulumi gke utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const packageName = require(__dirname + '/../package.json').name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getCluster } from '@pulumi/gcp/container'; | |
import * as k8s from '@pulumi/kubernetes'; | |
import * as pulumi from '@pulumi/pulumi'; | |
import { defaultProject, defaultZone } from './gcp-constants'; | |
import { packageConfig } from './misc'; | |
import { memoize } from 'lodash'; | |
const gcpConfig = new pulumi.Config('gcp'); | |
// most functions in this module, especially the ones which perform I/O, are memoized. This is to improve lookup speed in bigger pulumi programs which deploy muliple apps AND | |
// to avoid resource id conflicts in pulumi when importing the same cluster / resource multiple times. | |
export const getClusterDataByName = memoize((clusterName: string) => { | |
return getCluster({ | |
name: clusterName, | |
project: gcpConfig.get('project') || defaultProject, | |
zone: gcpConfig.get('zone') || defaultZone | |
}); | |
}); | |
export const getK8sProviderByClusterName: (clusterName: string) => k8s.Provider = memoize((clusterName: string) => { | |
return new k8s.Provider(clusterName, { | |
kubeconfig: getClusterDataByName(clusterName).then(({ endpoint, masterAuths }) => | |
createKubectlConfig({ endpoint, clusterCaCertificate: masterAuths[0].clusterCaCertificate }) | |
) | |
}); | |
}); | |
export const getK8sProviderFromInferredCluster = memoize(() => | |
getK8sProviderByClusterName(packageConfig.require('cluster')) | |
); | |
export function createKubectlConfig({ | |
endpoint, | |
clusterCaCertificate | |
}: { | |
endpoint: string; | |
clusterCaCertificate: string; | |
}) { | |
const context = `dynamic-context`; | |
return `apiVersion: v1 | |
clusters: | |
- cluster: | |
certificate-authority-data: ${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 | |
`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as pulumi from '@pulumi/pulumi'; | |
import { packageName } from './constants'; | |
export const context = { | |
get inferredEnvironment(): string { | |
return pulumi.runtime | |
.getStack()! | |
.split('-') | |
.pop() as string; | |
}, | |
get inferredNamespace(): string { | |
return `solvvy-${this.inferredEnvironment}`; | |
} | |
}; | |
export const packageConfig = new pulumi.Config(packageName); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment