-
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
Add option to specify base image for cli jar command #2972
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
40101ae
add base image option
mpeddada1 7c04571
javadoc
mpeddada1 134bc0a
modify scope
mpeddada1 2ca84c9
clean up tests
mpeddada1 be5811b
windows test
mpeddada1 a3e721e
Merge branch 'master' of github.com:GoogleContainerTools/jib into cli…
mpeddada1 aacc3d1
move duplicate code into separate class
mpeddada1 119bb8d
javadoc
mpeddada1 1e00b9b
comment fixes
mpeddada1 e0124c1
use emptySet() instead of Collections.EMPTY_SET
mpeddada1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
82 changes: 82 additions & 0 deletions
82
jib-cli/src/main/java/com/google/cloud/tools/jib/cli/ContainerBuilders.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,82 @@ | ||
/* | ||
* Copyright 2021 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.cli; | ||
|
||
import static com.google.cloud.tools.jib.api.Jib.DOCKER_DAEMON_IMAGE_PREFIX; | ||
import static com.google.cloud.tools.jib.api.Jib.REGISTRY_IMAGE_PREFIX; | ||
import static com.google.cloud.tools.jib.api.Jib.TAR_IMAGE_PREFIX; | ||
|
||
import com.google.cloud.tools.jib.api.DockerDaemonImage; | ||
import com.google.cloud.tools.jib.api.ImageReference; | ||
import com.google.cloud.tools.jib.api.InvalidImageReferenceException; | ||
import com.google.cloud.tools.jib.api.Jib; | ||
import com.google.cloud.tools.jib.api.JibContainerBuilder; | ||
import com.google.cloud.tools.jib.api.RegistryImage; | ||
import com.google.cloud.tools.jib.api.TarImage; | ||
import com.google.cloud.tools.jib.api.buildplan.Platform; | ||
import com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory; | ||
import com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers; | ||
import com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger; | ||
import java.io.FileNotFoundException; | ||
import java.nio.file.Paths; | ||
import java.util.Set; | ||
|
||
/** Helper class for creating JibContainerBuilders from JibCli specifications. */ | ||
public class ContainerBuilders { | ||
|
||
/** | ||
* Creates a {@link JibContainerBuilder} depending on the base image specified. | ||
* | ||
* @param baseImageReference base image reference | ||
* @param platforms platforms for multi-platform support in build command | ||
* @param commonCliOptions common cli options | ||
* @param logger console logger | ||
* @return a {@link JibContainerBuilder} | ||
* @throws InvalidImageReferenceException if the baseImage reference cannot be parsed | ||
* @throws FileNotFoundException if credential helper file cannot be found | ||
*/ | ||
public static JibContainerBuilder create( | ||
String baseImageReference, | ||
Set<Platform> platforms, | ||
CommonCliOptions commonCliOptions, | ||
ConsoleLogger logger) | ||
throws InvalidImageReferenceException, FileNotFoundException { | ||
if (baseImageReference.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) { | ||
return Jib.from( | ||
DockerDaemonImage.named(baseImageReference.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, ""))); | ||
} | ||
if (baseImageReference.startsWith(TAR_IMAGE_PREFIX)) { | ||
return Jib.from( | ||
TarImage.at(Paths.get(baseImageReference.replaceFirst(TAR_IMAGE_PREFIX, "")))); | ||
} | ||
ImageReference imageReference = | ||
ImageReference.parse(baseImageReference.replaceFirst(REGISTRY_IMAGE_PREFIX, "")); | ||
RegistryImage registryImage = RegistryImage.named(imageReference); | ||
DefaultCredentialRetrievers defaultCredentialRetrievers = | ||
DefaultCredentialRetrievers.init( | ||
CredentialRetrieverFactory.forImage( | ||
imageReference, | ||
logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage()))); | ||
Credentials.getFromCredentialRetrievers(commonCliOptions, defaultCredentialRetrievers) | ||
.forEach(registryImage::addCredentialRetriever); | ||
JibContainerBuilder containerBuilder = Jib.from(registryImage); | ||
if (!platforms.isEmpty()) { | ||
containerBuilder.setPlatforms(platforms); | ||
} | ||
return containerBuilder; | ||
} | ||
} |
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
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
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
31 changes: 31 additions & 0 deletions
31
jib-cli/src/test/java/com/google/cloud/tools/jib/api/JibContainerBuilderTestHelper.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,31 @@ | ||
/* | ||
* Copyright 2021 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.api; | ||
|
||
import com.google.cloud.tools.jib.configuration.BuildContext; | ||
|
||
/** Test helper to expose package-private members of {@link JibContainerBuilder}. */ | ||
public class JibContainerBuilderTestHelper { | ||
|
||
public static BuildContext toBuildContext( | ||
JibContainerBuilder jibContainerBuilder, Containerizer containerizer) | ||
throws CacheDirectoryCreationException { | ||
return jibContainerBuilder.toBuildContext(containerizer); | ||
} | ||
|
||
private JibContainerBuilderTestHelper() {} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think here we should try to check if the jar is java8 or java11 and chose the right distroless? (perhaps in a followup PR)
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.
Yup, good point - we have an issue for this #2871. Will make this change in the next PR.