|
| 1 | +/* eslint-disable no-console */ |
| 2 | +/* eslint-disable require-jsdoc */ |
| 3 | +/* eslint-disable node/no-unpublished-require */ |
| 4 | +const CodeEngineV1 = require('../dist/ibm-cloud-code-engine/v1'); // require('ibm-code-engine-sdk/ibm-cloud-code-engine/v1'); |
| 5 | +const { IamAuthenticator } = require('../dist/auth'); // require('ibm-code-engine-sdk/auth'); |
| 6 | +const k8s = require('@kubernetes/client-node'); |
| 7 | + |
| 8 | +if (!process.env.CE_API_KEY || !process.env.CE_PROJECT_ID || !process.env.CE_PROJECT_REGION) { |
| 9 | + throw new Error( |
| 10 | + 'You must set the envrionment variables CE_API_KEY, CE_PROJECT_REGION and CE_PROJECT_ID before using the example.' |
| 11 | + ); |
| 12 | +} |
| 13 | + |
| 14 | +// Create an IAM authenticator. |
| 15 | +const authenticator = new IamAuthenticator({ |
| 16 | + apikey: process.env.CE_API_KEY, |
| 17 | + clientId: 'bx', |
| 18 | + clientSecret: 'bx', |
| 19 | +}); |
| 20 | + |
| 21 | +// Construct the Code Engine client using the IAM authenticator. |
| 22 | +const ceClient = new CodeEngineV1({ |
| 23 | + authenticator, |
| 24 | + serviceUrl: `https://api.${process.env.CE_PROJECT_REGION}.codeengine.cloud.ibm.com/api/v1`, |
| 25 | +}); |
| 26 | + |
| 27 | +async function main() { |
| 28 | + // Get tokens using the Authenticator. |
| 29 | + const tokenResponse = await authenticator.tokenManager.requestToken(); |
| 30 | + |
| 31 | + // Get Code Engine project config using the Code Engine client. |
| 32 | + const configResponse = await ceClient.listKubeconfig({ |
| 33 | + refreshToken: tokenResponse.result.refresh_token, |
| 34 | + id: process.env.CE_PROJECT_ID, |
| 35 | + }); |
| 36 | + |
| 37 | + // Setup Kubernetes client. |
| 38 | + const kubeConfig = new k8s.KubeConfig(); |
| 39 | + kubeConfig.loadFromString(configResponse.result); |
| 40 | + const kubeClient = kubeConfig.makeApiClient(k8s.CoreV1Api); |
| 41 | + |
| 42 | + // Get something from project. |
| 43 | + const { namespace } = kubeConfig.getCurrentContextObject(); |
| 44 | + const configMapList = await kubeClient.listNamespacedConfigMap(namespace); |
| 45 | + console.log( |
| 46 | + `Project ${process.env.CE_PROJECT_ID} has ${configMapList.body.items.length} configmaps.` |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +main(); |
0 commit comments