Search in sources :

Example 1 with CSRNotApprovedException

use of io.kubernetes.client.util.exception.CSRNotApprovedException in project java by kubernetes-client.

the class CSRUtils method waitUntilCertificateSigned.

/**
 * Wait until the CertificateSigningRequest is approved within a timeout of 30 minutes.
 *
 * @param apiClient the api client
 * @param csrObjectName the csr object name
 * @param retryInterval the retry interval
 * @param timeout the timeout
 * @return the byte [ ]
 * @throws CSRNotApprovedException the csr not approved exception
 */
public static byte[] waitUntilCertificateSigned(ApiClient apiClient, String csrObjectName, Duration retryInterval, Duration timeout) throws CSRNotApprovedException {
    CertificatesV1Api api = new CertificatesV1Api(apiClient);
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    try {
        AtomicReference<byte[]> certRef = new AtomicReference<>();
        boolean certificateSigned = Wait.poll(retryInterval, timeout, () -> {
            try {
                V1CertificateSigningRequest current = api.readCertificateSigningRequest(csrObjectName, null);
                CSRUtils.getCertificate(current).ifPresent(cert -> certRef.set(cert));
                return true;
            } catch (ApiException e) {
                LOG.info("Failed acquiring latest state of CertificateSigningRequest resource {} from the cluster", csrObjectName);
                return false;
            }
        });
        if (!certificateSigned) {
            LOG.error("Timeout exceed but the CertificateSigningRequest {} is not approved", csrObjectName);
            throw new CSRNotApprovedException("Timeout - CertificateSigningRequest not approved: " + csrObjectName);
        }
        LOG.info("Successfully acquired certificate from CertificateSigningRequest {}", csrObjectName);
        return certRef.get();
    } finally {
        service.shutdown();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CSRNotApprovedException(io.kubernetes.client.util.exception.CSRNotApprovedException) AtomicReference(java.util.concurrent.atomic.AtomicReference) V1CertificateSigningRequest(io.kubernetes.client.openapi.models.V1CertificateSigningRequest) CertificatesV1Api(io.kubernetes.client.openapi.apis.CertificatesV1Api) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

ApiException (io.kubernetes.client.openapi.ApiException)1 CertificatesV1Api (io.kubernetes.client.openapi.apis.CertificatesV1Api)1 V1CertificateSigningRequest (io.kubernetes.client.openapi.models.V1CertificateSigningRequest)1 CSRNotApprovedException (io.kubernetes.client.util.exception.CSRNotApprovedException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1