2 * ============LICENSE_START=======================================================
3 * Modifications Copyright (C) 2025 OpenInfra Foundation Europe
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.oran.smo.teiv.adapters.focom_to_teiv_adapter;
22 import io.fabric8.kubernetes.client.Config;
23 import io.fabric8.kubernetes.client.KubernetesClient;
24 import io.fabric8.kubernetes.client.KubernetesClientBuilder;
25 import lombok.extern.slf4j.Slf4j;
26 import org.springframework.beans.factory.annotation.Value;
27 import org.springframework.context.annotation.Bean;
28 import org.springframework.context.annotation.Configuration;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
36 public class KubernetesConfig {
38 @Value("${focom.kubeconfig:}")
39 private String focomKubeconfigPath;
41 @Value("${o2ims.kubeconfig:}")
42 private String o2imsKubeconfigPath;
44 @Bean(name = "focomKubernetesClient")
45 public KubernetesClient focomKubernetesClient() throws IOException {
46 log.info("Creating Focom Kubernetes client");
47 return buildClient(focomKubeconfigPath);
50 @Bean(name = "o2imsKubernetesClient")
51 public KubernetesClient o2imsKubernetesClient() throws IOException {
52 log.info("Creating o2ims Kubernetes client");
53 return buildClient(o2imsKubeconfigPath);
56 private KubernetesClient buildClient(String path) throws IOException {
59 if (path != null && !path.isBlank()) {
60 log.debug("Using explicit kubeconfig from path: {}", path);
61 InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);
63 if (inputStream == null) {
64 throw new IOException("Kubeconfig file not found in classpath: " + path);
66 String kubeconfig = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
67 config = Config.fromKubeconfig(kubeconfig);
69 log.info("No kubeconfig path provided. Using default auto-configured Kubernetes client");
70 config = Config.autoConfigure(null);
72 return new KubernetesClientBuilder().withConfig(config).build();