use of com.enonic.kubernetes.client.v1.xp7deployment.Xp7DeploymentStatus in project xp-operator by enonic.
the class MutationApi method xp7deployment.
private void xp7deployment(MutationRequest mt) {
// Collect old and new object
Xp7Deployment oldR = (Xp7Deployment) mt.getAdmissionReview().getRequest().getOldObject();
Xp7Deployment newR = (Xp7Deployment) mt.getAdmissionReview().getRequest().getObject();
// Create default status
Xp7DeploymentStatus defStatus = new Xp7DeploymentStatus().withMessage("Waiting for pods").withState(Xp7DeploymentStatus.State.PENDING).withXp7DeploymentStatusFields(new Xp7DeploymentStatusFields().withXp7DeploymentStatusFieldsPods(new LinkedList<>()));
if (newR.getSpec() != null && newR.getSpec().getEnabled() != null && !newR.getSpec().getEnabled()) {
defStatus.setState(Xp7DeploymentStatus.State.STOPPED);
defStatus.setMessage("XP deployment stopped");
}
// 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;
}
List<Xp7DeploymentSpecNodesPreinstalledApps> preInstall = newR.getSpec().getNodesPreinstalledApps();
if (preInstall == null || preInstall.isEmpty()) {
patch(mt, true, "/spec/nodesPreinstalledApps", newR.getSpec().getNodesPreinstalledApps(), preInstalledApps);
} else {
List<String> names = preInstall.stream().map(Xp7DeploymentSpecNodesPreinstalledApps::getName).collect(Collectors.toList());
List<Xp7DeploymentSpecNodesPreinstalledApps> newList = new LinkedList<>(preInstall);
preInstalledApps.stream().filter(a -> !names.contains(a.getName())).forEach(newList::add);
patch(mt, true, "/spec/nodesPreinstalledApps", newR.getSpec().getNodesPreinstalledApps(), newList);
}
}
use of com.enonic.kubernetes.client.v1.xp7deployment.Xp7DeploymentStatus in project xp-operator by enonic.
the class OperatorXp7DeploymentStatus method handle.
private void handle(final Pod pod) {
Optional<Xp7Deployment> xp7Deployment = searchers.xp7Deployment().find(inSameNamespaceAs(pod));
if (xp7Deployment.isEmpty()) {
return;
}
// Get current status
Xp7DeploymentStatus currentStatus = xp7Deployment.get().getStatus();
int oldStatusHash = currentStatus.hashCode();
// Get all pods in deployment
List<Pod> pods = searchers.pod().stream().filter(isEnonicManaged()).filter(isPartOfDeployment(xp7Deployment.get())).collect(Collectors.toList());
// Set pod fields
currentStatus.setXp7DeploymentStatusFields(buildFields(pods));
// Get expected number of pods
int expectedNumberOfPods = expectedNumberOfPods(xp7Deployment.get());
// If pod count does not match
if (pods.size() != expectedNumberOfPods) {
updateOnChange(xp7Deployment.get(), oldStatusHash, currentStatus.withState(Xp7DeploymentStatus.State.PENDING).withMessage("Pod count mismatch"));
return;
}
// If deployment is disabled
if (!xp7Deployment.get().getSpec().getEnabled()) {
updateOnChange(xp7Deployment.get(), oldStatusHash, currentStatus.withState(Xp7DeploymentStatus.State.STOPPED).withMessage("OK"));
return;
}
// Iterate over pods and check status
List<String> waitingForPods = new LinkedList<>();
for (Xp7DeploymentStatusFieldsPod p : currentStatus.getXp7DeploymentStatusFields().getXp7DeploymentStatusFieldsPods()) {
if (!p.getPhase().equals("Running") || !p.getReady()) {
waitingForPods.add(p.getName());
}
}
// If we are still waiting
if (!waitingForPods.isEmpty()) {
waitingForPods.sort(String::compareTo);
updateOnChange(xp7Deployment.get(), oldStatusHash, currentStatus.withState(Xp7DeploymentStatus.State.PENDING).withMessage(String.format("Waiting for pods: %s", waitingForPods.stream().collect(Collectors.joining(", ")))));
return;
}
// Return OK
updateOnChange(xp7Deployment.get(), oldStatusHash, currentStatus.withState(Xp7DeploymentStatus.State.RUNNING).withMessage("OK"));
}
Aggregations