Skip to content

Commit 6a03f7a

Browse files
authored
[System Tests] using fluent api for CR deployment (kroxylicious#2008)
* using fluent api for CR deployment Signed-off-by: Francisco Vila <fvila@redhat.com> * fix format Signed-off-by: Francisco Vila <fvila@redhat.com> * added KIND suffix to constants and fixed operator set up when kafka is already installed Signed-off-by: Francisco Vila <fvila@redhat.com> * introduce Sam's suggestions for KroxyliciousResource generic class Signed-off-by: Francisco Vila <fvila@redhat.com> --------- Signed-off-by: Francisco Vila <fvila@redhat.com>
1 parent 5ceb08f commit 6a03f7a

File tree

12 files changed

+334
-68
lines changed

12 files changed

+334
-68
lines changed

kroxylicious-systemtests/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
<artifactId>kubernetes-httpclient-jdk</artifactId>
9292
<scope>runtime</scope>
9393
</dependency>
94+
<dependency>
95+
<groupId>io.fabric8</groupId>
96+
<artifactId>kubernetes-model-common</artifactId>
97+
</dependency>
9498
<dependency>
9599
<groupId>io.strimzi</groupId>
96100
<artifactId>api</artifactId>
@@ -149,6 +153,12 @@
149153
<groupId>io.kroxylicious</groupId>
150154
<artifactId>kroxylicious-api</artifactId>
151155
</dependency>
156+
<dependency>
157+
<groupId>io.kroxylicious</groupId>
158+
<artifactId>kroxylicious-operator</artifactId>
159+
<version>${project.version}</version>
160+
<scope>compile</scope>
161+
</dependency>
152162
<dependency>
153163
<groupId>io.kroxylicious</groupId>
154164
<artifactId>kroxylicious-kms-provider-hashicorp-vault-test-support</artifactId>

kroxylicious-systemtests/src/main/java/io/kroxylicious/systemtests/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ private Constants() {
2323
public static final String KROXYLICIOUS_OPERATOR_NAMESPACE = "kroxylicious-operator";
2424
public static final String KROXY_DEPLOYMENT_NAME = "kroxylicious-proxy";
2525
public static final String KROXYLICIOUS_PROXY_SIMPLE_NAME = "simple";
26+
public static final String KROXYLICIOUS_INGRESS_CLUSTER_IP = "cluster-ip";
2627

2728
/**
2829
* The service name for kroxylicious. Used for the bootstrap url

kroxylicious-systemtests/src/main/java/io/kroxylicious/systemtests/installation/kroxylicious/Kroxylicious.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66

77
package io.kroxylicious.systemtests.installation.kroxylicious;
88

9-
import java.io.File;
109
import java.time.Duration;
11-
import java.util.Arrays;
12-
import java.util.List;
13-
import java.util.Objects;
1410

1511
import org.slf4j.Logger;
1612
import org.slf4j.LoggerFactory;
@@ -23,8 +19,11 @@
2319
import io.kroxylicious.systemtests.resources.manager.ResourceManager;
2420
import io.kroxylicious.systemtests.templates.kroxylicious.KroxyliciousConfigMapTemplates;
2521
import io.kroxylicious.systemtests.templates.kroxylicious.KroxyliciousDeploymentTemplates;
22+
import io.kroxylicious.systemtests.templates.kroxylicious.KroxyliciousKafkaClusterRefTemplates;
23+
import io.kroxylicious.systemtests.templates.kroxylicious.KroxyliciousKafkaProxyIngressTemplates;
24+
import io.kroxylicious.systemtests.templates.kroxylicious.KroxyliciousKafkaProxyTemplates;
2625
import io.kroxylicious.systemtests.templates.kroxylicious.KroxyliciousServiceTemplates;
27-
import io.kroxylicious.systemtests.utils.DeploymentUtils;
26+
import io.kroxylicious.systemtests.templates.kroxylicious.KroxyliciousVirtualKafkaClusterTemplates;
2827

2928
import static io.kroxylicious.systemtests.k8s.KubeClusterResource.kubeClient;
3029
import static org.awaitility.Awaitility.await;
@@ -66,8 +65,14 @@ private void deployPortPerBrokerPlain(int replicas) {
6665
/**
6766
* Deploy - Port Identifies Node with no filters config
6867
*/
69-
public void deployPortIdentifiesNodeWithNoFilters() {
70-
deployKroxyliciousExample(Constants.PATH_TO_OPERATOR_SIMPLE_FILES);
68+
public void deployPortIdentifiesNodeWithNoFilters(String clusterName) {
69+
resourceManager.createResourceFromBuilder(
70+
KroxyliciousKafkaProxyTemplates.defaultKafkaProxyDeployment(deploymentNamespace, Constants.KROXYLICIOUS_PROXY_SIMPLE_NAME),
71+
KroxyliciousKafkaProxyIngressTemplates.defaultKafkaProxyIngressDeployment(deploymentNamespace, Constants.KROXYLICIOUS_INGRESS_CLUSTER_IP,
72+
Constants.KROXYLICIOUS_PROXY_SIMPLE_NAME),
73+
KroxyliciousKafkaClusterRefTemplates.defaultKafkaClusterRefDeployment(deploymentNamespace, clusterName),
74+
KroxyliciousVirtualKafkaClusterTemplates.defaultVirtualKafkaClusterDeployment(deploymentNamespace, clusterName, Constants.KROXYLICIOUS_PROXY_SIMPLE_NAME,
75+
clusterName, Constants.KROXYLICIOUS_INGRESS_CLUSTER_IP));
7176
}
7277

7378
/**
@@ -111,24 +116,6 @@ public String getBootstrap(String serviceNamePrefix) {
111116
return bootstrap;
112117
}
113118

114-
/**
115-
* Deploy kroxylicious example.
116-
*
117-
* @param path the path
118-
*/
119-
public void deployKroxyliciousExample(String path) {
120-
LOGGER.info("Deploying Kroxylicious from path {}", path);
121-
DeploymentUtils.deployYamlFiles(deploymentNamespace, getExampleFiles(path));
122-
}
123-
124-
private static List<File> getExampleFiles(String examplePath) {
125-
return Arrays.stream(Objects.requireNonNull(new File(examplePath).listFiles()))
126-
.sorted()
127-
.filter(File::isFile)
128-
.filter(file -> file.getName().endsWith(".yaml"))
129-
.toList();
130-
}
131-
132119
/**
133120
* Gets number of replicas.
134121
*
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright Kroxylicious Authors.
3+
*
4+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
package io.kroxylicious.systemtests.resources.kroxylicious;
8+
9+
import io.fabric8.kubernetes.api.model.DeletionPropagation;
10+
import io.fabric8.kubernetes.api.model.HasMetadata;
11+
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
12+
import io.fabric8.kubernetes.client.dsl.MixedOperation;
13+
import io.fabric8.kubernetes.client.dsl.Resource;
14+
15+
import io.kroxylicious.systemtests.k8s.KubeClusterResource;
16+
import io.kroxylicious.systemtests.resources.ResourceType;
17+
18+
public class KroxyliciousResource<T extends HasMetadata> implements ResourceType<T> {
19+
private final Class<T> resourceClass;
20+
21+
public KroxyliciousResource(Class<T> resourceClass) {
22+
this.resourceClass = resourceClass;
23+
}
24+
25+
/**
26+
* Kafka Proxy mixed operation.
27+
*
28+
* @return the mixed operation
29+
*/
30+
public MixedOperation<T, KubernetesResourceList<T>, Resource<T>> kubeClient() {
31+
return KubeClusterResource.kubeClient().getClient().resources(resourceClass);
32+
}
33+
34+
@Override
35+
public String getKind() {
36+
return resourceClass.getSimpleName();
37+
}
38+
39+
@Override
40+
public T get(String namespace, String name) {
41+
return kubeClient().inNamespace(namespace).withName(name).get();
42+
}
43+
44+
@Override
45+
public void create(T resource) {
46+
kubeClient().inNamespace(resource.getMetadata().getNamespace()).resource(resource).create();
47+
}
48+
49+
@Override
50+
public void delete(T resource) {
51+
kubeClient().inNamespace(resource.getMetadata().getNamespace()).withName(
52+
resource.getMetadata().getName()).withPropagationPolicy(DeletionPropagation.FOREGROUND).delete();
53+
}
54+
55+
@Override
56+
public void update(T resource) {
57+
kubeClient().inNamespace(resource.getMetadata().getNamespace()).resource(resource).update();
58+
}
59+
60+
@Override
61+
public boolean waitForReadiness(T resource) {
62+
return resource != null;
63+
}
64+
}

kroxylicious-systemtests/src/main/java/io/kroxylicious/systemtests/resources/manager/ResourceManager.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
import org.slf4j.Logger;
1818
import org.slf4j.LoggerFactory;
1919

20+
import io.fabric8.kubernetes.api.builder.Builder;
2021
import io.fabric8.kubernetes.api.model.HasMetadata;
2122
import io.fabric8.kubernetes.client.CustomResource;
2223
import io.fabric8.kubernetes.client.dsl.MixedOperation;
2324
import io.skodjob.testframe.resources.ResourceItem;
2425
import io.strimzi.api.kafka.model.common.Spec;
2526
import io.strimzi.api.kafka.model.kafka.Status;
2627

28+
import io.kroxylicious.kubernetes.api.v1alpha1.KafkaProxy;
29+
import io.kroxylicious.kubernetes.api.v1alpha1.KafkaProxyIngress;
30+
import io.kroxylicious.kubernetes.api.v1alpha1.KafkaService;
31+
import io.kroxylicious.kubernetes.api.v1alpha1.VirtualKafkaCluster;
2732
import io.kroxylicious.systemtests.Constants;
2833
import io.kroxylicious.systemtests.enums.ConditionStatus;
2934
import io.kroxylicious.systemtests.k8s.HelmClient;
@@ -34,6 +39,7 @@
3439
import io.kroxylicious.systemtests.resources.ResourceType;
3540
import io.kroxylicious.systemtests.resources.kroxylicious.ConfigMapResource;
3641
import io.kroxylicious.systemtests.resources.kroxylicious.DeploymentResource;
42+
import io.kroxylicious.systemtests.resources.kroxylicious.KroxyliciousResource;
3743
import io.kroxylicious.systemtests.resources.kroxylicious.SecretResource;
3844
import io.kroxylicious.systemtests.resources.kroxylicious.ServiceResource;
3945
import io.kroxylicious.systemtests.resources.kubernetes.ClusterOperatorCustomResourceDefinition;
@@ -107,9 +113,46 @@ public static HelmClient helmClient() {
107113
new ConfigMapResource(),
108114
new DeploymentResource(),
109115
new SecretResource(),
110-
new ClusterOperatorCustomResourceDefinition()
116+
new ClusterOperatorCustomResourceDefinition(),
117+
new KroxyliciousResource<>(KafkaProxy.class),
118+
new KroxyliciousResource<>(KafkaService.class),
119+
new KroxyliciousResource<>(KafkaProxyIngress.class),
120+
new KroxyliciousResource<>(VirtualKafkaCluster.class)
111121
};
112122

123+
/**
124+
* Create resource with wait.
125+
*
126+
* @param <T> the type parameter
127+
* @param resources the resources
128+
*/
129+
@SafeVarargs
130+
public final <T extends HasMetadata> void createResourceWithoutWait(Builder<T>... resources) {
131+
createResource(false, Arrays.stream(resources).map(Builder::build).toList().toArray(new HasMetadata[0]));
132+
}
133+
134+
/**
135+
* Create resource with wait.
136+
*
137+
* @param <T> the type parameter
138+
* @param resourceBuilder the resourcesBuilder to construct the resource from
139+
*/
140+
public final <T extends HasMetadata> T createResourceWithWait(Builder<T> resourceBuilder) {
141+
T builtResource = resourceBuilder.build();
142+
createResource(true, builtResource);
143+
return builtResource;
144+
}
145+
146+
/**
147+
* Create resource with wait.
148+
*
149+
* @param resources the resources
150+
*/
151+
@SafeVarargs
152+
public final void createResourceFromBuilder(Builder<? extends HasMetadata>... resources) {
153+
createResource(true, Arrays.stream(resources).map(Builder::build).toList().toArray(new HasMetadata[0]));
154+
}
155+
113156
/**
114157
* Create resource without wait.
115158
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright Kroxylicious Authors.
3+
*
4+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
package io.kroxylicious.systemtests.templates.kroxylicious;
8+
9+
import io.kroxylicious.kubernetes.api.v1alpha1.KafkaServiceBuilder;
10+
import io.kroxylicious.systemtests.Constants;
11+
12+
public class KroxyliciousKafkaClusterRefTemplates {
13+
14+
private KroxyliciousKafkaClusterRefTemplates() {
15+
}
16+
17+
/**
18+
* Default kafka cluster ref deployment.
19+
*
20+
* @param namespaceName the namespace name
21+
* @param clusterRefName the cluster ref name
22+
* @return the kafka service builder
23+
*/
24+
public static KafkaServiceBuilder defaultKafkaClusterRefDeployment(String namespaceName, String clusterRefName) {
25+
// @formatter:off
26+
return new KafkaServiceBuilder()
27+
.withNewMetadata()
28+
.withName(clusterRefName)
29+
.withNamespace(namespaceName)
30+
.endMetadata()
31+
.withNewSpec()
32+
.withBootstrapServers("%s-kafka-bootstrap.%s.svc.cluster.local:9092".formatted(clusterRefName, Constants.KAFKA_DEFAULT_NAMESPACE))
33+
.endSpec();
34+
// @formatter:on
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright Kroxylicious Authors.
3+
*
4+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
package io.kroxylicious.systemtests.templates.kroxylicious;
8+
9+
import io.kroxylicious.kubernetes.api.v1alpha1.KafkaProxyIngressBuilder;
10+
11+
import static io.kroxylicious.kubernetes.api.v1alpha1.kafkaproxyingressspec.ClusterIP.Protocol.TCP;
12+
13+
public class KroxyliciousKafkaProxyIngressTemplates {
14+
15+
private KroxyliciousKafkaProxyIngressTemplates() {
16+
}
17+
18+
/**
19+
* Default kafka proxy ingress deployment.
20+
*
21+
* @param namespaceName the namespace name
22+
* @param ingressName the ingress name
23+
* @param proxyName the name of the proxy to reference
24+
* @return the kafka proxy ingress builder
25+
*/
26+
public static KafkaProxyIngressBuilder defaultKafkaProxyIngressDeployment(String namespaceName, String ingressName, String proxyName) {
27+
// @formatter:off
28+
return new KafkaProxyIngressBuilder()
29+
.withNewMetadata()
30+
.withName(ingressName)
31+
.withNamespace(namespaceName)
32+
.endMetadata()
33+
.withNewSpec()
34+
.withNewClusterIP()
35+
.withProtocol(TCP)
36+
.endClusterIP()
37+
.withNewProxyRef()
38+
.withName(proxyName)
39+
.endProxyRef()
40+
.endSpec();
41+
// @formatter:on
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Kroxylicious Authors.
3+
*
4+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
package io.kroxylicious.systemtests.templates.kroxylicious;
8+
9+
import io.kroxylicious.kubernetes.api.v1alpha1.KafkaProxyBuilder;
10+
11+
public class KroxyliciousKafkaProxyTemplates {
12+
13+
private KroxyliciousKafkaProxyTemplates() {
14+
}
15+
16+
/**
17+
* Default kafka proxy deployment.
18+
*
19+
* @param namespaceName the namespace name
20+
* @param name the name
21+
* @return the kafka proxy builder
22+
*/
23+
public static KafkaProxyBuilder defaultKafkaProxyDeployment(String namespaceName, String name) {
24+
// @formatter:off
25+
return new KafkaProxyBuilder()
26+
.withNewMetadata()
27+
.withName(name)
28+
.withNamespace(namespaceName)
29+
.endMetadata();
30+
// @formatter:on
31+
}
32+
}

0 commit comments

Comments
 (0)