Skip to content

Commit

Permalink
Fix eclipse-jkube#253: Refactor JKubeServiceHub's BuildService electi…
Browse files Browse the repository at this point in the history
…on mechanism

+ Use ServiceLoader to load all Build Services and pick up service
  which is applicable in current context. This requires BuildService
  implementations to have zero-arg constructors so I need to set the
  elements from setters instead.

+ Added isApplicable(), setJKubeServiceHub(), methods in
  BuildService interface

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Jul 2, 2021
1 parent 2a2c30a commit 379d5bf
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 89 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Usage:
./scripts/extract-changelog-for-version.sh 1.3.37 5
```
### 1.4.0-SNAPSHOT
* Fix #253: Refactor JKubeServiceHub's BuildService election mechanism via ServiceLoader
* Fix #741: Private constructor added to Utility classes
* Fix #725: Upgrade HttpClient from 4.5.10 to 4.5.13
* Fix #653: `k8s:watch` port-forward websocket error due to wrong arguments in PortForwardService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ public interface BuildService {
*/
void postProcess(BuildServiceConfig config);

/**
* Check whether provided Build Service implementation is applicable in current context or not.
*
* @param jKubeServiceHub {@link JKubeServiceHub}
* @return boolean value specifying whether provided BuildService implementation should be used.
*/
boolean isApplicable(JKubeServiceHub jKubeServiceHub);

/**
* Set {@link JKubeServiceHub}
*
* @param jKubeServiceHub {@link JKubeServiceHub}
*/
void setJKubeServiceHub(JKubeServiceHub jKubeServiceHub);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package org.eclipse.jkube.kit.config.service;

import java.io.Closeable;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;

import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.openshift.client.OpenShiftClient;
Expand All @@ -30,11 +32,8 @@
import org.eclipse.jkube.kit.common.util.LazyBuilder;
import org.eclipse.jkube.kit.config.access.ClusterAccess;
import org.eclipse.jkube.kit.config.access.ClusterConfiguration;
import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.config.resource.RuntimeMode;
import org.eclipse.jkube.kit.config.service.kubernetes.DockerBuildService;
import org.eclipse.jkube.kit.config.service.kubernetes.JibBuildService;
import org.eclipse.jkube.kit.config.service.kubernetes.KubernetesUndeployService;
import org.eclipse.jkube.kit.config.service.openshift.OpenshiftBuildService;
import org.eclipse.jkube.kit.config.service.openshift.OpenshiftUndeployService;
Expand All @@ -53,6 +52,7 @@ public class JKubeServiceHub implements Closeable {
@Getter
@Setter
private RuntimeMode platformMode;
@Getter
private KitLogger log;
@Getter
private ServiceHub dockerServiceHub;
Expand Down Expand Up @@ -166,30 +166,29 @@ private void initClusterAccessAndLazyBuilders() {
}
return new KubernetesUndeployService(this, log);
});
buildService = new LazyBuilder<>(() -> {
BuildService ret;
if (JKubeBuildStrategy.jib == buildServiceConfig.getJKubeBuildStrategy()) {
return new JibBuildService(JKubeServiceHub.this, log);
}
// Creating platform-dependent services
if (platformMode == RuntimeMode.OPENSHIFT) {
validateIfConnectedToCluster();
if (!isOpenShift(client)) {
throw new IllegalStateException("OpenShift platform has been specified but OpenShift has not been detected!");
}
// OpenShift services
ret = new OpenshiftBuildService((OpenShiftClient) client, log, JKubeServiceHub.this);
} else {
// Kubernetes services
ret = new DockerBuildService(JKubeServiceHub.this);
}
return ret;
});
buildService = new LazyBuilder<>(this::resolveBuildService);
}

private void validateIfConnectedToCluster() {
if (client == null) {
throw new IllegalArgumentException("Connection to Cluster required. Please check if offline mode is set to false");
}
}

private BuildService resolveBuildService() {
ServiceLoader<BuildService> buildServices = ServiceLoader.load(BuildService.class, Thread.currentThread().getContextClassLoader());
Iterator<BuildService> buildServiceIterator = buildServices.iterator();

BuildService selectedBuildService = null;
while (buildServiceIterator.hasNext()) {
BuildService b = buildServiceIterator.next();
if (b.isApplicable(JKubeServiceHub.this)) {
selectedBuildService = b;
selectedBuildService.setJKubeServiceHub(JKubeServiceHub.this);
break;
}
}

return selectedBuildService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.config.resource.RuntimeMode;
import org.eclipse.jkube.kit.config.service.BuildService;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;
import org.eclipse.jkube.kit.config.service.JKubeServiceException;
Expand All @@ -32,7 +33,9 @@ public class DockerBuildService implements BuildService {

private JKubeServiceHub jKubeServiceHub;

public DockerBuildService(JKubeServiceHub jKubeServiceHub) {
public DockerBuildService() { }

DockerBuildService(JKubeServiceHub jKubeServiceHub) {
Objects.requireNonNull(jKubeServiceHub.getDockerServiceHub(), "dockerServiceHub");
Objects.requireNonNull(jKubeServiceHub.getBuildServiceConfig(), "BuildServiceConfig is required");
this.jKubeServiceHub = jKubeServiceHub;
Expand Down Expand Up @@ -68,4 +71,13 @@ public void postProcess(BuildServiceConfig config) {
// No post processing required
}

@Override
public boolean isApplicable(JKubeServiceHub jKubeServiceHub) {
return jKubeServiceHub.getRuntimeMode() == RuntimeMode.KUBERNETES;
}

@Override
public void setJKubeServiceHub(JKubeServiceHub jKubeServiceHub) {
this.jKubeServiceHub = jKubeServiceHub;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.jkube.kit.config.image.ImageName;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy;
import org.eclipse.jkube.kit.config.service.BuildService;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;
import org.eclipse.jkube.kit.config.service.JKubeServiceException;
Expand All @@ -53,13 +54,14 @@ public class JibBuildService implements BuildService {
"docker.io", "index.docker.io", "registry.hub.docker.com"
);
private static final String PUSH_REGISTRY = "jkube.docker.push.registry";
private final JKubeServiceHub jKubeServiceHub;
private final KitLogger log;
private JKubeServiceHub jKubeServiceHub;
private KitLogger log;

public JibBuildService(JKubeServiceHub jKubeServiceHub, KitLogger logger) {
public JibBuildService() { }

JibBuildService(JKubeServiceHub jKubeServiceHub) {
Objects.requireNonNull(jKubeServiceHub.getBuildServiceConfig(), "BuildServiceConfig is required");
this.jKubeServiceHub = jKubeServiceHub;
this.log = logger;
setJKubeServiceHub(jKubeServiceHub);
}

@Override
Expand Down Expand Up @@ -118,6 +120,17 @@ public void postProcess(BuildServiceConfig config) {
// No post processing required
}

@Override
public boolean isApplicable(JKubeServiceHub jKubeServiceHub) {
return jKubeServiceHub.getBuildServiceConfig().getJKubeBuildStrategy() == JKubeBuildStrategy.jib;
}

@Override
public void setJKubeServiceHub(JKubeServiceHub jKubeServiceHub) {
this.jKubeServiceHub = jKubeServiceHub;
this.log = jKubeServiceHub.getLog();
}

static ImageConfiguration prependRegistry(ImageConfiguration imageConfiguration, String registry) {
ImageName imageName = new ImageName(imageConfiguration.getName());
if (!imageName.hasRegistry() && registry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@
import org.eclipse.jkube.kit.common.util.IoUtil;
import org.eclipse.jkube.kit.common.util.KubernetesHelper;
import org.eclipse.jkube.kit.common.util.OpenshiftHelper;
import org.eclipse.jkube.kit.config.access.ClusterAccess;
import org.eclipse.jkube.kit.config.access.ClusterConfiguration;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.image.ImageName;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.config.resource.RuntimeMode;
import org.eclipse.jkube.kit.config.service.BuildService;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;
import org.eclipse.jkube.kit.config.service.JKubeServiceException;
Expand Down Expand Up @@ -84,6 +87,7 @@

import static org.eclipse.jkube.kit.build.api.helper.BuildUtil.extractBaseFromConfiguration;

import static org.eclipse.jkube.kit.common.util.OpenshiftHelper.isOpenShift;
import static org.eclipse.jkube.kit.config.service.openshift.ImageStreamService.resolveImageStreamName;

/**
Expand All @@ -99,28 +103,28 @@ public class OpenshiftBuildService implements BuildService {
public static final String REQUESTS = "requests";
public static final String LIMITS = "limits";

private final OpenShiftClient client;
private final KitLogger log;
private final JKubeServiceHub jKubeServiceHub;
private final BuildServiceConfig config;
private OpenShiftClient client;
private KitLogger log;
private JKubeServiceHub jKubeServiceHub;
private BuildServiceConfig config;
private AuthConfigFactory authConfigFactory;

public OpenshiftBuildService() { }

public OpenshiftBuildService(OpenShiftClient client, KitLogger log, JKubeServiceHub jKubeServiceHub) {
Objects.requireNonNull(client, "client");
Objects.requireNonNull(log, "log");
Objects.requireNonNull(jKubeServiceHub.getConfiguration(), "JKubeConfiguration");
Objects.requireNonNull(jKubeServiceHub.getDockerServiceHub(), "dockerServiceHub");
Objects.requireNonNull(jKubeServiceHub.getBuildServiceConfig(), "config");

OpenshiftBuildService(OpenShiftClient client, JKubeServiceHub jKubeServiceHub) {
this.client = client;
this.log = log;
this.jKubeServiceHub = jKubeServiceHub;
config = jKubeServiceHub.getBuildServiceConfig();
setJKubeServiceHub(jKubeServiceHub);
}

public void setOpenShiftClient(OpenShiftClient openShiftClient) {
this.client = openShiftClient;
}

@Override
public void build(ImageConfiguration imageConfig) throws JKubeServiceException {
Objects.requireNonNull(jKubeServiceHub.getConfiguration(), "JKubeConfiguration");
Objects.requireNonNull(jKubeServiceHub.getDockerServiceHub(), "dockerServiceHub");
Objects.requireNonNull(jKubeServiceHub.getBuildServiceConfig(), "config");
String buildName = null;
try {
final ImageConfiguration.ImageConfigurationBuilder applicableImageConfigBuilder = imageConfig.toBuilder();
Expand All @@ -137,6 +141,7 @@ public void build(ImageConfiguration imageConfig) throws JKubeServiceException {
KubernetesListBuilder builder = new KubernetesListBuilder();

// Check for buildconfig / imagestream / pullSecret and create them if necessary
initOpenShiftClient();
String openshiftPullSecret = config.getOpenshiftPullSecret();
final boolean usePullSecret = checkOrCreatePullSecret(client, builder, openshiftPullSecret, applicableImageConfig);
if (usePullSecret) {
Expand Down Expand Up @@ -232,6 +237,18 @@ public void postProcess(BuildServiceConfig config) {
config.attachArtifact("is", getImageStreamFile());
}

@Override
public boolean isApplicable(JKubeServiceHub jKubeServiceHub) {
return jKubeServiceHub.getRuntimeMode() == RuntimeMode.OPENSHIFT;
}

@Override
public void setJKubeServiceHub(JKubeServiceHub jKubeServiceHub) {
this.jKubeServiceHub = jKubeServiceHub;
this.config = jKubeServiceHub.getBuildServiceConfig();
this.log = jKubeServiceHub.getLog();
}

protected String updateOrCreateBuildConfig(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder, ImageConfiguration imageConfig, String openshiftPullSecret) {
ImageName imageName = new ImageName(imageConfig.getName());
String buildName = getS2IBuildName(config, imageName);
Expand Down Expand Up @@ -776,4 +793,18 @@ private Map<String, Map<String, Quantity>> getRequestsAndLimits() {
return keyToQuantityMap;
}

private void initOpenShiftClient() {
if (client == null) {
ClusterAccess clusterAccess = jKubeServiceHub.getClusterAccess();
if (clusterAccess == null) {
clusterAccess = new ClusterAccess(log,
ClusterConfiguration.from(System.getProperties(), jKubeServiceHub.getConfiguration().getProject().getProperties()).build());
}
client = clusterAccess.createDefaultClient();
if (!isOpenShift(client)) {
throw new IllegalStateException("OpenShift platform has been specified but OpenShift has not been detected!");
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.eclipse.jkube.kit.config.service.kubernetes.JibBuildService
org.eclipse.jkube.kit.config.service.kubernetes.DockerBuildService
org.eclipse.jkube.kit.config.service.openshift.OpenshiftBuildService
Loading

0 comments on commit 379d5bf

Please sign in to comment.