Skip to content

Commit 36dc1a8

Browse files
committed
Remove version check when talking to controllers
This change is targeted for a release that supports 3.9.0 or later Signed-off-by: Gantigmaa Selenge <[email protected]>
1 parent 316fc14 commit 36dc1a8

File tree

3 files changed

+12
-74
lines changed

3 files changed

+12
-74
lines changed

cluster-operator/src/main/java/io/strimzi/operator/cluster/operator/resource/KafkaRoller.java

+11-45
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.strimzi.operator.cluster.operator.resource.events.KubernetesRestartEventPublisher;
2121
import io.strimzi.operator.cluster.operator.resource.kubernetes.PodOperator;
2222
import io.strimzi.operator.common.AdminClientProvider;
23-
import io.strimzi.operator.common.Annotations;
2423
import io.strimzi.operator.common.BackOff;
2524
import io.strimzi.operator.common.Reconciliation;
2625
import io.strimzi.operator.common.ReconciliationLogger;
@@ -209,19 +208,10 @@ private boolean maybeInitBrokerAdminClient() {
209208
* Initializes controllerAdminClient if it has not been initialized yet
210209
* @return true if the creation of AC succeeded, false otherwise
211210
*/
212-
private boolean maybeInitControllerAdminClient(String currentVersion) {
211+
private boolean maybeInitControllerAdminClient() {
213212
if (this.controllerAdminClient == null) {
214-
// Prior to 3.9.0, Kafka did not support directly connecting to controllers nodes
215-
// via Kafka Admin API when running in KRaft mode.
216-
// Therefore, use brokers to initialise adminClient for quorum health check
217-
// when the version is older than 3.9.0.
218213
try {
219-
if (KafkaVersion.compareDottedVersions(currentVersion, "3.9.0") >= 0) {
220-
this.controllerAdminClient = controllerAdminClient(nodes);
221-
} else {
222-
this.controllerAdminClient = brokerAdminClient(Set.of());
223-
224-
}
214+
this.controllerAdminClient = controllerAdminClient(nodes);
225215
} catch (ForceableProblem | FatalProblem e) {
226216
LOGGER.warnCr(reconciliation, "Failed to create controllerAdminClient.", e);
227217
return false;
@@ -455,11 +445,9 @@ private void restartIfNecessary(NodeRef nodeRef, RestartContext restartContext)
455445
// change and the desired roles still apply.
456446
boolean isBroker = Labels.booleanLabel(pod, Labels.STRIMZI_BROKER_ROLE_LABEL, nodeRef.broker());
457447
boolean isController = Labels.booleanLabel(pod, Labels.STRIMZI_CONTROLLER_ROLE_LABEL, nodeRef.controller());
458-
// This is relevant when creating admin client for controllers
459-
String currentVersion = Annotations.stringAnnotation(pod, KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, "0.0.0", null);
460448

461449
try {
462-
checkIfRestartOrReconfigureRequired(nodeRef, isController, isBroker, restartContext, currentVersion);
450+
checkIfRestartOrReconfigureRequired(nodeRef, isController, isBroker, restartContext);
463451
if (restartContext.forceRestart) {
464452
LOGGER.debugCr(reconciliation, "Pod {} can be rolled now", nodeRef);
465453
restartAndAwaitReadiness(pod, operationTimeoutMs, TimeUnit.MILLISECONDS, restartContext);
@@ -589,7 +577,7 @@ private void markRestartContextWithForceRestart(RestartContext restartContext) {
589577
* Determine whether the pod should be restarted, or the broker reconfigured.
590578
*/
591579
@SuppressWarnings("checkstyle:CyclomaticComplexity")
592-
private void checkIfRestartOrReconfigureRequired(NodeRef nodeRef, boolean isController, boolean isBroker, RestartContext restartContext, String currentVersion) throws ForceableProblem, InterruptedException, FatalProblem, UnforceableProblem {
580+
private void checkIfRestartOrReconfigureRequired(NodeRef nodeRef, boolean isController, boolean isBroker, RestartContext restartContext) throws ForceableProblem, InterruptedException, FatalProblem {
593581
RestartReasons reasonToRestartPod = restartContext.restartReasons;
594582
if (restartContext.podStuck && !reasonToRestartPod.contains(RestartReason.POD_HAS_OLD_REVISION)) {
595583
// If the pod is unschedulable then deleting it, or trying to open an Admin client to it will make no difference
@@ -612,10 +600,13 @@ private void checkIfRestartOrReconfigureRequired(NodeRef nodeRef, boolean isCont
612600

613601
// if it is a pure controller, initialise the admin client specifically for controllers
614602
if (isController && !isBroker) {
615-
if (!maybeInitControllerAdminClient(currentVersion)) {
616-
handleFailedAdminClientForController(nodeRef, restartContext, reasonToRestartPod, currentVersion);
603+
if (!maybeInitControllerAdminClient()) {
604+
LOGGER.infoCr(reconciliation, "Pod {} needs to be restarted, because it does not seem to responding to connection attempts", nodeRef);
605+
reasonToRestartPod.add(RestartReason.POD_UNRESPONSIVE);
606+
markRestartContextWithForceRestart(restartContext);
617607
return;
618608
}
609+
LOGGER.debugCr(reconciliation, "Initialising KafkaQuorumCheck for controller pod {}", nodeRef);
619610
restartContext.quorumCheck = quorumCheck(controllerAdminClient, nodeRef);
620611
}
621612

@@ -629,6 +620,7 @@ private void checkIfRestartOrReconfigureRequired(NodeRef nodeRef, boolean isCont
629620

630621
// If it is a mixed node, initialise quorum check with the broker admin client
631622
if (isController) {
623+
LOGGER.debugCr(reconciliation, "Initialising KafkaQuorumCheck for mixed roles pod {}", nodeRef);
632624
restartContext.quorumCheck = quorumCheck(brokerAdminClient, nodeRef);
633625
}
634626
}
@@ -637,7 +629,6 @@ private void checkIfRestartOrReconfigureRequired(NodeRef nodeRef, boolean isCont
637629
// connect to the node and that it's capable of responding.
638630
Config nodeConfig;
639631
try {
640-
System.out.println("TINA Getting node config for " + nodeRef.nodeId());
641632
nodeConfig = nodeConfig(nodeRef, isController && !isBroker);
642633
} catch (ForceableProblem e) {
643634
if (restartContext.backOff.done()) {
@@ -678,21 +669,6 @@ private void checkIfRestartOrReconfigureRequired(NodeRef nodeRef, boolean isCont
678669
restartContext.nodeLoggingDiff = nodeLoggingDiff;
679670
}
680671

681-
private void handleFailedAdminClientForController(NodeRef nodeRef, RestartContext restartContext, RestartReasons reasonToRestartPod, String currentVersion) throws UnforceableProblem {
682-
if (KafkaVersion.compareDottedVersions(currentVersion, "3.9.0") >= 0) {
683-
// If the version supports talking to controllers, force restart this pod when the admin client cannot be initialised.
684-
// There is no reason to continue as we won't be able to connect an admin client to this pod for other checks later.
685-
LOGGER.infoCr(reconciliation, "KafkaQuorumCheck cannot be initialised for {} because none of the controllers do not seem to responding to connection attempts.", nodeRef);
686-
reasonToRestartPod.add(RestartReason.POD_UNRESPONSIVE);
687-
markRestartContextWithForceRestart(restartContext);
688-
} else {
689-
// If the version does not support talking to controllers, the admin client should be connecting to the broker nodes.
690-
// Since connection to the brokers failed, throw an UnforceableProblem so that broker nodes can be checked later
691-
// which may potentially resolve the connection issue.
692-
throw new UnforceableProblem("KafkaQuorumCheck cannot be initialised for " + nodeRef + " because none of the brokers do not seem to responding to connection attempts");
693-
}
694-
}
695-
696672
/**
697673
* Returns a config of the given broker.
698674
* @param nodeRef The reference of the broker.
@@ -701,13 +677,11 @@ private void handleFailedAdminClientForController(NodeRef nodeRef, RestartContex
701677
/* test */ Config nodeConfig(NodeRef nodeRef, boolean isPureController) throws ForceableProblem, InterruptedException {
702678
ConfigResource resource = new ConfigResource(ConfigResource.Type.BROKER, String.valueOf(nodeRef.nodeId()));
703679
if (isPureController) {
704-
System.out.println("Getting controller config");
705680
return await(VertxUtil.kafkaFutureToVertxFuture(reconciliation, vertx, controllerAdminClient.describeConfigs(singletonList(resource)).values().get(resource)),
706681
30, TimeUnit.SECONDS,
707682
error -> new ForceableProblem("Error getting controller config: " + error, error)
708683
);
709684
} else {
710-
System.out.println("Getting broker config");
711685
return await(VertxUtil.kafkaFutureToVertxFuture(reconciliation, vertx, brokerAdminClient.describeConfigs(singletonList(resource)).values().get(resource)),
712686
30, TimeUnit.SECONDS,
713687
error -> new ForceableProblem("Error getting broker config: " + error, error)
@@ -926,15 +900,7 @@ protected Future<Void> restart(Pod pod, RestartContext restartContext) {
926900
* empty set, use the brokers service to bootstrap the client.
927901
*/
928902
/* test */ Admin brokerAdminClient(Set<NodeRef> nodes) throws ForceableProblem, FatalProblem {
929-
// If no nodes are passed, initialize the admin client using the bootstrap service
930-
// This is still needed for versions older than 3.9.0, so that when only controller nodes being rolled,
931-
// it can use brokers to get quorum information via AdminClient.
932-
String bootstrapHostnames;
933-
if (nodes.isEmpty()) {
934-
bootstrapHostnames = String.format("%s:%s", DnsNameGenerator.of(namespace, KafkaResources.bootstrapServiceName(cluster)).serviceDnsName(), KafkaCluster.REPLICATION_PORT);
935-
} else {
936-
bootstrapHostnames = nodes.stream().filter(NodeRef::broker).map(node -> DnsNameGenerator.podDnsName(namespace, KafkaResources.brokersServiceName(cluster), node.podName()) + ":" + KafkaCluster.REPLICATION_PORT).collect(Collectors.joining(","));
937-
}
903+
String bootstrapHostnames = nodes.stream().filter(NodeRef::broker).map(node -> DnsNameGenerator.podDnsName(namespace, KafkaResources.brokersServiceName(cluster), node.podName()) + ":" + KafkaCluster.REPLICATION_PORT).collect(Collectors.joining(","));
938904

939905
try {
940906
LOGGER.debugCr(reconciliation, "Creating AdminClient for {}", bootstrapHostnames);

cluster-operator/src/test/java/io/strimzi/operator/cluster/operator/assembly/KafkaAssemblyOperatorKRaftMockTest.java

-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import io.strimzi.operator.cluster.PlatformFeaturesAvailability;
2929
import io.strimzi.operator.cluster.ResourceUtils;
3030
import io.strimzi.operator.cluster.model.CertUtils;
31-
import io.strimzi.operator.cluster.model.KafkaCluster;
3231
import io.strimzi.operator.cluster.model.KafkaVersion;
3332
import io.strimzi.operator.cluster.model.PodSetUtils;
3433
import io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier;
@@ -62,7 +61,6 @@
6261

6362
import static org.hamcrest.CoreMatchers.hasItems;
6463
import static org.hamcrest.CoreMatchers.is;
65-
import static org.hamcrest.CoreMatchers.not;
6664
import static org.hamcrest.CoreMatchers.notNullValue;
6765
import static org.hamcrest.CoreMatchers.nullValue;
6866
import static org.hamcrest.CoreMatchers.startsWith;

cluster-operator/src/test/java/io/strimzi/operator/cluster/operator/resource/KafkaRollerTest.java

+1-27
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.fabric8.kubernetes.client.KubernetesClientException;
1010
import io.strimzi.api.kafka.model.kafka.KafkaResources;
1111
import io.strimzi.operator.cluster.KafkaVersionTestUtils;
12-
import io.strimzi.operator.cluster.model.KafkaCluster;
1312
import io.strimzi.operator.cluster.model.NodeRef;
1413
import io.strimzi.operator.cluster.model.RestartReason;
1514
import io.strimzi.operator.cluster.model.RestartReasons;
@@ -162,7 +161,7 @@ private static AdminClientProvider givenControllerFutureFailsWithTimeout() {
162161

163162
@Test
164163
public void testTalkingToControllersLatestVersion(VertxTestContext testContext) {
165-
PodOperator podOps = mockPodOpsWithVersion(podId -> succeededFuture(), KafkaVersionTestUtils.getLatestVersion().version());
164+
PodOperator podOps = mockPodOps(podId -> succeededFuture());
166165
AdminClientProvider mock = mock(AdminClientProvider.class);
167166
when(mock.createControllerAdminClient(anyString(), any(), any())).thenThrow(new RuntimeException("An error while try to create an admin client with bootstrap controllers"));
168167

@@ -177,26 +176,6 @@ public void testTalkingToControllersLatestVersion(VertxTestContext testContext)
177176
asList(0));
178177
}
179178

180-
@Test
181-
public void testTalkingToControllersWithOldVersion(VertxTestContext testContext) throws InterruptedException {
182-
PodOperator podOps = mockPodOpsWithVersion(podId -> succeededFuture(), "3.8.0");
183-
184-
AdminClientProvider mock = mock(AdminClientProvider.class);
185-
when(mock.createAdminClient(anyString(), any(), any())).thenThrow(new RuntimeException("An error while try to create an admin client with bootstrap brokers"));
186-
187-
TestingKafkaRoller kafkaRoller = new TestingKafkaRoller(addKraftPodNames(0, 0, 1), podOps,
188-
noException(), null, noException(), noException(), noException(),
189-
brokerId -> succeededFuture(true),
190-
true, mock, mockKafkaAgentClientProvider(), true, null, -1);
191-
192-
// If the controller has older version (< 3.9.0), we should only be creating admin client for brokers
193-
// and when the operator cannot connect to brokers, we expect to fail initialising KafkaQuorumCheck
194-
doFailingRollingRestart(testContext, kafkaRoller,
195-
asList(0),
196-
KafkaRoller.UnforceableProblem.class, "KafkaQuorumCheck cannot be initialised for c-kafka-0/0 because none of the brokers do not seem to responding to connection attempts",
197-
emptyList());
198-
}
199-
200179
private static KafkaAgentClientProvider mockKafkaAgentClientProvider() {
201180
return mock(KafkaAgentClientProvider.class);
202181
}
@@ -835,17 +814,12 @@ public void clearRestarted() {
835814
}
836815

837816
private PodOperator mockPodOps(Function<Integer, Future<Void>> readiness) {
838-
return mockPodOpsWithVersion(readiness, KafkaVersionTestUtils.getLatestVersion().version());
839-
}
840-
841-
private PodOperator mockPodOpsWithVersion(Function<Integer, Future<Void>> readiness, String version) {
842817
PodOperator podOps = mock(PodOperator.class);
843818
when(podOps.get(any(), any())).thenAnswer(
844819
invocation -> new PodBuilder()
845820
.withNewMetadata()
846821
.withNamespace(invocation.getArgument(0))
847822
.withName(invocation.getArgument(1))
848-
.addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, version)
849823
.endMetadata()
850824
.build()
851825
);

0 commit comments

Comments
 (0)