@@ -24,39 +24,41 @@ import * as request from 'request-promise';
2424import * as format from '../utils/format' ;
2525import { log } from '../utils/log' ;
2626import * as validator from '../utils/validator' ;
27- import { failure } from '../utils/format' ;
2827import { spawnSync } from 'child_process' ;
2928import { CLI_PACKAGE } from '../paths' ;
29+ import { warn } from '../utils/format' ;
3030
3131export 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' ) ) ;
0 commit comments