Skip to content

Commit 3078ea8

Browse files
Surayya Huseyn ZadaSurayya Huseyn Zada
authored andcommitted
check if containerd store is enabled before loading
1 parent a0c3a56 commit 3078ea8

17 files changed

+113
-82
lines changed

src/Containers/Microsoft.NET.Build.Containers/ImagePublisher.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ await PushToLocalRegistryAsync(
7272
BuildEngine,
7373
telemetry,
7474
cancellationToken,
75-
destinationImageReference.LocalRegistry!.LoadAsync,
76-
logTipAboutContainerd : true).ConfigureAwait(false);
75+
destinationImageReference.LocalRegistry!.LoadAsync).ConfigureAwait(false);
7776
break;
7877
case DestinationImageReferenceKind.RemoteRegistry:
7978
await PushToRemoteRegistryAsync(
@@ -110,8 +109,7 @@ private static async Task PushToLocalRegistryAsync<T>(
110109
IBuildEngine? BuildEngine,
111110
Telemetry telemetry,
112111
CancellationToken cancellationToken,
113-
Func<T, SourceImageReference, DestinationImageReference, CancellationToken, Task> loadFunc,
114-
bool logTipAboutContainerd = false)
112+
Func<T, SourceImageReference, DestinationImageReference, CancellationToken, Task> loadFunc)
115113
{
116114
ILocalRegistry localRegistry = destinationImageReference.LocalRegistry!;
117115
if (!(await localRegistry.IsAvailableAsync(cancellationToken).ConfigureAwait(false)))
@@ -148,10 +146,6 @@ private static async Task PushToLocalRegistryAsync<T>(
148146
{
149147
telemetry.LogLocalLoadError();
150148
Log.LogErrorFromException(dle, showStackTrace: false);
151-
if (logTipAboutContainerd && dle.Message.Contains("no such file or directory"))
152-
{
153-
Log.LogMessage(MessageImportance.High, Strings.TipToEnableContainerdForMultiArch);
154-
}
155149
}
156150
}
157151

src/Containers/Microsoft.NET.Build.Containers/LocalDaemons/DockerCli.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,16 @@ private async Task LoadAsync<T>(
8787
SourceImageReference sourceReference,
8888
DestinationImageReference destinationReference,
8989
Func<T, SourceImageReference, DestinationImageReference, Stream, CancellationToken, Task> writeStreamFunc,
90-
CancellationToken cancellationToken)
90+
CancellationToken cancellationToken,
91+
bool checkContainerdStore = false)
9192
{
9293
cancellationToken.ThrowIfCancellationRequested();
9394

95+
if (checkContainerdStore && !IsContainerdStoreEnabledForDocker())
96+
{
97+
throw new DockerLoadException(Strings.ImageLoadFailed_ContainerdStoreDisabled);
98+
}
99+
94100
string commandPath = await FindFullCommandPath(cancellationToken);
95101

96102
// call `docker load` and get it ready to receive input
@@ -125,7 +131,7 @@ public async Task LoadAsync(BuiltImage image, SourceImageReference sourceReferen
125131
=> await LoadAsync(image, sourceReference, destinationReference, WriteDockerImageToStreamAsync, cancellationToken);
126132

127133
public async Task LoadAsync(BuiltImage[] images, SourceImageReference sourceReference, DestinationImageReference destinationReference, CancellationToken cancellationToken)
128-
=> await LoadAsync(images, sourceReference, destinationReference, WriteMultiArchOciImageToStreamAsync, cancellationToken);
134+
=> await LoadAsync(images, sourceReference, destinationReference, WriteMultiArchOciImageToStreamAsync, cancellationToken, checkContainerdStore: true);
129135

130136
public async Task<bool> IsAvailableAsync(CancellationToken cancellationToken)
131137
{
@@ -619,6 +625,37 @@ private static bool IsPodmanAlias()
619625
}
620626
}
621627

628+
private static bool IsContainerdStoreEnabledForDocker()
629+
{
630+
try
631+
{
632+
// We don't need to check if this is docker, because there is no "DriverStatus" for podman
633+
if (!GetDockerConfig().RootElement.TryGetProperty("DriverStatus", out var driverStatus) || driverStatus.ValueKind != JsonValueKind.Array)
634+
{
635+
return false;
636+
}
637+
638+
foreach (var item in driverStatus.EnumerateArray())
639+
{
640+
if (item.ValueKind != JsonValueKind.Array || item.GetArrayLength() != 2) continue;
641+
642+
var array = item.EnumerateArray().ToArray();
643+
// The usual output is [driver-type io.containerd.snapshotter.v1]
644+
if (array[0].GetString() == "driver-type" && array[1].GetString()!.StartsWith("io.containerd.snapshotter"))
645+
{
646+
return true;
647+
}
648+
}
649+
650+
return false;
651+
}
652+
catch
653+
{
654+
return false;
655+
}
656+
}
657+
658+
622659
#if NET
623660
private async Task<bool> TryRunVersionCommandAsync(string command, CancellationToken cancellationToken)
624661
{

src/Containers/Microsoft.NET.Build.Containers/Resources/Strings.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Containers/Microsoft.NET.Build.Containers/Resources/Strings.resx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@
189189
<value>CONTAINER1009: Failed to load image from local registry. stdout: {0}</value>
190190
<comment>{StrBegin="CONTAINER1009: "}</comment>
191191
</data>
192+
<data name="ImageLoadFailed_ContainerdStoreDisabled" xml:space="preserve">
193+
<value>CONTAINER1020: Failed to load image because containerd image store is not enabled for Docker. Tip: You can enable it by checking 'Use containerd for pulling and storing images' in Docker Desktop settings.</value>
194+
<comment>{StrBegin="CONTAINER1020: "}</comment>
195+
</data>
192196
<data name="InvalidEnvVar" xml:space="preserve">
193197
<value>CONTAINER2015: {0}: '{1}' was not a valid Environment Variable. Ignoring.</value>
194198
<comment>{StrBegin="CONTAINER2015: "}</comment>
@@ -401,10 +405,6 @@
401405
<value>Image index creation for Podman is not supported.</value>
402406
<comment/>
403407
</data>
404-
<data name="TipToEnableContainerdForMultiArch" xml:space="preserve">
405-
<value>Tip: For multi-arch image publishing, ensure that 'Use containerd for pulling and storing images' is checked in Docker Desktop settings.</value>
406-
<comment/>
407-
</data>
408408
<data name="LocalDocker_FailedToGetConfig" xml:space="preserve">
409409
<value>Error while reading daemon config: {0}</value>
410410
<comment>{0} is the exception message that ends with period</comment>

src/Containers/Microsoft.NET.Build.Containers/Resources/xlf/Strings.cs.xlf

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Containers/Microsoft.NET.Build.Containers/Resources/xlf/Strings.de.xlf

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Containers/Microsoft.NET.Build.Containers/Resources/xlf/Strings.es.xlf

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Containers/Microsoft.NET.Build.Containers/Resources/xlf/Strings.fr.xlf

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Containers/Microsoft.NET.Build.Containers/Resources/xlf/Strings.it.xlf

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Containers/Microsoft.NET.Build.Containers/Resources/xlf/Strings.ja.xlf

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)