use of com.enonic.kubernetes.client.v1.xp7app.Xp7App in project xp-operator by enonic.
the class MutationApi method xp7app.
private void xp7app(MutationRequest mt) {
// Collect old and new object
Xp7App oldR = (Xp7App) mt.getAdmissionReview().getRequest().getOldObject();
Xp7App newR = (Xp7App) mt.getAdmissionReview().getRequest().getObject();
// Create default status
Xp7AppStatus defStatus = new Xp7AppStatus().withMessage("Created").withState(Xp7AppStatus.State.PENDING).withXp7AppStatusFields(new Xp7AppStatusFields());
// Get OP
AdmissionOperation op = getOperation(mt.getAdmissionReview());
// Ensure status
switch(op) {
case // Always set the default status on new objects
CREATE:
patch(mt, true, "/status", newR.getStatus(), defStatus);
break;
case UPDATE:
if (newR.getSpec() != null && !newR.getSpec().getUrl().equals(oldR.getSpec().getUrl())) {
// On url change, set default status
patch(mt, true, "/status", newR.getStatus(), defStatus);
} else {
// Else make sure the old status is not removed
patch(mt, false, "/status", newR.getStatus(), oldR.getStatus());
}
break;
case DELETE:
// Set pending deletion status
oldR.getStatus().setState(Xp7AppStatus.State.PENDING);
oldR.getStatus().setMessage("Pending deletion");
patch(mt, true, "/status", newR.getStatus(), oldR.getStatus());
break;
}
// Ensure enabled
patch(mt, false, "/spec/enabled", newR.getSpec().getEnabled(), true);
if (op == AdmissionOperation.CREATE) {
// Ensure finalizers
List<String> oldFinalizers = ((HasMetadata) mt.getAdmissionReview().getRequest().getObject()).getMetadata().getFinalizers();
Set<String> newFinalizers = oldFinalizers != null ? new HashSet<>(oldFinalizers) : new HashSet<>();
String uninstallFinalizer = cfgStr("operator.charts.values.finalizers.app.uninstall");
if (!newFinalizers.contains(uninstallFinalizer)) {
newFinalizers.add(uninstallFinalizer);
patch(mt, true, "/metadata/finalizers", null, newFinalizers);
}
// Ensure owner reference
ensureOwnerReference(mt);
}
}
use of com.enonic.kubernetes.client.v1.xp7app.Xp7App in project xp-operator by enonic.
the class HandlerStatus method updateStatus.
private boolean updateStatus(final Optional<Xp7App> optionalApp, final Optional<Xp7Deployment> optionalDeployment, final Optional<AppInfo> optionalAppInfo, final Optional<String> error) {
if (optionalApp.isEmpty()) {
// This can happen if apps are installed in XP but not with the operator
return false;
}
Xp7App app = optionalApp.get();
if (error.isPresent()) {
return setStatus(app, Xp7AppStatus.State.ERROR, error.get(), optionalAppInfo);
}
if (optionalDeployment.isEmpty()) {
return setStatus(app, Xp7AppStatus.State.ERROR, "XP deployment not found", optionalAppInfo);
}
Xp7Deployment deployment = optionalDeployment.get();
// Deployment is stopped
if (!deployment.getSpec().getEnabled()) {
return setStatus(app, Xp7AppStatus.State.STOPPED, "XP deployment stopped", optionalAppInfo);
}
// lets not change that until we have some app info
if (notSuccessfullyInstalled().test(app) && optionalAppInfo.isEmpty() && app.getStatus().getState() == Xp7AppStatus.State.ERROR) {
return false;
}
// No app info
if (optionalAppInfo.isEmpty()) {
return setStatus(app, Xp7AppStatus.State.PENDING, "Waiting for XP to start", optionalAppInfo);
}
AppInfo appInfo = optionalAppInfo.get();
switch(appInfo.state()) {
case "started":
if (app.getSpec().getEnabled()) {
return setStatus(app, Xp7AppStatus.State.RUNNING, "OK", optionalAppInfo);
} else {
return setStatus(app, Xp7AppStatus.State.PENDING, "Has not been stopped", optionalAppInfo);
}
case "stopped":
if (app.getSpec().getEnabled()) {
return setStatus(app, Xp7AppStatus.State.PENDING, "Has not been started", optionalAppInfo);
} else {
return setStatus(app, Xp7AppStatus.State.STOPPED, "OK", optionalAppInfo);
}
default:
return setStatus(app, Xp7AppStatus.State.ERROR, String.format("Invalid app state '%s'", appInfo.state()), optionalAppInfo);
}
}
use of com.enonic.kubernetes.client.v1.xp7app.Xp7App in project xp-operator by enonic.
the class OperatorXp7AppStatus method accept.
@Override
public void accept(final AppEvent appEvent) {
// Match with k8s resource
Optional<Xp7App> optionalXp7App = searchers.xp7App().find(inNamespace(appEvent.namespace()), matchesKey(appEvent.key()));
// If no match is found, exit
if (optionalXp7App.isEmpty()) {
return;
}
Xp7App app = optionalXp7App.get();
// If app has been uninstalled but not deleted in K8s
if (appEvent.type() == AppEventType.UNINSTALLED) {
if (isNotDeleted().test(app)) {
triggerAppReinstall(clients, app);
}
return;
}
// If app version in XP does not match K8s
if (!appEvent.info().version().equals(app.getStatus().getXp7AppStatusFields().getXp7AppStatusFieldsAppInfo().getVersion())) {
triggerAppReinstall(clients, app);
return;
}
// Else update status
handlerStatus.updateStatus(app, appEvent);
}
use of com.enonic.kubernetes.client.v1.xp7app.Xp7App in project xp-operator by enonic.
the class AdmissionApi method xp7app.
private void xp7app(AdmissionReview admissionReview) {
AdmissionOperation op = getOperation(admissionReview);
if (op == AdmissionOperation.DELETE) {
return;
}
Xp7App newApp = (Xp7App) admissionReview.getRequest().getObject();
// Check spec
Preconditions.checkState(newApp.getSpec() != null, "'spec' cannot be null");
Preconditions.checkState(newApp.getSpec().getUrl() != null, "'spec.url' cannot be null");
Preconditions.checkState(newApp.getSpec().getEnabled() != null, "'spec.enabled' cannot be null");
// Check status
Preconditions.checkState(newApp.getStatus() != null, "'status' cannot be null");
Preconditions.checkState(newApp.getStatus().getMessage() != null, "'status.message' cannot be null");
Preconditions.checkState(newApp.getStatus().getState() != null, "'status.state' cannot be null");
Preconditions.checkState(newApp.getStatus().getXp7AppStatusFields() != null, "'status.fields' cannot be null");
if (isBeingBackupRestored().negate().and(h -> op == AdmissionOperation.CREATE).test(newApp)) {
assertXp7Deployment(admissionReview, null);
}
}
use of com.enonic.kubernetes.client.v1.xp7app.Xp7App in project xp-operator by enonic.
the class CrudTest method v1Xp7App.
@Test
void v1Xp7App() throws IOException, URISyntaxException {
EnonicKubernetesClient client = new DefaultEnonicKubernetesClient(server.getClient());
CustomResourceDefinition crd = client.k8s().apiextensions().v1().customResourceDefinitions().load(getClass().getResourceAsStream("/crds/apps.yaml")).get();
client.k8s().apiextensions().v1().customResourceDefinitions().create(crd);
// Create crd client
MixedOperation<Xp7App, Xp7App.Xp7AppList, Resource<Xp7App>> crdClient = client.enonic().v1().crds().xp7apps();
// Create resource
ObjectMetaBuilder metadataBuilder = new ObjectMetaBuilder().withNamespace("test").withName("test-name");
Xp7App resource = new Xp7App().withSpec(new Xp7AppSpec().withUrl("test"));
resource.setMetadata(metadataBuilder.build());
assertCrd(resource, "/crud-app.json");
// Send to server
crdClient.create(resource);
// List
Xp7App.Xp7AppList list = crdClient.list();
assertNotNull(list);
assertEquals(1, list.getItems().size());
assertEqualsCrd(resource, list.getItems().get(0));
// Fetch from server
Xp7App get = crdClient.withName("test-name").get();
assertNotNull(get);
assertEqualsCrd(resource, get);
// Test put
resource.setMetadata(metadataBuilder.withLabels(Map.of("test2", "test2")).build());
resource.setSpec(new Xp7AppSpec().withUrl("test"));
crdClient.withName("test-name").replace(resource);
// Delete from server
assertTrue(crdClient.withName("test-name").delete());
}
Aggregations