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