Search in sources :

Example 1 with Xp7Config

use of com.enonic.kubernetes.client.v1.xp7config.Xp7Config in project xp-operator by enonic.

the class MutationApi method xp7config.

private void xp7config(MutationRequest mt) {
    // Collect old and new object
    Xp7Config oldR = (Xp7Config) mt.getAdmissionReview().getRequest().getOldObject();
    Xp7Config newR = (Xp7Config) mt.getAdmissionReview().getRequest().getObject();
    // Create default status
    Xp7ConfigStatus defStatus = new Xp7ConfigStatus().withMessage("Not loaded").withState(Xp7ConfigStatus.State.PENDING);
    // 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().equals(oldR.getSpec())) {
                // On any change 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:
            // Do nothing
            break;
    }
    // Ensure defaults
    if (newR.getSpec() != null) {
        patch(mt, false, "/spec/dataBase64", newR.getSpec().getDataBase64(), false);
        patch(mt, false, "/spec/nodeGroup", newR.getSpec().getNodeGroup(), cfgStr("operator.charts.values.allNodesKey"));
    }
    if (op == AdmissionOperation.CREATE) {
        // Ensure owner reference
        ensureOwnerReference(mt);
    }
}
Also used : AdmissionOperation(com.enonic.kubernetes.operator.api.AdmissionOperation) Xp7Config(com.enonic.kubernetes.client.v1.xp7config.Xp7Config) Xp7ConfigStatus(com.enonic.kubernetes.client.v1.xp7config.Xp7ConfigStatus)

Example 2 with Xp7Config

use of com.enonic.kubernetes.client.v1.xp7config.Xp7Config in project xp-operator by enonic.

the class OperatorConfigMapSync method handle.

private void handle(final ConfigMap configMap) {
    // Namespace is being deleted
    if (searchers.namespace().stream().anyMatch(contains(configMap).and(isDeleted()))) {
        return;
    }
    // Get ConfigMap nodegroup
    String nodeGroup = configMap.getMetadata().getLabels().get(cfgStr("operator.charts.values.labelKeys.nodeGroup"));
    // Collect all data from Xp7Config that has nodeGroup 'all' or '<nodeGroup>'
    List<Xp7Config> configs = searchers.xp7Config().stream().filter(inSameNamespaceAs(configMap)).filter(inNodeGroupAllOr(nodeGroup)).collect(Collectors.toList());
    // Create variables for data
    Map<String, String> data = new HashMap<>();
    Map<String, String> binaryData = new HashMap<>();
    // Populate data
    StringBuilder index = new StringBuilder();
    for (Xp7Config c : configs) {
        if (Objects.equals(c.getSpec().getDataBase64(), true)) {
            binaryData.put(c.getSpec().getFile(), c.getSpec().getData());
        } else {
            data.put(c.getSpec().getFile(), c.getSpec().getData());
        }
        index.append(c.getMetadata().getName()).append("\t").append(c.getSpec().getNodeGroup()).append("\t").append(c.getSpec().getFile()).append("\n");
    }
    // We add this to be sure that events are always sent when small changes are made
    data.put("operator-hash", Hashing.sha256().hashBytes(index.toString().getBytes()).toString());
    // Get old data
    Map<String, String> oldData = configMap.getData() != null ? configMap.getData() : Collections.emptyMap();
    Map<String, String> oldBinaryData = configMap.getBinaryData() != null ? configMap.getBinaryData() : Collections.emptyMap();
    // If data is not the same, do the update
    if (!Objects.equals(oldData, data) || !Objects.equals(oldBinaryData, binaryData)) {
        K8sLogHelper.logEdit(clients.k8s().configMaps().inNamespace(configMap.getMetadata().getNamespace()).withName(configMap.getMetadata().getName()), c -> {
            c.setData(data);
            c.setBinaryData(binaryData);
            return c;
        });
    }
}
Also used : Xp7Config(com.enonic.kubernetes.client.v1.xp7config.Xp7Config) HashMap(java.util.HashMap)

Example 3 with Xp7Config

use of com.enonic.kubernetes.client.v1.xp7config.Xp7Config in project xp-operator by enonic.

the class AdmissionApi method xp7config.

private void xp7config(AdmissionReview admissionReview) {
    AdmissionOperation op = getOperation(admissionReview);
    if (op == AdmissionOperation.DELETE) {
        return;
    }
    Xp7Config newConfig = (Xp7Config) admissionReview.getRequest().getObject();
    // Check spec
    Preconditions.checkState(newConfig.getSpec() != null, "'spec' cannot be null");
    Preconditions.checkState(newConfig.getSpec().getNodeGroup() != null, "'spec.nodeGroup' cannot be null");
    Preconditions.checkState(newConfig.getSpec().getData() != null, "'spec.data' cannot be null");
    Preconditions.checkState(newConfig.getSpec().getFile() != null, "'spec.file' cannot be null");
    Preconditions.checkState(newConfig.getSpec().getDataBase64() != null, "'spec.dataBase64' cannot be null");
    // Check status
    Preconditions.checkState(newConfig.getStatus() != null, "'status' cannot be null");
    Preconditions.checkState(newConfig.getStatus().getMessage() != null, "'status.message' cannot be null");
    Preconditions.checkState(newConfig.getStatus().getState() != null, "'status.state' cannot be null");
    // Check for file clash
    List<Xp7Config> presentConfigs = searchers.xp7Config().stream().filter(inSameNamespaceAs(newConfig)).filter(withName(newConfig.getMetadata().getName()).negate()).filter(fieldEquals(newConfig, c -> c.getSpec().getFile())).filter(inNodeGroupAllOr(newConfig.getSpec().getNodeGroup())).collect(Collectors.toList());
    if (!presentConfigs.isEmpty()) {
        Preconditions.checkState(false, "XpConfig '%s' already defines file '%s'", presentConfigs.get(0).getMetadata().getName(), presentConfigs.get(0).getSpec().getFile());
    }
    // Check for present deployment
    if (isBeingBackupRestored().negate().and(h -> op == AdmissionOperation.CREATE).test(newConfig)) {
        assertXp7Deployment(admissionReview, Collections.singleton(newConfig.getSpec().getNodeGroup()));
    }
}
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) Xp7Config(com.enonic.kubernetes.client.v1.xp7config.Xp7Config)

Example 4 with Xp7Config

use of com.enonic.kubernetes.client.v1.xp7config.Xp7Config in project xp-operator by enonic.

the class CrudTest method v1xp7Config.

@Test
void v1xp7Config() throws IOException, URISyntaxException {
    EnonicKubernetesClient client = new DefaultEnonicKubernetesClient(server.getClient());
    // Create crd
    CustomResourceDefinition crd = client.k8s().apiextensions().v1().customResourceDefinitions().load(getClass().getResourceAsStream("/crds/configs.yaml")).get();
    client.k8s().apiextensions().v1().customResourceDefinitions().create(crd);
    // Create crd client
    MixedOperation<Xp7Config, Xp7Config.Xp7ConfigList, Resource<Xp7Config>> crdClient = client.enonic().v1().crds().xp7configs();
    // Create resource
    ObjectMetaBuilder metadataBuilder = new ObjectMetaBuilder().withNamespace("test").withName("test-name");
    Xp7Config resource = new Xp7Config().withSpec(new Xp7ConfigSpec().withData("test").withFile("test.cfg").withNodeGroup("test"));
    resource.setMetadata(metadataBuilder.build());
    assertCrd(resource, "/crud-config.json");
    // Send to server
    crdClient.create(resource);
    // List
    Xp7Config.Xp7ConfigList list = crdClient.list();
    assertNotNull(list);
    assertEquals(1, list.getItems().size());
    assertEqualsCrd(resource, list.getItems().get(0));
    assertEquals(resource, list.getItems().get(0));
    // Fetch from server
    Xp7Config get = crdClient.withName("test-name").get();
    assertNotNull(get);
    assertEquals(resource, get);
    // Test put
    resource.setMetadata(metadataBuilder.withLabels(Map.of("test2", "test2")).build());
    resource.setSpec(new Xp7ConfigSpec().withData("test2").withFile("test2.cfg").withNodeGroup("test2"));
    crdClient.withName("test-name").replace(resource);
    // Delete from server
    assertTrue(crdClient.withName("test-name").delete());
}
Also used : Xp7Config(com.enonic.kubernetes.client.v1.xp7config.Xp7Config) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) Resource(io.fabric8.kubernetes.client.dsl.Resource) CustomResource(io.fabric8.kubernetes.client.CustomResource) Xp7ConfigSpec(com.enonic.kubernetes.client.v1.xp7config.Xp7ConfigSpec) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

Xp7Config (com.enonic.kubernetes.client.v1.xp7config.Xp7Config)4 AdmissionOperation (com.enonic.kubernetes.operator.api.AdmissionOperation)2 Domain (com.enonic.kubernetes.client.v1.domain.Domain)1 Xp7App (com.enonic.kubernetes.client.v1.xp7app.Xp7App)1 Xp7ConfigSpec (com.enonic.kubernetes.client.v1.xp7config.Xp7ConfigSpec)1 Xp7ConfigStatus (com.enonic.kubernetes.client.v1.xp7config.Xp7ConfigStatus)1 Xp7Deployment (com.enonic.kubernetes.client.v1.xp7deployment.Xp7Deployment)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 Predicates.withName (com.enonic.kubernetes.kubernetes.Predicates.withName)1 BaseAdmissionApi (com.enonic.kubernetes.operator.api.BaseAdmissionApi)1 Mapping (com.enonic.kubernetes.operator.ingress.Mapping)1