Skip to content

Commit

Permalink
Add support for specifying a pull policy for modules (#1233)
Browse files Browse the repository at this point in the history
Add support for specifying an image pull policy for modules
  • Loading branch information
shantanu1singh authored Jun 17, 2019
1 parent 968148c commit a39543c
Show file tree
Hide file tree
Showing 70 changed files with 1,114 additions and 343 deletions.
13 changes: 13 additions & 0 deletions THIRDPARTYNOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -3121,6 +3121,19 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

***

zonyitoo/rust-ini
The MIT License (MIT)

Copyright (c) 2014 Y. T. CHUNG

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

***

fizyk20/generic-array
The MIT License (MIT)
Copyright (c) 2015 Bartlomiej Kaminski
Expand Down
14 changes: 14 additions & 0 deletions edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Core/IModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Core
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
Expand Down Expand Up @@ -81,6 +82,16 @@ public enum RestartPolicy
Unknown
}

[JsonConverter(typeof(StringEnumConverter))]
public enum ImagePullPolicy
{
[EnumMember(Value = "on-create")]
OnCreate = 0,

[EnumMember(Value = "never")]
Never = 1,
}

public interface IModule : IEquatable<IModule>
{
[JsonIgnore]
Expand All @@ -98,6 +109,9 @@ public interface IModule : IEquatable<IModule>
[JsonProperty(PropertyName = "restartPolicy")]
RestartPolicy RestartPolicy { get; }

[JsonProperty(PropertyName = "imagePullPolicy")]
ImagePullPolicy ImagePullPolicy { get; }

[JsonIgnore]
ConfigurationInfo ConfigurationInfo { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public virtual string Name

public virtual RestartPolicy RestartPolicy => RestartPolicy.Never;

public virtual ImagePullPolicy ImagePullPolicy => ImagePullPolicy.OnCreate;

public virtual ConfigurationInfo ConfigurationInfo => new ConfigurationInfo();

public IDictionary<string, EnvVal> Env { get; } = ImmutableDictionary<string, EnvVal>.Empty;
Expand Down Expand Up @@ -58,6 +60,9 @@ public class UnknownEdgeAgentModule : UnknownModule, IEdgeAgentModule
[JsonIgnore]
public override RestartPolicy RestartPolicy => RestartPolicy.Never;

[JsonIgnore]
public override ImagePullPolicy ImagePullPolicy => ImagePullPolicy.OnCreate;

[JsonIgnore]
public override ModuleStatus DesiredStatus => ModuleStatus.Unknown;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.Azure.Devices.Edge.Agent.Docker
{
using System.Collections.Generic;
using System.Threading.Tasks;
using global::Docker.DotNet;
using Microsoft.Azure.Devices.Edge.Agent.Core;
Expand Down Expand Up @@ -30,9 +31,15 @@ public async Task<ICommand> CreateAsync(IModuleWithIdentity module, IRuntimeInfo
if (module.Module is DockerModule dockerModule)
{
CombinedDockerConfig combinedDockerConfig = this.combinedConfigProvider.GetCombinedConfig(dockerModule, runtimeInfo);
return new GroupCommand(
new PullCommand(this.client, combinedDockerConfig),
await CreateCommand.BuildAsync(this.client, dockerModule, module.ModuleIdentity, this.dockerLoggerConfig, this.configSource, module.Module is EdgeHubDockerModule));

var commands = new List<ICommand>();
if (module.Module.ImagePullPolicy != ImagePullPolicy.Never)
{
commands.Add(new PullCommand(this.client, combinedDockerConfig));
}

commands.Add(await CreateCommand.BuildAsync(this.client, dockerModule, module.ModuleIdentity, this.dockerLoggerConfig, this.configSource, module.Module is EdgeHubDockerModule));
return new GroupCommand(commands.ToArray());
}

return NullCommand.Instance;
Expand All @@ -43,11 +50,20 @@ public async Task<ICommand> UpdateAsync(IModule current, IModuleWithIdentity nex
if (current is DockerModule currentDockerModule && next.Module is DockerModule nextDockerModule)
{
CombinedDockerConfig combinedDockerConfig = this.combinedConfigProvider.GetCombinedConfig(nextDockerModule, runtimeInfo);
return new GroupCommand(
new PullCommand(this.client, combinedDockerConfig),
new StopCommand(this.client, currentDockerModule),
new RemoveCommand(this.client, currentDockerModule),
await CreateCommand.BuildAsync(this.client, nextDockerModule, next.ModuleIdentity, this.dockerLoggerConfig, this.configSource, next.Module is EdgeHubDockerModule));
var commands = new List<ICommand>();
if (next.Module.ImagePullPolicy != ImagePullPolicy.Never)
{
commands.Add(new PullCommand(this.client, combinedDockerConfig));
}

commands.AddRange(
new ICommand[]
{
new StopCommand(this.client, currentDockerModule),
new RemoveCommand(this.client, currentDockerModule),
await CreateCommand.BuildAsync(this.client, nextDockerModule, next.ModuleIdentity, this.dockerLoggerConfig, this.configSource, next.Module is EdgeHubDockerModule)
});
return new GroupCommand(commands.ToArray());
}

return NullCommand.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class DockerDesiredModule : DockerModule
RestartPolicy restartPolicy,
string type,
DockerConfig settings,
ImagePullPolicy imagePullPolicy,
ConfigurationInfo configuration,
IDictionary<string, EnvVal> env)
: base(string.Empty, version, desiredStatus, restartPolicy, settings, configuration, env)
: base(string.Empty, version, desiredStatus, restartPolicy, settings, imagePullPolicy, configuration, env)
{
Preconditions.CheckArgument(type?.Equals("docker") ?? false);
this.DesiredStatus = Preconditions.CheckIsDefined(desiredStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task<ModuleSet> GetModulesAsync(CancellationToken token)

if (!moduleSet.Modules.TryGetValue(dockerRuntimeInfo.Name, out IModule configModule) || !(configModule is DockerModule dockerModule))
{
dockerModule = new DockerModule(dockerRuntimeInfo.Name, string.Empty, ModuleStatus.Unknown, Core.RestartPolicy.Unknown, new DockerConfig(Constants.UnknownImage, new CreateContainerParameters()), new ConfigurationInfo(), null);
dockerModule = new DockerModule(dockerRuntimeInfo.Name, string.Empty, ModuleStatus.Unknown, Core.RestartPolicy.Unknown, new DockerConfig(Constants.UnknownImage, new CreateContainerParameters()), ImagePullPolicy.OnCreate, new ConfigurationInfo(), null);
}

Option<ModuleState> moduleStateOption = await this.moduleStateStore.Get(moduleRuntimeInfo.Name);
Expand Down Expand Up @@ -89,6 +89,7 @@ public async Task<ModuleSet> GetModulesAsync(CancellationToken token)
moduleState.RestartCount,
moduleState.LastRestartTimeUtc,
moduleRuntimeStatus,
dockerModule.ImagePullPolicy,
dockerModule.ConfigurationInfo,
dockerModule.Env);
break;
Expand All @@ -101,6 +102,7 @@ public async Task<ModuleSet> GetModulesAsync(CancellationToken token)
dockerRuntimeInfo.Description,
dockerRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
lastExitTime,
dockerModule.ImagePullPolicy,
dockerModule.ConfigurationInfo,
dockerModule.Env);
break;
Expand All @@ -119,6 +121,7 @@ public async Task<ModuleSet> GetModulesAsync(CancellationToken token)
moduleState.RestartCount,
moduleState.LastRestartTimeUtc,
moduleRuntimeStatus,
dockerModule.ImagePullPolicy,
dockerModule.ConfigurationInfo,
dockerModule.Env);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public DockerModule(
ModuleStatus desiredStatus,
RestartPolicy restartPolicy,
DockerConfig config,
ImagePullPolicy imagePullPolicy,
ConfigurationInfo configurationInfo,
IDictionary<string, EnvVal> env)
{
Expand All @@ -25,6 +26,7 @@ public DockerModule(
this.DesiredStatus = Preconditions.CheckIsDefined(desiredStatus);
this.Config = Preconditions.CheckNotNull(config, nameof(config));
this.RestartPolicy = Preconditions.CheckIsDefined(restartPolicy);
this.ImagePullPolicy = Preconditions.CheckIsDefined(imagePullPolicy);
this.ConfigurationInfo = configurationInfo ?? new ConfigurationInfo(string.Empty);
this.Env = env?.ToImmutableDictionary() ?? ImmutableDictionary<string, EnvVal>.Empty;
}
Expand All @@ -41,6 +43,9 @@ public DockerModule(
[JsonProperty(PropertyName = "restartPolicy")]
public virtual RestartPolicy RestartPolicy { get; }

[JsonProperty(PropertyName = "imagePullPolicy")]
public virtual ImagePullPolicy ImagePullPolicy { get; }

[JsonProperty(Required = Required.Always, PropertyName = "type")]
public virtual string Type => "docker";

Expand Down Expand Up @@ -75,6 +80,7 @@ public virtual bool Equals(IModule<DockerConfig> other)
this.DesiredStatus == other.DesiredStatus &&
this.Config.Equals(other.Config) &&
this.RestartPolicy == other.RestartPolicy &&
this.ImagePullPolicy == other.ImagePullPolicy &&
EnvDictionaryComparer.Equals(this.Env, other.Env);
}

Expand All @@ -87,6 +93,7 @@ public virtual bool OnlyModuleStatusChanged(IModule other)
this.DesiredStatus != other.DesiredStatus &&
this.Config.Equals(dockerModule.Config) &&
this.RestartPolicy == other.RestartPolicy &&
this.ImagePullPolicy == other.ImagePullPolicy &&
EnvDictionaryComparer.Equals(this.Env, other.Env);
}

Expand All @@ -104,6 +111,7 @@ public override int GetHashCode()
hashCode = (hashCode * 397) ^ (int)this.DesiredStatus;
hashCode = (hashCode * 397) ^ (this.Config != null ? this.Config.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ this.RestartPolicy.GetHashCode();
hashCode = (hashCode * 397) ^ this.ImagePullPolicy.GetHashCode();
hashCode = (hashCode * 397) ^ EnvDictionaryComparer.GetHashCode(this.Env);
return hashCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public DockerRuntimeModule(
int restartCount,
DateTime lastRestartTime,
ModuleStatus runtimeStatus,
ImagePullPolicy imagePullPolicy,
ConfigurationInfo configuration,
IDictionary<string, EnvVal> env)
: base(name, version, desiredStatus, restartPolicy, config, configuration, env)
: base(name, version, desiredStatus, restartPolicy, config, imagePullPolicy, configuration, env)
{
this.ExitCode = exitCode;
this.StatusDescription = statusDescription;
Expand All @@ -50,6 +51,7 @@ public DockerRuntimeModule(
int restartCount,
DateTime lastRestartTimeUtc,
ModuleStatus runtimeStatus,
ImagePullPolicy imagePullPolicy,
ConfigurationInfo configurationInfo,
IDictionary<string, EnvVal> env)
: this(
Expand All @@ -65,6 +67,7 @@ public DockerRuntimeModule(
restartCount,
lastRestartTimeUtc,
runtimeStatus,
imagePullPolicy,
configurationInfo,
env)
{
Expand Down Expand Up @@ -153,6 +156,7 @@ public override int GetHashCode()
this.RestartCount,
this.LastRestartTimeUtc,
newStatus,
this.ImagePullPolicy,
this.ConfigurationInfo,
this.Env);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Docker
public class EdgeAgentDockerModule : DockerModule, IEdgeAgentModule
{
[JsonConstructor]
public EdgeAgentDockerModule(string type, DockerConfig settings, ConfigurationInfo configuration, IDictionary<string, EnvVal> env, string version = "")
: base(Core.Constants.EdgeAgentModuleName, version, ModuleStatus.Running, RestartPolicy.Always, settings, configuration, env)
public EdgeAgentDockerModule(string type, DockerConfig settings, ImagePullPolicy imagePullPolicy, ConfigurationInfo configuration, IDictionary<string, EnvVal> env, string version = "")
: base(Core.Constants.EdgeAgentModuleName, version, ModuleStatus.Running, RestartPolicy.Always, settings, imagePullPolicy, configuration, env)
{
Preconditions.CheckArgument(type?.Equals("docker") ?? false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Docker
using System;
using System.Collections.Generic;
using Microsoft.Azure.Devices.Edge.Agent.Core;
using Microsoft.Azure.Devices.Edge.Util;
using Newtonsoft.Json;

public class EdgeAgentDockerRuntimeModule : DockerRuntimeModule, IEdgeAgentModule
Expand All @@ -16,6 +17,7 @@ public EdgeAgentDockerRuntimeModule(
string statusDescription,
DateTime lastStartTimeUtc,
DateTime lastExitTime,
ImagePullPolicy imagePullPolicy,
ConfigurationInfo configuration,
IDictionary<string, EnvVal> env,
string version = "")
Expand All @@ -32,6 +34,7 @@ public EdgeAgentDockerRuntimeModule(
0,
DateTime.MinValue,
runtimeStatus,
imagePullPolicy,
configuration,
env)
{
Expand Down Expand Up @@ -70,6 +73,7 @@ public EdgeAgentDockerRuntimeModule(
this.StatusDescription,
this.LastStartTimeUtc,
this.LastExitTimeUtc,
this.ImagePullPolicy,
this.ConfigurationInfo,
this.Env,
this.Version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public EdgeHubDockerModule(
ModuleStatus status,
RestartPolicy restartPolicy,
DockerConfig settings,
ImagePullPolicy imagePullPolicy,
ConfigurationInfo configuration,
IDictionary<string, EnvVal> env,
string version = "")
: base(Core.Constants.EdgeHubModuleName, version, status, restartPolicy, settings, configuration, env)
: base(Core.Constants.EdgeHubModuleName, version, status, restartPolicy, settings, imagePullPolicy, configuration, env)
{
Preconditions.CheckArgument(type?.Equals("docker") ?? false);
this.DesiredStatus = Preconditions.CheckIsDefined(status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public EdgeHubDockerRuntimeModule(
int restartCount,
DateTime lastRestartTime,
ModuleStatus runtimeStatus,
ImagePullPolicy imagePullPolicy,
ConfigurationInfo configuration,
IDictionary<string, EnvVal> env,
string version = "")
Expand All @@ -37,6 +38,7 @@ public EdgeHubDockerRuntimeModule(
restartCount,
lastRestartTime,
runtimeStatus,
imagePullPolicy,
configuration,
env)
{
Expand All @@ -63,6 +65,7 @@ public EdgeHubDockerRuntimeModule(
int restartCount,
DateTime lastRestartTimeUtc,
ModuleStatus runtimeStatus,
ImagePullPolicy imagePullPolicy,
ConfigurationInfo configurationInfo,
IDictionary<string, EnvVal> env,
string version = "")
Expand All @@ -77,6 +80,7 @@ public EdgeHubDockerRuntimeModule(
restartCount,
lastRestartTimeUtc,
runtimeStatus,
imagePullPolicy,
configurationInfo,
env,
version)
Expand Down Expand Up @@ -105,6 +109,7 @@ public EdgeHubDockerRuntimeModule(
this.RestartCount,
this.LastRestartTimeUtc,
newStatus,
this.ImagePullPolicy,
this.ConfigurationInfo,
this.Env);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public string Show()

static ModuleSpec BuildModuleSpec(IModule module, IEnumerable<EnvVar> envVars, object settings)
{
return new ModuleSpec(module.Name, module.Type, settings, envVars);
return new ModuleSpec(module.Name, module.Type, module.ImagePullPolicy, settings, envVars);
}

static IEnumerable<EnvVar> GetEnvVars(IDictionary<string, EnvVal> moduleEnvVars, IModuleIdentity identity, IConfigSource configSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Task ExecuteAsync(CancellationToken token)

static ModuleSpec BuildModuleSpec(IModule module, object settings)
{
return new ModuleSpec(module.Name, module.Type, settings);
return new ModuleSpec(module.Name, module.Type, module.ImagePullPolicy, settings);
}
}
}
Loading

0 comments on commit a39543c

Please sign in to comment.