Skip to content

Commit

Permalink
fix fabric8io#4014: consolidating more operation calls
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Dec 21, 2022
1 parent ab0e886 commit 39bef54
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 40 deletions.
Expand Up @@ -729,6 +729,10 @@ protected Status handleDeploymentRollback(DeploymentRollback deploymentRollback)
return operation(Scope.RESOURCE, "POST", deploymentRollback, Status.class, "rollback");
}

protected T handleApproveOrDeny(T csr) {
return operation(Scope.RESOURCE, "PUT", csr, type, "approval");
}

@Override
public <X> X operation(Scope scope, String method, Object payload, Class<X> responseType, String... path) {
String operationName = scope.name();
Expand Down
Expand Up @@ -451,13 +451,6 @@ protected Map<String, String> getParameters() {
return Collections.emptyMap();
}

protected <T extends HasMetadata> T handleApproveOrDeny(T csr, Class<T> type) throws IOException, InterruptedException {
String uri = URLUtils.join(getResourceUrl(null, csr.getMetadata().getName(), false).toString(), "approval");
HttpRequest.Builder requestBuilder = httpClient.newHttpRequestBuilder()
.put(JSON, JSON_MAPPER.writeValueAsString(csr)).uri(uri);
return handleResponse(requestBuilder, type);
}

/**
* Send a raw get - where the type should be one of String, Reader, InputStream
* <br>
Expand Down
Expand Up @@ -22,14 +22,11 @@
import io.fabric8.kubernetes.api.model.certificates.v1.CertificateSigningRequestStatus;
import io.fabric8.kubernetes.api.model.certificates.v1.CertificateSigningRequestStatusBuilder;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.CertificateSigningRequestResource;
import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperation;
import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.OperationContext;

import java.io.IOException;

public class CertificateSigningRequestOperationsImpl extends
HasMetadataOperation<CertificateSigningRequest, CertificateSigningRequestList, CertificateSigningRequestResource<CertificateSigningRequest>>
implements CertificateSigningRequestResource<CertificateSigningRequest> {
Expand Down Expand Up @@ -60,16 +57,9 @@ public CertificateSigningRequest deny(CertificateSigningRequestCondition certifi

private CertificateSigningRequest addStatusToCSRAndSubmit(
CertificateSigningRequestCondition certificateSigningRequestCondition) {
try {
CertificateSigningRequest fromServerCsr = get();
fromServerCsr.setStatus(createCertificateSigningRequestStatus(certificateSigningRequestCondition));
return handleApproveOrDeny(fromServerCsr, CertificateSigningRequest.class);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw KubernetesClientException.launderThrowable(forOperationType("approval " + type), ie);
} catch (IOException e) {
throw KubernetesClientException.launderThrowable(forOperationType("approval " + type), e);
}
CertificateSigningRequest fromServerCsr = get();
fromServerCsr.setStatus(createCertificateSigningRequestStatus(certificateSigningRequestCondition));
return handleApproveOrDeny(fromServerCsr);
}

private CertificateSigningRequestStatus createCertificateSigningRequestStatus(
Expand Down
Expand Up @@ -22,14 +22,11 @@
import io.fabric8.kubernetes.api.model.certificates.v1beta1.CertificateSigningRequestStatus;
import io.fabric8.kubernetes.api.model.certificates.v1beta1.CertificateSigningRequestStatusBuilder;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.V1beta1CertificateSigningRequestResource;
import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperation;
import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.OperationContext;

import java.io.IOException;

public class CertificateSigningRequestOperationsImpl extends
HasMetadataOperation<CertificateSigningRequest, CertificateSigningRequestList, V1beta1CertificateSigningRequestResource<CertificateSigningRequest>>
implements V1beta1CertificateSigningRequestResource<CertificateSigningRequest> {
Expand Down Expand Up @@ -67,15 +64,8 @@ private CertificateSigningRequestStatus createCertificateSigningRequestStatus(

private CertificateSigningRequest addStatusToCSRAndSubmit(
CertificateSigningRequestCondition certificateSigningRequestCondition) {
try {
CertificateSigningRequest fromServerCsr = get();
fromServerCsr.setStatus(createCertificateSigningRequestStatus(certificateSigningRequestCondition));
return handleApproveOrDeny(fromServerCsr, CertificateSigningRequest.class);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw KubernetesClientException.launderThrowable(forOperationType("approval " + type), ie);
} catch (IOException e) {
throw KubernetesClientException.launderThrowable(forOperationType("approval " + type), e);
}
CertificateSigningRequest fromServerCsr = get();
fromServerCsr.setStatus(createCertificateSigningRequestStatus(certificateSigningRequestCondition));
return handleApproveOrDeny(fromServerCsr);
}
}
Expand Up @@ -52,7 +52,6 @@
import io.fabric8.kubernetes.client.dsl.internal.PortForwarderWebsocket;
import io.fabric8.kubernetes.client.dsl.internal.uploadable.PodUpload;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.http.HttpRequest;
import io.fabric8.kubernetes.client.http.WebSocket;
import io.fabric8.kubernetes.client.lib.FilenameUtils;
import io.fabric8.kubernetes.client.utils.URLUtils;
Expand Down Expand Up @@ -251,18 +250,13 @@ private boolean handleEvict(HasMetadata eviction) {
throw new KubernetesClientException("Name not specified, but operation requires it.");
}

URL requestUrl = new URL(URLUtils.join(getResourceUrl().toString(), "eviction"));
HttpRequest.Builder requestBuilder = httpClient.newHttpRequestBuilder()
.post(JSON, JSON_MAPPER.writeValueAsString(eviction)).url(requestUrl);
handleResponse(requestBuilder, null);
operation(Scope.RESOURCE, "POST", eviction, null, "eviction");
return true;
} catch (KubernetesClientException e) {
if (e.getCode() != HTTP_TOO_MANY_REQUESTS) {
throw e;
}
return false;
} catch (IOException exception) {
throw KubernetesClientException.launderThrowable(forOperationType("evict"), exception);
}
}

Expand Down

0 comments on commit 39bef54

Please sign in to comment.