@@ -1020,116 +1020,4 @@ export class Component extends OpenShiftItem {
10201020 await commands . executeCommand ( 'workbench.view.explorer' ) ;
10211021 await commands . executeCommand ( 'revealInExplorer' , context . contextPath ) ;
10221022 }
1023-
1024- @vsCommand ( 'openshift.component.import' )
1025- // @clusterRequired () - not required because available only from context menu in application explorer
1026- static async import ( component : OpenShiftObject ) : Promise < string | null > {
1027- const prjName = component . getParent ( ) . getParent ( ) . getName ( ) ;
1028- const appName = component . getParent ( ) . getName ( ) ;
1029- const compName = component . getName ( ) ;
1030- // get pvcs and urls based on label selector
1031- const componentResult = await Component . odo . execute (
1032- new CommandText ( 'oc get dc' , undefined , [
1033- new CommandOption ( '-l' , `app.kubernetes.io/instance=${ compName } ` ) ,
1034- new CommandOption ( '--namespace' , prjName ) ,
1035- new CommandOption ( '-o' , 'json' )
1036- ] ) ,
1037- Platform . getUserHomePath ( ) ,
1038- false ) ;
1039- const componentJson = JSON . parse ( componentResult . stdout ) . items [ 0 ] ;
1040- const componentType = componentJson . metadata . annotations [ 'app.kubernetes.io/component-source-type' ] ;
1041- if ( componentType === SourceType . BINARY ) {
1042- return 'Import for binary OpenShift Components is not supported.' ;
1043- } if ( componentType !== SourceType . GIT && componentType !== SourceType . LOCAL ) {
1044- throw new VsCommandError ( `Cannot import unknown Component type '${ componentType } '.` , 'Cannot import unknown Component type' ) ;
1045- }
1046-
1047- const workspaceFolder = await selectWorkspaceFolder ( ) ;
1048- if ( ! workspaceFolder ) return null ;
1049- return Progress . execFunctionWithProgress ( `Importing component '${ compName } '` , async ( ) => {
1050- try {
1051- // use annotations to understand what kind of component is imported
1052- // metadata:
1053- // annotations:
1054- // app.kubernetes.io/component-source-type: binary
1055- // app.openshift.io/vcs-uri: 'file:///helloworld.war'
1056- // not supported yet
1057-
1058- // metadata:
1059- // annotations:
1060- // app.kubernetes.io/component-source-type: local
1061- // app.openshift.io/vcs-uri: 'file:///./'
1062-
1063- // metadata:
1064- // annotations:
1065- // app.kubernetes.io/component-source-type: git
1066- // app.kubernetes.io/url: 'https://github.com/dgolovin/nodejs-ex'
1067-
1068- if ( componentType === SourceType . GIT ) {
1069- const bcResult = await Component . odo . execute ( new CommandText ( 'oc get' , `bc/${ componentJson . metadata . name } ` , [ new CommandOption ( '--namespace' , prjName ) , new CommandOption ( '-o' , 'json' , false ) ] ) ) ;
1070- const bcJson = JSON . parse ( bcResult . stdout ) ;
1071- const compTypeName = componentJson . metadata . labels [ 'app.kubernetes.io/name' ] ;
1072- const compTypeVersion = componentJson . metadata . labels [ 'app.openshift.io/runtime-version' ] ;
1073- const gitUrl = componentJson . metadata . annotations [ 'app.openshift.io/vcs-uri' ] || componentJson . metadata . annotations [ 'app.kubernetes.io/url' ] ;
1074- const gitRef = bcJson . spec . source . git . ref || 'master' ;
1075- await Component . odo . execute ( Command . createGitComponent ( prjName , appName , compTypeName , compTypeVersion , compName , gitUrl , gitRef ) , workspaceFolder . fsPath ) ;
1076- } else { // componentType === ComponentType.Local
1077- await Component . odo . execute ( Command . createLocalComponent (
1078- prjName ,
1079- appName ,
1080- componentJson . metadata . labels [ 'app.kubernetes.io/name' ] ,
1081- componentJson . metadata . labels [ 'app.openshift.io/runtime-version' ] ,
1082- undefined ,
1083- compName , workspaceFolder . fsPath ) ) ;
1084- }
1085- // import storage if present
1086- if ( componentJson . spec . template . spec . containers [ 0 ] . volumeMounts ) {
1087- const volumeMounts : any [ ] = componentJson . spec . template . spec . containers [ 0 ] . volumeMounts . filter ( ( volume : { name : string } ) => ! volume . name . startsWith ( compName ) ) ;
1088- const volumes : any [ ] = componentJson . spec . template . spec . volumes . filter ( ( volume : { persistentVolumeClaim : any ; name : string } ) => volume . persistentVolumeClaim !== undefined && ! volume . name . startsWith ( compName ) ) ;
1089- const storageData : Partial < { mountPath : string ; pvcName : string } > [ ] = volumes . map ( ( volume ) => {
1090- const data : Partial < { mountPath : string ; pvcName : string } > = { } ;
1091- const mount = volumeMounts . find ( ( item ) => item . name === volume . name ) ;
1092- data . mountPath = mount . mountPath ;
1093- data . pvcName = volume . persistentVolumeClaim . claimName ;
1094- return data ;
1095- } ) ;
1096- // eslint-disable-next-line no-restricted-syntax
1097- for ( const storage of storageData ) {
1098- try {
1099- // eslint-disable-next-line no-await-in-loop
1100- const pvcResult = await Component . odo . execute ( new CommandText ( 'oc get' , `pvc/${ storage . pvcName } ` , [ new CommandOption ( '--namespace' , prjName ) , new CommandOption ( '-o' , 'json' ) ] ) , Platform . getUserHomePath ( ) , false ) ;
1101- const pvcJson = JSON . parse ( pvcResult . stdout ) ;
1102- const storageName = pvcJson . metadata . labels [ 'app.kubernetes.io/storage-name' ] ;
1103- const size = pvcJson . spec . resources . requests . storage ;
1104- // eslint-disable-next-line no-await-in-loop
1105- await Component . odo . execute ( Command . createStorage ( storageName , storage . mountPath , size ) , workspaceFolder . fsPath ) ;
1106- } catch ( ignore ) {
1107- // means there is no storage attached to component
1108- }
1109- }
1110- }
1111- // import routes if present
1112- try {
1113- const routeResult = await Component . odo . execute ( new CommandText ( 'oc get route' , undefined , [ new CommandOption ( '-l' , `app.kubernetes.io/instance=${ compName } ,app.kubernetes.io/part-of=${ appName } ` ) , new CommandOption ( '--namespace' , prjName ) , new CommandOption ( '-o' , 'json' ) ] ) , Platform . getUserHomePath ( ) , false ) ;
1114- const routeJson = JSON . parse ( routeResult . stdout ) ;
1115- const routeData : Partial < { name : string ; port : string } > [ ] = routeJson . items . map ( ( element : any ) => ( { name : element . metadata . labels [ 'odo.openshift.io/url-name' ] , port : element . spec . port . targetPort } ) ) ;
1116- // eslint-disable-next-line no-restricted-syntax
1117- for ( const url of routeData ) {
1118- Component . odo . execute ( Command . createComponentCustomUrl ( url . name , url . port ) , workspaceFolder . fsPath ) ;
1119- }
1120- } catch ( ignore ) {
1121- // means there is no routes to the component
1122- }
1123- const wsFolder = workspace . getWorkspaceFolder ( workspaceFolder ) ;
1124- if ( wsFolder ) {
1125- Component . odo . addWorkspaceComponent ( wsFolder , component ) ;
1126- } else {
1127- workspace . updateWorkspaceFolders ( workspace . workspaceFolders ? workspace . workspaceFolders . length : 0 , null , { uri : workspaceFolder } ) ;
1128- }
1129- return `Component '${ compName } ' was successfully imported.` ;
1130- } catch ( errGetCompJson ) {
1131- throw new VsCommandError ( `Component import failed with error '${ errGetCompJson . message } '.` , 'Component import failed' ) ;
1132- }
1133- } ) ; // create component with the same name
1134- }
11351023}
0 commit comments