@@ -28,18 +28,48 @@ import (
28
28
const LIVENESS_ENDPOINT = "/health/liveness"
29
29
const READINESS_ENDPOINT = "/health/readiness"
30
30
31
+ type DeployDecorator interface {
32
+ UpdateAnnotations (fn.Function , map [string ]string ) map [string ]string
33
+ UpdateLabels (fn.Function , map [string ]string ) map [string ]string
34
+ }
35
+
36
+ type DeployerOpt func (* Deployer )
37
+
31
38
type Deployer struct {
32
39
// Namespace with which to override that set on the default configuration (such as the ~/.kube/config).
33
40
// If left blank, deployment will commence to the configured namespace.
34
41
Namespace string
35
42
// verbose logging enablement flag.
36
43
verbose bool
44
+
45
+ decorator DeployDecorator
46
+ }
47
+
48
+ func NewDeployer (opts ... DeployerOpt ) * Deployer {
49
+ d := & Deployer {}
50
+
51
+ for _ , opt := range opts {
52
+ opt (d )
53
+ }
54
+
55
+ return d
56
+ }
57
+
58
+ func WithDeployerNamespace (namespace string ) DeployerOpt {
59
+ return func (d * Deployer ) {
60
+ d .Namespace = namespace
61
+ }
62
+ }
63
+
64
+ func WithDeployerVerbose (verbose bool ) DeployerOpt {
65
+ return func (d * Deployer ) {
66
+ d .verbose = verbose
67
+ }
37
68
}
38
69
39
- func NewDeployer (namespaceOverride string , verbose bool ) * Deployer {
40
- return & Deployer {
41
- Namespace : namespaceOverride ,
42
- verbose : verbose ,
70
+ func WithDeployerDecorator (decorator DeployDecorator ) DeployerOpt {
71
+ return func (d * Deployer ) {
72
+ d .decorator = decorator
43
73
}
44
74
}
45
75
@@ -94,7 +124,7 @@ func (d *Deployer) Deploy(ctx context.Context, f fn.Function) (fn.DeploymentResu
94
124
referencedSecrets := sets .NewString ()
95
125
referencedConfigMaps := sets .NewString ()
96
126
97
- service , err := generateNewService (f )
127
+ service , err := generateNewService (f , d . decorator )
98
128
if err != nil {
99
129
err = fmt .Errorf ("knative deployer failed to generate the Knative Service: %v" , err )
100
130
return fn.DeploymentResult {}, err
@@ -195,7 +225,7 @@ func (d *Deployer) Deploy(ctx context.Context, f fn.Function) (fn.DeploymentResu
195
225
return fn.DeploymentResult {}, err
196
226
}
197
227
198
- _ , err = client .UpdateServiceWithRetry (ctx , f .Name , updateService (f , newEnv , newEnvFrom , newVolumes , newVolumeMounts ), 3 )
228
+ _ , err = client .UpdateServiceWithRetry (ctx , f .Name , updateService (f , newEnv , newEnvFrom , newVolumes , newVolumeMounts , d . decorator ), 3 )
199
229
if err != nil {
200
230
err = fmt .Errorf ("knative deployer failed to update the Knative Service: %v" , err )
201
231
return fn.DeploymentResult {}, err
@@ -240,7 +270,7 @@ func setHealthEndpoints(f fn.Function, c *corev1.Container) *corev1.Container {
240
270
return c
241
271
}
242
272
243
- func generateNewService (f fn.Function ) (* v1.Service , error ) {
273
+ func generateNewService (f fn.Function , decorator DeployDecorator ) (* v1.Service , error ) {
244
274
container := corev1.Container {
245
275
Image : f .ImageWithDigest (),
246
276
}
@@ -262,16 +292,21 @@ func generateNewService(f fn.Function) (*v1.Service, error) {
262
292
}
263
293
container .VolumeMounts = newVolumeMounts
264
294
265
- labels , err := processLabels (f )
295
+ labels , err := processLabels (f , decorator )
266
296
if err != nil {
267
297
return nil , err
268
298
}
269
299
300
+ annotations := f .Annotations
301
+ if decorator != nil {
302
+ annotations = decorator .UpdateAnnotations (f , annotations )
303
+ }
304
+
270
305
service := & v1.Service {
271
306
ObjectMeta : metav1.ObjectMeta {
272
307
Name : f .Name ,
273
308
Labels : labels ,
274
- Annotations : f . Annotations ,
309
+ Annotations : annotations ,
275
310
},
276
311
Spec : v1.ServiceSpec {
277
312
ConfigurationSpec : v1.ConfigurationSpec {
@@ -297,14 +332,18 @@ func generateNewService(f fn.Function) (*v1.Service, error) {
297
332
return service , nil
298
333
}
299
334
300
- func updateService (f fn.Function , newEnv []corev1.EnvVar , newEnvFrom []corev1.EnvFromSource , newVolumes []corev1.Volume , newVolumeMounts []corev1.VolumeMount ) func (service * v1.Service ) (* v1.Service , error ) {
335
+ func updateService (f fn.Function , newEnv []corev1.EnvVar , newEnvFrom []corev1.EnvFromSource , newVolumes []corev1.Volume , newVolumeMounts []corev1.VolumeMount , decorator DeployDecorator ) func (service * v1.Service ) (* v1.Service , error ) {
301
336
return func (service * v1.Service ) (* v1.Service , error ) {
302
337
// Removing the name so the k8s server can fill it in with generated name,
303
338
// this prevents conflicts in Revision name when updating the KService from multiple places.
304
339
service .Spec .Template .Name = ""
305
340
306
341
// Don't bother being as clever as we are with env variables
307
342
// Just set the annotations and labels to be whatever we find in func.yaml
343
+ if decorator != nil {
344
+ service .ObjectMeta .Annotations = decorator .UpdateAnnotations (f , service .ObjectMeta .Annotations )
345
+ }
346
+
308
347
for k , v := range f .Annotations {
309
348
service .ObjectMeta .Annotations [k ] = v
310
349
}
@@ -328,7 +367,7 @@ func updateService(f fn.Function, newEnv []corev1.EnvVar, newEnvFrom []corev1.En
328
367
return service , err
329
368
}
330
369
331
- labels , err := processLabels (f )
370
+ labels , err := processLabels (f , decorator )
332
371
if err != nil {
333
372
return service , err
334
373
}
@@ -354,7 +393,7 @@ func updateService(f fn.Function, newEnv []corev1.EnvVar, newEnvFrom []corev1.En
354
393
// value: value1
355
394
// - key: EXAMPLE2 # Label from the local ENV var
356
395
// value: {{ env:MY_ENV }}
357
- func processLabels (f fn.Function ) (map [string ]string , error ) {
396
+ func processLabels (f fn.Function , decorator DeployDecorator ) (map [string ]string , error ) {
358
397
labels := map [string ]string {
359
398
labels .FunctionKey : labels .FunctionValue ,
360
399
labels .FunctionNameKey : f .Name ,
@@ -365,6 +404,11 @@ func processLabels(f fn.Function) (map[string]string, error) {
365
404
labels .DeprecatedFunctionRuntimeKey : f .Runtime ,
366
405
// --- end of handling usage of deprecated runtime labels
367
406
}
407
+
408
+ if decorator != nil {
409
+ labels = decorator .UpdateLabels (f , labels )
410
+ }
411
+
368
412
for _ , label := range f .Labels {
369
413
if label .Key != nil && label .Value != nil {
370
414
if strings .HasPrefix (* label .Value , "{{" ) {
0 commit comments