Skip to content

Commit

Permalink
fix: Fix the auth error returned by controller due to token expiration (
Browse files Browse the repository at this point in the history
  • Loading branch information
CH3CHO authored Mar 6, 2024
1 parent f339f27 commit 74b6289
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.alibaba.higress.sdk.constant.KubernetesConstants;
import com.alibaba.higress.sdk.constant.KubernetesConstants.Label;
import com.alibaba.higress.sdk.constant.Separators;
import com.alibaba.higress.sdk.exception.BusinessException;
import com.alibaba.higress.sdk.http.HttpStatus;
import com.alibaba.higress.sdk.service.kubernetes.crd.mcp.V1McpBridge;
import com.alibaba.higress.sdk.service.kubernetes.crd.mcp.V1McpBridgeList;
Expand Down Expand Up @@ -102,7 +103,7 @@ public class KubernetesClientService {

private final String controllerJwtPolicy;

private String controllerAccessToken;
private final String controllerAccessToken;

private boolean ingressV1Supported;

Expand All @@ -121,9 +122,6 @@ public KubernetesClientService(HigressServiceConfig config) throws IOException {

if (inCluster) {
client = ClientBuilder.cluster().build();
if (StringUtils.isEmpty(controllerAccessToken)) {
controllerAccessToken = readTokenFromFile();
}
log.info("init KubernetesClientService InCluster");
} else {
String kubeConfigPath = !Strings.isNullOrEmpty(kubeConfig) ? kubeConfig : KUBE_CONFIG_DEFAULT_PATH;
Expand Down Expand Up @@ -160,11 +158,16 @@ public List<RegistryzService> gatewayServiceList() throws IOException {
Request request = buildControllerRequest("/debug/registryz");
log.info("gatewayServiceList url {}", request.url());
try (Response response = okHttpClient.newCall(request).execute()) {
if (response.body() != null) {
String responseString = new String(response.body().bytes());
if (StringUtils.isNotEmpty(responseString)) {
return JSON.parseArray(responseString, RegistryzService.class);
}
if (!response.isSuccessful()) {
throw new BusinessException(
"Failed to get gateway service list from controller. Code=" + response.code());
}
if (response.body() == null) {
throw new BusinessException("Empty response got from controller when loading gateway service list.");
}
String responseString = new String(response.body().bytes());
if (StringUtils.isNotEmpty(responseString)) {
return JSON.parseArray(responseString, RegistryzService.class);
}
}
return null;
Expand All @@ -174,11 +177,15 @@ public Map<String, Map<String, IstioEndpointShard>> gatewayServiceEndpoint() thr
Request request = buildControllerRequest("/debug/endpointShardz");
log.info("gatewayServiceEndpoint url {}", request.url());
try (Response response = okHttpClient.newCall(request).execute()) {
if (response.body() != null) {
String responseString = new String(response.body().bytes());
if (StringUtils.isNotEmpty(responseString)) {
return JSON.parseObject(responseString, new TypeReference<>() {});
}
if (!response.isSuccessful()) {
throw new BusinessException("Failed to get service endpoints from controller. Code=" + response.code());
}
if (response.body() == null) {
throw new BusinessException("Empty response got from controller when loading service endpoints.");
}
String responseString = new String(response.body().bytes());
if (StringUtils.isNotEmpty(responseString)) {
return JSON.parseObject(responseString, new TypeReference<>() {});
}
}
return null;
Expand Down Expand Up @@ -507,11 +514,14 @@ private void checkResponseStatus(V1Status status) {
// TODO: Throw exception accordingly.
}

private Request buildControllerRequest(String path) {
private Request buildControllerRequest(String path) throws IOException {
String serviceHost = inCluster ? controllerServiceName + "." + controllerNamespace : controllerServiceHost;
String url = "http://" + serviceHost + ":" + controllerServicePort + path;
Request.Builder builder = new Request.Builder().url(url);
String token = controllerAccessToken;
if (Strings.isNullOrEmpty(token) && inCluster) {
token = readTokenFromFile();
}
if (!Strings.isNullOrEmpty(token)) {
builder.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
}
Expand Down

0 comments on commit 74b6289

Please sign in to comment.