18
18
19
19
const _ = require ( 'lodash' ) ;
20
20
const BbPromise = require ( 'bluebird' ) ;
21
+ const Config = require ( '../lib/config' ) ;
22
+ const crypto = require ( 'crypto' ) ;
21
23
const deploy = require ( '../lib/deploy' ) ;
22
24
const fs = require ( 'fs' ) ;
23
25
const helpers = require ( '../lib/helpers' ) ;
@@ -86,19 +88,27 @@ class KubelessDeploy {
86
88
deployFunction ( ) {
87
89
const runtime = this . serverless . service . provider . runtime ;
88
90
const populatedFunctions = [ ] ;
91
+ const kubelessConfig = new Config ( ) ;
89
92
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 )
102
112
. catch ( ( ) => {
103
113
// No requirements found
104
114
} )
@@ -107,6 +117,7 @@ class KubelessDeploy {
107
117
_ . assign ( { } , description , {
108
118
id : name ,
109
119
content : functionContent ,
120
+ checksum : `sha256:${ shasum . digest ( 'hex' ) } ` ,
110
121
deps : requirementsContent ,
111
122
image : description . image || this . serverless . service . provider . image ,
112
123
events : _ . map ( description . events , ( event ) => {
@@ -127,12 +138,13 @@ class KubelessDeploy {
127
138
resolve ( ) ;
128
139
}
129
140
} ) ;
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
+ }
134
146
}
135
- }
147
+ } ) ;
136
148
} ) ;
137
149
} ) ;
138
150
} ) . then ( ( ) => deploy (
0 commit comments