-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check configured platforms for single image building #2793
Changes from all commits
ceb6e29
d64ba0f
2919682
e89e511
70a3897
5ed7661
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,8 @@ static Callable<LocalImage> retrieveDockerDaemonLayersStep( | |
Optional<LocalImage> cachedImage = | ||
getCachedDockerImage(buildContext.getBaseImageLayersCache(), dockerImageDetails); | ||
if (cachedImage.isPresent()) { | ||
PlatformChecker.checkManifestPlatform( | ||
buildContext, cachedImage.get().configurationTemplate); | ||
return cachedImage.get(); | ||
} | ||
|
||
|
@@ -125,11 +127,14 @@ static Callable<LocalImage> retrieveDockerDaemonLayersStep( | |
dockerClient.save(imageReference, tarPath, throttledProgressReporter); | ||
} | ||
|
||
return cacheDockerImageTar( | ||
buildContext, | ||
tarPath, | ||
progressEventDispatcher.newChildProducer(), | ||
tempDirectoryProvider); | ||
LocalImage localImage = | ||
cacheDockerImageTar( | ||
buildContext, | ||
tarPath, | ||
progressEventDispatcher.newChildProducer(), | ||
tempDirectoryProvider); | ||
PlatformChecker.checkManifestPlatform(buildContext, localImage.configurationTemplate); | ||
return localImage; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using a local |
||
} | ||
}; | ||
} | ||
|
@@ -139,9 +144,13 @@ static Callable<LocalImage> retrieveTarLayersStep( | |
ProgressEventDispatcher.Factory progressEventDispatcherFactory, | ||
Path tarPath, | ||
TempDirectoryProvider tempDirectoryProvider) { | ||
return () -> | ||
cacheDockerImageTar( | ||
buildContext, tarPath, progressEventDispatcherFactory, tempDirectoryProvider); | ||
return () -> { | ||
LocalImage localImage = | ||
cacheDockerImageTar( | ||
buildContext, tarPath, progressEventDispatcherFactory, tempDirectoryProvider); | ||
PlatformChecker.checkManifestPlatform(buildContext, localImage.configurationTemplate); | ||
return localImage; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When |
||
}; | ||
} | ||
|
||
static Callable<ImagesAndRegistryClient> returnImageAndRegistryClientStep( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2020 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.cloud.tools.jib.builder.steps; | ||
|
||
import com.google.cloud.tools.jib.api.LogEvent; | ||
import com.google.cloud.tools.jib.api.buildplan.Platform; | ||
import com.google.cloud.tools.jib.configuration.BuildContext; | ||
import com.google.cloud.tools.jib.event.EventHandlers; | ||
import com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate; | ||
import com.google.common.base.Verify; | ||
import java.util.Set; | ||
|
||
/** Provides helper methods to check platforms. */ | ||
public class PlatformChecker { | ||
|
||
/** | ||
* Assuming the base image is not a manifest list, checks and warns misconfigured platforms. | ||
* | ||
* @param buildContext the {@link BuildContext} | ||
* @param containerConfig container configuration JSON of the base image | ||
*/ | ||
mpeddada1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static void checkManifestPlatform( | ||
BuildContext buildContext, ContainerConfigurationTemplate containerConfig) { | ||
EventHandlers eventHandlers = buildContext.getEventHandlers(); | ||
String baseImageName = | ||
buildContext.getBaseImageConfiguration().getTarPath().isPresent() | ||
? buildContext.getBaseImageConfiguration().getTarPath().get().toString() | ||
: buildContext.getBaseImageConfiguration().getImage().toString(); | ||
|
||
Set<Platform> platforms = buildContext.getContainerConfiguration().getPlatforms(); | ||
Verify.verify(!platforms.isEmpty()); | ||
|
||
if (platforms.size() != 1) { | ||
eventHandlers.dispatch( | ||
LogEvent.warn( | ||
"platforms configured, but '" + baseImageName + "' is not a manifest list")); | ||
} else { | ||
Platform platform = platforms.iterator().next(); | ||
if (!platform.getArchitecture().equals(containerConfig.getArchitecture()) | ||
|| !platform.getOs().equals(containerConfig.getOs())) { | ||
|
||
// Unfortunately, "platforms" has amd64/linux by default even if the user didn't explicitly | ||
// configure it. Skip reporting to suppress false alarm. | ||
if (!(platform.getArchitecture().equals("amd64") && platform.getOs().equals("linux"))) { | ||
mpeddada1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String warning = | ||
"the configured platform (%s/%s) doesn't match the platform (%s/%s) of the base " | ||
+ "image (%s)"; | ||
eventHandlers.dispatch( | ||
LogEvent.warn( | ||
String.format( | ||
warning, | ||
platform.getArchitecture(), | ||
platform.getOs(), | ||
containerConfig.getArchitecture(), | ||
containerConfig.getOs(), | ||
baseImageName))); | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -233,6 +233,7 @@ private List<Image> pullBaseImages( | |
BuildableManifestTemplate imageManifest = (BuildableManifestTemplate) manifestTemplate; | ||
ContainerConfigurationTemplate containerConfig = | ||
pullContainerConfigJson(manifestAndDigest, registryClient, progressEventDispatcher); | ||
PlatformChecker.checkManifestPlatform(buildContext, containerConfig); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When pulling a remote manifest (not cached). |
||
cache.writeMetadata(baseImageConfig.getImage(), imageManifest, containerConfig); | ||
return Collections.singletonList( | ||
JsonToImageTranslator.toImage(imageManifest, containerConfig)); | ||
|
@@ -379,10 +380,14 @@ List<Image> getCachedBaseImages() | |
return Collections.singletonList( | ||
JsonToImageTranslator.toImage((V21ManifestTemplate) manifest)); | ||
} | ||
|
||
ContainerConfigurationTemplate containerConfig = | ||
Verify.verifyNotNull(manifestsAndConfigs.get(0).getConfig()); | ||
PlatformChecker.checkManifestPlatform(buildContext, containerConfig); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using a cached manifest for a remote base image. |
||
|
||
return Collections.singletonList( | ||
JsonToImageTranslator.toImage( | ||
(BuildableManifestTemplate) Verify.verifyNotNull(manifest), | ||
Verify.verifyNotNull(manifestsAndConfigs.get(0).getConfig()))); | ||
(BuildableManifestTemplate) Verify.verifyNotNull(manifest), containerConfig)); | ||
} | ||
|
||
// Manifest list cached. Identify matching platforms and check if all of them are cached. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2020 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.cloud.tools.jib.builder.steps; | ||
|
||
import com.google.cloud.tools.jib.api.ImageReference; | ||
import com.google.cloud.tools.jib.api.LogEvent; | ||
import com.google.cloud.tools.jib.api.buildplan.Platform; | ||
import com.google.cloud.tools.jib.builder.ProgressEventDispatcher; | ||
import com.google.cloud.tools.jib.configuration.BuildContext; | ||
import com.google.cloud.tools.jib.configuration.ContainerConfiguration; | ||
import com.google.cloud.tools.jib.configuration.ImageConfiguration; | ||
import com.google.cloud.tools.jib.event.EventHandlers; | ||
import com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate; | ||
import com.google.cloud.tools.jib.registry.RegistryClient; | ||
import com.google.common.collect.ImmutableSet; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
/** Tests for {@link PlatformChecker}. */ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class PlatformCheckerTest { | ||
|
||
@Mock private ProgressEventDispatcher.Factory progressDispatcherFactory; | ||
@Mock private BuildContext buildContext; | ||
@Mock private RegistryClient registryClient; | ||
@Mock private ImageConfiguration imageConfiguration; | ||
@Mock private ContainerConfiguration containerConfig; | ||
@Mock private EventHandlers eventHandlers; | ||
|
||
@Before | ||
public void setUp() { | ||
Mockito.when(buildContext.getBaseImageConfiguration()) | ||
.thenReturn(ImageConfiguration.builder(ImageReference.scratch()).build()); | ||
Mockito.when(buildContext.getEventHandlers()).thenReturn(eventHandlers); | ||
Mockito.when(buildContext.getContainerConfiguration()).thenReturn(containerConfig); | ||
} | ||
|
||
@Test | ||
public void testCheckManifestPlatform_mismatch() { | ||
Mockito.when(containerConfig.getPlatforms()) | ||
.thenReturn(ImmutableSet.of(new Platform("configured arch", "configured OS"))); | ||
|
||
ContainerConfigurationTemplate containerConfigJson = new ContainerConfigurationTemplate(); | ||
containerConfigJson.setArchitecture("actual arch"); | ||
containerConfigJson.setOs("actual OS"); | ||
|
||
PlatformChecker.checkManifestPlatform(buildContext, containerConfigJson); | ||
|
||
Mockito.verify(eventHandlers) | ||
.dispatch( | ||
LogEvent.warn( | ||
"the configured platform (configured arch/configured OS) doesn't match the " | ||
+ "platform (actual arch/actual OS) of the base image (scratch)")); | ||
} | ||
|
||
@Test | ||
public void testCheckManifestPlatform_noWarningIfDefaultAmd64Linux() { | ||
Mockito.when(containerConfig.getPlatforms()) | ||
.thenReturn(ImmutableSet.of(new Platform("amd64", "linux"))); | ||
|
||
ContainerConfigurationTemplate containerConfigJson = new ContainerConfigurationTemplate(); | ||
containerConfigJson.setArchitecture("actual arch"); | ||
containerConfigJson.setOs("actual OS"); | ||
|
||
PlatformChecker.checkManifestPlatform(buildContext, containerConfigJson); | ||
|
||
Mockito.verifyNoInteractions(eventHandlers); | ||
} | ||
|
||
@Test | ||
public void testCheckManifestPlatform_multiplePlatformsConfigured() { | ||
Mockito.when(containerConfig.getPlatforms()) | ||
.thenReturn(ImmutableSet.of(new Platform("amd64", "linux"), new Platform("arch", "os"))); | ||
|
||
PlatformChecker.checkManifestPlatform(buildContext, new ContainerConfigurationTemplate()); | ||
|
||
Mockito.verify(eventHandlers) | ||
.dispatch(LogEvent.warn("platforms configured, but 'scratch' is not a manifest list")); | ||
} | ||
|
||
@Test | ||
public void testCheckManifestPlatform_tarBaseImage() { | ||
Path tar = Paths.get("/foo/bar.tar"); | ||
Mockito.when(buildContext.getBaseImageConfiguration()) | ||
.thenReturn(ImageConfiguration.builder(ImageReference.scratch()).setTarPath(tar).build()); | ||
Mockito.when(containerConfig.getPlatforms()) | ||
.thenReturn(ImmutableSet.of(new Platform("amd64", "linux"), new Platform("arch", "os"))); | ||
|
||
PlatformChecker.checkManifestPlatform(buildContext, new ContainerConfigurationTemplate()); | ||
|
||
Mockito.verify(eventHandlers) | ||
.dispatch( | ||
LogEvent.warn( | ||
"platforms configured, but '" + tar.toString() + "' is not a manifest list")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is when a local
docker://
base image is cached.