Skip to content

Commit e58df16

Browse files
Type generation from a cluster openAPI spec should be optional (#160)
1 parent a482988 commit e58df16

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

ts/create-kpt-functions/src/cmd/type_create.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,41 @@ import * as request from 'request-promise';
2424
import * as format from '../utils/format';
2525
import { log } from '../utils/log';
2626
import * as validator from '../utils/validator';
27-
import { failure } from '../utils/format';
2827
import { spawnSync } from 'child_process';
2928
import { CLI_PACKAGE } from '../paths';
29+
import { warn } from '../utils/format';
3030

3131
export async function typeCreate(packageDir: string) {
3232
const desc = 'Generating types from OpenAPI spec.';
3333
log(format.startMarker(desc));
34-
34+
// url for default swagger.json openAPI type definitions
35+
let url = `https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/kyaml/openapi/kubernetesapi/swagger.json`;
3536
// Get the kubeconfig context the user wants to use.
3637
const kc = new KubeConfig();
3738
kc.loadFromDefault();
3839
const contexts = kc.contexts;
39-
if (contexts.length == 0) {
40+
if (contexts.length != 0) {
41+
const currentContext = kc.currentContext;
42+
const contextIdx = chooseContext(contexts, currentContext);
43+
const useContext = contexts[contextIdx];
44+
const cluster = kc.clusters.find(c => c.name === useContext.cluster);
45+
if (!cluster) {
46+
throw new Error('Cluster for specified context not found.');
47+
}
48+
kc.setCurrentContext(useContext.name);
49+
// set the url to cluster openAPI if cluster exists
50+
url = `${cluster.server}/openapi/v2`;
51+
} else {
4052
log(
41-
failure(
42-
'No contexts found in kubeconfig file. Please set a context entry in kubeconfig.'
53+
warn(
54+
'No contexts found in kubeconfig file. Using default openAPI type definitions.'
4355
)
4456
);
45-
process.exit(1);
4657
}
47-
const currentContext = kc.currentContext;
48-
const contextIdx = chooseContext(contexts, currentContext);
49-
const useContext = contexts[contextIdx];
50-
const cluster = kc.clusters.find(c => c.name === useContext.cluster);
51-
if (!cluster) {
52-
throw new Error('Cluster for specified context not found.');
53-
}
54-
55-
// Download the swagger file from the cluster.
58+
// Download the swagger file from the url.
5659
const opts: request.Options = {
57-
url: `${cluster.server}/openapi/v2`,
60+
url,
5861
};
59-
kc.setCurrentContext(useContext.name);
6062
kc.applyToRequest(opts);
6163
const out = await request.get(opts);
6264
const tmp = mkdtempSync(resolve(tmpdir(), 'kpt-init'));

ts/create-kpt-functions/src/utils/format.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export function failure(s: string): string {
3030
return chalk.red(chalk.bold(s));
3131
}
3232

33+
/**
34+
* Formats an input string to indicate a warn condition.
35+
*/
36+
export function warn(s: string): string {
37+
return chalk.yellow(chalk.bold(s));
38+
}
39+
3340
/**
3441
* Marks the starting point of an operation.
3542
*/

0 commit comments

Comments
 (0)