Skip to content

Commit

Permalink
Add docs, improve naming, remove start imports
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <pdipilat@redhat.com>
  • Loading branch information
pierDipi committed Nov 10, 2021
1 parent e19b137 commit e0de981
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import java.util.function.Function;

import static dev.knative.eventing.kafka.broker.core.utils.Logging.keyValue;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ProbeRequestUtil.PROBE_HASH_HEADER_NAME;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ProbeRequestUtil.isProbeRequest;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ControlPlaneProbeRequestUtil.PROBE_HASH_HEADER_NAME;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ControlPlaneProbeRequestUtil.isControlPlaneProbeRequest;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;

Expand Down Expand Up @@ -125,7 +125,7 @@ public void handle(HttpServerRequest request) {
return;
}

if (isProbeRequest(request)) {
if (isControlPlaneProbeRequest(request)) {
request.response()
.putHeader(PROBE_HASH_HEADER_NAME, request.getHeader(PROBE_HASH_HEADER_NAME))
.setStatusCode(OK.code()).end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;

public class ProbeRequestUtil {
public final class ControlPlaneProbeRequestUtil {

public static final String PROBE_HEADER_NAME = "K-Network-Probe";
private ControlPlaneProbeRequestUtil() {
}

// Control plane probing is based on the Knative Networking Data Plane Contract.
// For a description of what these headers are see [1].
//
// [1]: https://github.com/knative/pkg/blob/b558677ab03404ed118ba3e179a7438fe2d8509a/network/network.go#L38-L51
public static final String PROBE_HEADER_NAME = "K-Network-Probe";
public static final String PROBE_HEADER_VALUE = "probe";

public static final String PROBE_HASH_HEADER_NAME = "K-Network-Hash";

public static boolean isProbeRequest(final HttpServerRequest request) {
/**
* @param request HTTP request
* @return true if the provided request conforms to the Knative Networking Data Plane Contract, false otherwise.
*/
public static boolean isControlPlaneProbeRequest(final HttpServerRequest request) {
final var headers = request.headers();
return HttpMethod.GET.equals(request.method()) &&
headers.contains(PROBE_HEADER_NAME) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.slf4j.LoggerFactory;

import static dev.knative.eventing.kafka.broker.core.utils.Logging.keyValue;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ProbeRequestUtil.*;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ControlPlaneProbeRequestUtil.isControlPlaneProbeRequest;
import static io.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED;

/**
Expand All @@ -42,7 +42,7 @@ public MethodNotAllowedHandler(final Handler<HttpServerRequest> next) {

@Override
public void handle(final HttpServerRequest request) {
if (HttpMethod.POST.equals(request.method()) || isProbeRequest(request)) {
if (HttpMethod.POST.equals(request.method()) || isControlPlaneProbeRequest(request)) {
this.next.handle(request);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import dev.knative.eventing.kafka.broker.core.security.AuthProvider;
import dev.knative.eventing.kafka.broker.core.testing.CloudEventSerializerMock;
import dev.knative.eventing.kafka.broker.receiver.impl.handler.IngressRequestHandlerImpl;
import dev.knative.eventing.kafka.broker.receiver.impl.handler.ProbeRequestUtil;
import dev.knative.eventing.kafka.broker.receiver.impl.handler.ControlPlaneProbeRequestUtil;
import dev.knative.eventing.kafka.broker.receiver.main.ReceiverEnv;
import io.cloudevents.CloudEvent;
import io.cloudevents.core.v1.CloudEventBuilder;
Expand Down Expand Up @@ -371,7 +371,7 @@ private static Function<HttpRequest<Buffer>, Future<HttpResponse<Buffer>>> probe
final CloudEvent event) {
return request -> request
.method(HttpMethod.GET)
.putHeader(ProbeRequestUtil.PROBE_HEADER_NAME, ProbeRequestUtil.PROBE_HEADER_VALUE)
.putHeader(ControlPlaneProbeRequestUtil.PROBE_HEADER_NAME, ControlPlaneProbeRequestUtil.PROBE_HEADER_VALUE)
.send();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import org.junit.jupiter.api.Test;

import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ProbeRequestUtil.PROBE_HEADER_NAME;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ProbeRequestUtil.PROBE_HEADER_VALUE;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ProbeRequestUtil.isProbeRequest;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ControlPlaneProbeRequestUtil.PROBE_HEADER_NAME;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ControlPlaneProbeRequestUtil.PROBE_HEADER_VALUE;
import static dev.knative.eventing.kafka.broker.receiver.impl.handler.ControlPlaneProbeRequestUtil.isControlPlaneProbeRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -37,7 +37,7 @@ public void shouldBeProbeRequest() {
when(request.method()).thenReturn(HttpMethod.GET);
when(request.headers()).thenReturn(headers);

assertThat(isProbeRequest(request)).isTrue();
assertThat(isControlPlaneProbeRequest(request)).isTrue();
}

@Test
Expand All @@ -48,7 +48,7 @@ public void shouldNotBeProbeRequestWrongValue() {
when(request.method()).thenReturn(HttpMethod.GET);
when(request.headers()).thenReturn(headers);

assertThat(isProbeRequest(request)).isFalse();
assertThat(isControlPlaneProbeRequest(request)).isFalse();
}

@Test
Expand All @@ -58,7 +58,7 @@ public void shouldNotBeProbeRequestNoProbeHeader() {
when(request.method()).thenReturn(HttpMethod.GET);
when(request.headers()).thenReturn(headers);

assertThat(isProbeRequest(request)).isFalse();
assertThat(isControlPlaneProbeRequest(request)).isFalse();
}

@Test
Expand All @@ -68,6 +68,6 @@ public void shouldNotBeProbeRequestWrongMethod() {
when(request.method()).thenReturn(HttpMethod.POST);
when(request.headers()).thenReturn(headers);

assertThat(isProbeRequest(request)).isFalse();
assertThat(isControlPlaneProbeRequest(request)).isFalse();
}
}

0 comments on commit e0de981

Please sign in to comment.