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;
29 public class InferenceServiceValidator implements Runnable {
31 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
33 private final KserveClient kserveClient;
35 private final int statusCheckInterval;
37 //Timeout for the thread to exit.
38 private final int timeout;
40 private final String inferenceServiceName;
42 private final String namespace;
45 * Constructor for PodStatusValidator.
47 * @param inferenceServiceName name of the inference service
48 * @param namespace kubernetes namespace
49 * @param timeout timeout for the thread to exit
50 * @param statusCheckInterval Interval to check pod status
52 public InferenceServiceValidator(String inferenceServiceName, String namespace, int timeout,
53 int statusCheckInterval, KserveClient kserveClient) {
54 this.inferenceServiceName = inferenceServiceName;
55 this.namespace = namespace;
56 this.timeout = timeout;
57 this.statusCheckInterval = statusCheckInterval;
58 this.kserveClient = kserveClient;
65 logger.info("Polling the status of deployed Inference Service {} in namespace {}", inferenceServiceName,
68 verifyInferenceServiceStatus();
69 } catch (KserveException | IOException e) {
70 throw new KserveException("Error verifying the status of the inference service. Exiting", e);
75 * Verify inference service status.
76 * @throws KserveException exception
77 * @throws IOException exception
78 * @throws InterruptedException exception
79 * @throws ApiException exception
81 private void verifyInferenceServiceStatus()
82 throws KserveException, IOException, InterruptedException, ApiException {
83 var isVerified = false;
84 long endTime = System.currentTimeMillis() + (timeout * 1000L);
86 while (!isVerified && System.currentTimeMillis() < endTime) {
87 var output = kserveClient.getInferenceServiceStatus(namespace, inferenceServiceName);
88 isVerified = output.equalsIgnoreCase(Boolean.TRUE.toString());
90 logger.info("Waiting for the inference service {} to be active ", inferenceServiceName);
91 // Recheck status of pods in specific intervals.
92 Thread.sleep(statusCheckInterval * 1000L);
94 logger.info("Inference Service {} is Ready to use ", inferenceServiceName);
98 throw new KserveException("Time out Exception verifying the status of the inference service");