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

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

Merged
merged 1 commit into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Bugs
* Fix: #2442: Wrong resource kind in `ProjectRequestHandler` causes ClassCastException when handling Project resources.
* 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