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.custom.V1Patch;
024import io.kubernetes.client.openapi.ApiException;
025import io.kubernetes.client.util.generic.dynamic.Dynamics;
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 org.jdrupes.vmoperator.common.K8sV1StatefulSetStub;
032import org.jdrupes.vmoperator.manager.events.VmChannel;
033import org.jdrupes.vmoperator.manager.events.VmDefChanged;
034import org.jdrupes.vmoperator.util.GsonPtr;
035import org.yaml.snakeyaml.LoaderOptions;
036import org.yaml.snakeyaml.Yaml;
037import org.yaml.snakeyaml.constructor.SafeConstructor;
038
039/**
040 * Delegee for reconciling the stateful set (effectively the pod).
041 */
042@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
043/* default */ class StatefulSetReconciler {
044
045    protected final Logger logger = Logger.getLogger(getClass().getName());
046    private final Configuration fmConfig;
047
048    /**
049     * Instantiates a new config map reconciler.
050     *
051     * @param fmConfig the fm config
052     */
053    public StatefulSetReconciler(Configuration fmConfig) {
054        this.fmConfig = fmConfig;
055    }
056
057    /**
058     * Reconcile stateful set.
059     *
060     * @param event the event
061     * @param model the model
062     * @param channel the channel
063     * @throws IOException Signals that an I/O exception has occurred.
064     * @throws TemplateException the template exception
065     * @throws ApiException the api exception
066     */
067    public void reconcile(VmDefChanged event, Map<String, Object> model,
068            VmChannel channel)
069            throws IOException, TemplateException, ApiException {
070        var metadata = event.vmDefinition().getMetadata();
071
072        // Combine template and data and parse result
073        var fmTemplate = fmConfig.getTemplate("runnerSts.ftl.yaml");
074        StringWriter out = new StringWriter();
075        fmTemplate.process(model, out);
076        // Avoid Yaml.load due to
077        // https://github.com/kubernetes-client/java/issues/2741
078        var stsDef = Dynamics.newFromYaml(
079            new Yaml(new SafeConstructor(new LoaderOptions())), out.toString());
080
081        // If exists apply changes only when transitioning state
082        // or not running.
083        var stsStub = K8sV1StatefulSetStub.get(channel.client(),
084            metadata.getNamespace(), metadata.getName());
085        var stsModel = stsStub.model().orElse(null);
086        if (stsModel != null) {
087            var current = stsModel.getSpec().getReplicas();
088            var desired = GsonPtr.to(stsDef.getRaw())
089                .to("spec").getAsInt("replicas").orElse(1);
090            if (current == 1 && desired == 1) {
091                return;
092            }
093        }
094
095        // Do apply changes
096        PatchOptions opts = new PatchOptions();
097        opts.setForce(true);
098        opts.setFieldManager("kubernetes-java-kubectl-apply");
099        if (stsStub.patch(V1Patch.PATCH_FORMAT_APPLY_YAML,
100            new V1Patch(channel.client().getJSON().serialize(stsDef)), opts)
101            .isEmpty()) {
102            logger.warning(
103                () -> "Could not patch stateful set for " + stsStub.name());
104        }
105    }
106
107}