Exit with error on connection failure with kubernetes API.

This commit is contained in:
Michael Lipp 2024-01-12 20:34:37 +01:00
parent 5ec67ba61a
commit d85c957b33
4 changed files with 74 additions and 3 deletions

View file

@ -0,0 +1,43 @@
/*
* VM-Operator
* Copyright (C) 2024 Michael N. Lipp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jdrupes.vmoperator.manager.events;
import org.jgrapes.core.events.Stop;
/**
* Like {@link Stop}, but sets an exit status.
*/
@SuppressWarnings("PMD.ShortClassName")
public class Exit extends Stop {
private final int exitStatus;
/**
* Instantiates a new exit.
*
* @param exitStatus the exit status
*/
public Exit(int exitStatus) {
this.exitStatus = exitStatus;
}
public int exitStatus() {
return exitStatus;
}
}

View file

@ -30,6 +30,7 @@ import java.util.logging.Level;
import static org.jdrupes.vmoperator.common.Constants.VM_OP_GROUP;
import static org.jdrupes.vmoperator.common.Constants.VM_OP_KIND_VM;
import org.jdrupes.vmoperator.common.K8s;
import org.jdrupes.vmoperator.manager.events.Exit;
import org.jdrupes.vmoperator.manager.events.ModifyVm;
import org.jdrupes.vmoperator.manager.events.VmDefChanged;
import org.jgrapes.core.Channel;
@ -37,7 +38,6 @@ import org.jgrapes.core.Component;
import org.jgrapes.core.annotation.Handler;
import org.jgrapes.core.events.HandlingError;
import org.jgrapes.core.events.Start;
import org.jgrapes.core.events.Stop;
import org.jgrapes.util.events.ConfigurationUpdate;
/**
@ -146,7 +146,7 @@ public class Controller extends Component {
logger.severe(() -> "Namespace to control not configured and"
+ " no file in kubernetes directory.");
event.cancel(true);
fire(new Stop());
fire(new Exit(2));
return;
}
logger.fine(() -> "Controlling namespace \"" + namespace + "\".");

View file

@ -41,6 +41,7 @@ import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import static org.jdrupes.vmoperator.manager.Constants.VM_OP_NAME;
import org.jdrupes.vmoperator.manager.events.Exit;
import org.jdrupes.vmoperator.util.FsdUtils;
import org.jgrapes.core.Channel;
import org.jgrapes.core.Component;
@ -82,6 +83,7 @@ public class Manager extends Component {
private static Manager app;
private String clusterName;
private String namespace = "unknown";
private static int exitStatus;
/**
* Instantiates a new manager.
@ -224,6 +226,16 @@ public class Manager extends Component {
event.stop();
}
/**
* On exit.
*
* @param event the event
*/
@Handler
public void onExit(Exit event) {
exitStatus = event.exitStatus();
}
/**
* On stop.
*
@ -294,6 +306,10 @@ public class Manager extends Component {
// Start the application
Components.start(app);
// Wait for (regular) termination
Components.awaitExhaustion();
System.exit(exitStatus);
} catch (IOException | InterruptedException
| org.apache.commons.cli.ParseException e) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, e,

View file

@ -52,6 +52,7 @@ import org.jdrupes.vmoperator.common.K8s;
import static org.jdrupes.vmoperator.manager.Constants.APP_NAME;
import static org.jdrupes.vmoperator.manager.Constants.VM_OP_KIND_VM;
import static org.jdrupes.vmoperator.manager.Constants.VM_OP_NAME;
import org.jdrupes.vmoperator.manager.events.Exit;
import org.jdrupes.vmoperator.manager.events.VmChannel;
import org.jdrupes.vmoperator.manager.events.VmDefChanged;
import org.jdrupes.vmoperator.manager.events.VmDefChanged.Type;
@ -105,7 +106,18 @@ public class VmWatcher extends Component {
* @throws ApiException
*/
@Handler(priority = 10)
public void onStart(Start event) throws IOException, ApiException {
public void onStart(Start event) {
try {
startWatching();
} catch (IOException | ApiException e) {
logger.log(Level.SEVERE, e,
() -> "Cannot watch VMs, terminating.");
event.cancel(true);
fire(new Exit(1));
}
}
private void startWatching() throws IOException, ApiException {
// Get namespace
if (namespaceToWatch == null) {
var path = Path