Skip to content

Commit

Permalink
Performance : mutualize kubernetes client (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt authored Dec 17, 2024
1 parent 17f946e commit ab4808f
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -16,19 +18,39 @@ public class KubernetesClientProvider {

private static final Logger LOGGER = LoggerFactory.getLogger(KubernetesClientProvider.class);

private static Map<String, KubernetesClient> rootKubernetesClientCache = new HashMap<>();

private static KubernetesClient userKubernetesClientCache = null;

/**
* This returns the root client which has extended permissions. Currently cluster-admin. User
* calls should be done using the userClient which only has user permissions.
*
* @param region
* @return
*/
public KubernetesClient getRootClient(Region region) {
final Config config = getDefaultConfiguration(region).build();
return new KubernetesClientBuilder().withConfig(config).build();
public synchronized KubernetesClient getRootClient(Region region) {
if (!rootKubernetesClientCache.containsKey(region.getId())) {
final Config config = getDefaultConfiguration(region).build();
rootKubernetesClientCache.put(
region.getId(), new KubernetesClientBuilder().withConfig(config).build());
}
return rootKubernetesClientCache.get(region.getId());
}

public KubernetesClient getUserClient(Region region, User user) {
// In case of SERVICEACCOUNT authentication, we can safely mutualize and use a single
// KubernetesClient
if (region.getServices().getAuthenticationMode()
== Region.Services.AuthenticationMode.SERVICEACCOUNT) {
if (userKubernetesClientCache != null) {
return userKubernetesClientCache;
}
Config config = getDefaultConfiguration(region).build();
KubernetesClient client = new KubernetesClientBuilder().withConfig(config).build();
userKubernetesClientCache = client;
return client;
}
final Config config = getDefaultConfiguration(region).build();
String username = user.getIdep();
if (region.getServices().getUsernamePrefix() != null) {
Expand Down

0 comments on commit ab4808f

Please sign in to comment.