import * as k8s from '@kubernetes/client-node' ; import * as fs from 'fs' ; import * as yaml from 'js-yaml' ; import { promisify } from 'util' ; param specPath return export async function apply ( specPath ) { const kc = new k8s . KubeConfig ( ) ; kc . loadFromDefault ( ) ; const client = k8s . KubernetesObjectApi . makeApiClient ( kc ) ; const fsReadFileP = promisify ( fs . readFile ) ; const specString = await fsReadFileP ( specPath , 'utf8' ) ; const specs = yaml . loadAll ( specString ) ; const validSpecs = specs . filter ( ( s ) => s && s . kind && s . metadata ) ; const created = [ ] ; for ( const spec of validSpecs ) { spec . metadata = spec . metadata || { } ; spec . metadata . annotations = spec . metadata . annotations || { } ; delete spec . metadata . annotations [ 'kubectl.kubernetes.io/last-applied-configuration' ] ; spec . metadata . annotations [ 'kubectl.kubernetes.io/last-applied-configuration' ] = JSON . stringify ( spec ) ; try { await client . read ( spec ) ; const response = await client . patch ( spec ) ; created . push ( response . body ) ; } catch ( e ) { const response = await client . create ( spec ) ; created . push ( response . body ) ; } } return created ; }