001/* 002 * VM-Operator 003 * Copyright (C) 2023 Michael N. Lipp 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <https://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.vmoperator.manager; 020 021import freemarker.template.Configuration; 022import freemarker.template.TemplateException; 023import io.kubernetes.client.openapi.ApiException; 024import io.kubernetes.client.util.generic.dynamic.Dynamics; 025import io.kubernetes.client.util.generic.options.ListOptions; 026import io.kubernetes.client.util.generic.options.PatchOptions; 027import java.io.IOException; 028import java.io.StringWriter; 029import java.util.Map; 030import java.util.logging.Logger; 031import static org.jdrupes.vmoperator.common.Constants.APP_NAME; 032import org.jdrupes.vmoperator.common.Constants.DisplaySecret; 033import org.jdrupes.vmoperator.common.K8sClient; 034import org.jdrupes.vmoperator.common.K8sV1PodStub; 035import org.jdrupes.vmoperator.common.K8sV1SecretStub; 036import org.jdrupes.vmoperator.common.VmDefinition; 037import org.jdrupes.vmoperator.common.VmDefinition.RequestedVmState; 038import org.jdrupes.vmoperator.manager.events.VmChannel; 039import org.yaml.snakeyaml.LoaderOptions; 040import org.yaml.snakeyaml.Yaml; 041import org.yaml.snakeyaml.constructor.SafeConstructor; 042 043/** 044 * Delegee for reconciling the pod. 045 */ 046/* default */ class PodReconciler { 047 048 protected final Logger logger = Logger.getLogger(getClass().getName()); 049 private final Configuration fmConfig; 050 051 /** 052 * Instantiates a new pod reconciler. 053 * 054 * @param fmConfig the fm config 055 */ 056 public PodReconciler(Configuration fmConfig) { 057 this.fmConfig = fmConfig; 058 } 059 060 /** 061 * Reconcile the pod. 062 * 063 * @param vmDef the vm def 064 * @param model the model 065 * @param channel the channel 066 * @param specChanged the spec changed 067 * @throws IOException Signals that an I/O exception has occurred. 068 * @throws TemplateException the template exception 069 * @throws ApiException the api exception 070 */ 071 public void reconcile(VmDefinition vmDef, Map<String, Object> model, 072 VmChannel channel, boolean specChanged) 073 throws IOException, TemplateException, ApiException { 074 // Get pod stub. 075 var podStub = K8sV1PodStub.get(channel.client(), vmDef.namespace(), 076 vmDef.name()); 077 078 // Nothing to do if exists and should be running 079 if (vmDef.vmState() == RequestedVmState.RUNNING 080 && podStub.model().isPresent()) { 081 return; 082 } 083 084 // Delete if running but should be stopped 085 if (vmDef.vmState() == RequestedVmState.STOPPED) { 086 if (podStub.model().isPresent()) { 087 podStub.delete(); 088 } 089 return; 090 } 091 092 // Create pod. First combine template and data and parse result 093 logger.fine(() -> "Create/update pod " + podStub.name()); 094 addDisplaySecret(channel.client(), model, vmDef); 095 var fmTemplate = fmConfig.getTemplate("runnerPod.ftl.yaml"); 096 StringWriter out = new StringWriter(); 097 fmTemplate.process(model, out); 098 // Avoid Yaml.load due to 099 // https://github.com/kubernetes-client/java/issues/2741 100 var podDef = Dynamics.newFromYaml( 101 new Yaml(new SafeConstructor(new LoaderOptions())), out.toString()); 102 103 // Do apply changes 104 PatchOptions opts = new PatchOptions(); 105 opts.setForce(true); 106 opts.setFieldManager("kubernetes-java-kubectl-apply"); 107 if (podStub.apply(podDef).isEmpty()) { 108 logger.warning( 109 () -> "Could not patch pod for " + podStub.name()); 110 } 111 } 112 113 private void addDisplaySecret(K8sClient client, Map<String, Object> model, 114 VmDefinition vmDef) throws ApiException { 115 ListOptions options = new ListOptions(); 116 options.setLabelSelector("app.kubernetes.io/name=" + APP_NAME + "," 117 + "app.kubernetes.io/component=" + DisplaySecret.NAME + "," 118 + "app.kubernetes.io/instance=" + vmDef.name()); 119 var dsStub = K8sV1SecretStub 120 .list(client, vmDef.namespace(), options).stream().findFirst(); 121 if (dsStub.isPresent()) { 122 dsStub.get().model().ifPresent(m -> { 123 model.put("displaySecret", m.getMetadata().getName()); 124 }); 125 } 126 } 127 128}