bbf00b4cd48ba59b4032c1f27a4b5386e640827b
[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         String isvcStatus = null;
85         long endTime = System.currentTimeMillis() + (timeout * 1000L);
86
87         while (!isVerified && System.currentTimeMillis() < endTime) {
88             isvcStatus = kserveClient.getInferenceServiceStatus(namespace, inferenceServiceName);
89             isVerified = isvcStatus.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             if (isvcStatus != null && isvcStatus.isEmpty()) {
100                 throw new KserveException("Kserve setup is unavailable for inference service to be deployed");
101             } else {
102                 throw new KserveException("Time out Exception verifying the status of the inference service");
103             }
104         }
105     }
106 }