|
| 1 | +/* |
| 2 | + * VM-Operator |
| 3 | + * Copyright (C) 2025 Michael N. Lipp |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as |
| 7 | + * published by the Free Software Foundation, either version 3 of the |
| 8 | + * License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.jdrupes.vmoperator.manager; |
| 20 | + |
| 21 | +import io.kubernetes.client.openapi.ApiException; |
| 22 | +import io.kubernetes.client.openapi.models.V1Pod; |
| 23 | +import io.kubernetes.client.openapi.models.V1PodList; |
| 24 | +import io.kubernetes.client.util.Watch.Response; |
| 25 | +import io.kubernetes.client.util.generic.options.ListOptions; |
| 26 | +import java.io.IOException; |
| 27 | +import java.time.Duration; |
| 28 | +import java.time.Instant; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.concurrent.ConcurrentHashMap; |
| 32 | +import java.util.logging.Level; |
| 33 | +import static org.jdrupes.vmoperator.common.Constants.APP_NAME; |
| 34 | +import static org.jdrupes.vmoperator.common.Constants.VM_OP_NAME; |
| 35 | +import org.jdrupes.vmoperator.common.K8sClient; |
| 36 | +import org.jdrupes.vmoperator.common.K8sObserver.ResponseType; |
| 37 | +import org.jdrupes.vmoperator.common.K8sV1PodStub; |
| 38 | +import org.jdrupes.vmoperator.manager.events.ChannelDictionary; |
| 39 | +import org.jdrupes.vmoperator.manager.events.PodChanged; |
| 40 | +import org.jdrupes.vmoperator.manager.events.VmChannel; |
| 41 | +import org.jdrupes.vmoperator.manager.events.VmDefChanged; |
| 42 | +import org.jgrapes.core.Channel; |
| 43 | +import org.jgrapes.core.annotation.Handler; |
| 44 | + |
| 45 | +/** |
| 46 | + * Watches for changes of pods that run VMs. |
| 47 | + */ |
| 48 | +public class PodMonitor extends AbstractMonitor<V1Pod, V1PodList, VmChannel> { |
| 49 | + |
| 50 | + private final ChannelDictionary<String, VmChannel, ?> channelDictionary; |
| 51 | + |
| 52 | + private final Map<String, PendingChange> pendingChanges |
| 53 | + = new ConcurrentHashMap<>(); |
| 54 | + |
| 55 | + /** |
| 56 | + * Instantiates a new pod monitor. |
| 57 | + * |
| 58 | + * @param componentChannel the component channel |
| 59 | + * @param channelDictionary the channel dictionary |
| 60 | + */ |
| 61 | + public PodMonitor(Channel componentChannel, |
| 62 | + ChannelDictionary<String, VmChannel, ?> channelDictionary) { |
| 63 | + super(componentChannel, V1Pod.class, V1PodList.class); |
| 64 | + this.channelDictionary = channelDictionary; |
| 65 | + context(K8sV1PodStub.CONTEXT); |
| 66 | + ListOptions options = new ListOptions(); |
| 67 | + options.setLabelSelector("app.kubernetes.io/name=" + APP_NAME + "," |
| 68 | + + "app.kubernetes.io/component=" + APP_NAME + "," |
| 69 | + + "app.kubernetes.io/managed-by=" + VM_OP_NAME); |
| 70 | + options(options); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void prepareMonitoring() throws IOException, ApiException { |
| 75 | + client(new K8sClient()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void handleChange(K8sClient client, Response<V1Pod> change) { |
| 80 | + String vmName = change.object.getMetadata().getLabels() |
| 81 | + .get("app.kubernetes.io/instance"); |
| 82 | + if (vmName == null) { |
| 83 | + return; |
| 84 | + } |
| 85 | + var channel = channelDictionary.channel(vmName).orElse(null); |
| 86 | + var responseType = ResponseType.valueOf(change.type); |
| 87 | + if (channel != null && channel.vmDefinition() != null) { |
| 88 | + pendingChanges.remove(vmName); |
| 89 | + channel.fire(new PodChanged(change.object, responseType)); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // VM definition not available yet, may happen during startup |
| 94 | + if (responseType == ResponseType.DELETED) { |
| 95 | + return; |
| 96 | + } |
| 97 | + purgePendingChanges(); |
| 98 | + logger.finer(() -> "Add pending pod change for " + vmName); |
| 99 | + pendingChanges.put(vmName, new PendingChange(Instant.now(), change)); |
| 100 | + } |
| 101 | + |
| 102 | + private void purgePendingChanges() { |
| 103 | + Instant tooOld = Instant.now().minus(Duration.ofMinutes(15)); |
| 104 | + for (var itr = pendingChanges.entrySet().iterator(); itr.hasNext();) { |
| 105 | + var change = itr.next(); |
| 106 | + if (change.getValue().from().isBefore(tooOld)) { |
| 107 | + itr.remove(); |
| 108 | + logger.finer( |
| 109 | + () -> "Cleaned pending pod change for " + change.getKey()); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Check for pending changes. |
| 116 | + * |
| 117 | + * @param event the event |
| 118 | + * @param channel the channel |
| 119 | + */ |
| 120 | + @Handler |
| 121 | + public void onVmDefChanged(VmDefChanged event, VmChannel channel) { |
| 122 | + Optional.ofNullable(pendingChanges.remove(event.vmDefinition().name())) |
| 123 | + .map(PendingChange::change).ifPresent(change -> { |
| 124 | + logger.finer(() -> "Firing pending pod change for " |
| 125 | + + event.vmDefinition().name()); |
| 126 | + channel.fire(new PodChanged(change.object, |
| 127 | + ResponseType.valueOf(change.type))); |
| 128 | + if (logger.isLoggable(Level.FINER) |
| 129 | + && pendingChanges.isEmpty()) { |
| 130 | + logger.finer("No pending pod changes left."); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private record PendingChange(Instant from, Response<V1Pod> change) { |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments