f7cd5d0e60074999a3c79820630eb40b044e663e
[smo/teiv.git] /
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20 package org.oran.smo.teiv.adapters.focom_to_teiv_adapter;
21
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;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
33
34 @Slf4j
35 @Configuration
36 public class KubernetesConfig {
37
38     @Value("${focom.kubeconfig:}")
39     private String focomKubeconfigPath;
40
41     @Value("${o2ims.kubeconfig:}")
42     private String o2imsKubeconfigPath;
43
44     @Bean(name = "focomKubernetesClient")
45     public KubernetesClient focomKubernetesClient() throws IOException {
46         log.info("Creating Focom Kubernetes client");
47         return buildClient(focomKubeconfigPath);
48     }
49
50     @Bean(name = "o2imsKubernetesClient")
51     public KubernetesClient o2imsKubernetesClient() throws IOException {
52         log.info("Creating o2ims Kubernetes client");
53         return buildClient(o2imsKubeconfigPath);
54     }
55
56     private KubernetesClient buildClient(String path) throws IOException {
57         Config config;
58
59         if (path != null && !path.isBlank()) {
60             log.debug("Using explicit kubeconfig from path: {}", path);
61             InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);
62
63             if (inputStream == null) {
64                 throw new IOException("Kubeconfig file not found in classpath: " + path);
65             }
66             String kubeconfig = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
67             config = Config.fromKubeconfig(kubeconfig);
68         } else {
69             log.info("No kubeconfig path provided. Using default auto-configured Kubernetes client");
70             config = Config.autoConfigure(null);
71         }
72         return new KubernetesClientBuilder().withConfig(config).build();
73     }
74 }