Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance : mutualize kubernetes client #537

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading