/**********************************************************************
 * Copyright (c) 2021 Red Hat, Inc.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 ***********************************************************************/ import { injectable } from 'inversify' ; /**
 * Convert meta.yaml into a devfile 2.0 syntax
 */ @ injectable ( ) export class MetaYamlToDevfileYaml { componentsFromContainer ( container ) { const components = [ ] ; const component = { name : container . name , container : { image : container . image , } , } ; if ( container . command ) { component . container . command = container . command ; } if ( container . args ) { component . container . args = container . args ; } if ( container . env ) { component . container . env = container . env ; } if ( container . attributes ) { component . attributes = container . attributes ; } if ( container . volumes ) { component . container . volumeMounts = container . volumes . map ( ( volume ) => ( { name : volume . name , path : volume . mountPath , } ) ) ; container . volumes . map ( ( volume ) => { const volumeComponent = { name : volume . name , volume : { } , } ; if ( volume . ephemeral === true ) { volumeComponent . volume . ephemeral = true ; } components . push ( volumeComponent ) ; } ) ; } if ( container . mountSources ) { component . container . mountSources = container . mountSources ; } if ( container . memoryLimit ) { component . container . memoryLimit = container . memoryLimit ; } if ( container . memoryRequest ) { component . container . memoryRequest = container . memoryRequest ; } if ( container . cpuLimit ) { component . container . cpuLimit = container . cpuLimit ; } if ( container . cpuRequest ) { component . container . cpuRequest = container . cpuRequest ; } components . unshift ( component ) ; return components ; } convert ( inputMetaYaml ) { const metaYaml = JSON . parse ( JSON . stringify ( inputMetaYaml ) ) ; if ( ! metaYaml || metaYaml . type === 'VS Code extension' ) { return ; } const devfileYaml = { schemaVersion : '2.1.0' , metadata : { name : metaYaml . displayName , } , } ; const metaYamlSpec = metaYaml . spec ; let components = [ ] ; const endpoints = [ ] ; if ( metaYamlSpec . endpoints && metaYamlSpec . endpoints . length > 0 ) { metaYamlSpec . endpoints . forEach ( ( endpoint ) => { const devfileEndpoint = { name : endpoint . name , attributes : endpoint . attributes , } ; devfileEndpoint . targetPort = endpoint . targetPort ; if ( endpoint . public === true ) { devfileEndpoint . exposure = 'public' ; } if ( endpoint . path ) { devfileEndpoint . path = endpoint . path ; } if ( endpoint . attributes && endpoint . attributes [ 'type' ] === 'ide' ) { devfileEndpoint . attributes [ 'type' ] = 'main' ; } if ( devfileEndpoint . attributes && devfileEndpoint . attributes . secure === true ) { devfileEndpoint . secure = false ; delete devfileEndpoint . attributes [ 'secure' ] ; if ( devfileEndpoint . attributes . protocol ) { devfileEndpoint . attributes . protocol = `${ devfileEndpoint . attributes . protocol }s` ; } } if ( devfileEndpoint . attributes && devfileEndpoint . attributes . protocol ) { devfileEndpoint . protocol = devfileEndpoint . attributes . protocol ; delete devfileEndpoint . attributes [ 'protocol' ] ; } endpoints . push ( devfileEndpoint ) ; } ) ; } if ( metaYamlSpec . containers ) { metaYamlSpec . containers . forEach ( ( container ) => { const componentsFromContainer = this . componentsFromContainer ( container ) ; components = components . concat ( componentsFromContainer ) ; } ) ; components [ 0 ] . container . endpoints = endpoints ; } if ( metaYamlSpec . initContainers && metaYamlSpec . initContainers . length === 1 ) { const initContainer = metaYamlSpec . initContainers [ 0 ] ; const componentsFromContainer = this . componentsFromContainer ( initContainer ) ; const commands = devfileYaml . commands || [ ] ; commands . push ( { id : 'init-container-command' , apply : { component : componentsFromContainer [ 0 ] . name , } , } ) ; devfileYaml . commands = commands ; const events = devfileYaml . events || { } ; const preStartEvents = events . preStart || [ ] ; preStartEvents . push ( 'init-container-command' ) ; events . preStart = preStartEvents ; devfileYaml . events = events ; components = components . concat ( componentsFromContainer ) ; } components = components . filter ( function ( item , pos , self ) { return self . findIndex ( value => value . name === item . name ) === pos ; } ) ; devfileYaml . components = components ; return devfileYaml ; } }