52 lines
1.7 KiB
Java
52 lines
1.7 KiB
Java
package tk.antoine.roux.infrastructure.out;
|
|
|
|
import io.kubernetes.client.openapi.ApiClient;
|
|
import io.kubernetes.client.openapi.apis.CoreV1Api;
|
|
import io.kubernetes.client.util.ClientBuilder;
|
|
import io.kubernetes.client.util.KubeConfig;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Profile;
|
|
import tk.antoine.roux.infrastructure.BackofficeProperties;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.time.Duration;
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
public class KubernetesClientConfiguration {
|
|
|
|
@Bean
|
|
@Profile("!dev")
|
|
public ApiClient prodClient() throws IOException {
|
|
return ClientBuilder.cluster().build();
|
|
}
|
|
|
|
@Bean
|
|
@Profile("dev")
|
|
public ApiClient devClient(BackofficeProperties backofficeProperties) throws IOException {
|
|
String kubeConfigPath = backofficeProperties.kubernetes().api().kubeconfig();
|
|
File kubeConfigFile = new File(kubeConfigPath);
|
|
try (BufferedReader kubeConfigReader =
|
|
new BufferedReader(
|
|
new InputStreamReader(
|
|
new FileInputStream(kubeConfigFile), StandardCharsets.UTF_8))) {
|
|
|
|
KubeConfig kubeConfig = KubeConfig.loadKubeConfig(kubeConfigReader);
|
|
kubeConfig.setFile(kubeConfigFile);
|
|
|
|
return ClientBuilder.kubeconfig(kubeConfig).setPingInterval(Duration.ofSeconds(2)).build();
|
|
}
|
|
}
|
|
|
|
@Bean
|
|
public CoreV1Api getCoreApi(ApiClient apiClient) {
|
|
return new CoreV1Api(apiClient);
|
|
}
|
|
|
|
}
|