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

Refactor connector preparation our of KafkaMirrorMaker2AssemblyOperator #9593

Merged
merged 1 commit into from
Jan 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package io.strimzi.operator.cluster.model;

import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.strimzi.api.kafka.model.common.CertSecretSource;
Expand Down Expand Up @@ -61,8 +60,6 @@ public class KafkaMirrorMaker2Cluster extends KafkaConnectCluster {
protected static final String MIRRORMAKER_2_TLS_CERTS_BASE_VOLUME_MOUNT = "/opt/kafka/mm2-certs/";
protected static final String MIRRORMAKER_2_PASSWORD_VOLUME_MOUNT = "/opt/kafka/mm2-password/";

private List<KafkaMirrorMaker2ClusterSpec> clusters;

private static final Map<String, String> DEFAULT_POD_LABELS = new HashMap<>();
static {
String value = System.getenv(CO_ENV_VAR_CUSTOM_MIRROR_MAKER2_POD_LABELS);
Expand All @@ -71,16 +68,21 @@ public class KafkaMirrorMaker2Cluster extends KafkaConnectCluster {
}
}

private final KafkaMirrorMaker2Connectors connectors;

private List<KafkaMirrorMaker2ClusterSpec> clusters;

/**
* Constructor
*
* @param reconciliation The reconciliation
* @param resource Kubernetes resource with metadata containing the namespace and cluster name
* @param sharedEnvironmentProvider Shared environment provider
*/
private KafkaMirrorMaker2Cluster(Reconciliation reconciliation, HasMetadata resource, SharedEnvironmentProvider sharedEnvironmentProvider) {
private KafkaMirrorMaker2Cluster(Reconciliation reconciliation, KafkaMirrorMaker2 resource, SharedEnvironmentProvider sharedEnvironmentProvider) {
super(reconciliation, resource, KafkaMirrorMaker2Resources.componentName(resource.getMetadata().getName()), COMPONENT_TYPE, sharedEnvironmentProvider);

this.connectors = KafkaMirrorMaker2Connectors.fromCrd(reconciliation, resource);
this.serviceName = KafkaMirrorMaker2Resources.serviceName(cluster);
this.loggingAndMetricsConfigMapName = KafkaMirrorMaker2Resources.metricsAndLogConfigMapName(cluster);
}
Expand Down Expand Up @@ -376,4 +378,11 @@ protected String getCommand() {
protected Map<String, String> defaultPodLabels() {
return DEFAULT_POD_LABELS;
}

/**
* @return Returns the Mirror Maker 2 Connectors model
*/
public KafkaMirrorMaker2Connectors connectors() {
return connectors;
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,10 @@ public static KafkaMirrorMaker2 createEmptyKafkaMirrorMaker2(String namespace, S
.withLabels(TestUtils.map(Labels.KUBERNETES_DOMAIN + "part-of", "tests",
"my-user-label", "cromulent"))
.build())
.withNewSpec().endSpec();
.withNewSpec()
.withClusters(List.of())
.withMirrors(List.of())
.endSpec();

if (replicas != null) {
kafkaMirrorMaker2Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public class KafkaMirrorMaker2ClusterTest {
.withLivenessProbe(new Probe(healthDelay, healthTimeout))
.withConnectCluster(targetClusterAlias)
.withClusters(targetCluster)
.withMirrors(List.of())
.endSpec()
.build();

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ public void testReconcileUpdate(VertxTestContext context) {
.withLabels(TestUtils.map("foo", "bar"))
.build())
.withNewSpec()
.withReplicas(REPLICAS)
.withReplicas(REPLICAS)
.withClusters(List.of())
.withMirrors(List.of())
.endSpec()
.build()).create();

Expand Down Expand Up @@ -190,7 +192,9 @@ public void testPauseReconcile(VertxTestContext context) {
.withAnnotations(singletonMap("strimzi.io/pause-reconciliation", "true"))
.build())
.withNewSpec()
.withReplicas(REPLICAS)
.withReplicas(REPLICAS)
.withClusters(List.of())
.withMirrors(List.of())
.endSpec()
.build()).create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import io.strimzi.operator.cluster.KafkaVersionTestUtils;
import io.strimzi.operator.cluster.PlatformFeaturesAvailability;
import io.strimzi.operator.cluster.ResourceUtils;
import io.strimzi.operator.cluster.model.AuthenticationUtilsTest;
import io.strimzi.operator.cluster.model.KafkaMirrorMaker2Cluster;
import io.strimzi.operator.cluster.model.KafkaVersion;
import io.strimzi.operator.cluster.model.MockSharedEnvironmentProvider;
Expand Down Expand Up @@ -65,11 +64,9 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArraySet;

import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -109,6 +106,8 @@ public class KafkaMirrorMaker2AssemblyOperatorPodSetTest {
.endMetadata()
.withNewSpec()
.withReplicas(3)
.withClusters(List.of())
.withMirrors(List.of())
.endSpec()
.build();
private static final KafkaMirrorMaker2Cluster CLUSTER = KafkaMirrorMaker2Cluster.fromCrd(RECONCILIATION, MM2, VERSIONS, SHARED_ENV_PROVIDER);
Expand Down Expand Up @@ -1454,121 +1453,6 @@ public void testManualRollingUpdatePerPod(VertxTestContext context) {
})));
}

@Test
public void testAddClusterToMirrorMaker2ConnectorConfigWithPlain() {
var config = new HashMap<String, Object>();
var prefix = "prefix";
var cluster =
new KafkaMirrorMaker2ClusterSpecBuilder(true)
.withAlias("sourceClusterAlias")
.withBootstrapServers("sourceClusterAlias.sourceNamespace.svc:9092")
.withNewKafkaClientAuthenticationPlain()
.withUsername("shaza")
.withNewPasswordSecret()
.withPassword("pa55word")
.endPasswordSecret()
.endKafkaClientAuthenticationPlain()
.build();

KafkaMirrorMaker2AssemblyOperator.addClusterToMirrorMaker2ConnectorConfig(config, cluster, prefix);

var jaasConfig = (String) config.remove("prefixsasl.jaas.config");
var configEntry = AuthenticationUtilsTest.parseJaasConfig(jaasConfig);
assertThat("org.apache.kafka.common.security.plain.PlainLoginModule", is(configEntry.getLoginModuleName()));
assertThat(Map.of(
"username", "shaza",
"password", "${file:/tmp/strimzi-mirrormaker2-connector.properties:sourceClusterAlias.sasl.password}"),
is(configEntry.getOptions()));

assertThat(new TreeMap<>(Map.of("prefixalias", "sourceClusterAlias",
"prefixsecurity.protocol", "SASL_PLAINTEXT",
"prefixsasl.mechanism", "PLAIN",
"prefixbootstrap.servers", "sourceClusterAlias.sourceNamespace.svc:9092")),
is(new TreeMap<>(config)));
}

@Test
public void testAddClusterToMirrorMaker2ConnectorConfigWithScram() {
var config = new HashMap<String, Object>();
var prefix = "prefix";
var cluster =
new KafkaMirrorMaker2ClusterSpecBuilder(true)
.withAlias("sourceClusterAlias")
.withBootstrapServers("sourceClusterAlias.sourceNamespace.svc:9092")
.withNewKafkaClientAuthenticationScramSha512()
.withUsername("shaza")
.withNewPasswordSecret()
.withPassword("pa55word")
.endPasswordSecret()
.endKafkaClientAuthenticationScramSha512()
.build();

KafkaMirrorMaker2AssemblyOperator.addClusterToMirrorMaker2ConnectorConfig(config, cluster, prefix);

var jaasConfig = (String) config.remove("prefixsasl.jaas.config");
var configEntry = AuthenticationUtilsTest.parseJaasConfig(jaasConfig);
assertThat("org.apache.kafka.common.security.scram.ScramLoginModule", is(configEntry.getLoginModuleName()));
assertThat(Map.of(
"username", "shaza",
"password", "${file:/tmp/strimzi-mirrormaker2-connector.properties:sourceClusterAlias.sasl.password}"),
is(configEntry.getOptions()));

assertThat(new TreeMap<>(Map.of("prefixalias", "sourceClusterAlias",
"prefixsecurity.protocol", "SASL_PLAINTEXT",
"prefixsasl.mechanism", "SCRAM-SHA-512",
"prefixbootstrap.servers", "sourceClusterAlias.sourceNamespace.svc:9092")),
is(new TreeMap<>(config)));
}

@Test
public void testAddClusterToMirrorMaker2ConnectorConfigWithOauth() {
var config = new HashMap<String, Object>();
var prefix = "prefix";
var cluster =
new KafkaMirrorMaker2ClusterSpecBuilder(true)
.withAlias("sourceClusterAlias")
.withBootstrapServers("sourceClusterAlias.sourceNamespace.svc:9092")
.withNewKafkaClientAuthenticationOAuth()
.withNewAccessToken()
.withKey("accessTokenKey")
.withSecretName("accessTokenSecretName")
.endAccessToken()
.withNewClientSecret()
.withKey("clientSecretKey")
.withSecretName("clientSecretSecretName")
.endClientSecret()
.withNewPasswordSecret()
.withPassword("passwordSecretPassword")
.withSecretName("passwordSecretSecretName")
.endPasswordSecret()
.withNewRefreshToken()
.withKey("refreshTokenKey")
.withSecretName("refreshTokenSecretName")
.endRefreshToken()
.endKafkaClientAuthenticationOAuth()
.build();

KafkaMirrorMaker2AssemblyOperator.addClusterToMirrorMaker2ConnectorConfig(config, cluster, prefix);

var jaasConfig = (String) config.remove("prefixsasl.jaas.config");
var configEntry = AuthenticationUtilsTest.parseJaasConfig(jaasConfig);
assertThat("org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule", is(configEntry.getLoginModuleName()));
assertThat(Map.of(
"oauth.client.secret", "${file:/tmp/strimzi-mirrormaker2-connector.properties:sourceClusterAlias.oauth.client.secret}",
"oauth.access.token", "${file:/tmp/strimzi-mirrormaker2-connector.properties:sourceClusterAlias.oauth.access.token}",
"oauth.refresh.token", "${file:/tmp/strimzi-mirrormaker2-connector.properties:sourceClusterAlias.oauth.refresh.token}",
"oauth.password.grant.password", "${file:/tmp/strimzi-mirrormaker2-connector.properties:sourceClusterAlias.oauth.password.grant.password}"),
is(configEntry.getOptions()));

assertThat(Map.of(
"prefixalias", "sourceClusterAlias",
"prefixbootstrap.servers", "sourceClusterAlias.sourceNamespace.svc:9092",
"prefixsasl.login.callback.handler.class", "io.strimzi.kafka.oauth.client.JaasClientOauthLoginCallbackHandler",
"prefixsasl.mechanism", "OAUTHBEARER",
"prefixsecurity.protocol", "SASL_PLAINTEXT"),
is(config));
}

private KafkaConnectApi createConnectClientMock() {
KafkaConnectApi mockConnectClient = mock(KafkaConnectApi.class);
when(mockConnectClient.list(any(), anyString(), anyInt())).thenReturn(Future.succeededFuture(emptyList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ kind: KafkaMirrorMaker2
metadata:
name: my-mm2-cluster
spec:
clusters: []
mirrors: []
template:
pod:
affinity:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ kind: KafkaMirrorMaker2
metadata:
name: my-mm2-cluster
spec:
clusters: []
mirrors: []
template:
pod:
tolerations:
Expand Down