-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check configured platforms for single image building (#2793)
* Check configured platforms for single image building * Fix copyright header * Check cached manifest too * Log when using cached local image too * Fix style violation * Fix path separator issue in tests on Windows
- Loading branch information
1 parent
8862aa3
commit 7d29959
Showing
4 changed files
with
215 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PlatformChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
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"))) { | ||
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))); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PlatformCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |