-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add test action that connects local test to a Kubernetes service
- Uses Kubernetes client port forward to connect a local port with an exposed port on a service or pod running on Kubernetes - Enables users to invoke the Kubernetes service from the local host
- Loading branch information
1 parent
3a2062c
commit 0be28df
Showing
27 changed files
with
1,561 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
...kubernetes/src/main/java/org/citrusframework/kubernetes/actions/ServiceConnectAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* Copyright the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.citrusframework.kubernetes.actions; | ||
|
||
import io.fabric8.kubernetes.client.LocalPortForward; | ||
import org.citrusframework.context.TestContext; | ||
import org.citrusframework.exceptions.CitrusRuntimeException; | ||
import org.citrusframework.http.client.HttpClient; | ||
import org.citrusframework.http.client.HttpClientBuilder; | ||
import org.citrusframework.http.server.HttpServer; | ||
import org.citrusframework.kubernetes.KubernetesSettings; | ||
import org.citrusframework.util.StringUtils; | ||
|
||
import static org.citrusframework.kubernetes.actions.KubernetesActionBuilder.kubernetes; | ||
|
||
/** | ||
* Action connects the test to a Kubernetes service so clients may invoke the service. | ||
* This is for services that are only accessible from within the cluster (e.g. service type is ClusterIP). | ||
* The test action connects to the service via port forwarding and exposes a Http client to the Citrus context | ||
* so other test actions can access the service running in Kubernetes. | ||
*/ | ||
public class ServiceConnectAction extends AbstractKubernetesAction { | ||
|
||
private final String clientName; | ||
private final String serviceName; | ||
private final String port; | ||
private final String localPort; | ||
|
||
public ServiceConnectAction(Builder builder) { | ||
super("service-connect", builder); | ||
|
||
this.serviceName = builder.serviceName; | ||
this.port = builder.port; | ||
this.clientName = builder.clientName; | ||
this.localPort = builder.localPort; | ||
} | ||
|
||
@Override | ||
public void doExecute(TestContext context) { | ||
if (KubernetesSettings.isLocal()) { | ||
if (context.getReferenceResolver().isResolvable(serviceName)) { | ||
HttpServer server = context.getReferenceResolver().resolve(serviceName, HttpServer.class); | ||
HttpClient serviceClient = new HttpClientBuilder() | ||
.requestUrl("http://localhost:%d".formatted(server.getPort())) | ||
.build(); | ||
context.getReferenceResolver().bind(clientName, serviceClient); | ||
} | ||
|
||
return; | ||
} | ||
|
||
LocalPortForward portForward; | ||
if (StringUtils.hasText(localPort)) { | ||
portForward = getKubernetesClient().services() | ||
.inNamespace(namespace(context)) | ||
.withName(serviceName) | ||
.portForward(Integer.parseInt(context.replaceDynamicContentInString(port)), Integer.parseInt(context.replaceDynamicContentInString(localPort))); | ||
} else { | ||
portForward = getKubernetesClient().services() | ||
.inNamespace(namespace(context)) | ||
.withName(serviceName) | ||
.portForward(Integer.parseInt(context.replaceDynamicContentInString(port))); | ||
} | ||
|
||
if (context.getReferenceResolver().isResolvable(clientName)) { | ||
throw new CitrusRuntimeException("Failed to bind Kubernetes service client '%s' - client already exists".formatted(clientName)); | ||
} | ||
|
||
HttpClient serviceClient = new HttpClientBuilder() | ||
.requestUrl("http://localhost:%d".formatted(portForward.getLocalPort())) | ||
.build(); | ||
context.getReferenceResolver().bind(clientName, serviceClient); | ||
|
||
if (context.getReferenceResolver().isResolvable(serviceName + ":port-forward")) { | ||
throw new CitrusRuntimeException("Failed to bind Kubernetes service port forward '%s' - already exists".formatted(serviceName + ":port-forward")); | ||
} | ||
context.getReferenceResolver().bind(serviceName + ":port-forward", portForward); | ||
|
||
if (isAutoRemoveResources()) { | ||
context.doFinally(kubernetes().client(getKubernetesClient()) | ||
.services() | ||
.disconnect(serviceName) | ||
.inNamespace(getNamespace())); | ||
} | ||
} | ||
|
||
/** | ||
* Action builder. | ||
*/ | ||
public static class Builder extends AbstractKubernetesAction.Builder<ServiceConnectAction, Builder> { | ||
|
||
private String clientName; | ||
private String localPort; | ||
private String serviceName = KubernetesSettings.getServiceName(); | ||
private String port; | ||
|
||
public Builder service(String serviceName) { | ||
this.serviceName = serviceName; | ||
return this; | ||
} | ||
|
||
public Builder client(String clientName) { | ||
this.clientName = clientName; | ||
return this; | ||
} | ||
|
||
public Builder port(String port) { | ||
this.port = port; | ||
return this; | ||
} | ||
|
||
public Builder port(int port) { | ||
this.port = String.valueOf(port); | ||
return this; | ||
} | ||
|
||
public Builder portMapping(String port, String localPort) { | ||
if (port != null) { | ||
port(port); | ||
} | ||
|
||
if (localPort != null) { | ||
localPort(localPort); | ||
} | ||
return this; | ||
} | ||
|
||
public Builder portMapping(int port, int localPort) { | ||
port(port); | ||
localPort(localPort); | ||
return this; | ||
} | ||
|
||
public Builder localPort(String localPort) { | ||
this.localPort = localPort; | ||
return this; | ||
} | ||
|
||
public Builder localPort(int localPort) { | ||
this.localPort = String.valueOf(localPort); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ServiceConnectAction doBuild() { | ||
if (clientName == null) { | ||
client(serviceName + ".client"); | ||
} | ||
|
||
return new ServiceConnectAction(this); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...ernetes/src/main/java/org/citrusframework/kubernetes/actions/ServiceDisconnectAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.citrusframework.kubernetes.actions; | ||
|
||
import java.io.IOException; | ||
|
||
import io.fabric8.kubernetes.client.LocalPortForward; | ||
import org.citrusframework.context.TestContext; | ||
import org.citrusframework.kubernetes.KubernetesSettings; | ||
|
||
/** | ||
* Action closes port forward for a Kubernetes service. | ||
* Resolves port forward from Citrus context and closes it when still alive. | ||
*/ | ||
public class ServiceDisconnectAction extends AbstractKubernetesAction { | ||
|
||
private final String serviceName; | ||
|
||
public ServiceDisconnectAction(Builder builder) { | ||
super("service-disconnect", builder); | ||
|
||
this.serviceName = builder.serviceName; | ||
} | ||
|
||
@Override | ||
public void doExecute(TestContext context) { | ||
logger.info("Disconnect from Kubernetes service '{}'", serviceName); | ||
|
||
if (KubernetesSettings.isLocal()) { | ||
return; | ||
} | ||
|
||
if (context.getReferenceResolver().isResolvable(serviceName + ":port-forward", LocalPortForward.class)) { | ||
LocalPortForward portForward = context.getReferenceResolver().resolve(serviceName + ":port-forward", LocalPortForward.class); | ||
try { | ||
if (portForward.isAlive()) { | ||
portForward.close(); | ||
logger.info("Successfully disconnected from Kubernetes service '{}'", serviceName); | ||
} | ||
} catch (IOException e) { | ||
logger.warn("Failed to close local port forward for Kubernetes service '{}'", serviceName); | ||
} | ||
} else { | ||
logger.warn("Failed to disconnect from Kubernetes service '{}' - no port forward available", serviceName); | ||
} | ||
} | ||
|
||
/** | ||
* Action builder. | ||
*/ | ||
public static class Builder extends AbstractKubernetesAction.Builder<ServiceDisconnectAction, Builder> { | ||
|
||
private String serviceName = KubernetesSettings.getServiceName(); | ||
|
||
public Builder service(String serviceName) { | ||
this.serviceName = serviceName; | ||
return this; | ||
} | ||
|
||
@Override | ||
public ServiceDisconnectAction doBuild() { | ||
return new ServiceDisconnectAction(this); | ||
} | ||
} | ||
} |
Oops, something went wrong.