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.PatchOptions;
026import java.io.IOException;
027import java.io.StringWriter;
028import java.util.Map;
029import java.util.logging.Logger;
030import org.jdrupes.vmoperator.common.K8sV1PodStub;
031import org.jdrupes.vmoperator.common.VmDefinition.RequestedVmState;
032import org.jdrupes.vmoperator.manager.events.VmChannel;
033import org.jdrupes.vmoperator.manager.events.VmDefChanged;
034import org.yaml.snakeyaml.LoaderOptions;
035import org.yaml.snakeyaml.Yaml;
036import org.yaml.snakeyaml.constructor.SafeConstructor;
037
038/**
039 * Delegee for reconciling the pod.
040 */
041@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
042/* default */ class PodReconciler {
043
044    protected final Logger logger = Logger.getLogger(getClass().getName());
045    private final Configuration fmConfig;
046
047    /**
048     * Instantiates a new pod reconciler.
049     *
050     * @param fmConfig the fm config
051     */
052    public PodReconciler(Configuration fmConfig) {
053        this.fmConfig = fmConfig;
054    }
055
056    /**
057     * Reconcile the pod.
058     *
059     * @param event the event
060     * @param model the model
061     * @param channel the channel
062     * @throws IOException Signals that an I/O exception has occurred.
063     * @throws TemplateException the template exception
064     * @throws ApiException the api exception
065     */
066    public void reconcile(VmDefChanged event, Map<String, Object> model,
067            VmChannel channel)
068            throws IOException, TemplateException, ApiException {
069        // Don't do anything if stateful set is still in use (pre v3.4)
070        if ((Boolean) model.get("usingSts")) {
071            return;
072        }
073
074        // Get pod stub.
075        var vmDef = event.vmDefinition();
076        var podStub = K8sV1PodStub.get(channel.client(), vmDef.namespace(),
077            vmDef.name());
078
079        // Nothing to do if exists and should be running
080        if (vmDef.vmState() == RequestedVmState.RUNNING
081            && podStub.model().isPresent()) {
082            return;
083        }
084
085        // Delete if running but should be stopped
086        if (vmDef.vmState() == RequestedVmState.STOPPED) {
087            if (podStub.model().isPresent()) {
088                podStub.delete();
089            }
090            return;
091        }
092
093        // Create pod. First combine template and data and parse result
094        var fmTemplate = fmConfig.getTemplate("runnerPod.ftl.yaml");
095        StringWriter out = new StringWriter();
096        fmTemplate.process(model, out);
097        // Avoid Yaml.load due to
098        // https://github.com/kubernetes-client/java/issues/2741
099        var podDef = Dynamics.newFromYaml(
100            new Yaml(new SafeConstructor(new LoaderOptions())), out.toString());
101
102        // Do apply changes
103        PatchOptions opts = new PatchOptions();
104        opts.setForce(true);
105        opts.setFieldManager("kubernetes-java-kubectl-apply");
106        if (podStub.apply(podDef).isEmpty()) {
107            logger.warning(
108                () -> "Could not patch pod for " + podStub.name());
109        }
110    }
111
112}