Skip to content

Commit

Permalink
GH-00 : Minor refactoring and fix typo in test package name (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Prashanth Ramakrishnan <[email protected]>
  • Loading branch information
prashanth-92 and Prashanth Ramakrishnan authored Dec 13, 2023
1 parent adc8a87 commit 5efa965
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public class ConfigMapController implements Reconciler<ConfigMapCustomResource>,
@Override
public UpdateControl<ConfigMapCustomResource> reconcile(final ConfigMapCustomResource resource, final Context<ConfigMapCustomResource> context) {
try {
log.info("Context being reloaded for [{}]",context.getSecondaryResource(ConfigMap.class).map(map -> map.getMetadata().getName()).orElse("None"));
val reloadedResource = context.getSecondaryResource(ConfigMap.class)
.map(map -> map.getMetadata().getName())
.orElse("None");
log.info("Context being reloaded for [{}]", reloadedResource);
specValidator.validateResourceContent(resource);
createConfigMap(resource);
resource.setStatus(ResourceStatus.builder().build());
Expand Down Expand Up @@ -73,7 +76,7 @@ private ConfigMap buildConfigMap(final ConfigMapCustomResource resource) {

@Override
public Map<String, EventSource> prepareEventSources(final EventSourceContext<ConfigMapCustomResource> context) {
InformerConfiguration<ConfigMap> configuration =
final InformerConfiguration<ConfigMap> configuration =
InformerConfiguration.from(ConfigMap.class, context)
.build();
return EventSourceInitializer
Expand Down
37 changes: 22 additions & 15 deletions src/main/java/com/rcube/configmap/validators/SpecValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import com.networknt.schema.ValidationMessage;
import com.rcube.configmap.operator.ConfigMapCustomResource;
import com.rcube.configmap.operator.ResourceStatus;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
@Slf4j
@AllArgsConstructor
Expand All @@ -24,7 +24,7 @@ public void validateResourceContent(final ConfigMapCustomResource resource) {
final Map<String, String> schemaResource = resource.getSpec().getConfig().getSchema();

if (schemaResource == null || CollectionUtils.isEmpty(schemaResource)) {
log.warn("Resource [{}] doesnt have a schema, it is recommended to have one", resource.getMetadata().getName());
log.warn("Resource [{}] does not have a schema, it is recommended to have one", resource.getMetadata().getName());
return;
}

Expand All @@ -33,15 +33,7 @@ public void validateResourceContent(final ConfigMapCustomResource resource) {
if (CollectionUtils.isEmpty(dataResource)) {
throw new RuntimeException("Missing required fields in the data configuration.");
}
final Map<String, Set<ValidationMessage>> schemaValidationResult = new HashMap<>();
dataResource.forEach((key, data) -> {
if (schemaResource.containsKey(key)) {
final Set<ValidationMessage> validation = jsonContentValidators.validate(schemaResource.get(key), data);
if (!CollectionUtils.isEmpty(validation)) {
schemaValidationResult.put(key, validation);
}
}
});
final Map<String, Set<ValidationMessage>> schemaValidationResult = getValidationFailures(dataResource, schemaResource);
if (!CollectionUtils.isEmpty(schemaValidationResult)) {
throw new RuntimeException(String.format("Invalid data format %s", schemaValidationResult));
}
Expand All @@ -51,4 +43,19 @@ public void validateResourceContent(final ConfigMapCustomResource resource) {
throw ex;
}
}

@NotNull
private Map<String, Set<ValidationMessage>> getValidationFailures(final Map<String, String> dataResource,
final Map<String, String> schemaResource) {
final Map<String, Set<ValidationMessage>> schemaValidationResult = new HashMap<>();
dataResource.forEach((key, data) -> {
if (schemaResource.containsKey(key)) {
final Set<ValidationMessage> validation = jsonContentValidators.validate(schemaResource.get(key), data);
if (!CollectionUtils.isEmpty(validation)) {
schemaValidationResult.put(key, validation);
}
}
});
return schemaValidationResult;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rcube.configmap.conifgvalidator;
package com.rcube.configmap.configvalidator;

import com.rcube.configmap.TestDataUtil;
import com.rcube.configmap.operator.ConfigMapCustomResource;
Expand Down

0 comments on commit 5efa965

Please sign in to comment.