b5e7892eb989a03ee2f3595540dcaeacbdebe150
[policy/clamp.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
21 package org.onap.policy.clamp.acm.participant.kserve.k8s;
22
23 import com.fasterxml.jackson.databind.JsonNode;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import io.kubernetes.client.openapi.ApiClient;
26 import io.kubernetes.client.openapi.ApiException;
27 import io.kubernetes.client.openapi.apis.CustomObjectsApi;
28 import java.io.IOException;
29 import java.lang.invoke.MethodHandles;
30 import javax.annotation.PostConstruct;
31 import lombok.RequiredArgsConstructor;
32 import okhttp3.Response;
33 import org.onap.policy.clamp.acm.participant.kserve.parameters.CustomResourceDefinitionParameters;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 @RequiredArgsConstructor
40 public class KserveClient {
41
42     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
43     private final ApiClient apiClient;
44
45     private final CustomResourceDefinitionParameters crdParams;
46
47     CustomObjectsApi customObjectsApi;
48
49     @PostConstruct
50     void initialize() {
51         apiClient.setDebugging(logger.isDebugEnabled());
52         customObjectsApi = new CustomObjectsApi(apiClient);
53     }
54
55     /**
56      * Deploy the inference service.
57      *
58      * @param namespace   k8s namespace
59      * @param jsonContent k8s payload
60      * @throws ApiException exception
61      */
62     public boolean deployInferenceService(String namespace, String jsonContent) throws ApiException, IOException {
63         var httpCall = customObjectsApi.createNamespacedCustomObjectCall(crdParams.getGroup(), crdParams.getVersion(),
64                 namespace, crdParams.getPlural(), jsonContent.getBytes(), null, null, null, null);
65         try (Response httpResponse = httpCall.execute()) {
66             logger.debug("Response of inference service deploy in namespace {} is {}", namespace, httpResponse);
67             return httpResponse.isSuccessful();
68         }
69     }
70
71     /**
72      * Undeploy inference service.
73      *
74      * @param namespace            k8s namespace
75      * @param inferenceServiceName name of the inference service
76      * @throws ApiException exception
77      */
78     public boolean undeployInferenceService(String namespace, String inferenceServiceName)
79             throws ApiException, IOException {
80         var httpCall = customObjectsApi.deleteNamespacedCustomObjectCall(crdParams.getGroup(), crdParams.getVersion(),
81                 namespace, crdParams.getPlural(), inferenceServiceName, crdParams.getGracePeriod(), false, null, null,
82                 null, null);
83         try (Response httpResponse = httpCall.execute()) {
84             logger.debug("Response of inference service undeploy in namespace {} is {}", namespace, httpResponse);
85             return httpResponse.isSuccessful();
86         }
87     }
88
89     /**
90      * Get the status of Inference service.
91      *
92      * @param namespace            k8s namespace
93      * @param inferenceServiceName name of the inference service
94      * @return State of the inference service
95      * @throws ApiException exception on k8s client
96      * @throws IOException  exception
97      */
98     public String getInferenceServiceStatus(String namespace, String inferenceServiceName)
99             throws ApiException, IOException {
100         var httpCall =
101                 customObjectsApi.getNamespacedCustomObjectCall(crdParams.getGroup(), crdParams.getVersion(), namespace,
102                         crdParams.getPlural(), inferenceServiceName, null);
103         try (Response httpResponse = httpCall.execute()) {
104             logger.debug("Response of getting inference service in {} is {}", namespace, httpResponse);
105             if (httpResponse.isSuccessful() && httpResponse.body() != null) {
106                 JsonNode jsonNode = new ObjectMapper().readTree(httpResponse.body().string());
107                 return jsonNode.at("/status/conditions/2/status").asText();
108             }
109         }
110         return Boolean.FALSE.toString();
111     }
112 }