Search in sources :

Example 1 with Xp7App

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);
    }
}
Also used : AdmissionOperation(com.enonic.kubernetes.operator.api.AdmissionOperation) Xp7AppStatus(com.enonic.kubernetes.client.v1.xp7app.Xp7AppStatus) Xp7AppStatusFields(com.enonic.kubernetes.client.v1.xp7app.Xp7AppStatusFields) Xp7App(com.enonic.kubernetes.client.v1.xp7app.Xp7App)

Example 2 with Xp7App

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);
    }
}
Also used : Xp7Deployment(com.enonic.kubernetes.client.v1.xp7deployment.Xp7Deployment) Xp7App(com.enonic.kubernetes.client.v1.xp7app.Xp7App) AppInfo(com.enonic.kubernetes.apis.xp.service.AppInfo) Xp7AppStatusFieldsAppInfo(com.enonic.kubernetes.client.v1.xp7app.Xp7AppStatusFieldsAppInfo)

Example 3 with Xp7App

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);
}
Also used : Xp7App(com.enonic.kubernetes.client.v1.xp7app.Xp7App)

Example 4 with Xp7App

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);
    }
}
Also used : AdmissionOperation(com.enonic.kubernetes.operator.api.AdmissionOperation) Mapping(com.enonic.kubernetes.operator.ingress.Mapping) Xp7App(com.enonic.kubernetes.client.v1.xp7app.Xp7App) Produces(javax.ws.rs.Produces) Predicates.inNodeGroupAllOr(com.enonic.kubernetes.kubernetes.Predicates.inNodeGroupAllOr) HTTPIngressPath(io.fabric8.kubernetes.api.model.networking.v1.HTTPIngressPath) Xp7Config(com.enonic.kubernetes.client.v1.xp7config.Xp7Config) Path(javax.ws.rs.Path) Xp7DeploymentSpecNodeGroup(com.enonic.kubernetes.client.v1.xp7deployment.Xp7DeploymentSpecNodeGroup) BaseAdmissionApi(com.enonic.kubernetes.operator.api.BaseAdmissionApi) HashSet(java.util.HashSet) Matcher(java.util.regex.Matcher) Xp7Deployment(com.enonic.kubernetes.client.v1.xp7deployment.Xp7Deployment) Consumes(javax.ws.rs.Consumes) Configuration.cfgStr(com.enonic.kubernetes.common.Configuration.cfgStr) ComparableVersion(org.apache.maven.artifact.versioning.ComparableVersion) Configuration.cfgIfBool(com.enonic.kubernetes.common.Configuration.cfgIfBool) Domain(com.enonic.kubernetes.client.v1.domain.Domain) Predicates.matchAnnotationPrefix(com.enonic.kubernetes.kubernetes.Predicates.matchAnnotationPrefix) POST(javax.ws.rs.POST) Predicates.inSameNamespaceAs(com.enonic.kubernetes.kubernetes.Predicates.inSameNamespaceAs) Predicates.fieldEquals(com.enonic.kubernetes.kubernetes.Predicates.fieldEquals) OperatorXp7ConfigSync.getAnnotationMappings(com.enonic.kubernetes.operator.ingress.OperatorXp7ConfigSync.getAnnotationMappings) Collection(java.util.Collection) Predicates.withName(com.enonic.kubernetes.kubernetes.Predicates.withName) Set(java.util.Set) Validator.dns1123(com.enonic.kubernetes.common.Validator.dns1123) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Predicates.isBeingBackupRestored(com.enonic.kubernetes.kubernetes.Predicates.isBeingBackupRestored) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) Validator(com.enonic.kubernetes.common.Validator) List(java.util.List) AdmissionReview(io.fabric8.kubernetes.api.model.admission.v1.AdmissionReview) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Pattern(java.util.regex.Pattern) AdmissionOperation(com.enonic.kubernetes.operator.api.AdmissionOperation) Collections(java.util.Collections) Xp7App(com.enonic.kubernetes.client.v1.xp7app.Xp7App)

Example 5 with Xp7App

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());
}
Also used : CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) Resource(io.fabric8.kubernetes.client.dsl.Resource) CustomResource(io.fabric8.kubernetes.client.CustomResource) Xp7AppSpec(com.enonic.kubernetes.client.v1.xp7app.Xp7AppSpec) Xp7App(com.enonic.kubernetes.client.v1.xp7app.Xp7App) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

Xp7App (com.enonic.kubernetes.client.v1.xp7app.Xp7App)5 Xp7Deployment (com.enonic.kubernetes.client.v1.xp7deployment.Xp7Deployment)2 AdmissionOperation (com.enonic.kubernetes.operator.api.AdmissionOperation)2 AppInfo (com.enonic.kubernetes.apis.xp.service.AppInfo)1 Domain (com.enonic.kubernetes.client.v1.domain.Domain)1 Xp7AppSpec (com.enonic.kubernetes.client.v1.xp7app.Xp7AppSpec)1 Xp7AppStatus (com.enonic.kubernetes.client.v1.xp7app.Xp7AppStatus)1 Xp7AppStatusFields (com.enonic.kubernetes.client.v1.xp7app.Xp7AppStatusFields)1 Xp7AppStatusFieldsAppInfo (com.enonic.kubernetes.client.v1.xp7app.Xp7AppStatusFieldsAppInfo)1 Xp7Config (com.enonic.kubernetes.client.v1.xp7config.Xp7Config)1 Xp7DeploymentSpecNodeGroup (com.enonic.kubernetes.client.v1.xp7deployment.Xp7DeploymentSpecNodeGroup)1 Configuration.cfgIfBool (com.enonic.kubernetes.common.Configuration.cfgIfBool)1 Configuration.cfgStr (com.enonic.kubernetes.common.Configuration.cfgStr)1 Validator (com.enonic.kubernetes.common.Validator)1 Validator.dns1123 (com.enonic.kubernetes.common.Validator.dns1123)1 Predicates.fieldEquals (com.enonic.kubernetes.kubernetes.Predicates.fieldEquals)1 Predicates.inNodeGroupAllOr (com.enonic.kubernetes.kubernetes.Predicates.inNodeGroupAllOr)1 Predicates.inSameNamespaceAs (com.enonic.kubernetes.kubernetes.Predicates.inSameNamespaceAs)1 Predicates.isBeingBackupRestored (com.enonic.kubernetes.kubernetes.Predicates.isBeingBackupRestored)1 Predicates.matchAnnotationPrefix (com.enonic.kubernetes.kubernetes.Predicates.matchAnnotationPrefix)1