Skip to content

Commit

Permalink
Fix #2467: OpenShiftClient cannot replace existing resource with API …
Browse files Browse the repository at this point in the history
…version =! v1

BackwardCompatibilityInterceptor should only convert openshift4 to openshift3 requests
on receiving 404
  • Loading branch information
rohanKanojia committed Sep 10, 2020
1 parent 453a819 commit 7c7dc9b
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 4.11-SNAPSHOT

#### Bugs
* Fix #2467: OpenShiftClient cannot replace existing resource with API version =! v1

#### Improvements
* Fix #2473: Removed unused ValidationMessages.properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import okio.Buffer;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -134,7 +135,7 @@ public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(request);
if (isOpenshiftApiRequest(request)) {
return handleOpenshiftRequests(request, response, chain);
} else if (!response.isSuccessful() && responseCodeToTransformations.keySet().contains(response.code())) {
} else if (!response.isSuccessful() && responseCodeToTransformations.containsKey(response.code())) {
String url = request.url().toString();
Matcher matcher = getMatcher(url);
ResourceKey key = getKey(matcher);
Expand Down Expand Up @@ -170,7 +171,7 @@ private static ResourceKey getKey(Matcher m) {
}

private static Response handleOpenshiftRequests(Request request, Response response, Chain chain) throws IOException{
if (!response.isSuccessful()) {
if (!response.isSuccessful() && response.code() == HttpURLConnection.HTTP_NOT_FOUND) {
ResourceKey target = getResourceKeyFromRequest(request);
if (target != null) {
String requestUrl = request.url().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import io.fabric8.commons.ReadyEntity;
import io.fabric8.openshift.api.model.BuildConfig;
import io.fabric8.openshift.api.model.BuildConfigList;
import io.fabric8.openshift.api.model.BuildSourceBuilder;
import io.fabric8.openshift.client.OpenShiftClient;
import org.arquillian.cube.kubernetes.api.Session;
import org.arquillian.cube.openshift.impl.requirement.RequiresOpenshift;
import org.arquillian.cube.requirement.ArquillianConditionalRunner;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -88,4 +88,23 @@ public void delete() {
boolean bDeleted = client.buildConfigs().inNamespace(session.getNamespace()).withName("bc-delete").delete();
assertTrue(bDeleted);
}

@Test
public void createOrReplace() {
// Given
BuildConfig buildConfig = client.buildConfigs().inNamespace(session.getNamespace()).withName("bc-createorreplace").get();

// When
buildConfig.getSpec().setSource(new BuildSourceBuilder()
.withNewGit()
.withUri("https://github.com/openshift/test2")
.endGit()
.build());
buildConfig = client.buildConfigs().inNamespace(session.getNamespace()).createOrReplace(buildConfig);

// Then
assertNotNull(buildConfig);
assertEquals("bc-createorreplace", buildConfig.getMetadata().getName());
assertEquals("https://github.com/openshift/test2", buildConfig.getSpec().getSource().getGit().getUri());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,19 @@ public void delete() {
assertTrue(bDeleted);
}

@Test
public void createOrReplace() {
// Given
DeploymentConfig deploymentConfig = client.deploymentConfigs().inNamespace(session.getNamespace()).withName("dc-createorreplace").get();

// When
deploymentConfig.getSpec().getTemplate().getSpec().getContainers().get(0).setImage("openshift/hello-openshift:v3.8");
deploymentConfig = client.deploymentConfigs().inNamespace(session.getNamespace()).createOrReplace(deploymentConfig);

// Then
assertNotNull(deploymentConfig);
assertEquals("dc-createorreplace", deploymentConfig.getMetadata().getName());
assertEquals("openshift/hello-openshift:v3.8", deploymentConfig.getSpec().getTemplate().getSpec().getContainers().get(0).getImage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,19 @@ public void delete() {
assertTrue(bDeleted);
}

@Test
public void createOrReplace() {
// Given
ImageStream imageStream = client.imageStreams().inNamespace(session.getNamespace()).withName("is-createorreplace").get();

// When
imageStream.getSpec().setDockerImageRepository("docker.io/openshift/ruby-centos-2");
imageStream = client.imageStreams().inNamespace(session.getNamespace()).createOrReplace(imageStream);

// Then
assertNotNull(imageStream);
assertEquals("is-createorreplace", imageStream.getMetadata().getName());
assertEquals("docker.io/openshift/ruby-centos-2", imageStream.getSpec().getDockerImageRepository());
}

}
18 changes: 18 additions & 0 deletions kubernetes-itests/src/test/java/io/fabric8/openshift/RouteIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

import static junit.framework.TestCase.assertNotNull;
Expand Down Expand Up @@ -94,4 +95,21 @@ public void delete() {
assertTrue(bDeleted);
}

@Test
public void createOrReplace() {
// Given
Route route = client.routes().inNamespace(currentNamespace).withName("route-createorreplace").get();

// When
route.getMetadata().setAnnotations(Collections.singletonMap("foo", "bar"));
route.getSpec().setHost("test.fabric8.io");
route = client.routes().inNamespace(currentNamespace).createOrReplace(route);

// Then
assertNotNull(route);
assertEquals("route-createorreplace", route.getMetadata().getName());
assertEquals("bar", route.getMetadata().getAnnotations().get("foo"));
assertEquals("test.fabric8.io", route.getSpec().getHost());
}

}
30 changes: 30 additions & 0 deletions kubernetes-itests/src/test/resources/buildconfig-it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,33 @@ spec:
to:
kind: "ImageStreamTag"
name: "origin-ruby-sample:latest"
---
kind: "BuildConfig"
apiVersion: "v1"
metadata:
name: bc-createorreplace
spec:
triggers:
- type: "GitHub"
github:
secret: "secret101"
- type: "Generic"
generic:
secret: "secret101"
- type: "ImageChange"
source:
type: "Git"
git:
uri: "https://github.com/openshift/ruby-hello-world"
dockerfile: "FROM openshift/ruby-22-centos7\nUSER example"
strategy:
type: "Source"
sourceStrategy:
from:
kind: "ImageStreamTag"
name: "ruby-20-centos7:latest"
output:
to:
kind: "ImageStreamTag"
name: "origin-ruby-sample:latest"

32 changes: 32 additions & 0 deletions kubernetes-itests/src/test/resources/deploymentconfig-it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,35 @@ spec:
name: "origin-ruby-sample:latest"
strategy:
type: "Rolling"
---
kind: "DeploymentConfig"
apiVersion: "v1"
metadata:
name: dc-createorreplace
spec:
template:
metadata:
labels:
name: "frontend"
spec:
containers:
- name: "helloworld"
image: "openshift/origin-ruby-sample"
ports:
- containerPort: 8080
protocol: "TCP"
replicas: 5
selector:
name: "frontend"
triggers:
- type: "ConfigChange"
- type: "ImageChange"
imageChangeParams:
automatic: true
containerNames:
- "helloworld"
from:
kind: "ImageStreamTag"
name: "origin-ruby-sample:latest"
strategy:
type: "Rolling"
7 changes: 7 additions & 0 deletions kubernetes-itests/src/test/resources/imagestream-it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ metadata:
name: "is-delete"
spec:
dockerImageRepository: "docker.io/openshift/ruby-20-centos7"
---
apiVersion: "v1"
kind: "ImageStream"
metadata:
name: "is-createorreplace"
spec:
dockerImageRepository: "docker.io/openshift/ruby-20-centos7"
10 changes: 10 additions & 0 deletions kubernetes-itests/src/test/resources/route-it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ spec:
to:
kind: Service
name: service-name
---
apiVersion: v1
kind: Route
metadata:
name: route-createorreplace
spec:
host: www.example.com
to:
kind: Service
name: service-name

0 comments on commit 7c7dc9b

Please sign in to comment.