@@ -21,8 +21,8 @@ const BbPromise = require('bluebird');
21
21
const Api = require ( 'kubernetes-client' ) ;
22
22
const CRD = require ( './crd' ) ;
23
23
const helpers = require ( './helpers' ) ;
24
- const ingressHelper = require ( './ingress' ) ;
25
24
const moment = require ( 'moment' ) ;
25
+ const url = require ( 'url' ) ;
26
26
27
27
/** Supported message queue types */
28
28
const MQTypes = Object . freeze ( { kafka : 'Kafka' , nats : 'NATS' } ) ;
@@ -434,6 +434,49 @@ function deployScheduleTrigger(name, namespace, schedule, options) {
434
434
} ) ;
435
435
}
436
436
437
+ function deployHttpTrigger ( name , namespace , path , options ) {
438
+ if ( options . ingress . enableTlsAcme && options . ingress . tlsSecretName ) {
439
+ throw new Error ( 'Cannot specify both enableTlsAcme and tlsSecretName' ) ;
440
+ }
441
+ // fix for issue https://github.com/kubeless/kubeless/issues/1124
442
+ const baseAnnotations = {
443
+ 'kubernetes.io/ingress.class' : options . ingress . class || 'nginx' ,
444
+ } ;
445
+ // end fix
446
+ const trigger = {
447
+ apiVersion : 'kubeless.io/v1beta1' ,
448
+ kind : 'HTTPTrigger' ,
449
+ metadata : {
450
+ name,
451
+ namespace,
452
+ labels : {
453
+ 'created-by' : 'kubeless' ,
454
+ } ,
455
+ annotations : Object . assign ( baseAnnotations , options . ingress . additionalAnnotations ) ,
456
+ } ,
457
+ spec : {
458
+ 'basic-auth-secret' : options . ingress . basicAuthSecretName || '' ,
459
+ 'cors-enable' : options . ingress . cors || false ,
460
+ 'function-name' : name ,
461
+ gateway : options . ingress . class || 'nginx' , // not working: https://github.com/kubeless/kubeless/issues/1124
462
+ 'host-name' : options . hostname ,
463
+ path : path . replace ( / ^ \/ / , '' ) ,
464
+ tls : options . ingress . enableTlsAcme || false ,
465
+ 'tls-secret' : options . ingress . tlsSecretName || '' ,
466
+ } ,
467
+ } ;
468
+ const httpTriggerApi = new CRD ( 'apis/kubeless.io' , 'v1beta1' , namespace , 'httptriggers' ) ;
469
+ options . log ( `Creating http trigger for: ${ trigger . metadata . name } ` ) ;
470
+ return httpTriggerApi . getItem ( trigger . metadata . name )
471
+ . then ( ( res ) => {
472
+ if ( res . code === 404 ) {
473
+ return httpTriggerApi . post ( { body : trigger } ) ;
474
+ }
475
+ options . log ( 'Updating existing http trigger' ) ;
476
+ return httpTriggerApi . put ( trigger . metadata . name , { body : trigger } ) ;
477
+ } ) ;
478
+ }
479
+
437
480
function deployFunction ( f , namespace , runtime , contentType , options ) {
438
481
const functionsApi = new CRD ( 'apis/kubeless.io' , 'v1beta1' , namespace , 'functions' ) ;
439
482
let environment = options . environment ? parseEnv ( options . environment ) : null ;
@@ -527,10 +570,27 @@ function handleMQTDeployment(trigger, name, namespace, options) {
527
570
}
528
571
529
572
function deployTrigger ( event , funcName , namespace , service , options ) {
530
- let triggerPromise = new BbPromise ( ( r ) => r ( ) ) ;
573
+ let triggerPromise ;
574
+ let config ;
575
+ let defaultHostname ;
531
576
switch ( event . type ) {
532
577
case 'http' :
533
- // TODO: Rely on Kubeless httptrigger object when it support paths
578
+ if ( _ . isEmpty ( event . path ) ) {
579
+ throw new Error ( 'You should specify a path for the trigger event' ) ;
580
+ }
581
+ config = helpers . loadKubeConfig ( ) ;
582
+ // eslint-disable-next-line max-len
583
+ defaultHostname = `${ url . parse ( helpers . getKubernetesAPIURL ( config ) ) . hostname } .${ options . defaultDNSResolution || 'nip.io' } ` ;
584
+ triggerPromise = deployHttpTrigger (
585
+ funcName ,
586
+ namespace ,
587
+ event . path ,
588
+ {
589
+ log : options . log ,
590
+ ingress : options . ingress || { } ,
591
+ hostname : event . hostname || options . hostname || defaultHostname ,
592
+ }
593
+ ) ;
534
594
break ;
535
595
case 'trigger' :
536
596
if ( _ . isEmpty ( event . trigger ) ) {
@@ -586,38 +646,20 @@ function deploy(functions, runtime, service, options) {
586
646
if ( res . code && res . code !== 200 ) {
587
647
errors . push ( res . message ) ;
588
648
}
589
- counter ++ ;
590
- helpers . checkFinished ( counter , elements , errors , resolve , reject , {
591
- onSuccess : ( ) => ingressHelper . addIngressRuleIfNecessary ( service , functions , {
592
- verbose : options . verbose ,
593
- log : options . log ,
594
- hostname : options . hostname ,
595
- defaultDNSResolution : options . defaultDNSResolution ,
596
- ingress : options . ingress ,
597
- namespace : ns ,
598
- } ) ,
649
+ _ . each ( description . events , event => {
650
+ deployTrigger ( event , description . id , ns , service , opts )
651
+ . catch ( triggerErr => errors . push ( triggerErr ) )
652
+ . then ( ( tr ) => {
653
+ if ( tr && tr . code && tr . code !== 200 ) {
654
+ errors . push ( tr . message ) ;
655
+ }
656
+ counter ++ ;
657
+ helpers . checkFinished ( counter , elements , errors , resolve , reject ) ;
658
+ } ) ;
599
659
} ) ;
660
+ counter ++ ;
661
+ helpers . checkFinished ( counter , elements , errors , resolve , reject ) ;
600
662
} ) ;
601
- _ . each ( description . events , event => {
602
- deployTrigger ( event , description . id , ns , service , opts )
603
- . catch ( triggerErr => errors . push ( triggerErr ) )
604
- . then ( ( res ) => {
605
- if ( res && res . code && res . code !== 200 ) {
606
- errors . push ( res . message ) ;
607
- }
608
- counter ++ ;
609
- helpers . checkFinished ( counter , elements , errors , resolve , reject , {
610
- onSuccess : ( ) => ingressHelper . addIngressRuleIfNecessary ( service , functions , {
611
- verbose : options . verbose ,
612
- log : options . log ,
613
- hostname : options . hostname ,
614
- defaultDNSResolution : options . defaultDNSResolution ,
615
- ingress : options . ingress ,
616
- namespace : ns ,
617
- } ) ,
618
- } ) ;
619
- } ) ;
620
- } ) ;
621
663
} else {
622
664
counter ++ ;
623
665
opts . log (
0 commit comments