diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionController.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionController.java index 7b0cee8f..d64dc2f1 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionController.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionController.java @@ -25,10 +25,9 @@ public AdmissionController(AdmissionRequestHandler admissionRequestHandler) { public AdmissionReview handle(AdmissionReview admissionReview) { var response = admissionRequestHandler.handle(admissionReview.getRequest()); - AdmissionReview responseAdmissionReview = new AdmissionReview(); + var responseAdmissionReview = new AdmissionReview(); responseAdmissionReview.setResponse(response); response.setUid(admissionReview.getRequest().getUid()); return responseAdmissionReview; } - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionRequestHandler.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionRequestHandler.java index bddbf991..07dd7b5f 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionRequestHandler.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionRequestHandler.java @@ -6,5 +6,4 @@ public interface AdmissionRequestHandler { AdmissionResponse handle(AdmissionRequest admissionRequest); - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionUtils.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionUtils.java index c1259790..5ae7cb1f 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionUtils.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AdmissionUtils.java @@ -17,9 +17,15 @@ public class AdmissionUtils { private AdmissionUtils() {} + public static AdmissionResponse allowedAdmissionResponse() { + var admissionResponse = new AdmissionResponse(); + admissionResponse.setAllowed(true); + return admissionResponse; + } + public static AdmissionResponse notAllowedExceptionToAdmissionResponse( NotAllowedException notAllowedException) { - AdmissionResponse admissionResponse = new AdmissionResponse(); + var admissionResponse = new AdmissionResponse(); admissionResponse.setAllowed(false); admissionResponse.setStatus(notAllowedException.getStatus()); return admissionResponse; @@ -33,17 +39,16 @@ public static KubernetesResource getTargetResource(AdmissionRequest admissionReq public static AdmissionResponse admissionResponseFromMutation(KubernetesResource originalResource, KubernetesResource mutatedResource) { - AdmissionResponse admissionResponse = new AdmissionResponse(); + var admissionResponse = new AdmissionResponse(); admissionResponse.setAllowed(true); admissionResponse.setPatchType(JSON_PATCH); var originalResNode = mapper.valueToTree(originalResource); var mutatedResNode = mapper.valueToTree(mutatedResource); var diff = JsonDiff.asJson(originalResNode, mutatedResNode); - String base64Diff = + var base64Diff = Base64.getEncoder().encodeToString(diff.toString().getBytes(StandardCharsets.UTF_8)); admissionResponse.setPatch(base64Diff); return admissionResponse; } - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionController.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionController.java index 373469f3..eed32d15 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionController.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionController.java @@ -6,6 +6,7 @@ import io.fabric8.kubernetes.api.model.admission.v1.AdmissionReview; import io.javaoperatorsdk.webhook.admission.mutation.AsyncDefaultAdmissionRequestMutator; import io.javaoperatorsdk.webhook.admission.mutation.AsyncMutator; +import io.javaoperatorsdk.webhook.admission.mutation.Mutator; import io.javaoperatorsdk.webhook.admission.validation.AsyncDefaultAdmissionRequestValidator; import io.javaoperatorsdk.webhook.admission.validation.Validator; @@ -13,10 +14,14 @@ public class AsyncAdmissionController { private final AsyncAdmissionRequestHandler requestHandler; - public AsyncAdmissionController(AsyncMutator mutator) { + public AsyncAdmissionController(Mutator mutator) { this(new AsyncDefaultAdmissionRequestMutator<>(mutator)); } + public AsyncAdmissionController(AsyncMutator asyncMutator) { + this(new AsyncDefaultAdmissionRequestMutator<>(asyncMutator)); + } + public AsyncAdmissionController(Validator validator) { this(new AsyncDefaultAdmissionRequestValidator<>(validator)); } @@ -28,11 +33,10 @@ public AsyncAdmissionController(AsyncAdmissionRequestHandler requestHandler) { public CompletionStage handle(AdmissionReview admissionReview) { return requestHandler.handle(admissionReview.getRequest()) .thenApply(r -> { - AdmissionReview responseAdmissionReview = new AdmissionReview(); + var responseAdmissionReview = new AdmissionReview(); responseAdmissionReview.setResponse(r); r.setUid(admissionReview.getRequest().getUid()); return responseAdmissionReview; }); } - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionRequestHandler.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionRequestHandler.java index ab99106f..768e2faa 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionRequestHandler.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionRequestHandler.java @@ -8,5 +8,4 @@ public interface AsyncAdmissionRequestHandler { CompletionStage handle(AdmissionRequest admissionRequest); - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/AsyncDefaultAdmissionRequestMutator.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/AsyncDefaultAdmissionRequestMutator.java index a64185a0..deefae9d 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/AsyncDefaultAdmissionRequestMutator.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/AsyncDefaultAdmissionRequestMutator.java @@ -1,12 +1,12 @@ package io.javaoperatorsdk.webhook.admission.mutation; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionStage; import io.fabric8.kubernetes.api.model.KubernetesResource; import io.fabric8.kubernetes.api.model.admission.v1.AdmissionRequest; import io.fabric8.kubernetes.api.model.admission.v1.AdmissionResponse; -import io.javaoperatorsdk.webhook.admission.AdmissionUtils; import io.javaoperatorsdk.webhook.admission.AsyncAdmissionRequestHandler; import io.javaoperatorsdk.webhook.admission.NotAllowedException; import io.javaoperatorsdk.webhook.admission.Operation; @@ -15,37 +15,48 @@ import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.admissionResponseFromMutation; import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.getTargetResource; +import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.notAllowedExceptionToAdmissionResponse; public class AsyncDefaultAdmissionRequestMutator implements AsyncAdmissionRequestHandler { - private final AsyncMutator mutator; + private final AsyncMutator asyncMutator; private final Cloner cloner; - public AsyncDefaultAdmissionRequestMutator(AsyncMutator mutator) { + public AsyncDefaultAdmissionRequestMutator(Mutator mutator) { this(mutator, new ObjectMapperCloner<>()); } - public AsyncDefaultAdmissionRequestMutator(AsyncMutator mutator, Cloner cloner) { - this.mutator = mutator; + public AsyncDefaultAdmissionRequestMutator(Mutator mutator, Cloner cloner) { + this((AsyncMutator) (resource, operation) -> CompletableFuture.supplyAsync( + () -> mutator.mutate(resource, operation)), cloner); + } + + public AsyncDefaultAdmissionRequestMutator(AsyncMutator asyncMutator) { + this(asyncMutator, new ObjectMapperCloner<>()); + } + + public AsyncDefaultAdmissionRequestMutator(AsyncMutator asyncMutator, Cloner cloner) { + this.asyncMutator = asyncMutator; this.cloner = cloner; } @Override + @SuppressWarnings("unchecked") public CompletionStage handle(AdmissionRequest admissionRequest) { - Operation operation = Operation.valueOf(admissionRequest.getOperation()); + var operation = Operation.valueOf(admissionRequest.getOperation()); var originalResource = (T) getTargetResource(admissionRequest, operation); var clonedResource = cloner.clone(originalResource); - CompletionStage admissionResponse; - try { - var mutatedResource = mutator.mutate(clonedResource, operation); - admissionResponse = - mutatedResource.thenApply(mr -> admissionResponseFromMutation(originalResource, mr)); - } catch (NotAllowedException e) { - admissionResponse = CompletableFuture - .supplyAsync(() -> AdmissionUtils.notAllowedExceptionToAdmissionResponse(e)); - } - return admissionResponse; + return asyncMutator.mutate(clonedResource, operation) + .thenApply(resource -> admissionResponseFromMutation(originalResource, resource)) + .exceptionally(e -> { + if (e instanceof CompletionException) { + if (e.getCause() instanceof NotAllowedException) { + return notAllowedExceptionToAdmissionResponse((NotAllowedException) e.getCause()); + } + throw new IllegalStateException(e.getCause()); + } + throw new IllegalStateException(e); + }); } - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/AsyncMutator.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/AsyncMutator.java index 2df8a5b0..7969b5ae 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/AsyncMutator.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/AsyncMutator.java @@ -9,5 +9,4 @@ public interface AsyncMutator { CompletionStage mutate(T resource, Operation operation) throws NotAllowedException; - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/DefaultAdmissionRequestMutator.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/DefaultAdmissionRequestMutator.java index 8b5287aa..408be9b2 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/DefaultAdmissionRequestMutator.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/DefaultAdmissionRequestMutator.java @@ -4,7 +4,6 @@ import io.fabric8.kubernetes.api.model.admission.v1.AdmissionRequest; import io.fabric8.kubernetes.api.model.admission.v1.AdmissionResponse; import io.javaoperatorsdk.webhook.admission.AdmissionRequestHandler; -import io.javaoperatorsdk.webhook.admission.AdmissionUtils; import io.javaoperatorsdk.webhook.admission.NotAllowedException; import io.javaoperatorsdk.webhook.admission.Operation; import io.javaoperatorsdk.webhook.clone.Cloner; @@ -12,6 +11,7 @@ import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.admissionResponseFromMutation; import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.getTargetResource; +import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.notAllowedExceptionToAdmissionResponse; public class DefaultAdmissionRequestMutator implements AdmissionRequestHandler { @@ -29,18 +29,16 @@ public DefaultAdmissionRequestMutator(Mutator mutator, Cloner cloner) { } @Override + @SuppressWarnings("unchecked") public AdmissionResponse handle(AdmissionRequest admissionRequest) { - Operation operation = Operation.valueOf(admissionRequest.getOperation()); + var operation = Operation.valueOf(admissionRequest.getOperation()); var originalResource = (T) getTargetResource(admissionRequest, operation); var clonedResource = cloner.clone(originalResource); - AdmissionResponse admissionResponse; try { var mutatedResource = mutator.mutate(clonedResource, operation); - admissionResponse = admissionResponseFromMutation(originalResource, mutatedResource); + return admissionResponseFromMutation(originalResource, mutatedResource); } catch (NotAllowedException e) { - admissionResponse = AdmissionUtils.notAllowedExceptionToAdmissionResponse(e); + return notAllowedExceptionToAdmissionResponse(e); } - return admissionResponse; } - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/Mutator.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/Mutator.java index 0fea8ad1..6aa1e103 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/Mutator.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/mutation/Mutator.java @@ -12,5 +12,4 @@ public interface Mutator { T mutate(T resource, Operation operation) throws NotAllowedException; - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/AsyncDefaultAdmissionRequestValidator.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/AsyncDefaultAdmissionRequestValidator.java index a838dc9f..8e10afb9 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/AsyncDefaultAdmissionRequestValidator.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/AsyncDefaultAdmissionRequestValidator.java @@ -7,12 +7,13 @@ import io.fabric8.kubernetes.api.model.KubernetesResource; import io.fabric8.kubernetes.api.model.admission.v1.AdmissionRequest; import io.fabric8.kubernetes.api.model.admission.v1.AdmissionResponse; -import io.javaoperatorsdk.webhook.admission.AdmissionUtils; import io.javaoperatorsdk.webhook.admission.AsyncAdmissionRequestHandler; import io.javaoperatorsdk.webhook.admission.NotAllowedException; import io.javaoperatorsdk.webhook.admission.Operation; +import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.allowedAdmissionResponse; import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.getTargetResource; +import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.notAllowedExceptionToAdmissionResponse; public class AsyncDefaultAdmissionRequestValidator implements AsyncAdmissionRequestHandler { @@ -26,9 +27,8 @@ public AsyncDefaultAdmissionRequestValidator(Validator validator) { @Override @SuppressWarnings("unchecked") public CompletionStage handle(AdmissionRequest admissionRequest) { - Operation operation = Operation.valueOf(admissionRequest.getOperation()); + var operation = Operation.valueOf(admissionRequest.getOperation()); var originalResource = (T) getTargetResource(admissionRequest, operation); - var asyncValidate = CompletableFuture.runAsync(() -> validator.validate(originalResource, operation)); return asyncValidate @@ -36,20 +36,11 @@ public CompletionStage handle(AdmissionRequest admissionReque .exceptionally(e -> { if (e instanceof CompletionException) { if (e.getCause() instanceof NotAllowedException) { - return AdmissionUtils.notAllowedExceptionToAdmissionResponse( - (NotAllowedException) e.getCause()); - } else { - throw new IllegalStateException(e.getCause()); + return notAllowedExceptionToAdmissionResponse((NotAllowedException) e.getCause()); } - } else { - throw new IllegalStateException(e); + throw new IllegalStateException(e.getCause()); } + throw new IllegalStateException(e); }); } - - private AdmissionResponse allowedAdmissionResponse() { - AdmissionResponse admissionResponse = new AdmissionResponse(); - admissionResponse.setAllowed(true); - return admissionResponse; - } } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/DefaultAdmissionRequestValidator.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/DefaultAdmissionRequestValidator.java index 4b810efc..75f43b99 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/DefaultAdmissionRequestValidator.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/DefaultAdmissionRequestValidator.java @@ -4,11 +4,12 @@ import io.fabric8.kubernetes.api.model.admission.v1.AdmissionRequest; import io.fabric8.kubernetes.api.model.admission.v1.AdmissionResponse; import io.javaoperatorsdk.webhook.admission.AdmissionRequestHandler; -import io.javaoperatorsdk.webhook.admission.AdmissionUtils; import io.javaoperatorsdk.webhook.admission.NotAllowedException; import io.javaoperatorsdk.webhook.admission.Operation; +import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.allowedAdmissionResponse; import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.getTargetResource; +import static io.javaoperatorsdk.webhook.admission.AdmissionUtils.notAllowedExceptionToAdmissionResponse; public class DefaultAdmissionRequestValidator implements AdmissionRequestHandler { @@ -20,22 +21,15 @@ public DefaultAdmissionRequestValidator(Validator validator) { } @Override + @SuppressWarnings("unchecked") public AdmissionResponse handle(AdmissionRequest admissionRequest) { - Operation operation = Operation.valueOf(admissionRequest.getOperation()); + var operation = Operation.valueOf(admissionRequest.getOperation()); var originalResource = (T) getTargetResource(admissionRequest, operation); - AdmissionResponse admissionResponse; try { validator.validate(originalResource, operation); - admissionResponse = allowedAdmissionResponse(); + return allowedAdmissionResponse(); } catch (NotAllowedException e) { - admissionResponse = AdmissionUtils.notAllowedExceptionToAdmissionResponse(e); + return notAllowedExceptionToAdmissionResponse(e); } - return admissionResponse; - } - - private AdmissionResponse allowedAdmissionResponse() { - AdmissionResponse admissionResponse = new AdmissionResponse(); - admissionResponse.setAllowed(true); - return admissionResponse; } } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/Validator.java b/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/Validator.java index 9dc8de45..b465bf0f 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/Validator.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/admission/validation/Validator.java @@ -7,5 +7,4 @@ public interface Validator { void validate(T resource, Operation operation) throws NotAllowedException; - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/clone/Cloner.java b/core/src/main/java/io/javaoperatorsdk/webhook/clone/Cloner.java index 1f1a74a7..e0ae9e08 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/clone/Cloner.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/clone/Cloner.java @@ -9,5 +9,4 @@ public interface Cloner { * @return a deep copy of the given object if it isn't {@code null}, {@code null} otherwise */ R clone(R object); - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/clone/ObjectMapperCloner.java b/core/src/main/java/io/javaoperatorsdk/webhook/clone/ObjectMapperCloner.java index 5a993f9f..874f413c 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/clone/ObjectMapperCloner.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/clone/ObjectMapperCloner.java @@ -8,6 +8,7 @@ public class ObjectMapperCloner implements Cloner { private final ObjectMapper objectMapper = new ObjectMapper(); @Override + @SuppressWarnings("unchecked") public T clone(T object) { if (object == null) { return null; diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionController.java b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionController.java index a08ccd42..b3dbedf2 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionController.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionController.java @@ -14,7 +14,10 @@ import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.apiextensions.v1.ConversionReview; -import static io.javaoperatorsdk.webhook.conversion.Commons.*; +import static io.javaoperatorsdk.webhook.conversion.Commons.MAPPER_ALREADY_REGISTERED_FOR_VERSION_MESSAGE; +import static io.javaoperatorsdk.webhook.conversion.Commons.createErrorResponse; +import static io.javaoperatorsdk.webhook.conversion.Commons.createResponse; +import static io.javaoperatorsdk.webhook.conversion.Commons.throwMissingMapperForVersion; public class AsyncConversionController implements AsyncConversionRequestHandler { @@ -24,7 +27,7 @@ public class AsyncConversionController implements AsyncConversionRequestHandler private final Map mappers = new HashMap<>(); public void registerMapper(AsyncMapper mapper) { - String version = mapper.getClass().getDeclaredAnnotation(TargetVersion.class).value(); + var version = mapper.getClass().getDeclaredAnnotation(TargetVersion.class).value(); if (mappers.get(version) != null) { throw new IllegalStateException(MAPPER_ALREADY_REGISTERED_FOR_VERSION_MESSAGE + version); } @@ -44,7 +47,6 @@ public CompletionStage handle(ConversionReview conversionRevie conversionReview.getRequest().getUid(), e); return CompletableFuture.completedStage(createErrorResponse(e, conversionReview)); } - } @SuppressWarnings("unchecked") @@ -55,22 +57,22 @@ private CompletionStage> convertObjects(List obje completableFutures[i] = mapObject(objects.get(i), targetVersion); } return CompletableFuture.allOf(completableFutures).thenApply(r -> { - List result = new ArrayList<>(completableFutures.length); - for (CompletableFuture cf : completableFutures) { + var result = new ArrayList(completableFutures.length); + for (var cf : completableFutures) { result.add(cf.join()); } return result; }); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings("unchecked") private CompletableFuture mapObject(HasMetadata resource, String targetVersion) { - String sourceVersion = Utils.versionOfApiVersion(resource.getApiVersion()); - AsyncMapper sourceToHubMapper = mappers.get(sourceVersion); + var sourceVersion = Utils.versionOfApiVersion(resource.getApiVersion()); + var sourceToHubMapper = mappers.get(sourceVersion); if (sourceToHubMapper == null) { throwMissingMapperForVersion(sourceVersion); } - AsyncMapper hubToTarget = mappers.get(targetVersion); + var hubToTarget = mappers.get(targetVersion); if (hubToTarget == null) { throwMissingMapperForVersion(targetVersion); } @@ -78,5 +80,4 @@ private CompletableFuture mapObject(HasMetadata resource, String ta .thenApply(r -> hubToTarget.fromHub(r).toCompletableFuture().join()) .toCompletableFuture(); } - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionRequestHandler.java b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionRequestHandler.java index fe12e086..13fabf3e 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionRequestHandler.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionRequestHandler.java @@ -7,5 +7,4 @@ public interface AsyncConversionRequestHandler { CompletionStage handle(ConversionReview conversionReview); - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncMapper.java b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncMapper.java index 1ce3539e..873cd0f0 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncMapper.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/AsyncMapper.java @@ -9,5 +9,4 @@ public interface AsyncMapper { CompletionStage toHub(R resource); CompletionStage fromHub(HUB hub); - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Commons.java b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Commons.java index 54909093..29060bfe 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Commons.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Commons.java @@ -17,7 +17,7 @@ public class Commons { public static ConversionReview createResponse(List convertedObjects, ConversionReview conversionReview) { - ConversionReview result = new ConversionReview(); + var result = new ConversionReview(); var response = new ConversionResponse(); response.setResult(new Status()); response.getResult().setStatus("Success"); @@ -30,7 +30,7 @@ public static ConversionReview createResponse(List convertedObjects public static ConversionReview createErrorResponse(Exception e, ConversionReview conversionReview) { - ConversionReview result = new ConversionReview(); + var result = new ConversionReview(); var response = new ConversionResponse(); response.setUid(conversionReview.getRequest().getUid()); response.setResult(new Status()); @@ -44,5 +44,4 @@ public static void throwMissingMapperForVersion(String version) { throw new MissingConversionMapperException( "Missing mapper from version: " + version); } - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/ConversionController.java b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/ConversionController.java index 122a672c..69a52257 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/ConversionController.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/ConversionController.java @@ -11,7 +11,10 @@ import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.apiextensions.v1.ConversionReview; -import static io.javaoperatorsdk.webhook.conversion.Commons.*; +import static io.javaoperatorsdk.webhook.conversion.Commons.MAPPER_ALREADY_REGISTERED_FOR_VERSION_MESSAGE; +import static io.javaoperatorsdk.webhook.conversion.Commons.createErrorResponse; +import static io.javaoperatorsdk.webhook.conversion.Commons.createResponse; +import static io.javaoperatorsdk.webhook.conversion.Commons.throwMissingMapperForVersion; public class ConversionController implements ConversionRequestHandler { @@ -21,7 +24,7 @@ public class ConversionController implements ConversionRequestHandler { private final Map mappers = new HashMap<>(); public void registerMapper(Mapper mapper) { - String version = mapper.getClass().getDeclaredAnnotation(TargetVersion.class).value(); + var version = mapper.getClass().getDeclaredAnnotation(TargetVersion.class).value(); if (mappers.get(version) != null) { throw new IllegalStateException(MAPPER_ALREADY_REGISTERED_FOR_VERSION_MESSAGE + version); } @@ -31,7 +34,7 @@ public void registerMapper(Mapper mapper) { @Override public ConversionReview handle(ConversionReview conversionReview) { try { - List convertedObjects = + var convertedObjects = convertObjects(conversionReview.getRequest().getObjects().stream() .map(HasMetadata.class::cast).collect(Collectors.toList()), Utils.versionOfApiVersion(conversionReview.getRequest().getDesiredAPIVersion())); @@ -42,15 +45,14 @@ public ConversionReview handle(ConversionReview conversionReview) { } } - @SuppressWarnings("unchecked") private List convertObjects(List objects, String targetVersion) { return objects.stream().map(r -> mapObject(r, targetVersion)) .collect(Collectors.toList()); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings("unchecked") private HasMetadata mapObject(HasMetadata resource, String targetVersion) { - String sourceVersion = Utils.versionOfApiVersion(resource.getApiVersion()); + var sourceVersion = Utils.versionOfApiVersion(resource.getApiVersion()); var sourceToHubMapper = mappers.get(sourceVersion); if (sourceToHubMapper == null) { throwMissingMapperForVersion(sourceVersion); @@ -61,5 +63,4 @@ private HasMetadata mapObject(HasMetadata resource, String targetVersion) { } return hubToTarget.fromHub(sourceToHubMapper.toHub(resource)); } - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/ConversionRequestHandler.java b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/ConversionRequestHandler.java index 1e22cdb1..3dda05f4 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/ConversionRequestHandler.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/ConversionRequestHandler.java @@ -5,5 +5,4 @@ public interface ConversionRequestHandler { ConversionReview handle(ConversionReview admissionRequest); - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Mapper.java b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Mapper.java index d88f2696..8e41a945 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Mapper.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Mapper.java @@ -7,5 +7,4 @@ public interface Mapper { HUB toHub(R resource); R fromHub(HUB hub); - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/TargetVersion.java b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/TargetVersion.java index ed77b01c..d6b62ae5 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/TargetVersion.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/TargetVersion.java @@ -17,5 +17,4 @@ * @return version **/ String value(); - } diff --git a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Utils.java b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Utils.java index aeb645c4..7f16d81e 100644 --- a/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Utils.java +++ b/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Utils.java @@ -12,5 +12,4 @@ public static String versionOfApiVersion(String apiVersion) { var lastDelimiter = apiVersion.lastIndexOf("/"); return apiVersion.substring(lastDelimiter + 1); } - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/admission/AdmissionControllerTest.java b/core/src/test/java/io/javaoperatorsdk/webhook/admission/AdmissionControllerTest.java index 1026f3b4..d2dfaae2 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/admission/AdmissionControllerTest.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/admission/AdmissionControllerTest.java @@ -3,8 +3,11 @@ import org.junit.jupiter.api.Test; import io.fabric8.kubernetes.api.model.HasMetadata; +import io.javaoperatorsdk.webhook.admission.mutation.Mutator; +import io.javaoperatorsdk.webhook.admission.validation.Validator; -import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.*; +import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.LABEL_TEST_VALUE; +import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.MISSING_REQUIRED_LABEL; class AdmissionControllerTest { @@ -12,23 +15,62 @@ class AdmissionControllerTest { @Test void validatesResource() { - AdmissionController admissionController = - new AdmissionController<>((resource, operation) -> { - if (resource.getMetadata().getLabels().get(AdmissionTestSupport.LABEL_KEY) == null) { - throw new NotAllowedException(MISSING_REQUIRED_LABEL); - } - }); + var admissionController = new AdmissionController((resource, operation) -> { + }); admissionTestSupport.validatesResource(admissionController::handle); } + @Test + void validatesResource_whenNotAllowedException() { + var admissionController = + new AdmissionController<>((Validator) (resource, operation) -> { + throw new NotAllowedException(MISSING_REQUIRED_LABEL); + }); + admissionTestSupport.notAllowedException(admissionController::handle); + } + + @Test + void validatesResource_whenOtherException() { + var admissionController = + new AdmissionController<>((Validator) (resource, operation) -> { + throw new IllegalArgumentException("Invalid resource"); + }); + + admissionTestSupport.assertThatThrownBy(admissionController::handle) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid resource"); + } + @Test void mutatesResource() { - AdmissionController admissionController = - new AdmissionController<>((resource, operation) -> { + var admissionController = + new AdmissionController((resource, operation) -> { resource.getMetadata().getLabels().putIfAbsent(AdmissionTestSupport.LABEL_KEY, LABEL_TEST_VALUE); return resource; }); admissionTestSupport.mutatesResource(admissionController::handle); } + + @Test + void mutatesResource_whenNotAllowedException() { + var admissionController = + new AdmissionController<>((Mutator) (resource, operation) -> { + throw new NotAllowedException(MISSING_REQUIRED_LABEL); + }); + + admissionTestSupport.notAllowedException(admissionController::handle); + } + + @Test + void mutatesResource_whenOtherException() { + var admissionController = + new AdmissionController<>((Mutator) (resource, operation) -> { + throw new IllegalArgumentException("Invalid resource"); + }); + + admissionTestSupport.assertThatThrownBy(admissionController::handle) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid resource"); + } } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/admission/AdmissionTestSupport.java b/core/src/test/java/io/javaoperatorsdk/webhook/admission/AdmissionTestSupport.java index 31b33e06..07eea799 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/admission/AdmissionTestSupport.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/admission/AdmissionTestSupport.java @@ -1,11 +1,13 @@ package io.javaoperatorsdk.webhook.admission; import java.io.IOException; -import java.io.InputStream; import java.util.Base64; import java.util.UUID; import java.util.function.Function; +import org.assertj.core.api.AbstractThrowableAssert; +import org.assertj.core.api.Assertions; + import io.fabric8.kubernetes.api.model.admission.v1.AdmissionRequest; import io.fabric8.kubernetes.api.model.admission.v1.AdmissionReview; import io.fabric8.kubernetes.api.model.apps.Deployment; @@ -20,14 +22,13 @@ public class AdmissionTestSupport { public static final String LABEL_TEST_VALUE = "mutation-test"; public static AdmissionReview createTestAdmissionReview() { - AdmissionReview admissionReview = new AdmissionReview(); - AdmissionRequest request = new AdmissionRequest(); + var admissionReview = new AdmissionReview(); + var request = new AdmissionRequest(); admissionReview.setRequest(request); request.setOperation(Operation.CREATE.name()); request.setUid(UUID.randomUUID().toString()); - Deployment deployment = null; - try (InputStream is = AdmissionTestSupport.class.getResourceAsStream("deployment.yaml")) { - deployment = Serialization.unmarshal(is, Deployment.class); + try (var is = AdmissionTestSupport.class.getResourceAsStream("deployment.yaml")) { + var deployment = Serialization.unmarshal(is, Deployment.class); request.setObject(deployment); } catch (IOException e) { throw new IllegalStateException(e); @@ -35,31 +36,43 @@ public static AdmissionReview createTestAdmissionReview() { return admissionReview; } - - void validatesResource(Function function) { var inputAdmissionReview = createTestAdmissionReview(); - var admissionReview = function.apply(inputAdmissionReview); assertThat(admissionReview.getResponse().getUid()) .isEqualTo(inputAdmissionReview.getRequest().getUid()); - assertThat(admissionReview.getResponse().getStatus().getCode()).isEqualTo(403); - assertThat(admissionReview.getResponse().getStatus().getMessage()) - .isEqualTo(MISSING_REQUIRED_LABEL); - assertThat(admissionReview.getResponse().getAllowed()).isFalse(); + assertThat(admissionReview.getResponse().getAllowed()).isTrue(); + assertThat(admissionReview.getResponse().getStatus()).isNull(); } void mutatesResource(Function function) { var inputAdmissionReview = createTestAdmissionReview(); - var admissionReview = function.apply(inputAdmissionReview); + assertThat(admissionReview.getResponse().getUid()) + .isEqualTo(inputAdmissionReview.getRequest().getUid()); assertThat(admissionReview.getResponse().getAllowed()).isTrue(); - String patch = new String(Base64.getDecoder().decode(admissionReview.getResponse().getPatch())); - assertThat(patch) - .isEqualTo( - "[{\"op\":\"add\",\"path\":\"/metadata/labels/app.kubernetes.io~1name\",\"value\":\"mutation-test\"}]"); + var patch = new String(Base64.getDecoder().decode(admissionReview.getResponse().getPatch())); + assertThat(patch).isEqualTo( + "[{\"op\":\"add\",\"path\":\"/metadata/labels/app.kubernetes.io~1name\",\"value\":\"mutation-test\"}]"); } + void notAllowedException(Function function) { + var inputAdmissionReview = createTestAdmissionReview(); + var admissionReview = function.apply(inputAdmissionReview); + + assertThat(admissionReview.getResponse().getUid()) + .isEqualTo(inputAdmissionReview.getRequest().getUid()); + assertThat(admissionReview.getResponse().getAllowed()).isFalse(); + assertThat(admissionReview.getResponse().getStatus().getCode()).isEqualTo(403); + assertThat(admissionReview.getResponse().getStatus().getMessage()).isEqualTo( + MISSING_REQUIRED_LABEL); + } + + AbstractThrowableAssert assertThatThrownBy( + Function function) { + var inputAdmissionReview = createTestAdmissionReview(); + return Assertions.assertThatThrownBy(() -> function.apply(inputAdmissionReview)); + } } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionControllerTest.java b/core/src/test/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionControllerTest.java index 646d60d2..c3975912 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionControllerTest.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/admission/AsyncAdmissionControllerTest.java @@ -1,38 +1,75 @@ package io.javaoperatorsdk.webhook.admission; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; +import java.util.concurrent.CompletionException; import org.junit.jupiter.api.Test; import io.fabric8.kubernetes.api.model.HasMetadata; import io.javaoperatorsdk.webhook.admission.mutation.AsyncMutator; +import io.javaoperatorsdk.webhook.admission.mutation.Mutator; +import io.javaoperatorsdk.webhook.admission.validation.Validator; -import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.*; +import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.LABEL_KEY; +import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.LABEL_TEST_VALUE; +import static io.javaoperatorsdk.webhook.admission.AdmissionTestSupport.MISSING_REQUIRED_LABEL; class AsyncAdmissionControllerTest { AdmissionTestSupport admissionTestSupport = new AdmissionTestSupport(); @Test - void validatesResource() throws ExecutionException, InterruptedException { - AsyncAdmissionController admissionController = - new AsyncAdmissionController<>((resource, operation) -> { - if (resource.getMetadata().getLabels().get(LABEL_KEY) == null) { - throw new NotAllowedException(MISSING_REQUIRED_LABEL); - } - }); - + void validatesResource() { + var admissionController = new AsyncAdmissionController((resource, operation) -> { + }); admissionTestSupport .validatesResource(res -> admissionController.handle(res).toCompletableFuture().join()); + } + + @Test + void validatesResource_whenNotAllowedException() { + var admissionController = + new AsyncAdmissionController<>((Validator) (resource, operation) -> { + throw new NotAllowedException(MISSING_REQUIRED_LABEL); + }); + admissionTestSupport + .notAllowedException(res -> admissionController.handle(res).toCompletableFuture().join()); + } + + @Test + void validatesResource_whenOtherException() { + var admissionController = + new AsyncAdmissionController<>((Validator) (resource, operation) -> { + throw new IllegalArgumentException("Invalid resource"); + }); + admissionTestSupport.assertThatThrownBy( + res -> admissionController.handle(res).toCompletableFuture() + .join()) + .isInstanceOf(CompletionException.class) + .hasCauseInstanceOf(IllegalStateException.class) + .hasRootCauseInstanceOf(IllegalArgumentException.class) + .hasRootCauseMessage("Invalid resource"); } @Test - void mutatesResource() throws ExecutionException, InterruptedException { - AsyncAdmissionController admissionController = + void mutatesResource_withMutator() { + var admissionController = + new AsyncAdmissionController<>((Mutator) (resource, + operation) -> { + resource.getMetadata().getLabels().putIfAbsent(LABEL_KEY, LABEL_TEST_VALUE); + return resource; + }); + + admissionTestSupport + .mutatesResource(res -> admissionController.handle(res).toCompletableFuture().join()); + } + + @Test + void mutatesResource_withAsyncMutator() { + var admissionController = new AsyncAdmissionController<>((AsyncMutator) (resource, operation) -> CompletableFuture.supplyAsync(() -> { resource.getMetadata().getLabels().putIfAbsent(LABEL_KEY, LABEL_TEST_VALUE); @@ -43,4 +80,61 @@ void mutatesResource() throws ExecutionException, InterruptedException { .mutatesResource(res -> admissionController.handle(res).toCompletableFuture().join()); } + @Test + void mutatesResource_withMutator_whenNotAllowedException() { + var admissionController = + new AsyncAdmissionController<>((Mutator) (resource, + operation) -> { + throw new NotAllowedException(MISSING_REQUIRED_LABEL); + }); + + admissionTestSupport.notAllowedException( + res -> admissionController.handle(res).toCompletableFuture().join()); + } + + @Test + void mutatesResource_withAsyncMutator_whenNotAllowedException() { + var admissionController = + new AsyncAdmissionController<>((AsyncMutator) (resource, + operation) -> CompletableFuture.supplyAsync(() -> { + throw new NotAllowedException(MISSING_REQUIRED_LABEL); + })); + + admissionTestSupport.notAllowedException( + res -> admissionController.handle(res).toCompletableFuture().join()); + } + + @Test + void mutatesResource_withMutator_whenOtherException() { + var admissionController = + new AsyncAdmissionController<>((Mutator) (resource, + operation) -> { + throw new IllegalArgumentException("Invalid resource"); + }); + + admissionTestSupport.assertThatThrownBy( + res -> admissionController.handle(res).toCompletableFuture() + .join()) + .isInstanceOf(CompletionException.class) + .hasCauseInstanceOf(IllegalStateException.class) + .hasRootCauseInstanceOf(IllegalArgumentException.class) + .hasRootCauseMessage("Invalid resource"); + } + + @Test + void mutatesResource_withAsyncMutator_whenOtherException() { + var admissionController = + new AsyncAdmissionController<>((AsyncMutator) (resource, + operation) -> CompletableFuture.supplyAsync(() -> { + throw new IllegalArgumentException("Invalid resource"); + })); + + admissionTestSupport.assertThatThrownBy( + res -> admissionController.handle(res).toCompletableFuture() + .join()) + .isInstanceOf(CompletionException.class) + .hasCauseInstanceOf(IllegalStateException.class) + .hasRootCauseInstanceOf(IllegalArgumentException.class) + .hasRootCauseMessage("Invalid resource"); + } } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionControllerTest.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionControllerTest.java index 4365e496..233b880f 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionControllerTest.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/AsyncConversionControllerTest.java @@ -8,7 +8,9 @@ import io.fabric8.kubernetes.api.model.apiextensions.v1.ConversionResponse; import io.fabric8.kubernetes.api.model.apiextensions.v1.ConversionReview; -import io.javaoperatorsdk.webhook.conversion.mapper.*; +import io.javaoperatorsdk.webhook.conversion.mapper.AsyncV1Mapper; +import io.javaoperatorsdk.webhook.conversion.mapper.AsyncV2Mapper; +import io.javaoperatorsdk.webhook.conversion.mapper.AsyncV3Mapper; class AsyncConversionControllerTest { @@ -27,7 +29,6 @@ void handlesSimpleConversion() { conversionTestSupport.handlesSimpleConversion(getConversionReviewConversionResponseFunction()); } - @Test void convertsVariousVersionsInSingleRequest() { conversionTestSupport @@ -49,5 +50,4 @@ private Function getConversionReviewConver } }; } - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/ConversionControllerTest.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/ConversionControllerTest.java index f040218a..af51a311 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/ConversionControllerTest.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/ConversionControllerTest.java @@ -43,5 +43,4 @@ void errorResponseOnMissingMapper() { private Function getConversionReviewConversionResponseFunction() { return request -> controller.handle(request).getResponse(); } - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/ConversionTestSupport.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/ConversionTestSupport.java index c239d4d1..60dc3451 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/ConversionTestSupport.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/ConversionTestSupport.java @@ -1,7 +1,6 @@ package io.javaoperatorsdk.webhook.conversion; import java.util.Arrays; -import java.util.List; import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; @@ -11,7 +10,12 @@ import io.fabric8.kubernetes.api.model.apiextensions.v1.ConversionRequest; import io.fabric8.kubernetes.api.model.apiextensions.v1.ConversionResponse; import io.fabric8.kubernetes.api.model.apiextensions.v1.ConversionReview; -import io.javaoperatorsdk.webhook.conversion.crd.*; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV1; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV1Spec; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV2; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV2Spec; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV3; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV3Spec; import static io.javaoperatorsdk.webhook.conversion.Commons.FAILED_STATUS_MESSAGE; import static org.assertj.core.api.Assertions.assertThat; @@ -31,12 +35,11 @@ public class ConversionTestSupport { void handlesSimpleConversion(Function func) { var request = createRequest(V2, v1resource()); - var response = func.apply(request); assertThat(response.getConvertedObjects()).hasSize(1); assertThat(response.getUid()).isEqualTo(request.getRequest().getUid()); - CustomResourceV2 convertedObject = (CustomResourceV2) response.getConvertedObjects().get(0); + var convertedObject = (CustomResourceV2) response.getConvertedObjects().get(0); assertThat(convertedObject.getMetadata()).isEqualTo(v1resource().getMetadata()); assertThat(convertedObject.getSpec().getAdditionalValue()).isEqualTo(DEFAULT_ADDITIONAL_VALUE); assertThat(convertedObject.getSpec().getValue()).isEqualTo(String.valueOf(VALUE)); @@ -44,11 +47,10 @@ void handlesSimpleConversion(Function func void convertsVariousVersionsInSingleRequest(Function func) { var request = createRequest(V3, v1resource(), v2resource(), v3resource()); - var response = func.apply(request); assertThat(response.getConvertedObjects()).hasSize(3); - List namesInOrder = response.getConvertedObjects().stream() + var namesInOrder = response.getConvertedObjects().stream() .map(HasMetadata.class::cast) .map(r -> r.getMetadata().getName()).collect(Collectors.toList()); assertThat(namesInOrder).containsExactly(V1_NAME, V2_NAME, V3_NAME); @@ -57,7 +59,6 @@ void convertsVariousVersionsInSingleRequest(Function func) { var request = createRequest("v4", v1resource()); - var response = func.apply(request); assertThat(response.getUid()).isEqualTo(request.getRequest().getUid()); @@ -66,8 +67,8 @@ void errorResponseOnMissingMapper(Function } public static ConversionReview createRequest(String targetVersion, HasMetadata... resources) { - ConversionReview review = new ConversionReview(); - ConversionRequest request = new ConversionRequest(); + var review = new ConversionReview(); + var request = new ConversionRequest(); request.setDesiredAPIVersion(API_GROUP + "/" + targetVersion); request.setUid(UUID.randomUUID().toString()); request.setObjects(Arrays.asList(resources)); @@ -76,37 +77,35 @@ public static ConversionReview createRequest(String targetVersion, HasMetadata.. } public static CustomResourceV1 v1resource() { - var r = new CustomResourceV1(); - r.setMetadata(new ObjectMetaBuilder() + var resourceV1 = new CustomResourceV1(); + resourceV1.setMetadata(new ObjectMetaBuilder() .withName(V1_NAME).withNamespace("default") .build()); - r.setSpec(new CustomResourceV1Spec()); - r.getSpec().setValue(VALUE); - return r; + resourceV1.setSpec(new CustomResourceV1Spec()); + resourceV1.getSpec().setValue(VALUE); + return resourceV1; } public static CustomResourceV2 v2resource() { - var r = new CustomResourceV2(); - r.setMetadata(new ObjectMetaBuilder() + var resourceV2 = new CustomResourceV2(); + resourceV2.setMetadata(new ObjectMetaBuilder() .withName(V2_NAME).withNamespace("default") .build()); - r.setSpec(new CustomResourceV2Spec()); - r.getSpec().setValue("2"); - r.getSpec().setAdditionalValue("additionalValueV2"); - return r; + resourceV2.setSpec(new CustomResourceV2Spec()); + resourceV2.getSpec().setValue("2"); + resourceV2.getSpec().setAdditionalValue("additionalValueV2"); + return resourceV2; } public static CustomResourceV3 v3resource() { - var r = new CustomResourceV3(); - r.setMetadata(new ObjectMetaBuilder() + var resourceV3 = new CustomResourceV3(); + resourceV3.setMetadata(new ObjectMetaBuilder() .withName(V3_NAME).withNamespace("default") .build()); - r.setSpec(new CustomResourceV3Spec()); - r.getSpec().setValue("3"); - r.getSpec().setAdditionalValue("additionalValueV3"); - r.getSpec().setThirdValue("thirdValue"); - return r; + resourceV3.setSpec(new CustomResourceV3Spec()); + resourceV3.getSpec().setValue("3"); + resourceV3.getSpec().setAdditionalValue("additionalValueV3"); + resourceV3.getSpec().setThirdValue("thirdValue"); + return resourceV3; } - - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/UtilsTest.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/UtilsTest.java index 7997721f..4b1c5faa 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/UtilsTest.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/UtilsTest.java @@ -3,7 +3,6 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; class UtilsTest { diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1.java index 7c277104..233d24f7 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1.java @@ -15,6 +15,4 @@ public class CustomResourceV1 extends CustomResource implements Namespaced { - - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1Spec.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1Spec.java index 7ffc65d8..72b7cac3 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1Spec.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1Spec.java @@ -12,5 +12,4 @@ public CustomResourceV1Spec setValue(int value) { this.value = value; return this; } - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1Status.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1Status.java index 64237de7..b79c75ae 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1Status.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV1Status.java @@ -12,5 +12,4 @@ public CustomResourceV1Status setValue1(int value1) { this.value1 = value1; return this; } - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV2.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV2.java index 75f25ead..55184845 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV2.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV2.java @@ -15,5 +15,4 @@ public class CustomResourceV2 extends CustomResource implements Namespaced { - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV2Status.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV2Status.java index 72b66388..751642b1 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV2Status.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV2Status.java @@ -12,5 +12,4 @@ public CustomResourceV2Status setValue1(int value1) { this.value1 = value1; return this; } - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV3.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV3.java index 76d7be68..f25b8873 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV3.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV3.java @@ -15,5 +15,4 @@ public class CustomResourceV3 extends CustomResource implements Namespaced { - } diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV3Spec.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV3Spec.java index acbdfda1..8de409f2 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV3Spec.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/crd/CustomResourceV3Spec.java @@ -3,9 +3,7 @@ public class CustomResourceV3Spec { private String value; - private String additionalValue; - private String thirdValue; public String getValue() { diff --git a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/mapper/CustomResourceV1Mapper.java b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/mapper/CustomResourceV1Mapper.java index 16ff7c21..dbe27866 100644 --- a/core/src/test/java/io/javaoperatorsdk/webhook/conversion/mapper/CustomResourceV1Mapper.java +++ b/core/src/test/java/io/javaoperatorsdk/webhook/conversion/mapper/CustomResourceV1Mapper.java @@ -2,7 +2,12 @@ import io.javaoperatorsdk.webhook.conversion.Mapper; import io.javaoperatorsdk.webhook.conversion.TargetVersion; -import io.javaoperatorsdk.webhook.conversion.crd.*; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV1; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV1Spec; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV1Status; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV3; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV3Spec; +import io.javaoperatorsdk.webhook.conversion.crd.CustomResourceV3Status; import static io.javaoperatorsdk.webhook.conversion.ConversionTestSupport.DEFAULT_ADDITIONAL_VALUE; import static io.javaoperatorsdk.webhook.conversion.ConversionTestSupport.DEFAULT_THIRD_VALUE; @@ -12,30 +17,30 @@ public class CustomResourceV1Mapper implements Mapper { diff --git a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/AdmissionControllers.java b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/AdmissionControllers.java index 5f3bfa1b..a9ad92f0 100644 --- a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/AdmissionControllers.java +++ b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/AdmissionControllers.java @@ -1,12 +1,12 @@ package io.javaoperatorsdk.webhook.sample.commons; import java.util.HashMap; -import java.util.concurrent.CompletableFuture; import io.fabric8.kubernetes.api.model.networking.v1.Ingress; import io.javaoperatorsdk.webhook.admission.AdmissionController; import io.javaoperatorsdk.webhook.admission.AsyncAdmissionController; import io.javaoperatorsdk.webhook.admission.NotAllowedException; +import io.javaoperatorsdk.webhook.admission.Operation; import io.javaoperatorsdk.webhook.admission.mutation.AsyncMutator; import io.javaoperatorsdk.webhook.admission.mutation.Mutator; import io.javaoperatorsdk.webhook.admission.validation.Validator; @@ -19,46 +19,22 @@ public class AdmissionControllers { // adds a label to the target resource public static AdmissionController mutatingController() { - return new AdmissionController<>((resource, operation) -> { - if (resource.getMetadata().getLabels() == null) { - resource.getMetadata().setLabels(new HashMap<>()); - } - resource.getMetadata().getLabels().putIfAbsent(MUTATION_TARGET_LABEL, "mutation-test"); - return resource; - }); + return new AdmissionController<>(new IngressMutator()); } // validates if a resource contains the target label public static AdmissionController validatingController() { - return new AdmissionController<>((resource, operation) -> { - if (resource.getMetadata().getLabels() == null - || resource.getMetadata().getLabels().get(VALIDATION_TARGET_LABEL) == null) { - throw new NotAllowedException("Missing label: " + VALIDATION_TARGET_LABEL); - } - }); + return new AdmissionController<>(new IngressValidator()); } public static AsyncAdmissionController asyncMutatingController() { - return new AsyncAdmissionController<>( - (AsyncMutator) (resource, operation) -> CompletableFuture.supplyAsync(() -> { - if (resource.getMetadata().getLabels() == null) { - resource.getMetadata().setLabels(new HashMap<>()); - } - resource.getMetadata().getLabels().putIfAbsent(MUTATION_TARGET_LABEL, "mutation-test"); - return resource; - })); + return new AsyncAdmissionController<>(new IngressMutator()); } public static AsyncAdmissionController asyncValidatingController() { - return new AsyncAdmissionController<>((resource, operation) -> { - if (resource.getMetadata().getLabels() == null - || resource.getMetadata().getLabels().get(VALIDATION_TARGET_LABEL) == null) { - throw new NotAllowedException("Missing label: " + VALIDATION_TARGET_LABEL); - } - }); + return new AsyncAdmissionController<>(new IngressValidator()); } - public static AdmissionController errorMutatingController() { return new AdmissionController<>((Validator) (resource, operation) -> { throw new IllegalStateException(ERROR_MESSAGE); @@ -82,4 +58,25 @@ public static AsyncAdmissionController errorAsyncValidatingController() throw new IllegalStateException(ERROR_MESSAGE); }); } + + private static class IngressMutator implements Mutator { + @Override + public Ingress mutate(Ingress resource, Operation operation) throws NotAllowedException { + if (resource.getMetadata().getLabels() == null) { + resource.getMetadata().setLabels(new HashMap<>()); + } + resource.getMetadata().getLabels().putIfAbsent(MUTATION_TARGET_LABEL, "mutation-test"); + return resource; + } + } + + private static class IngressValidator implements Validator { + @Override + public void validate(Ingress resource, Operation operation) throws NotAllowedException { + if (resource.getMetadata().getLabels() == null + || resource.getMetadata().getLabels().get(VALIDATION_TARGET_LABEL) == null) { + throw new NotAllowedException("Missing label: " + VALIDATION_TARGET_LABEL); + } + } + } } diff --git a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/ConversionControllers.java b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/ConversionControllers.java index 45a71de5..50d9b3d1 100644 --- a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/ConversionControllers.java +++ b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/ConversionControllers.java @@ -19,12 +19,10 @@ public static ConversionController conversionController() { return controller; } - public static AsyncConversionController asyncConversionController() { var controller = new AsyncConversionController(); controller.registerMapper(new AsyncV1Mapper()); controller.registerMapper(new AsyncV2Mapper()); return controller; } - } diff --git a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/customresource/MultiVersionCustomResource.java b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/customresource/MultiVersionCustomResource.java index 28429840..abd1a4f4 100644 --- a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/customresource/MultiVersionCustomResource.java +++ b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/customresource/MultiVersionCustomResource.java @@ -12,5 +12,4 @@ @ShortNames("tcr") public class MultiVersionCustomResource extends CustomResource { - } diff --git a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/customresource/MultiVersionCustomResourceSpecV2.java b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/customresource/MultiVersionCustomResourceSpecV2.java index 0aa7ce10..c1454940 100644 --- a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/customresource/MultiVersionCustomResourceSpecV2.java +++ b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/customresource/MultiVersionCustomResourceSpecV2.java @@ -12,5 +12,4 @@ public MultiVersionCustomResourceSpecV2 setAlteredValue(String alteredValue) { this.alteredValue = alteredValue; return this; } - } diff --git a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/AsyncV1Mapper.java b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/AsyncV1Mapper.java index c90d84a0..68d56d3d 100644 --- a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/AsyncV1Mapper.java +++ b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/AsyncV1Mapper.java @@ -8,10 +8,9 @@ import io.javaoperatorsdk.webhook.sample.commons.customresource.MultiVersionCustomResource; @TargetVersion("v1") -public class AsyncV1Mapper - implements AsyncMapper { +public class AsyncV1Mapper implements AsyncMapper { - private V1Mapper mapper = new V1Mapper(); + private final V1Mapper mapper = new V1Mapper(); @Override public CompletionStage toHub(MultiVersionCustomResource resource) { @@ -19,8 +18,7 @@ public CompletionStage toHub(MultiVersionCustomResource resourc } @Override - public CompletionStage fromHub( - MultiVersionHub hub) { + public CompletionStage fromHub(MultiVersionHub hub) { return CompletableFuture.completedStage(mapper.fromHub(hub)); } } diff --git a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/AsyncV2Mapper.java b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/AsyncV2Mapper.java index f9ae191f..bfe67957 100644 --- a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/AsyncV2Mapper.java +++ b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/AsyncV2Mapper.java @@ -8,10 +8,9 @@ import io.javaoperatorsdk.webhook.sample.commons.customresource.MultiVersionCustomResourceV2; @TargetVersion("v2") -public class AsyncV2Mapper - implements AsyncMapper { +public class AsyncV2Mapper implements AsyncMapper { - private V2Mapper mapper = new V2Mapper(); + private final V2Mapper mapper = new V2Mapper(); @Override public CompletionStage toHub(MultiVersionCustomResourceV2 resource) { diff --git a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/V2Mapper.java b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/V2Mapper.java index e5018e74..595986c0 100644 --- a/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/V2Mapper.java +++ b/samples/commons/src/main/java/io/javaoperatorsdk/webhook/sample/commons/mapper/V2Mapper.java @@ -6,8 +6,7 @@ import io.javaoperatorsdk.webhook.sample.commons.customresource.MultiVersionCustomResourceV2; @TargetVersion("v2") -public class V2Mapper - implements Mapper { +public class V2Mapper implements Mapper { @Override public MultiVersionHub toHub(MultiVersionCustomResourceV2 resource) { @@ -18,8 +17,7 @@ public MultiVersionHub toHub(MultiVersionCustomResourceV2 resource) { } @Override - public MultiVersionCustomResourceV2 fromHub( - MultiVersionHub hub) { + public MultiVersionCustomResourceV2 fromHub(MultiVersionHub hub) { var res = new MultiVersionCustomResourceV2(); res.setMetadata(hub.getMetadata()); res.setSpec(new MultiVersionCustomResourceSpecV2()); diff --git a/samples/commons/src/test/java/io/javaoperatorsdk/webhook/sample/EndToEndTestBase.java b/samples/commons/src/test/java/io/javaoperatorsdk/webhook/sample/AbstractEndToEndTest.java similarity index 88% rename from samples/commons/src/test/java/io/javaoperatorsdk/webhook/sample/EndToEndTestBase.java rename to samples/commons/src/test/java/io/javaoperatorsdk/webhook/sample/AbstractEndToEndTest.java index 52098399..87857677 100644 --- a/samples/commons/src/test/java/io/javaoperatorsdk/webhook/sample/EndToEndTestBase.java +++ b/samples/commons/src/test/java/io/javaoperatorsdk/webhook/sample/AbstractEndToEndTest.java @@ -7,7 +7,6 @@ import org.junit.jupiter.api.Test; import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; -import io.fabric8.kubernetes.api.model.apiextensions.v1.*; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClientBuilder; import io.fabric8.kubernetes.client.KubernetesClientException; @@ -16,13 +15,15 @@ import io.javaoperatorsdk.webhook.sample.commons.customresource.MultiVersionCustomResourceV2; import static io.javaoperatorsdk.webhook.sample.commons.AdmissionControllers.MUTATION_TARGET_LABEL; -import static io.javaoperatorsdk.webhook.sample.commons.Utils.*; import static io.javaoperatorsdk.webhook.sample.commons.Utils.SPIN_UP_GRACE_PERIOD; +import static io.javaoperatorsdk.webhook.sample.commons.Utils.addRequiredLabels; +import static io.javaoperatorsdk.webhook.sample.commons.Utils.testIngress; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import static org.awaitility.Awaitility.await; import static org.junit.jupiter.api.Assertions.assertThrows; -public class EndToEndTestBase { +@SuppressWarnings("deprecation") +public abstract class AbstractEndToEndTest { protected KubernetesClient client = new KubernetesClientBuilder().build(); @@ -57,14 +58,14 @@ void mutationHook() { @Test void conversionHook() { - await().atMost(Duration.ofSeconds(SPIN_UP_GRACE_PERIOD)).untilAsserted(() -> { - avoidRequestTimeout(() -> createV1Resource(TEST_CR_NAME)); - }); + await().atMost(Duration.ofSeconds(SPIN_UP_GRACE_PERIOD)) + .untilAsserted(() -> avoidRequestTimeout(() -> createV1Resource(TEST_CR_NAME))); MultiVersionCustomResourceV2 v2 = client.resources(MultiVersionCustomResourceV2.class).withName(TEST_CR_NAME).get(); assertThat(v2.getSpec().getAlteredValue()).isEqualTo("" + CR_SPEC_VALUE); } + @SuppressWarnings("SameParameterValue") private MultiVersionCustomResource createV1Resource(String name) { var res = new MultiVersionCustomResource(); res.setMetadata(new ObjectMetaBuilder() diff --git a/samples/quarkus/src/main/java/io/javaoperatorsdk/webhook/sample/conversion/ConversionControllerConfig.java b/samples/quarkus/src/main/java/io/javaoperatorsdk/webhook/sample/conversion/ConversionControllerConfig.java index c1f475d5..bc8dce04 100644 --- a/samples/quarkus/src/main/java/io/javaoperatorsdk/webhook/sample/conversion/ConversionControllerConfig.java +++ b/samples/quarkus/src/main/java/io/javaoperatorsdk/webhook/sample/conversion/ConversionControllerConfig.java @@ -17,5 +17,4 @@ public ConversionController conversionController() { public AsyncConversionController asyncConversionController() { return ConversionControllers.asyncConversionController(); } - } diff --git a/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/QuarkusWebhooksE2E.java b/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/QuarkusWebhooksE2E.java index cbe953eb..52c7f441 100644 --- a/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/QuarkusWebhooksE2E.java +++ b/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/QuarkusWebhooksE2E.java @@ -1,26 +1,23 @@ package io.javaoperatorsdk.webhook.sample; import java.io.IOException; -import java.io.InputStream; import java.net.URL; import org.junit.jupiter.api.BeforeAll; -import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClientBuilder; import static io.javaoperatorsdk.webhook.sample.commons.Utils.addConversionHookEndpointToCustomResource; import static io.javaoperatorsdk.webhook.sample.commons.Utils.applyAndWait; -class QuarkusWebhooksE2E extends EndToEndTestBase { +class QuarkusWebhooksE2E extends AbstractEndToEndTest { @BeforeAll static void deployService() throws IOException { - try (KubernetesClient client = new KubernetesClientBuilder().build(); - InputStream certManager = - new URL( - "https://github.com/cert-manager/cert-manager/releases/download/v1.10.1/cert-manager.yaml") - .openStream()) { + try (var client = new KubernetesClientBuilder().build(); + var certManager = new URL( + "https://github.com/cert-manager/cert-manager/releases/download/v1.10.1/cert-manager.yaml") + .openStream()) { applyAndWait(client, certManager); applyAndWait(client, "target/kubernetes/minikube.yml"); applyAndWait(client, "k8s/validating-webhook-configuration.yml"); diff --git a/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/admission/AdmissionEndpointTest.java b/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/admission/AdmissionEndpointTest.java index 2f59cfb3..fc8b2bfc 100644 --- a/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/admission/AdmissionEndpointTest.java +++ b/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/admission/AdmissionEndpointTest.java @@ -1,7 +1,6 @@ package io.javaoperatorsdk.webhook.sample.admission; import java.io.IOException; -import java.io.InputStream; import java.nio.charset.StandardCharsets; import org.apache.http.HttpStatus; @@ -11,6 +10,7 @@ import io.restassured.http.ContentType; import static io.restassured.RestAssured.given; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.is; @QuarkusTest @@ -89,11 +89,11 @@ public void testValidate(String path) { } private String jsonRequest() { - try (InputStream is = this.getClass().getResourceAsStream("/admission-request.json")) { + try (var is = this.getClass().getResourceAsStream("/admission-request.json")) { + assertThat(is).isNotNull(); return new String(is.readAllBytes(), StandardCharsets.UTF_8); } catch (IOException e) { throw new IllegalStateException(e); } } - } diff --git a/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/conversion/ConversionEndpointTest.java b/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/conversion/ConversionEndpointTest.java index 9a69a0fe..eb422c10 100644 --- a/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/conversion/ConversionEndpointTest.java +++ b/samples/quarkus/src/test/java/io/javaoperatorsdk/webhook/sample/conversion/ConversionEndpointTest.java @@ -1,7 +1,6 @@ package io.javaoperatorsdk.webhook.sample.conversion; import java.io.IOException; -import java.io.InputStream; import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.Test; @@ -12,6 +11,7 @@ import static io.javaoperatorsdk.webhook.sample.commons.ConversionControllers.ASYNC_CONVERSION_PATH; import static io.javaoperatorsdk.webhook.sample.commons.ConversionControllers.CONVERSION_PATH; import static io.restassured.RestAssured.given; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.is; @QuarkusTest @@ -66,7 +66,8 @@ private String request() { } private String request(String path) { - try (InputStream is = this.getClass().getResourceAsStream(path)) { + try (var is = this.getClass().getResourceAsStream(path)) { + assertThat(is).isNotNull(); return new String(is.readAllBytes(), StandardCharsets.UTF_8); } catch (IOException e) { throw new IllegalStateException(e); diff --git a/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionEndpoint.java b/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionEndpoint.java index ebdbfef1..fba5e882 100644 --- a/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionEndpoint.java +++ b/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionEndpoint.java @@ -62,5 +62,4 @@ public Mono asyncMutate(@RequestBody AdmissionReview admissionR public Mono asyncValidate(@RequestBody AdmissionReview admissionReview) { return Mono.fromCompletionStage(asyncValidatingController.handle(admissionReview)); } - } diff --git a/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionConfig.java b/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionConfig.java index 932cf717..98154da3 100644 --- a/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionConfig.java +++ b/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionConfig.java @@ -19,5 +19,4 @@ public ConversionController conversionController() { public AsyncConversionController asyncConversionController() { return ConversionControllers.asyncConversionController(); } - } diff --git a/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpoint.java b/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpoint.java index f16395d6..b8e3d577 100644 --- a/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpoint.java +++ b/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpoint.java @@ -37,5 +37,4 @@ public ConversionReview convert(@RequestBody ConversionReview conversionReview) public Mono convertAsync(@RequestBody ConversionReview conversionReview) { return Mono.fromCompletionStage(asyncConversionController.handle(conversionReview)); } - } diff --git a/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/SpringBootWebhooksE2E.java b/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/SpringBootWebhooksE2E.java index cee30a7d..ad944a3d 100644 --- a/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/SpringBootWebhooksE2E.java +++ b/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/SpringBootWebhooksE2E.java @@ -1,27 +1,24 @@ package io.javaoperatorsdk.webhook.sample.springboot; import java.io.IOException; -import java.io.InputStream; import java.net.URL; import org.junit.jupiter.api.BeforeAll; -import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClientBuilder; -import io.javaoperatorsdk.webhook.sample.EndToEndTestBase; +import io.javaoperatorsdk.webhook.sample.AbstractEndToEndTest; import static io.javaoperatorsdk.webhook.sample.commons.Utils.addConversionHookEndpointToCustomResource; import static io.javaoperatorsdk.webhook.sample.commons.Utils.applyAndWait; -class SpringBootWebhooksE2E extends EndToEndTestBase { +class SpringBootWebhooksE2E extends AbstractEndToEndTest { @BeforeAll static void deployService() throws IOException { - try (KubernetesClient client = new KubernetesClientBuilder().build(); - InputStream certManager = - new URL( - "https://github.com/cert-manager/cert-manager/releases/download/v1.10.1/cert-manager.yaml") - .openStream()) { + try (var client = new KubernetesClientBuilder().build(); + var certManager = new URL( + "https://github.com/cert-manager/cert-manager/releases/download/v1.10.1/cert-manager.yaml") + .openStream()) { applyAndWait(client, certManager); applyAndWait(client, "target/classes/META-INF/dekorate/kubernetes.yml"); applyAndWait(client, "k8s/validating-webhook-configuration.yml"); diff --git a/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionAdditionalTestEndpoint.java b/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionAdditionalTestEndpoint.java index 172e7ff8..469b3005 100644 --- a/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionAdditionalTestEndpoint.java +++ b/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionAdditionalTestEndpoint.java @@ -62,5 +62,4 @@ public Mono errorAsyncMutate(@RequestBody AdmissionReview admis public Mono errorAsyncValidate(@RequestBody AdmissionReview admissionReview) { return Mono.fromCompletionStage(errorAsyncValidatingController.handle(admissionReview)); } - } diff --git a/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionEndpointTest.java b/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionEndpointTest.java index 07fcd378..fd87e086 100644 --- a/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionEndpointTest.java +++ b/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/admission/AdmissionEndpointTest.java @@ -14,8 +14,14 @@ import org.springframework.web.reactive.function.BodyInserter; import org.springframework.web.reactive.function.BodyInserters; -import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionAdditionalTestEndpoint.*; -import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionEndpoint.*; +import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionAdditionalTestEndpoint.ERROR_ASYNC_MUTATE_PATH; +import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionAdditionalTestEndpoint.ERROR_ASYNC_VALIDATE_PATH; +import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionAdditionalTestEndpoint.ERROR_MUTATE_PATH; +import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionAdditionalTestEndpoint.ERROR_VALIDATE_PATH; +import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionEndpoint.ASYNC_MUTATE_PATH; +import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionEndpoint.ASYNC_VALIDATE_PATH; +import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionEndpoint.MUTATE_PATH; +import static io.javaoperatorsdk.webhook.sample.springboot.admission.AdmissionEndpoint.VALIDATE_PATH; @Import({AdmissionConfig.class, AdditionalAdmissionConfig.class}) @WebFluxTest({AdmissionEndpoint.class, AdmissionAdditionalTestEndpoint.class}) @@ -71,7 +77,6 @@ public void testInternalServerError(String path) { .expectStatus().is5xxServerError(); } - public void testMutate(String path) { webClient.post().uri("/" + path).contentType(MediaType.APPLICATION_JSON) .body(request()) @@ -89,7 +94,7 @@ public void testValidate(String path) { private BodyInserter request() { try { - ClassPathResource classPathResource = new ClassPathResource("admission-request.json"); + var classPathResource = new ClassPathResource("admission-request.json"); return BodyInserters .fromValue(new String(FileCopyUtils.copyToByteArray(classPathResource.getInputStream()))); } catch (IOException e) { diff --git a/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpointTest.java b/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpointTest.java index b4655917..2370e92a 100644 --- a/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpointTest.java +++ b/samples/spring-boot/src/test/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpointTest.java @@ -61,6 +61,7 @@ public void testConversion(String path) { .exchange() .expectStatus().isOk().expectBody(ConversionReview.class).consumeWith(res -> { var review = res.getResponseBody(); + assertThat(review).isNotNull(); var resource1 = ((MultiVersionCustomResourceV2) review.getResponse().getConvertedObjects().get(0)); assertThat(review.getResponse().getConvertedObjects()).hasSize(2); @@ -78,7 +79,7 @@ private BodyInserter errorRequest() { private BodyInserter requestFromResource(String resource) { try { - ClassPathResource classPathResource = new ClassPathResource(resource); + var classPathResource = new ClassPathResource(resource); return BodyInserters .fromValue(new String(FileCopyUtils.copyToByteArray(classPathResource.getInputStream()))); } catch (IOException e) {