|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Red Hat, IBM Corporation and others. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | + |
| 17 | +package com.autotune.analyzer.kruizeLayer.utils; |
| 18 | + |
| 19 | +import com.autotune.analyzer.kruizeLayer.KruizeLayer; |
| 20 | +import com.autotune.analyzer.kruizeLayer.presence.QueryBasedPresence; |
| 21 | +import com.autotune.database.service.ExperimentDBService; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * Utility class for layer detection operations |
| 31 | + */ |
| 32 | +public class LayerUtils { |
| 33 | + private static final Logger LOGGER = LoggerFactory.getLogger(LayerUtils.class); |
| 34 | + |
| 35 | + /** |
| 36 | + * Detects which layers are present for a given container in a namespace |
| 37 | + * |
| 38 | + * @param containerName The container name |
| 39 | + * @param workloadName The workload name |
| 40 | + * @param namespace The Kubernetes namespace |
| 41 | + * @return Map of detected layers (layer name -> KruizeLayer), or null if inputs are invalid |
| 42 | + */ |
| 43 | + public static Map<String, KruizeLayer> detectLayers(String containerName, |
| 44 | + String workloadName, |
| 45 | + String namespace) { |
| 46 | + // Validate inputs |
| 47 | + if (containerName == null || containerName.isBlank()) { |
| 48 | + LOGGER.warn("Container name is null or empty, cannot detect layers"); |
| 49 | + return null; |
| 50 | + } |
| 51 | + if (namespace == null || namespace.isBlank()) { |
| 52 | + LOGGER.warn("Namespace is null or empty, cannot detect layers"); |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + LOGGER.info("Detecting layers for container '{}' in namespace '{}'", containerName, namespace); |
| 57 | + |
| 58 | + // Load all available layers from database |
| 59 | + ExperimentDBService experimentDBService = new ExperimentDBService(); |
| 60 | + Map<String, KruizeLayer> allLayersMap = new HashMap<>(); |
| 61 | + |
| 62 | + try { |
| 63 | + experimentDBService.loadAllLayers(allLayersMap); |
| 64 | + } catch (Exception e) { |
| 65 | + LOGGER.error("Failed to load layers from database: {}", e.getMessage()); |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + if (allLayersMap.isEmpty()) { |
| 70 | + LOGGER.warn("No layers found in database"); |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + LOGGER.debug("Loaded {} layers from database", allLayersMap.size()); |
| 75 | + |
| 76 | + // Detect which layers are present |
| 77 | + Map<String, KruizeLayer> detectedLayers = new HashMap<>(); |
| 78 | + |
| 79 | + for (KruizeLayer layer : allLayersMap.values()) { |
| 80 | + try { |
| 81 | + boolean isDetected = false; |
| 82 | + |
| 83 | + // Use the layer's presence detector |
| 84 | + if (layer.getLayerPresence() != null && layer.getLayerPresence().getDetector() != null) { |
| 85 | + // For query-based detection, pass container name as well |
| 86 | + if (layer.getLayerPresence().getDetector() instanceof QueryBasedPresence) { |
| 87 | + QueryBasedPresence queryDetector = (QueryBasedPresence) layer.getLayerPresence().getDetector(); |
| 88 | + isDetected = queryDetector.detectPresence(namespace, workloadName, containerName); |
| 89 | + } else { |
| 90 | + // For other detector types (Always, Label) |
| 91 | + isDetected = layer.getLayerPresence().getDetector() |
| 92 | + .detectPresence(namespace, workloadName); |
| 93 | + } |
| 94 | + |
| 95 | + if (isDetected) { |
| 96 | + detectedLayers.put(layer.getLayerName(), layer); |
| 97 | + LOGGER.info("Detected layer: '{}' for container '{}'", |
| 98 | + layer.getLayerName(), containerName); |
| 99 | + } else { |
| 100 | + LOGGER.debug("Layer '{}' not detected for container '{}'", |
| 101 | + layer.getLayerName(), containerName); |
| 102 | + } |
| 103 | + } else { |
| 104 | + LOGGER.warn("Layer '{}' has no presence detector configured, skipping", |
| 105 | + layer.getLayerName()); |
| 106 | + } |
| 107 | + } catch (Exception e) { |
| 108 | + LOGGER.error("Error detecting layer '{}': {}", |
| 109 | + layer.getLayerName(), e.getMessage(), e); |
| 110 | + // Continue to next layer instead of failing completely |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (detectedLayers.isEmpty()) { |
| 115 | + LOGGER.info("No layers detected for container '{}' in namespace '{}'", |
| 116 | + containerName, namespace); |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + LOGGER.info("Detected {} layer(s) for container '{}': {}", |
| 121 | + detectedLayers.size(), containerName, detectedLayers.keySet()); |
| 122 | + |
| 123 | + return detectedLayers; |
| 124 | + } |
| 125 | +} |
0 commit comments