import { Ingress , InitContainers , EnvironmentVariables , Context , Service , ServiceDefinition , ExtraValues , Resources , ReplicaCount , PostgresInfo , HealthProbe , Features , Secrets , ValueType , XroadConfig , MountedFile , } from './types/input-types' ; export class ServiceBuilder < ServiceType > implements Service { extraAttributes ( attr ) { this . serviceDef . extraAttributes = attr ; return this ; } serviceDef : ServiceDefinition ; liveness ( path ) { if ( typeof path === 'string' ) { this . serviceDef . liveness . path = path ; } else { this . serviceDef . liveness = { ... this . serviceDef . liveness , ... path } ; } return this ; } readiness ( path ) { if ( typeof path === 'string' ) { this . serviceDef . readiness . path = path ; } else { this . serviceDef . readiness = { ... this . serviceDef . readiness , ... path } ; } return this ; } targetPort ( port ) { this . serviceDef . port = port ; return this ; } features ( features ) { this . serviceDef . features = features ; return this ; } constructor ( name ) { this . serviceDef = { liveness : { path : '/' , timeoutSeconds : 3 , initialDelaySeconds : 3 } , readiness : { path : '/' , timeoutSeconds : 3 , initialDelaySeconds : 3 } , env : { } , features : { } , name : name , grantNamespaces : [ ] , grantNamespacesEnabled : false , secrets : { CONFIGCAT_SDK_KEY : '/k8s/configcat/CONFIGCAT_SDK_KEY' } , ingress : { } , namespace : 'islandis' , serviceAccountEnabled : false , securityContext : { privileged : false , allowPrivilegeEscalation : false , } , xroadConfig : [ ] , files : [ ] , } ; } param name namespace ( name ) { this . serviceDef . namespace = name ; return this ; } param namespaces grantNamespaces ( ... namespaces ) { this . serviceDef . grantNamespaces = namespaces ; this . serviceDef . grantNamespacesEnabled = true ; return this ; } image ( name ) { this . serviceDef . image = name ; return this ; } setNamespace ( name ) { this . serviceDef . namespace = name ; } name ( ) { return this . serviceDef . name ; } private assertUnset < T > ( current , envs ) { const intersection = Object . keys ( { ... current , } ) . filter ( { } . hasOwnProperty . bind ( envs ) ) ; if ( intersection . length ) { throw new Error ( `Trying to set same environment variable multiple times: ${ intersection }` ) ; } } param key param value env ( envs ) { this . assertUnset ( this . serviceDef . env , envs ) ; this . serviceDef . env = { ... this . serviceDef . env , ... envs } ; return this ; } param . . configs xroad ( ... configs ) { this . serviceDef . xroadConfig = [ ... this . serviceDef . xroadConfig , ... configs ] ; return this ; } param . . files files ( ... files ) { this . serviceDef . files = [ ... this . serviceDef . files , ... files ] ; return this ; } param ic initContainer ( ic ) { if ( ic . postgres ) { ic . postgres = this . withDefaults ( ic . postgres ) ; } const uniqueNames = new Set ( ic . containers . map ( ( c ) => c . name ) ) ; if ( uniqueNames . size != ic . containers . length ) { throw new Error ( 'For multiple init containers, you must set a unique name for each container.' ) ; } this . serviceDef . initContainers = ic ; return this ; } param secrets secrets ( secrets ) { this . assertUnset ( this . serviceDef . secrets , secrets ) ; this . serviceDef . secrets = { ... this . serviceDef . secrets , ... secrets } ; return this ; } command ( cmd ) { this . serviceDef . cmds = cmd ; return this ; } args ( ... args ) { this . serviceDef . args = args ; return this ; } resources ( res ) { this . serviceDef . resources = res ; if ( res . limits && res . limits . memory ) { if ( this . serviceDef . env . NODE_OPTIONS ) { throw new Error ( 'NODE_OPTIONS already set. At the moment of writing, there is no known use case for this, so this might need to be revisited in the future.' ) ; } this . env ( { NODE_OPTIONS : `--max-old-space-size=${ parseInt ( res . limits . memory , 10 ) - 48 }` , } ) ; } return this ; } replicaCount ( replicaCount ) { this . serviceDef . replicaCount = replicaCount ; return this ; } private withDefaults = ( pi ) : PostgresInfo => { return { host : pi . host , username : pi . username ?? postgresIdentifier ( this . serviceDef . name ) , passwordSecret : pi . passwordSecret ?? `/k8s/${ this . serviceDef . name }/DB_PASSWORD` , name : pi . name ?? postgresIdentifier ( this . serviceDef . name ) , } ; } ; postgres ( postgres ? ) { this . serviceDef . postgres = this . withDefaults ( postgres ?? { } ) ; return this ; } redis ( host ? ) { return this ; } param ingress ingress ( ingress ) { this . serviceDef . ingress = ingress ; return this ; } param name serviceAccount ( name ? ) { this . serviceDef . accountName = name ?? this . serviceDef . name ; this . serviceDef . serviceAccountEnabled = true ; return this ; } } const postgresIdentifier = ( id ) => id . replace ( /[\W\s]/gi , '_' ) ; export const ref = ( renderer ) => { return renderer ; } ; export const service = < Service extends string > ( name ) : ServiceBuilder < Service > => { return new ServiceBuilder ( name ) ; } ; export const json = ( value ) : ValueType => JSON . stringify ( value ) ;