🎨 Format

This commit is contained in:
Andreas Dinauer 2025-12-20 18:42:59 +01:00
parent 16a5627b39
commit e165df6076
29 changed files with 125 additions and 146 deletions

18
pom.xml
View File

@ -163,6 +163,24 @@
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<version>2.29.0</version>
<configuration>
<configFile>https://git.dinauer.dev/andreas.dinauer/formatter/raw/branch/main/format.xml</configFile>
<lineEnding>LF</lineEnding>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>

View File

@ -101,44 +101,34 @@ public class ResourceResource
{
switch (resourceType)
{
case ResourceType.STATEFUL_SET ->
{
case ResourceType.STATEFUL_SET -> {
return statefulSetService;
}
case ResourceType.DEPLOYMENT ->
{
case ResourceType.DEPLOYMENT -> {
return deploymentService;
}
case ResourceType.SERVICE ->
{
case ResourceType.SERVICE -> {
return serviceService;
}
case ResourceType.INGRESS ->
{
case ResourceType.INGRESS -> {
return ingressService;
}
case ResourceType.POD ->
{
case ResourceType.POD -> {
return podService;
}
case ResourceType.CUSTOM_RESOURCE_DEFINITION ->
{
case ResourceType.CUSTOM_RESOURCE_DEFINITION -> {
return customResourceDefinitionService;
}
case ResourceType.NODE ->
{
case ResourceType.NODE -> {
return nodeService;
}
case ResourceType.SECRET ->
{
case ResourceType.SECRET -> {
return secretService;
}
case ResourceType.CONFIG_MAP ->
{
case ResourceType.CONFIG_MAP -> {
return configMapService;
}
default ->
{
default -> {
LOG.errorf("Invalid resource type %s.", resourceType);
throw new BadRequestException();
}

View File

@ -3,36 +3,33 @@ package dev.dinauer;
public enum ResourceType
{
// Cluster Resources
NODE("nodes"),
NAMESPACE("namespaces"),
CUSTOM_RESOURCE_DEFINITION("custom-resource-definitions"),
NODE("nodes"), NAMESPACE("namespaces"), CUSTOM_RESOURCE_DEFINITION("custom-resource-definitions"),
// Workloads
STATEFUL_SET("stateful-sets"),
DEPLOYMENT("deployments"),
POD("pods"),
STATEFUL_SET("stateful-sets"), DEPLOYMENT("deployments"), POD("pods"),
// Networking
SERVICE("services"),
INGRESS("ingresses"),
SERVICE("services"), INGRESS("ingresses"),
// Config
SECRET("secrets"),
CONFIG_MAP("config-maps"),
SECRET("secrets"), CONFIG_MAP("config-maps"),
// Storage
PVC("pvcs"),
PV("pvs");
PVC("pvcs"), PV("pvs");
private final String value;
ResourceType(String value) {
ResourceType(String value)
{
this.value = value;
}
public static ResourceType fromString(String text) {
for (ResourceType type : ResourceType.values()) {
if (type.value.equalsIgnoreCase(text)) {
public static ResourceType fromString(String text)
{
for (ResourceType type : ResourceType.values())
{
if (type.value.equalsIgnoreCase(text))
{
return type;
}
}
@ -40,7 +37,8 @@ public enum ResourceType
}
@Override
public String toString() {
public String toString()
{
return value;
}
}

View File

@ -49,8 +49,7 @@ public class ResourceWebsocket
ResourceType resourceType = ResourceType.fromString(rawResourceType);
if (tokenService.validateTokenByQueryString(session.getQueryString()))
{
executor.submit(() ->
{
executor.submit(() -> {
ResourceService<?> service = serviceFactory.getService(resourceType);
send(session, EventType.INIT, service.findByNamespace(namespace));
sessions.put(session, service.watch(namespace, getWatcher(session)));

View File

@ -55,55 +55,55 @@ public class ServiceFactory
{
switch (resourceType)
{
case ResourceType.STATEFUL_SET ->
case ResourceType.STATEFUL_SET:
{
return statefulSetService;
}
case ResourceType.DEPLOYMENT ->
case ResourceType.DEPLOYMENT:
{
return deploymentService;
}
case ResourceType.SERVICE ->
case ResourceType.SERVICE:
{
return serviceService;
}
case ResourceType.INGRESS ->
case ResourceType.INGRESS:
{
return ingressService;
}
case ResourceType.POD ->
case ResourceType.POD:
{
return podService;
}
case ResourceType.CUSTOM_RESOURCE_DEFINITION ->
case ResourceType.CUSTOM_RESOURCE_DEFINITION:
{
return customResourceDefinitionService;
}
case ResourceType.NODE ->
case ResourceType.NODE:
{
return nodeService;
}
case ResourceType.SECRET ->
case ResourceType.SECRET:
{
return secretService;
}
case ResourceType.CONFIG_MAP ->
case ResourceType.CONFIG_MAP:
{
return configMapService;
}
case ResourceType.PVC ->
case ResourceType.PVC:
{
return pvcService;
}
case ResourceType.PV ->
case ResourceType.PV:
{
return pvService;
}
case ResourceType.NAMESPACE ->
case ResourceType.NAMESPACE:
{
return namespaceService;
}
default ->
default :
{
LOG.errorf("Invalid resource type %s.", resourceType);
throw new BadRequestException();

View File

@ -35,11 +35,7 @@ public class LoginResource
UserEntity user = userOptional.get();
if (BcryptUtil.matches(login.password(), user.getPassword()))
{
return Jwt
.upn(user.getId())
.expiresAt(ZonedDateTime.now().plusDays(15).toInstant())
.groups(user.getRoles())
.sign();
return Jwt.upn(user.getId()).expiresAt(ZonedDateTime.now().plusDays(15).toInstant()).groups(user.getRoles()).sign();
}
LOG.info("Cannot access user. Forbidden");
throw new ForbiddenException(Response.status(403).type(MediaType.TEXT_PLAIN).entity("wrong_password").build());

View File

@ -52,7 +52,8 @@ public class UserResource
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
@RolesAllowed({"ADMIN", "OWNER"})
@RolesAllowed(
{"ADMIN", "OWNER"})
public void createUser(UserCreation user)
{
if (List.of("ADMIN", "USER").contains(user.role()))

View File

@ -91,14 +91,17 @@ public class MonitoringJobRunner
LOG.infof("Running %s %s monitoring.", config.getConfigName(), config.getType().toString().toLowerCase());
switch (config.getType())
{
case VOLUME ->
case VOLUME:
{
volumeMonitoringJobRunner.run(config);
return;
}
case WORKLOAD ->
case WORKLOAD:
{
memoryMonitoringJobRunner.run(config);
return;
}
}
throw new RuntimeException();
}
}

View File

@ -55,7 +55,7 @@ public class MonitoringResource
switch (configCreation.type())
{
case MonitoringType.VOLUME ->
case MonitoringType.VOLUME:
{
VolumeConfig volumeConfig = new VolumeConfig();
volumeConfig.setId(UUID.randomUUID().toString());

View File

@ -24,12 +24,10 @@ public class MonitoringService
TargetConfig targetConfig = config.getTargetConfig();
switch (targetConfig.getType())
{
case LABEL ->
{
case LABEL -> {
return podService.findByNamespaceAndLabels(targetConfig.getNamespace(), targetConfig.getLabels()).stream().filter(pod -> pod.getStatus().getPhase().equals("Running")).toList();
}
case DEPLOYMENT, STATEFUL_SET ->
{
case DEPLOYMENT, STATEFUL_SET -> {
throw new NotImplementedYet();
}
}

View File

@ -2,7 +2,5 @@ package dev.dinauer.monitoring.entity;
public enum MonitoringTargetType
{
DEPLOYMENT,
STATEFUL_SET,
LABEL
DEPLOYMENT, STATEFUL_SET, LABEL
}

View File

@ -97,7 +97,9 @@ public class TargetConfig
{
try
{
return OBJECT_MAPPER.readValue(labels, new TypeReference<Map<String, String>>() {});
return OBJECT_MAPPER.readValue(labels, new TypeReference<Map<String, String>>()
{
});
}
catch (JsonProcessingException e)
{

View File

@ -2,10 +2,6 @@ package dev.dinauer.monitoring.entity.creation;
import dev.dinauer.monitoring.entity.MonitoringType;
public record MonitoringConfigCreation(
String configName,
MonitoringType type,
String interval,
VolumeConfigCreation volumeConfig,
MonitoringTargetConfigCreation targetConfig
) {}
public record MonitoringConfigCreation(String configName, MonitoringType type, String interval, VolumeConfigCreation volumeConfig, MonitoringTargetConfigCreation targetConfig)
{
}

View File

@ -2,11 +2,6 @@ package dev.dinauer.monitoring.entity.creation;
import dev.dinauer.monitoring.entity.MonitoringTargetType;
public record MonitoringTargetConfigCreation(
MonitoringTargetType type,
String namespace,
String deploymentName,
String statefulSetName,
String labelKey,
String labelValue
) {}
public record MonitoringTargetConfigCreation(MonitoringTargetType type, String namespace, String deploymentName, String statefulSetName, String labelKey, String labelValue)
{
}

View File

@ -1,6 +1,5 @@
package dev.dinauer.monitoring.entity.creation;
public record VolumeConfigCreation(
String mountPath,
String containerName
) {}
public record VolumeConfigCreation(String mountPath, String containerName)
{
}

View File

@ -28,15 +28,7 @@ public class NodeMonitoringService
for (MonitoredNode node : nodes)
{
NodeMetrics nodeMetrics = node.getMetrics();
Map<String, Number> metrics = Stream.of(
new AbstractMap.SimpleEntry<>("RELATIVE_CPU", nodeMetrics.relativeCpuUsage()),
new AbstractMap.SimpleEntry<>("RELATIVE_MEMORY", nodeMetrics.relativeMemory()),
new AbstractMap.SimpleEntry<>("ABSOLUTE_MEMORY", nodeMetrics.absoluteMemory()),
new AbstractMap.SimpleEntry<>("ABSOLUTE_CPU", nodeMetrics.absoluteCpuUsage()),
new AbstractMap.SimpleEntry<>("RELATIVE_DISK_SPACE", nodeMetrics.relativeDiskUsage()),
new AbstractMap.SimpleEntry<>("TOTAL_DISK_SPACE", nodeMetrics.totalDiskSpace()))
.filter(entry -> entry.getValue() != null)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Map<String, Number> metrics = Stream.of(new AbstractMap.SimpleEntry<>("RELATIVE_CPU", nodeMetrics.relativeCpuUsage()), new AbstractMap.SimpleEntry<>("RELATIVE_MEMORY", nodeMetrics.relativeMemory()), new AbstractMap.SimpleEntry<>("ABSOLUTE_MEMORY", nodeMetrics.absoluteMemory()), new AbstractMap.SimpleEntry<>("ABSOLUTE_CPU", nodeMetrics.absoluteCpuUsage()), new AbstractMap.SimpleEntry<>("RELATIVE_DISK_SPACE", nodeMetrics.relativeDiskUsage()), new AbstractMap.SimpleEntry<>("TOTAL_DISK_SPACE", nodeMetrics.totalDiskSpace())).filter(entry -> entry.getValue() != null).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
bigBucketService.index(String.format("NODE-%s", node.getMetadata().getUid()), "NODE_METRICS", metrics);
}

View File

@ -10,20 +10,16 @@ public class Duration
long value = Long.parseLong(input.substring(0, input.length() - 1));
switch (unit)
{
case 's' ->
{
case 's' -> {
return value;
}
case 'm' ->
{
case 'm' -> {
return value * 60;
}
case 'h' ->
{
case 'h' -> {
return value * 60 * 60;
}
default ->
{
default -> {
throw new IllegalArgumentException(String.format("Invalid unit %s", unit));
}
}

View File

@ -63,7 +63,8 @@ public class DeploymentService implements ResourceService<Deployment>
@Override
public Watch watch(String namespace, Watcher<Deployment> watcher)
{
try (AppsAPIGroupDSL dsl = clientProvider.getClient().apps()) {
try (AppsAPIGroupDSL dsl = clientProvider.getClient().apps())
{
String version = dsl.deployments().inAnyNamespace().list().getMetadata().getResourceVersion();
if (namespace.equals("_all"))
{

View File

@ -28,11 +28,7 @@ public class PodService implements ResourceService<Pod>
if (podOptional.isPresent())
{
Pod pod = podOptional.get();
clientProvider.getClient()
.pods()
.inNamespace(pod.getMetadata().getNamespace())
.withName(pod.getMetadata().getName())
.delete();
clientProvider.getClient().pods().inNamespace(pod.getMetadata().getNamespace()).withName(pod.getMetadata().getName()).delete();
}
}

View File

@ -62,7 +62,8 @@ public class StatefulSetService implements ResourceService<StatefulSet>
@Override
public Watch watch(String namespace, Watcher<StatefulSet> watcher)
{
try (AppsAPIGroupDSL dsl = clientProvider.getClient().apps()) {
try (AppsAPIGroupDSL dsl = clientProvider.getClient().apps())
{
String version = dsl.statefulSets().inAnyNamespace().list().getMetadata().getResourceVersion();
if (namespace.equals("_all"))
{