Skip to content

Instantly share code, notes, and snippets.

@kooba
Created February 1, 2019 12:09
Show Gist options
  • Save kooba/e30276d3ff92b13079f76bd45ef935ae to your computer and use it in GitHub Desktop.
Save kooba/e30276d3ff92b13079f76bd45ef935ae to your computer and use it in GitHub Desktop.
Development in the Cloud
const kubernetes = require('@kubernetes/client-node');
const yaml = require('js-yaml');
const k8sClient = kubernetes.Config.defaultClient();
const BRIGADE_NAMESPACE = 'brigade';
const createEnvironmentConfigMap = async (name, projects) => {
const configMap = new kubernetes.V1ConfigMap();
const metadata = new kubernetes.V1ObjectMeta();
metadata.name = `environment-config-${name}`;
metadata.namespace = BRIGADE_NAMESPACE;
metadata.labels = {
type: 'preview-environment-config',
environmentName: name,
};
configMap.metadata = metadata;
configMap.data = {
environment: yaml.dump(projects),
};
try {
await k8sClient.createNamespacedConfigMap(BRIGADE_NAMESPACE, configMap);
} catch (error) {
if (error.body && error.body.code === 409) {
await k8sClient.replaceNamespacedConfigMap(
configMap.metadata.name, BRIGADE_NAMESPACE, configMap,
);
} else {
throw error;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment