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

Synchronize healtchecks and operator state #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions src/main/java/nb/kafka/operator/HealthServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,34 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.net.httpserver.HttpServer; // NOSONAR

import nb.kafka.operator.model.OperatorError;

@SuppressWarnings("restriction")
public final class HealthServer {
private final KafkaOperator operator;

private HealthServer() {
public HealthServer(KafkaOperator operator) {
this.operator = operator;
}

private static final Logger logger = LoggerFactory.getLogger(HealthServer.class);

public static void start(AppConfig config) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(config.getHealthsPort()), 0);
public void start(int port) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/healthy", exchange -> exchange.sendResponseHeaders(200, -1));
server.createContext("/ready", exchange -> {
if (isKafkaReachable(config)) {
if (operator.checkOperatorState()) {
exchange.sendResponseHeaders(200, -1);
} else {
exchange.sendResponseHeaders(500, -1);
}
});

logger.info("Starting health checks server on port: {}", config.getHealthsPort());
logger.info("Starting health checks server on port: {}", port);
server.start();
}

private static boolean isKafkaReachable(AppConfig config) {
try (KafkaAdmin ka = new KafkaAdminImpl(config)) {
ka.listTopics();
return true;
} catch (TimeoutException | InterruptedException | ExecutionException e) { // NOSONAR
logger.error(String.format(OperatorError.KAFKA_UNREACHABLE.toString(), config.getBootstrapServers()));
return false;
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/nb/kafka/operator/KafkaOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.micrometer.core.instrument.Gauge;
import nb.kafka.operator.importer.ConfigMapImporter;
import nb.kafka.operator.importer.TopicImporter;
import nb.kafka.operator.model.OperatorError;
import nb.kafka.operator.util.MeterManager;
import nb.kafka.operator.util.TopicValidator;
import nb.kafka.operator.watch.ConfigMapWatcher;
Expand Down Expand Up @@ -108,6 +109,20 @@ public void importTopics() {
topicImporter.importTopics();
}

public boolean checkOperatorState() {
if (operatorState == State.FAILED) {
return false;
}
try (KafkaAdmin ka = new KafkaAdminImpl(config)) {
ka.listTopics();
return true;
} catch (TimeoutException | InterruptedException | ExecutionException e) { // NOSONAR
operatorState = State.FAILED;
log.error(String.format(OperatorError.KAFKA_UNREACHABLE.toString(), config.getBootstrapServers()));
return false;
}
}

private void manageTopic(Topic topic) {
if (topic == null){
return;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/nb/kafka/operator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static void main(String[] args) throws IOException {
setupJmxRegistry(config.getOperatorId());
Runnable stopHttpServer = setupPrometheusRegistry(config.getMetricsPort());

HealthServer.start(config);
KafkaOperator operator = new KafkaOperator(config);
new HealthServer(operator).start(config.getHealthsPort());

Runtime.getRuntime().addShutdownHook(new Thread(operator::shutdown));
Runtime.getRuntime().addShutdownHook(new Thread(stopHttpServer));
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/nb/kafka/operator/KafkaOperatorStateTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package nb.kafka.operator;

import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.concurrent.ExecutionException;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -17,10 +19,11 @@
import nb.kafka.operator.watch.ConfigMapWatcher;

public class KafkaOperatorStateTest {
private AppConfig config = AppConfig.defaultConfig();
private AppConfig config;

@BeforeEach
void setUp() {
config = AppConfig.defaultConfig();
config.setBootstrapServers("localhost:9092");
}

Expand Down Expand Up @@ -75,4 +78,22 @@ public void failureWatchStateTest() throws Throwable {

assertEquals(State.FAILED, operator.getState());
}

@Test
public void healthCheckFailedStateTest() throws Throwable {
config.setKafkaTimeoutMs(100);

KubernetesClient kubeClientMock = mock(KubernetesClient.class);
KafkaAdmin kafkaAdminMock = mock(KafkaAdmin.class);
ConfigMapWatcher watcherMock = mock(ConfigMapWatcher.class);
doThrow(ExecutionException.class).when(kafkaAdminMock).listTopics();

KafkaOperator operator = new KafkaOperator(config, kubeClientMock, kafkaAdminMock, watcherMock,
MeterManager.defaultMeterManager());

assertEquals(State.CREATED, operator.getState());

assertFalse(operator.checkOperatorState());
assertEquals(State.FAILED, operator.getState());
}
}
5 changes: 0 additions & 5 deletions src/test/java/nb/kafka/operator/PartitionedTopicTest.java

This file was deleted.