Skip to content
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

Ported JIB implementation #14

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions jkube-kit/build/service/docker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,14 @@
<artifactId>hamcrest-all</artifactId>
</dependency>

<dependency>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-core</artifactId>
</dependency>

<dependency>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public class BuildService {
* @param imageConfig the image configuration
* @param imagePullManager the image pull manager
* @param buildContext the build context
* @throws Exception in case of any problems
* @throws IOException in case of any problems
*/
public void buildImage(ImageConfiguration imageConfig, ImagePullManager imagePullManager, BuildContext buildContext)
throws Exception {
throws IOException {

if (imagePullManager != null) {
autoPullBaseImage(imageConfig, imagePullManager, buildContext);
Expand Down Expand Up @@ -238,8 +238,7 @@ private Map<String, String> addBuildArgsFromDockerConfig() {
return buildArgs;
}

private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager imagePullManager, BuildContext buildContext)
throws Exception {
private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager imagePullManager, BuildContext buildContext) throws DockerAccessException, IOException {
BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();

if (buildConfig.getDockerArchive() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package io.jkube.kit.build.service.docker;

import com.google.cloud.tools.jib.api.Credential;
import com.google.cloud.tools.jib.api.ImageFormat;
import com.google.cloud.tools.jib.api.InvalidImageReferenceException;
import com.google.cloud.tools.jib.api.JibContainer;
import com.google.cloud.tools.jib.api.RegistryException;
import io.jkube.kit.build.maven.MavenBuildContext;
import io.jkube.kit.build.service.docker.helper.DeepCopy;
import io.jkube.kit.build.service.docker.helper.JibBuildServiceUtil;
import io.jkube.kit.common.KitLogger;

import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

public class JibBuildService {

private KitLogger log;
private BuildService.BuildContext dockerBuildContext;
private MavenBuildContext mojoParameters;

public JibBuildService(BuildService.BuildContext dockerBuildContext, MavenBuildContext mojoParameters, KitLogger log) {
Objects.requireNonNull(dockerBuildContext, "dockerBuildContext");
this.dockerBuildContext = dockerBuildContext;
this.mojoParameters = mojoParameters;
this.log = log;
}

public void buildImage(ImageConfiguration imageConfiguration, boolean isOfflineMode) {
try {
doJibBuild(JibBuildServiceUtil.getJibBuildConfiguration(dockerBuildContext, mojoParameters, imageConfiguration, log), isOfflineMode);
} catch (Exception ex) {
throw new UnsupportedOperationException(ex);
}
}

public JibContainer doJibBuild(JibBuildService.JibBuildConfiguration jibBuildConfiguration, boolean isOfflineMode) throws InvalidImageReferenceException, RegistryException, ExecutionException {
return JibBuildServiceUtil.buildImage(jibBuildConfiguration, log, isOfflineMode);
}

public static class JibBuildConfiguration {
private ImageConfiguration imageConfiguration;

private ImageFormat imageFormat;

private Credential credential;

private Path fatJarPath;

private String targetDir;

private String outputDir;

private JibBuildConfiguration() {}

public ImageConfiguration getImageConfiguration() { return imageConfiguration; }

public String getTargetDir() {
return targetDir;
}

public String getOutputDir() {
return outputDir;
}

public Credential getCredential() {
return credential;
}

public Path getFatJar() {
return fatJarPath;
}

public ImageFormat getImageFormat() {
return imageFormat;
}

public static class Builder {
private final JibBuildConfiguration configutil;
private final KitLogger logger;

public Builder(KitLogger logger) {
this(null, logger);
}

public Builder(JibBuildConfiguration that, KitLogger logger) {
this.logger = logger;
if (that == null) {
this.configutil = new JibBuildConfiguration();
} else {
this.configutil = DeepCopy.copy(that);
}
}

public Builder imageConfiguration(ImageConfiguration imageConfiguration) {
configutil.imageConfiguration = imageConfiguration;
return this;
}

public Builder imageFormat(ImageFormat imageFormat) {
configutil.imageFormat = imageFormat;
return this;
}

public Builder credential(Credential credential) {
configutil.credential = credential;
return this;
}

public Builder buildDirectory(String buildDir) {
configutil.fatJarPath = JibBuildServiceUtil.getFatJar(buildDir, logger);
return this;
}

public Builder targetDir(String targetDir) {
configutil.targetDir = targetDir;
return this;
}

public Builder outputDir(String outputDir) {
configutil.outputDir = outputDir;
return this;
}

public JibBuildConfiguration build() {
return configutil;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

import io.jkube.kit.build.api.auth.AuthConfig;
import io.jkube.kit.build.service.docker.access.DockerAccess;
import io.jkube.kit.build.service.docker.access.DockerAccessException;
import io.jkube.kit.build.service.docker.auth.AuthConfigFactory;
import io.jkube.kit.common.KitLogger;
import io.jkube.kit.common.util.EnvUtil;
import io.jkube.kit.config.image.ImageName;
import io.jkube.kit.config.image.build.BuildConfiguration;
import io.jkube.kit.config.image.build.ImagePullPolicy;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.settings.Settings;

/**
Expand Down Expand Up @@ -90,10 +88,9 @@ public void pushImages(Collection<ImageConfiguration> imageConfigs,
* @param pullManager image pull manager
* @param registryConfig registry configuration
* @param hasImage boolean variable indicating it has image or not
* @throws Exception exception
* @throws IOException exception
*/
public void pullImageWithPolicy(String image, ImagePullManager pullManager, RegistryConfig registryConfig, boolean hasImage)
throws Exception {
public void pullImageWithPolicy(String image, ImagePullManager pullManager, RegistryConfig registryConfig, boolean hasImage) throws IOException {

// Already pulled, so we don't need to take care
if (pullManager.hasAlreadyPulled(image)) {
Expand Down Expand Up @@ -150,7 +147,7 @@ private boolean imageRequiresPull(boolean hasImage, ImagePullPolicy pullPolicy,
}

private AuthConfig createAuthConfig(boolean isPush, String user, String registry, RegistryConfig config)
throws Exception {
throws IOException {

return config.getAuthConfigFactory().createAuthConfig(
isPush, config.isSkipExtendedAuth(), config.getAuthConfig(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ public void setLog(KitLogger log) {
* @param registry registry to use, might be null in which case a default registry is checked,
* @return the authentication configuration or <code>null</code> if none could be found
*
* @throws MojoFailureException mojo failure exception
* @throws IOException exception
*/
public AuthConfig createAuthConfig(boolean isPush, boolean skipExtendedAuth, Map authConfig, Settings settings, String user, String registry)
throws Exception {
throws IOException {

AuthConfig ret = createStandardAuthConfig(isPush, authConfig, settings, user, registry);
if (ret != null) {
Expand Down Expand Up @@ -202,7 +202,7 @@ private AuthConfig extendedAuthentication(AuthConfig standardAuthConfig, String
* @throws MojoFailureException
*/
private AuthConfig createStandardAuthConfig(boolean isPush, Map authConfigMap, Settings settings, String user, String registry)
throws Exception {
throws IOException {
AuthConfig ret;

// Check first for specific configuration based on direction (pull or push), then for a default value
Expand Down Expand Up @@ -319,7 +319,7 @@ private AuthConfig getAuthConfigFromEC2InstanceRole() throws IOException {
}
}

private AuthConfig getAuthConfigFromSystemProperties(LookupMode lookupMode) throws Exception {
private AuthConfig getAuthConfigFromSystemProperties(LookupMode lookupMode) throws IOException {
Properties props = System.getProperties();
String userKey = lookupMode.asSysProperty(AUTH_USERNAME);
String passwordKey = lookupMode.asSysProperty(AUTH_PASSWORD);
Expand All @@ -336,7 +336,7 @@ private AuthConfig getAuthConfigFromSystemProperties(LookupMode lookupMode) thro
}
}

private AuthConfig getAuthConfigFromOpenShiftConfig(LookupMode lookupMode, Map authConfigMap) throws MojoExecutionException {
private AuthConfig getAuthConfigFromOpenShiftConfig(LookupMode lookupMode, Map authConfigMap) {
Properties props = System.getProperties();
String useOpenAuthModeProp = lookupMode.asSysProperty(AUTH_USE_OPENSHIFT_AUTH);
// Check for system property
Expand All @@ -359,12 +359,12 @@ private AuthConfig getAuthConfigFromOpenShiftConfig(LookupMode lookupMode, Map a
}
}

private AuthConfig getAuthConfigFromPluginConfiguration(LookupMode lookupMode, Map authConfig) throws Exception {
private AuthConfig getAuthConfigFromPluginConfiguration(LookupMode lookupMode, Map authConfig) throws IllegalStateException {
Map mapToCheck = getAuthConfigMapToCheck(lookupMode,authConfig);

if (mapToCheck != null && mapToCheck.containsKey(AUTH_USERNAME)) {
if (!mapToCheck.containsKey(AUTH_PASSWORD)) {
throw new MojoExecutionException("No 'password' given while using <authConfig> in configuration for mode " + lookupMode);
throw new IllegalStateException("No 'password' given while using <authConfig> in configuration for mode " + lookupMode);
}
Map<String, String> cloneConfig = new HashMap<>(mapToCheck);
cloneConfig.put(AUTH_PASSWORD, decrypt(cloneConfig.get(AUTH_PASSWORD)));
Expand All @@ -374,7 +374,7 @@ private AuthConfig getAuthConfigFromPluginConfiguration(LookupMode lookupMode, M
}
}

private AuthConfig getAuthConfigFromSettings(Settings settings, String user, String registry) throws Exception {
private AuthConfig getAuthConfigFromSettings(Settings settings, String user, String registry) {
Server defaultServer = null;
Server found;
for (Server server : settings.getServers()) {
Expand Down Expand Up @@ -520,13 +520,13 @@ private AuthConfig parseUser(String userName, Map user) {
token, null, null);
}

private AuthConfig validateMandatoryOpenShiftLogin(AuthConfig openShiftAuthConfig, String useOpenAuthModeProp) throws MojoExecutionException {
private AuthConfig validateMandatoryOpenShiftLogin(AuthConfig openShiftAuthConfig, String useOpenAuthModeProp) throws IllegalStateException {
if (openShiftAuthConfig != null) {
return openShiftAuthConfig;
}
// No login found
String kubeConfigEnv = System.getenv("KUBECONFIG");
throw new MojoExecutionException(
throw new IllegalStateException(
String.format("System property %s set, but not active user and/or token found in %s. " +
"Please use 'oc login' for connecting to OpenShift.",
useOpenAuthModeProp, kubeConfigEnv != null ? kubeConfigEnv : "~/.kube/config"));
Expand All @@ -545,20 +545,20 @@ private Server checkForServer(Server server, String id, String registry, String
return null;
}

private String decrypt(String password) throws Exception {
private String decrypt(String password) throws IllegalStateException {
try {
// Done by reflection since I have classloader issues otherwise
Object secDispatcher = container.lookup(SecDispatcher.ROLE, "maven");
Method method = secDispatcher.getClass().getMethod("decrypt",String.class);
return (String) method.invoke(secDispatcher,password);
} catch (ComponentLookupException e) {
throw new Exception("Error looking security dispatcher",e);
throw new IllegalStateException("Error looking security dispatcher",e);
} catch (ReflectiveOperationException e) {
throw new Exception("Cannot decrypt password: " + e.getCause(),e);
throw new IllegalStateException("Cannot decrypt password: " + e.getCause(),e);
}
}

private AuthConfig createAuthConfigFromServer(Server server) throws Exception {
private AuthConfig createAuthConfigFromServer(Server server) {
return new AuthConfig(
server.getUsername(),
decrypt(server.getPassword()),
Expand Down
Loading