1
1
/*
2
- * Copyright 2019 IBM Corporation
2
+ * Copyright 2019-2020 IBM Corporation
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
14
14
* limitations under the License.
15
15
*/
16
16
17
+ import Debug from 'debug'
17
18
import { Arguments , Registrar , i18n } from '@kui-shell/core'
18
19
19
20
import flags from './flags'
@@ -23,6 +24,7 @@ import commandPrefix from '../command-prefix'
23
24
import { isUsage , doHelp } from '../../lib/util/help'
24
25
25
26
const strings = i18n ( 'plugin-kubectl' )
27
+ const debug = Debug ( 'plugin-kubectl/controller/kubectl/explain' )
26
28
27
29
function formatHref ( href : string ) {
28
30
if ( / a p i - c o n v e n t i o n s / . test ( href ) && ! / s i g - a r c h i t e c t u r e / . test ( href ) ) {
@@ -50,7 +52,7 @@ ${formatDocumentation(_[4])}
50
52
}
51
53
}
52
54
53
- // alternate patterns to match against
55
+ /** alternate patterns to match against */
54
56
const kvd = / ^ K I N D : \s + ( \S + ) \n V E R S I O N : \s + ( \S + ) \n \n D E S C R I P T I O N : \n ( \s * D E P R E C A T E D - ) ? ( [ \s \S ] + ) /
55
57
const kvdf = / ^ K I N D : \s + ( \S + ) \n V E R S I O N : \s + ( \S + ) \n \n D E S C R I P T I O N : \n ( \s * D E P R E C A T E D - ) ? ( [ \s \S ] + ) \n \n F I E L D S : \n ( [ \s \S ] + ) /
56
58
@@ -127,22 +129,43 @@ ${isDeprecated ? `### Warnings\n${strings('This API Resource is deprecated')}` :
127
129
return response
128
130
}
129
131
132
+ /**
133
+ * Cache of the getKind() lookup
134
+ *
135
+ */
136
+ const cache : Record < string , Promise < string > > = { }
137
+
130
138
/**
131
139
* @param kindAsProvidedByUser e.g. pod or po
132
140
* @return e.g. Pod
133
141
*
134
142
*/
135
143
export async function getKind ( command : string , args : Arguments , kindAsProvidedByUser : string ) : Promise < string > {
136
- try {
137
- const ourArgs = Object . assign ( { } , args , { command : `kubectl explain ${ kindAsProvidedByUser } ` } )
138
- const explained = await doExecWithStdout ( ourArgs , undefined , command )
139
-
140
- return explained . match ( / ^ K I N D : \s + ( .* ) / ) [ 1 ]
141
- } catch ( err ) {
142
- if ( ! / d o e s n o t e x i s t / i. test ( err . message ) ) {
143
- console . error ( `error explaining kind ${ kindAsProvidedByUser } ` , err )
144
- }
144
+ if ( ! cache [ kindAsProvidedByUser ] ) {
145
+ // otherwise, we need to do a more expensive call to `kubectl`
146
+ // eslint-disable-next-line no-async-promise-executor
147
+ cache [ kindAsProvidedByUser ] = new Promise < string > ( async ( resolve , reject ) => {
148
+ try {
149
+ const ourArgs = Object . assign ( { } , args , { command : `${ command } explain ${ kindAsProvidedByUser } ` } )
150
+ const explained = await doExecWithStdout ( ourArgs , undefined , command ) . catch ( err => {
151
+ // e.g. trying to explain CustomResourceDefinition.v1beta1.apiextensions.k8s.io
152
+ // or a resource kind that does not exist
153
+ debug ( err )
154
+ return `KIND: ${ kindAsProvidedByUser } `
155
+ } )
156
+
157
+ const kindFromServer = explained . match ( / ^ K I N D : \s + ( .* ) / ) [ 1 ]
158
+ resolve ( kindFromServer )
159
+ } catch ( err ) {
160
+ if ( ! / d o e s n o t e x i s t / i. test ( err . message ) ) {
161
+ console . error ( `error explaining kind ${ kindAsProvidedByUser } ` , err )
162
+ reject ( err )
163
+ }
164
+ }
165
+ } )
145
166
}
167
+
168
+ return cache [ kindAsProvidedByUser ]
146
169
}
147
170
148
171
export default ( registrar : Registrar ) => {
0 commit comments