65c4c7e27ceda870ce328b2433f40e95721b8eee
[policy/clamp.git] /
1 /*-
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
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  * ========================LICENSE_END===================================
17  */
18
19 package org.onap.policy.clamp.acm.participant.kserve.k8s;
20
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;
29
30 public class InferenceServiceValidator implements Runnable {
31
32     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
33
34     private final KserveClient kserveClient;
35
36     private final int statusCheckInterval;
37
38     //Timeout for the thread to exit.
39     private final int timeout;
40
41     private final String inferenceServiceName;
42
43     private final String namespace;
44
45     /**
46      * Constructor for PodStatusValidator.
47      *
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
52      */
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;
60     }
61
62
63     @SneakyThrows
64     @Override
65     public void run() {
66         logger.info("Polling the status of deployed Inference Service {} in namespace {}", inferenceServiceName,
67                 namespace);
68         try {
69             verifyInferenceServiceStatus();
70         } catch (KserveException | IOException e) {
71             throw new KserveException("Error verifying the status of the inference service. Exiting", e);
72         }
73     }
74
75     /**
76      * Verify inference service status.
77      * @throws KserveException exception
78      * @throws IOException exception
79      * @throws InterruptedException exception
80      * @throws ApiException exception
81      */
82     private void verifyInferenceServiceStatus()
83             throws KserveException, IOException, InterruptedException, ApiException {
84         var isVerified = false;
85         long endTime = System.currentTimeMillis() + (timeout * 1000L);
86
87         while (!isVerified && System.currentTimeMillis() < endTime) {
88             var output = kserveClient.getInferenceServiceStatus(namespace, inferenceServiceName);
89             isVerified = output.equalsIgnoreCase(Boolean.TRUE.toString());
90             if (!isVerified) {
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);
94             } else {
95                 logger.info("Inference Service {} is Ready to use ", inferenceServiceName);
96             }
97         }
98         if (!isVerified) {
99             throw new KserveException("Time out Exception verifying the status of the inference service");
100         }
101     }
102 }