Search in sources :

Example 1 with JsonPatch

use of io.fabric8.zjsonpatch.JsonPatch in project stackgres by ongres.

the class MutationResource method mutate.

default AdmissionReviewResponse mutate(T admissionReview, JsonPatchMutationPipeline<T> pipeline) {
    AdmissionRequest<?> request = admissionReview.getRequest();
    UUID requestUid = request.getUid();
    logger.info("Mutating admission review " + requestUid.toString() + " of kind " + request.getKind().toString());
    AdmissionResponse response = new AdmissionResponse();
    response.setUid(requestUid);
    AdmissionReviewResponse reviewResponse = new AdmissionReviewResponse();
    reviewResponse.setResponse(response);
    reviewResponse.setGroup(admissionReview.getGroup());
    reviewResponse.setKind(admissionReview.getKind());
    reviewResponse.setVersion(admissionReview.getVersion());
    try {
        pipeline.mutate(admissionReview).ifPresent(path -> {
            response.setPatchType("JSONPatch");
            String base64Path = Base64.getEncoder().encodeToString(path.getBytes(StandardCharsets.UTF_8));
            response.setPatch(base64Path);
        });
        response.setAllowed(true);
    } catch (Exception ex) {
        Status status = new StatusBuilder().withMessage(Optional.ofNullable(ex.getMessage()).orElse("null")).withCode(500).build();
        logger.error("cannot proceed with request " + requestUid.toString() + " cause: " + status.getMessage(), ex);
        response.setAllowed(false);
        response.setStatus(status);
    }
    return reviewResponse;
}
Also used : AdmissionReviewResponse(io.stackgres.operatorframework.admissionwebhook.AdmissionReviewResponse) Status(io.fabric8.kubernetes.api.model.Status) StatusBuilder(io.fabric8.kubernetes.api.model.StatusBuilder) UUID(java.util.UUID) AdmissionResponse(io.stackgres.operatorframework.admissionwebhook.AdmissionResponse)

Aggregations

Status (io.fabric8.kubernetes.api.model.Status)1 StatusBuilder (io.fabric8.kubernetes.api.model.StatusBuilder)1 AdmissionResponse (io.stackgres.operatorframework.admissionwebhook.AdmissionResponse)1 AdmissionReviewResponse (io.stackgres.operatorframework.admissionwebhook.AdmissionReviewResponse)1 UUID (java.util.UUID)1