@@ -25,8 +25,8 @@ namespace Synapse.Runtime.Docker.Services;
2525/// <param name="serviceProvider">The current <see cref="IServiceProvider"/></param>
2626/// <param name="loggerFactory">The service used to create <see cref="ILogger"/>s</param>
2727/// <param name="environment">The current <see cref="IHostEnvironment"/></param>
28- /// <param name="runner ">The service used to access the current <see cref="RunnerConfiguration"/></param>
29- public class DockerRuntime ( IServiceProvider serviceProvider , ILoggerFactory loggerFactory , IHostEnvironment environment , IOptions < RunnerConfiguration > runner )
28+ /// <param name="runnerConfigurationMonitor ">The service used to access the current <see cref="Resources. RunnerConfiguration"/></param>
29+ public class DockerRuntime ( IServiceProvider serviceProvider , ILoggerFactory loggerFactory , IHostEnvironment environment , IOptionsMonitor < RunnerConfiguration > runnerConfigurationMonitor )
3030 : WorkflowRuntimeBase ( loggerFactory )
3131{
3232
@@ -41,9 +41,14 @@ public class DockerRuntime(IServiceProvider serviceProvider, ILoggerFactory logg
4141 protected IHostEnvironment Environment { get ; } = environment ;
4242
4343 /// <summary>
44- /// Gets the current <see cref="RunnerConfiguration"/>
44+ /// Gets the service used to access the current <see cref="Resources. RunnerConfiguration"/>
4545 /// </summary>
46- protected RunnerConfiguration Runner => runner . Value ;
46+ protected IOptionsMonitor < RunnerConfiguration > RunnerConfigurationMonitor { get ; } = runnerConfigurationMonitor ;
47+
48+ /// <summary>
49+ /// Gets the current <see cref="Resources.RunnerConfiguration"/>
50+ /// </summary>
51+ protected RunnerConfiguration RunnerConfiguration => RunnerConfigurationMonitor . CurrentValue ;
4752
4853 /// <summary>
4954 /// Gets the service used to interact with the Docker API
@@ -62,9 +67,9 @@ public class DockerRuntime(IServiceProvider serviceProvider, ILoggerFactory logg
6267 /// <returns>A new awaitable <see cref="Task"/></returns>
6368 protected virtual Task InitializeAsync ( CancellationToken cancellationToken = default )
6469 {
65- if ( this . Runner . Runtime . Docker == null ) throw new NullReferenceException ( $ "Failed to initialize the Docker Runtime because the operator is not configured to use Docker as a runtime") ;
66- var dockerConfiguration = new DockerClientConfiguration ( this . Runner . Runtime . Docker . Api . Endpoint ) ;
67- this . Docker = dockerConfiguration . CreateClient ( string . IsNullOrWhiteSpace ( this . Runner . Runtime . Docker . Api . Version ) ? null : System . Version . Parse ( this . Runner . Runtime . Docker . Api . Version ! ) ) ;
70+ if ( this . RunnerConfiguration . Runtime . Docker == null ) throw new NullReferenceException ( $ "Failed to initialize the Docker Runtime because the operator is not configured to use Docker as a runtime") ;
71+ var dockerConfiguration = new DockerClientConfiguration ( this . RunnerConfiguration . Runtime . Docker . Api . Endpoint ) ;
72+ this . Docker = dockerConfiguration . CreateClient ( string . IsNullOrWhiteSpace ( this . RunnerConfiguration . Runtime . Docker . Api . Version ) ? null : System . Version . Parse ( this . RunnerConfiguration . Runtime . Docker . Api . Version ! ) ) ;
6873 return Task . CompletedTask ;
6974 }
7075
@@ -78,7 +83,7 @@ public override async Task<IWorkflowProcess> CreateProcessAsync(Workflow workflo
7883 {
7984 this . Logger . LogDebug ( "Creating a new Docker container for workflow instance '{workflowInstance}'..." , workflowInstance . GetQualifiedName ( ) ) ;
8085 if ( this . Docker == null ) await this . InitializeAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
81- var container = this . Runner . Runtime . Docker ! . ContainerTemplate . Clone ( ) ! ;
86+ var container = this . RunnerConfiguration . Runtime . Docker ! . ContainerTemplate . Clone ( ) ! ;
8287 try
8388 {
8489 await this . Docker ! . Images . InspectImageAsync ( container . Image , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -93,24 +98,24 @@ public override async Task<IWorkflowProcess> CreateProcessAsync(Workflow workflo
9398 }
9499 container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Runner . Namespace , workflowInstance . GetNamespace ( ) ! ) ;
95100 container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Runner . Name , $ "{ workflowInstance . GetName ( ) } -{ Guid . NewGuid ( ) . ToString ( "N" ) [ ..12 ] . ToLowerInvariant ( ) } ") ;
96- container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Api . Uri , this . Runner . Api . Uri . OriginalString ) ;
97- container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Runner . ContainerPlatform , this . Runner . ContainerPlatform ) ;
98- container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Runner . LifecycleEvents , ( this . Runner . PublishLifecycleEvents ?? true ) . ToString ( ) ) ;
99- container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Secrets . Directory , this . Runner . Runtime . Docker . Secrets . MountPath ) ;
101+ container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Api . Uri , this . RunnerConfiguration . Api . Uri . OriginalString ) ;
102+ container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Runner . ContainerPlatform , this . RunnerConfiguration . ContainerPlatform ) ;
103+ container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Runner . LifecycleEvents , ( this . RunnerConfiguration . PublishLifecycleEvents ?? true ) . ToString ( ) ) ;
104+ container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Secrets . Directory , this . RunnerConfiguration . Runtime . Docker . Secrets . MountPath ) ;
100105 container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . ServiceAccount . Name , serviceAccount . GetQualifiedName ( ) ) ;
101106 container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . ServiceAccount . Key , serviceAccount . Spec . Key ) ;
102107 container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Workflow . Instance , workflowInstance . GetQualifiedName ( ) ) ;
103108 container . SetEnvironmentVariable ( "DOCKER_HOST" , "unix:///var/run/docker.sock" ) ;
104109 container . User = "root" ;
105- if ( this . Runner . Certificates ? . Validate == false ) container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . SkipCertificateValidation , "true" ) ;
106- var hostConfig = this . Runner . Runtime . Docker . HostConfig ? . Clone ( ) ! ?? new ( ) ;
107- if ( ! Directory . Exists ( this . Runner . Runtime . Docker . Secrets . Directory ) ) Directory . CreateDirectory ( this . Runner . Runtime . Docker . Secrets . Directory ) ;
110+ if ( this . RunnerConfiguration . Certificates ? . Validate == false ) container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . SkipCertificateValidation , "true" ) ;
111+ var hostConfig = this . RunnerConfiguration . Runtime . Docker . HostConfig ? . Clone ( ) ! ?? new ( ) ;
112+ if ( ! Directory . Exists ( this . RunnerConfiguration . Runtime . Docker . Secrets . Directory ) ) Directory . CreateDirectory ( this . RunnerConfiguration . Runtime . Docker . Secrets . Directory ) ;
108113 hostConfig . Mounts ??= [ ] ;
109114 hostConfig . Mounts . Insert ( 0 , new ( )
110115 {
111116 Type = "bind" ,
112- Source = this . Runner . Runtime . Docker . Secrets . Directory ,
113- Target = this . Runner . Runtime . Docker . Secrets . MountPath
117+ Source = this . RunnerConfiguration . Runtime . Docker . Secrets . Directory ,
118+ Target = this . RunnerConfiguration . Runtime . Docker . Secrets . MountPath
114119 } ) ;
115120 hostConfig . Mounts . Insert ( 1 , new ( )
116121 {
@@ -128,7 +133,7 @@ public override async Task<IWorkflowProcess> CreateProcessAsync(Workflow workflo
128133 HostConfig = hostConfig
129134 } ;
130135 var result = await this . Docker ! . Containers . CreateContainerAsync ( parameters , cancellationToken ) . ConfigureAwait ( false ) ;
131- if ( this . Environment . RunsInDocker ( ) ) await this . Docker . Networks . ConnectNetworkAsync ( this . Runner . Runtime . Docker . Network , new NetworkConnectParameters ( ) { Container = result . ID } , cancellationToken ) ;
136+ if ( this . Environment . RunsInDocker ( ) ) await this . Docker . Networks . ConnectNetworkAsync ( this . RunnerConfiguration . Runtime . Docker . Network , new NetworkConnectParameters ( ) { Container = result . ID } , cancellationToken ) ;
132137 if ( result . Warnings . Count > 0 ) this . Logger . LogWarning ( "Warnings have been raised during container creation: {warnings}" , string . Join ( System . Environment . NewLine , result . Warnings ) ) ;
133138 var process = ActivatorUtilities . CreateInstance < DockerWorkflowProcess > ( this . ServiceProvider , this . Docker ! , result . ID ) ;
134139 this . Processes . TryAdd ( process . Id , process ) ;
0 commit comments