1818
1919const _ = require ( 'lodash' ) ;
2020const BbPromise = require ( 'bluebird' ) ;
21+ const Config = require ( '../lib/config' ) ;
22+ const crypto = require ( 'crypto' ) ;
2123const deploy = require ( '../lib/deploy' ) ;
2224const fs = require ( 'fs' ) ;
2325const helpers = require ( '../lib/helpers' ) ;
@@ -86,19 +88,27 @@ class KubelessDeploy {
8688 deployFunction ( ) {
8789 const runtime = this . serverless . service . provider . runtime ;
8890 const populatedFunctions = [ ] ;
91+ const kubelessConfig = new Config ( ) ;
8992 return new BbPromise ( ( resolve , reject ) => {
90- _ . each ( this . serverless . service . functions , ( description , name ) => {
91- const pkg = this . options . package ||
92- this . serverless . service . package . path ||
93- description . package . artifact ||
94- this . serverless . config . serverless . service . artifact ;
95- this . checkSize ( pkg ) ;
96- fs . readFile ( pkg , { encoding : 'base64' } , ( err , functionContent ) => {
97- if ( err ) {
98- reject ( err ) ;
99- } else if ( description . handler ) {
100- const files = helpers . getRuntimeFilenames ( runtime , description . handler ) ;
101- this . getFileContent ( pkg , files . deps )
93+ kubelessConfig . init ( ) . then ( ( ) => {
94+ _ . each ( this . serverless . service . functions , ( description , name ) => {
95+ const pkg = this . options . package ||
96+ this . serverless . service . package . path ||
97+ description . package . artifact ||
98+ this . serverless . config . serverless . service . artifact ;
99+ this . checkSize ( pkg ) ;
100+ const s = fs . createReadStream ( pkg ) ;
101+ const shasum = crypto . createHash ( 'sha256' ) ;
102+ let functionContent = '' ;
103+ s . on ( 'error' , ( err ) => reject ( err ) ) ;
104+ s . on ( 'data' , ( data ) => {
105+ shasum . update ( data ) ;
106+ functionContent += data . toString ( 'base64' ) ;
107+ } ) ;
108+ s . on ( 'end' , ( ) => {
109+ if ( description . handler ) {
110+ const depFile = helpers . getRuntimeDepfile ( runtime , kubelessConfig ) ;
111+ this . getFileContent ( pkg , depFile )
102112 . catch ( ( ) => {
103113 // No requirements found
104114 } )
@@ -107,6 +117,7 @@ class KubelessDeploy {
107117 _ . assign ( { } , description , {
108118 id : name ,
109119 content : functionContent ,
120+ checksum : `sha256:${ shasum . digest ( 'hex' ) } ` ,
110121 deps : requirementsContent ,
111122 image : description . image || this . serverless . service . provider . image ,
112123 events : _ . map ( description . events , ( event ) => {
@@ -127,12 +138,13 @@ class KubelessDeploy {
127138 resolve ( ) ;
128139 }
129140 } ) ;
130- } else {
131- populatedFunctions . push ( _ . assign ( { } , description , { id : name } ) ) ;
132- if ( populatedFunctions . length === _ . keys ( this . serverless . service . functions ) . length ) {
133- resolve ( ) ;
141+ } else {
142+ populatedFunctions . push ( _ . assign ( { } , description , { id : name } ) ) ;
143+ if ( populatedFunctions . length === _ . keys ( this . serverless . service . functions ) . length ) {
144+ resolve ( ) ;
145+ }
134146 }
135- }
147+ } ) ;
136148 } ) ;
137149 } ) ;
138150 } ) . then ( ( ) => deploy (
0 commit comments