Skip to content

Commit

Permalink
Fix eclipse-jkube#815: ClassCastException during oc:build when OpenSh…
Browse files Browse the repository at this point in the history
…ift not present

Using OpenShiftClient to store result of `clusterAccess.createDefaultClient()` seems to
cause ClassCastException when return value is of type DefaultKubernetesClient.

Instead, Use a temporaty KubernetesClient variable for `clusterAccess.createDefaultClient()`
and type cast it to OpenShiftClient in case it's adaptable to OpenShiftClient interface.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Aug 5, 2021
1 parent b25157b commit 0ab720a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 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.5.0-SNAPSHOT
* Fix #815: `java.lang.ClassCastException` during `oc:build` when OpenShift not present

### 1.4.0 (2021-07-27)
* Fix #253: Refactor JKubeServiceHub's BuildService election mechanism via ServiceLoader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import io.fabric8.kubernetes.client.KubernetesClient;
import org.eclipse.jkube.kit.build.api.auth.AuthConfig;
import org.eclipse.jkube.kit.build.service.docker.auth.AuthConfigFactory;
import org.eclipse.jkube.kit.common.KitLogger;
Expand Down Expand Up @@ -231,10 +232,11 @@ private void initClient() {
clusterAccess = new ClusterAccess(log,
ClusterConfiguration.from(System.getProperties(), jKubeServiceHub.getConfiguration().getProject().getProperties()).build());
}
client = clusterAccess.createDefaultClient();
if (!isOpenShift(client)) {
KubernetesClient k8sClient = clusterAccess.createDefaultClient();
if (!isOpenShift(k8sClient)) {
throw new IllegalStateException("OpenShift platform has been specified but OpenShift has not been detected!");
}
client = (OpenShiftClient) k8sClient;
}

private void validateSourceType(String buildName, BuildConfigSpec spec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
import mockit.Mocked;
import mockit.Verifications;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.service.JKubeServiceException;
import org.eclipse.jkube.kit.config.service.JKubeServiceHub;
import org.junit.Test;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;

@SuppressWarnings("unused")
public class OpenShiftBuildServiceTest {

Expand All @@ -39,4 +44,23 @@ public void push_withDefaults_shouldLogWarning() throws JKubeServiceException {
}};
// @formatter:on
}

@Test
public void initClient_withNoOpenShift_shouldThrowException() {
// Given
// @formatter:off
ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name("foo/bar:latest")
.build(BuildConfiguration.builder()
.from("baseimage:latest")
.build())
.build();
// @formatter:on
OpenshiftBuildService openshiftBuildService = new OpenshiftBuildService(jKubeServiceHub);

// When + Then
IllegalStateException illegalStateException = assertThrows(IllegalStateException.class, () -> openshiftBuildService.build(imageConfiguration));
assertThat(illegalStateException.getMessage())
.isEqualTo("OpenShift platform has been specified but OpenShift has not been detected!");
}
}

0 comments on commit 0ab720a

Please sign in to comment.