f8837a916578b10060c4a77600877de1c6f29740
[so/adapters/so-cnf-adapter.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
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.onap.so.cnfm.lcm.bpmn.flows.extclients.kubernetes;
21
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.Reader;
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27 import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.KubeConfigFileProcessingException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
31 import org.springframework.context.annotation.Scope;
32 import org.springframework.stereotype.Component;
33 import io.kubernetes.client.openapi.ApiClient;
34 import io.kubernetes.client.util.ClientBuilder;
35 import io.kubernetes.client.util.KubeConfig;
36
37 /**
38  *
39  * @author Waqas Ikram (waqas.ikram@est.tech)
40  *
41  */
42 @Component
43 @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
44 public class KubernetesClientProviderImpl implements KubernetesClientProvider {
45     private static final Logger logger = LoggerFactory.getLogger(KubernetesClientProviderImpl.class);
46     private static final Map<String, ApiClient> INSTANCES = new ConcurrentHashMap<>();
47
48     @Override
49     public ApiClient getApiClient(final String kubeConfigPath) {
50
51         ApiClient client = INSTANCES.get(kubeConfigPath.toString());
52         if (client == null) {
53             synchronized (this) {
54                 try (final Reader input = new FileReader(kubeConfigPath);) {
55                     logger.debug("{} Loading kube-config file", kubeConfigPath);
56                     final KubeConfig kubeConfig = KubeConfig.loadKubeConfig(input);
57                     logger.debug("{} kube-config loaded successfully", kubeConfigPath);
58                     client = ClientBuilder.kubeconfig(kubeConfig).build();
59                     logger.debug("ApiClient created successfully");
60                     INSTANCES.put(kubeConfigPath, client);
61                 } catch (final FileNotFoundException fileNotFoundException) {
62                     logger.error("{} KubeConfig not found", kubeConfigPath, fileNotFoundException);
63                     throw new KubeConfigFileProcessingException(kubeConfigPath + " kube-config file not found",
64                             fileNotFoundException);
65                 } catch (final Exception exception) {
66                     final String message = "Unexpected exception while processing kube-config file";
67                     logger.error(message, exception);
68                     throw new KubeConfigFileProcessingException(message, exception);
69                 }
70             }
71         }
72         logger.debug("Found ApiClient for {}", kubeConfigPath);
73         return client;
74     }
75
76     @Override
77     public void closeApiClient(final String kubeConfigFile) {
78         final ApiClient client = INSTANCES.get(kubeConfigFile);
79         if (client != null) {
80             logger.debug("Closing ApiClient and removing it from local cache for {}", kubeConfigFile);
81             INSTANCES.remove(kubeConfigFile);
82         }
83     }
84
85 }