Search in sources :

Example 1 with AdmissionResponseBuilder

use of io.fabric8.kubernetes.api.model.admission.v1.AdmissionResponseBuilder in project xp-operator by enonic.

the class BaseAdmissionApi method handle.

protected AdmissionReview handle(AdmissionReview admissionReview) throws JsonProcessingException {
    String apiName = this.getClass().getSimpleName();
    cfgIfBool("operator.api.debug", () -> {
        try {
            log.info(String.format("%s Request: %s", apiName, mapper.writerWithDefaultPrettyPrinter().writeValueAsString(admissionReview)));
        } catch (JsonProcessingException e) {
        // Ignore
        }
    });
    HasMetadata obj = getObject(admissionReview);
    String type = String.format("%s/%s", obj.getApiVersion(), obj.getKind());
    log.debug(String.format("%s called with type %s", apiName, type));
    Consumer<R> func = functionMap.get(obj.getClass());
    String error = null;
    if (func == null) {
        error = String.format("%s cannot handle resources of type %s", apiName, type);
    }
    R apiObject = null;
    if (error == null && !deleteEvent(admissionReview)) {
        try {
            apiObject = createApiObject(admissionReview);
            Objects.requireNonNull(func).accept(apiObject);
        } catch (Throwable e) {
            error = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName();
        }
    }
    AdmissionResponseBuilder builder = new AdmissionResponseBuilder().withUid(admissionReview.getRequest().getUid()).withAllowed(error == null);
    StatusBuilder statusBuilder = new StatusBuilder();
    if (error != null) {
        statusBuilder.withMessage(error);
        log.warn(String.format("%s failed for %s %s in NS %s: %s", apiName, obj.getKind(), obj.getMetadata().getName(), obj.getMetadata().getNamespace(), error));
    } else if (apiObject != null) {
        postRequestHook(apiObject, builder);
    }
    builder.withStatus(statusBuilder.build());
    admissionReview.setResponse(builder.build());
    cfgIfBool("operator.api.debug", () -> {
        try {
            log.info(String.format("%s Response: %s", apiName, mapper.writerWithDefaultPrettyPrinter().writeValueAsString(admissionReview)));
        } catch (JsonProcessingException e) {
        // Ignore
        }
    });
    return admissionReview;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatusBuilder(io.fabric8.kubernetes.api.model.StatusBuilder) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AdmissionResponseBuilder(io.fabric8.kubernetes.api.model.admission.v1.AdmissionResponseBuilder)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 StatusBuilder (io.fabric8.kubernetes.api.model.StatusBuilder)1 AdmissionResponseBuilder (io.fabric8.kubernetes.api.model.admission.v1.AdmissionResponseBuilder)1