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);
}
}
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;
});
}
}
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()));
}
}
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());
}
Aggregations