@@ -25,8 +25,8 @@ namespace Synapse.Runtime.Docker.Services;
25
25
/// <param name="serviceProvider">The current <see cref="IServiceProvider"/></param>
26
26
/// <param name="loggerFactory">The service used to create <see cref="ILogger"/>s</param>
27
27
/// <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 )
30
30
: WorkflowRuntimeBase ( loggerFactory )
31
31
{
32
32
@@ -41,9 +41,14 @@ public class DockerRuntime(IServiceProvider serviceProvider, ILoggerFactory logg
41
41
protected IHostEnvironment Environment { get ; } = environment ;
42
42
43
43
/// <summary>
44
- /// Gets the current <see cref="RunnerConfiguration"/>
44
+ /// Gets the service used to access the current <see cref="Resources. RunnerConfiguration"/>
45
45
/// </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 ;
47
52
48
53
/// <summary>
49
54
/// Gets the service used to interact with the Docker API
@@ -62,9 +67,9 @@ public class DockerRuntime(IServiceProvider serviceProvider, ILoggerFactory logg
62
67
/// <returns>A new awaitable <see cref="Task"/></returns>
63
68
protected virtual Task InitializeAsync ( CancellationToken cancellationToken = default )
64
69
{
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 ! ) ) ;
68
73
return Task . CompletedTask ;
69
74
}
70
75
@@ -78,7 +83,7 @@ public override async Task<IWorkflowProcess> CreateProcessAsync(Workflow workflo
78
83
{
79
84
this . Logger . LogDebug ( "Creating a new Docker container for workflow instance '{workflowInstance}'..." , workflowInstance . GetQualifiedName ( ) ) ;
80
85
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 ( ) ! ;
82
87
try
83
88
{
84
89
await this . Docker ! . Images . InspectImageAsync ( container . Image , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -93,24 +98,24 @@ public override async Task<IWorkflowProcess> CreateProcessAsync(Workflow workflo
93
98
}
94
99
container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Runner . Namespace , workflowInstance . GetNamespace ( ) ! ) ;
95
100
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 ) ;
100
105
container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . ServiceAccount . Name , serviceAccount . GetQualifiedName ( ) ) ;
101
106
container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . ServiceAccount . Key , serviceAccount . Spec . Key ) ;
102
107
container . SetEnvironmentVariable ( SynapseDefaults . EnvironmentVariables . Workflow . Instance , workflowInstance . GetQualifiedName ( ) ) ;
103
108
container . SetEnvironmentVariable ( "DOCKER_HOST" , "unix:///var/run/docker.sock" ) ;
104
109
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 ) ;
108
113
hostConfig . Mounts ??= [ ] ;
109
114
hostConfig . Mounts . Insert ( 0 , new ( )
110
115
{
111
116
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
114
119
} ) ;
115
120
hostConfig . Mounts . Insert ( 1 , new ( )
116
121
{
@@ -128,7 +133,7 @@ public override async Task<IWorkflowProcess> CreateProcessAsync(Workflow workflo
128
133
HostConfig = hostConfig
129
134
} ;
130
135
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 ) ;
132
137
if ( result . Warnings . Count > 0 ) this . Logger . LogWarning ( "Warnings have been raised during container creation: {warnings}" , string . Join ( System . Environment . NewLine , result . Warnings ) ) ;
133
138
var process = ActivatorUtilities . CreateInstance < DockerWorkflowProcess > ( this . ServiceProvider , this . Docker ! , result . ID ) ;
134
139
this . Processes . TryAdd ( process . Id , process ) ;
0 commit comments