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
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=========================================================
21 package org.onap.policy.clamp.acm.participant.kserve.k8s;
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 jakarta.annotation.PostConstruct;
29 import java.io.IOException;
30 import java.lang.invoke.MethodHandles;
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;
39 @RequiredArgsConstructor
40 public class KserveClient {
42 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
43 private final ApiClient apiClient;
45 private final CustomResourceDefinitionParameters crdParams;
47 CustomObjectsApi customObjectsApi;
51 apiClient.setDebugging(logger.isDebugEnabled());
52 customObjectsApi = new CustomObjectsApi(apiClient);
56 * Deploy the inference service.
58 * @param namespace k8s namespace
59 * @param jsonContent k8s payload
60 * @throws ApiException exception
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();
72 * Undeploy inference service.
74 * @param namespace k8s namespace
75 * @param inferenceServiceName name of the inference service
76 * @throws ApiException exception
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,
83 try (Response httpResponse = httpCall.execute()) {
84 logger.debug("Response of inference service undeploy in namespace {} is {}", namespace, httpResponse);
85 return httpResponse.isSuccessful();
90 * Get the status of Inference service.
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
98 public String getInferenceServiceStatus(String namespace, String inferenceServiceName)
99 throws ApiException, IOException {
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();
110 return Boolean.FALSE.toString();