@@ -64,25 +64,31 @@ func getGatewayK8sName(apiName string) string {
64
64
return "gateway-" + apiName
65
65
}
66
66
67
- func deploymentID () string {
67
+ func generateDeploymentID () string {
68
68
return k8s .RandomName ()[:10 ]
69
69
}
70
70
71
71
func UpdateAPI (apiConfig userconfig.API , force bool ) (* spec.API , string , error ) {
72
- prevK8sResources , err := getK8sResources (apiConfig )
72
+ prevK8sResources , err := getK8sResources (apiConfig . Name )
73
73
if err != nil {
74
74
return nil , "" , err
75
75
}
76
76
77
- deployID := deploymentID ()
78
- if prevK8sResources .apiDeployment != nil && prevK8sResources .apiDeployment .Labels ["deploymentID" ] != "" {
79
- deployID = prevK8sResources .apiDeployment .Labels ["deploymentID" ]
77
+ initialDeploymentTime := time .Now ().UnixNano ()
78
+ deploymentID := generateDeploymentID ()
79
+ if prevK8sResources .gatewayVirtualService != nil && prevK8sResources .gatewayVirtualService .Labels ["initialDeploymentTime" ] != "" {
80
+ var err error
81
+ initialDeploymentTime , err = k8s .ParseInt64Label (prevK8sResources .gatewayVirtualService , "initialDeploymentTime" )
82
+ if err != nil {
83
+ return nil , "" , err
84
+ }
85
+ deploymentID = prevK8sResources .gatewayVirtualService .Labels ["deploymentID" ]
80
86
}
81
87
82
- api := spec .GetAPISpec (& apiConfig , deployID , config .ClusterConfig .ClusterUID )
88
+ api := spec .GetAPISpec (& apiConfig , initialDeploymentTime , deploymentID , config .ClusterConfig .ClusterUID )
83
89
84
90
// resource creation
85
- if prevK8sResources .apiDeployment == nil {
91
+ if prevK8sResources .gatewayVirtualService == nil {
86
92
if err := config .AWS .UploadJSONToS3 (api , config .ClusterConfig .Bucket , api .Key ); err != nil {
87
93
return nil , "" , errors .Wrap (err , "upload api spec" )
88
94
}
@@ -91,7 +97,7 @@ func UpdateAPI(apiConfig userconfig.API, force bool) (*spec.API, string, error)
91
97
"apiName" : apiConfig .Name ,
92
98
}
93
99
94
- queueURL , err := createFIFOQueue (apiConfig .Name , deployID , tags )
100
+ queueURL , err := createFIFOQueue (apiConfig .Name , initialDeploymentTime , tags )
95
101
if err != nil {
96
102
return nil , "" , err
97
103
}
@@ -127,7 +133,12 @@ func UpdateAPI(apiConfig userconfig.API, force bool) (*spec.API, string, error)
127
133
return nil , "" , errors .Wrap (err , "upload api spec" )
128
134
}
129
135
130
- queueURL , err := getQueueURL (api .Name , prevK8sResources .gatewayVirtualService .Labels ["deploymentID" ])
136
+ initialDeploymentTime , err := k8s .ParseInt64Label (prevK8sResources .gatewayVirtualService , "initialDeploymentTime" )
137
+ if err != nil {
138
+ return nil , "" , err
139
+ }
140
+
141
+ queueURL , err := getQueueURL (api .Name , initialDeploymentTime )
131
142
if err != nil {
132
143
return nil , "" , err
133
144
}
@@ -150,6 +161,56 @@ func UpdateAPI(apiConfig userconfig.API, force bool) (*spec.API, string, error)
150
161
return api , fmt .Sprintf ("%s is up to date" , api .Resource .UserString ()), nil
151
162
}
152
163
164
+ func RefreshAPI (apiName string , force bool ) (string , error ) {
165
+ prevK8sResources , err := getK8sResources (apiName )
166
+ if err != nil {
167
+ return "" , err
168
+ } else if prevK8sResources .gatewayVirtualService == nil || prevK8sResources .apiDeployment == nil {
169
+ return "" , errors .ErrorUnexpected ("unable to find deployment" , apiName )
170
+ }
171
+
172
+ isUpdating , err := isAPIUpdating (prevK8sResources .apiDeployment )
173
+ if err != nil {
174
+ return "" , err
175
+ }
176
+
177
+ if isUpdating && ! force {
178
+ return "" , ErrorAPIUpdating (apiName )
179
+ }
180
+
181
+ apiID , err := k8s .GetLabel (prevK8sResources .gatewayVirtualService , "apiID" )
182
+ if err != nil {
183
+ return "" , err
184
+ }
185
+
186
+ api , err := operator .DownloadAPISpec (apiName , apiID )
187
+ if err != nil {
188
+ return "" , err
189
+ }
190
+
191
+ initialDeploymentTime , err := k8s .ParseInt64Label (prevK8sResources .gatewayVirtualService , "initialDeploymentTime" )
192
+ if err != nil {
193
+ return "" , err
194
+ }
195
+
196
+ api = spec .GetAPISpec (api .API , initialDeploymentTime , generateDeploymentID (), config .ClusterConfig .ClusterUID )
197
+
198
+ if err := config .AWS .UploadJSONToS3 (api , config .ClusterConfig .Bucket , api .Key ); err != nil {
199
+ return "" , errors .Wrap (err , "upload api spec" )
200
+ }
201
+
202
+ queueURL , err := getQueueURL (api .Name , initialDeploymentTime )
203
+ if err != nil {
204
+ return "" , err
205
+ }
206
+
207
+ if err = applyK8sResources (* api , prevK8sResources , queueURL ); err != nil {
208
+ return "" , err
209
+ }
210
+
211
+ return fmt .Sprintf ("updating %s" , api .Resource .UserString ()), nil
212
+ }
213
+
153
214
func DeleteAPI (apiName string , keepCache bool ) error {
154
215
err := parallel .RunFirstErr (
155
216
func () error {
@@ -158,7 +219,11 @@ func DeleteAPI(apiName string, keepCache bool) error {
158
219
return err
159
220
}
160
221
if vs != nil {
161
- queueURL , err := getQueueURL (apiName , vs .Labels ["deploymentID" ])
222
+ initialDeploymentTime , err := k8s .ParseInt64Label (vs , "initialDeploymentTime" )
223
+ if err != nil {
224
+ return err
225
+ }
226
+ queueURL , err := getQueueURL (apiName , initialDeploymentTime )
162
227
if err != nil {
163
228
return err
164
229
}
@@ -258,20 +323,19 @@ func GetAllAPIs(pods []kcore.Pod, deployments []kapps.Deployment) ([]schema.APIR
258
323
return asyncAPIs , nil
259
324
}
260
325
261
- func UpdateMetricsCron (deployment * kapps.Deployment ) error {
262
- // skip gateway deployments
263
- if deployment .Labels ["cortex.dev/async" ] != "api" {
264
- return nil
265
- }
266
-
267
- apiName := deployment .Labels ["apiName" ]
268
- deployID := deployment .Labels ["deploymentID" ]
326
+ func UpdateAPIMetricsCron (apiDeployment * kapps.Deployment ) error {
327
+ apiName := apiDeployment .Labels ["apiName" ]
269
328
270
329
if prevMetricsCron , ok := _metricsCrons [apiName ]; ok {
271
330
prevMetricsCron .Cancel ()
272
331
}
273
332
274
- queueURL , err := getQueueURL (apiName , deployID )
333
+ initialDeploymentTime , err := k8s .ParseInt64Label (apiDeployment , "initialDeploymentTime" )
334
+ if err != nil {
335
+ return err
336
+ }
337
+
338
+ queueURL , err := getQueueURL (apiName , initialDeploymentTime )
275
339
if err != nil {
276
340
return err
277
341
}
@@ -283,18 +347,13 @@ func UpdateMetricsCron(deployment *kapps.Deployment) error {
283
347
return nil
284
348
}
285
349
286
- func UpdateAutoscalerCron (deployment * kapps.Deployment , apiSpec spec.API ) error {
287
- // skip gateway deployments
288
- if deployment .Labels ["cortex.dev/async" ] != "api" {
289
- return nil
290
- }
291
-
292
- apiName := deployment .Labels ["apiName" ]
350
+ func UpdateAPIAutoscalerCron (apiDeployment * kapps.Deployment , apiSpec spec.API ) error {
351
+ apiName := apiDeployment .Labels ["apiName" ]
293
352
if prevAutoscalerCron , ok := _autoscalerCrons [apiName ]; ok {
294
353
prevAutoscalerCron .Cancel ()
295
354
}
296
355
297
- autoscaler , err := autoscalerlib .AutoscaleFn (deployment , & apiSpec , getMessagesInQueue )
356
+ autoscaler , err := autoscalerlib .AutoscaleFn (apiDeployment , & apiSpec , getMessagesInQueue )
298
357
if err != nil {
299
358
return err
300
359
}
@@ -304,16 +363,16 @@ func UpdateAutoscalerCron(deployment *kapps.Deployment, apiSpec spec.API) error
304
363
return nil
305
364
}
306
365
307
- func getK8sResources (apiConfig userconfig. API ) (resources , error ) {
366
+ func getK8sResources (apiName string ) (resources , error ) {
308
367
var deployment * kapps.Deployment
309
368
var apiConfigMap * kcore.ConfigMap
310
369
var gatewayDeployment * kapps.Deployment
311
370
var gatewayService * kcore.Service
312
371
var gatewayHPA * kautoscaling.HorizontalPodAutoscaler
313
372
var gatewayVirtualService * istioclientnetworking.VirtualService
314
373
315
- gatewayK8sName := getGatewayK8sName (apiConfig . Name )
316
- apiK8sName := workloads .K8sName (apiConfig . Name )
374
+ gatewayK8sName := getGatewayK8sName (apiName )
375
+ apiK8sName := workloads .K8sName (apiName )
317
376
318
377
err := parallel .RunFirstErr (
319
378
func () error {
@@ -382,11 +441,11 @@ func applyK8sResources(api spec.API, prevK8sResources resources, queueURL string
382
441
return err
383
442
}
384
443
385
- if err := UpdateMetricsCron (& apiDeployment ); err != nil {
444
+ if err := UpdateAPIMetricsCron (& apiDeployment ); err != nil {
386
445
return err
387
446
}
388
447
389
- if err := UpdateAutoscalerCron (& apiDeployment , api ); err != nil {
448
+ if err := UpdateAPIAutoscalerCron (& apiDeployment , api ); err != nil {
390
449
return err
391
450
}
392
451
0 commit comments