2 * ========================LICENSE_START=================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
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.
16 * ========================LICENSE_END===================================
19 package org.onap.policy.clamp.acm.participant.kserve.k8s;
21 import io.kubernetes.client.openapi.ApiException;
22 import java.io.IOException;
23 import java.lang.invoke.MethodHandles;
24 import lombok.SneakyThrows;
25 import org.onap.policy.clamp.acm.participant.kserve.exception.KserveException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
30 public class InferenceServiceValidator implements Runnable {
32 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
34 private final KserveClient kserveClient;
36 private final int statusCheckInterval;
38 //Timeout for the thread to exit.
39 private final int timeout;
41 private final String inferenceServiceName;
43 private final String namespace;
46 * Constructor for PodStatusValidator.
48 * @param inferenceServiceName name of the inference service
49 * @param namespace kubernetes namespace
50 * @param timeout timeout for the thread to exit
51 * @param statusCheckInterval Interval to check pod status
53 public InferenceServiceValidator(String inferenceServiceName, String namespace, int timeout,
54 int statusCheckInterval, KserveClient kserveClient) {
55 this.inferenceServiceName = inferenceServiceName;
56 this.namespace = namespace;
57 this.timeout = timeout;
58 this.statusCheckInterval = statusCheckInterval;
59 this.kserveClient = kserveClient;
66 logger.info("Polling the status of deployed Inference Service {} in namespace {}", inferenceServiceName,
69 verifyInferenceServiceStatus();
70 } catch (KserveException | IOException e) {
71 throw new KserveException("Error verifying the status of the inference service. Exiting", e);
76 * Verify inference service status.
77 * @throws KserveException exception
78 * @throws IOException exception
79 * @throws InterruptedException exception
80 * @throws ApiException exception
82 private void verifyInferenceServiceStatus()
83 throws KserveException, IOException, InterruptedException, ApiException {
84 var isVerified = false;
85 long endTime = System.currentTimeMillis() + (timeout * 1000L);
87 while (!isVerified && System.currentTimeMillis() < endTime) {
88 var output = kserveClient.getInferenceServiceStatus(namespace, inferenceServiceName);
89 isVerified = output.equalsIgnoreCase(Boolean.TRUE.toString());
91 logger.info("Waiting for the inference service {} to be active ", inferenceServiceName);
92 // Recheck status of pods in specific intervals.
93 Thread.sleep(statusCheckInterval * 1000L);
95 logger.info("Inference Service {} is Ready to use ", inferenceServiceName);
99 throw new KserveException("Time out Exception verifying the status of the inference service");