Skip to content

Commit 0c03fa6

Browse files
authored
Merge pull request #179 from freenowtech/read_properties_files_independently
Read the properties files independently. Fail in reading file or if f…
2 parents 0189d11 + 44cf010 commit 0c03fa6

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

plugins/kubernetesapi-report/src/main/java/com/freenow/sauron/plugins/readers/KubernetesPropertiesFilesReader.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import io.kubernetes.client.openapi.models.V1ObjectMeta;
1010
import java.io.IOException;
1111
import java.io.StringReader;
12+
import java.util.List;
1213
import java.util.Map;
1314
import java.util.NoSuchElementException;
1415
import java.util.Optional;
1516
import java.util.Properties;
17+
import java.util.stream.Collectors;
1618
import lombok.RequiredArgsConstructor;
1719
import lombok.extern.slf4j.Slf4j;
1820

@@ -40,50 +42,41 @@ public void read(DataSet input, String serviceLabel, Map<String, Map<String, Str
4042
{
4143
new RetryCommand<Void>(retryConfig).run(() ->
4244
{
43-
boolean foundAll = kubernetesGetObjectMetaCommand.get(serviceLabel, POD, input.getServiceName(), apiClient)
45+
kubernetesGetObjectMetaCommand.get(serviceLabel, POD, input.getServiceName(), apiClient)
4446
.map(V1ObjectMeta::getName)
45-
.map(podName -> exec(podName, input, propertiesFilesCheck, apiClient))
46-
.orElse(false);
47-
48-
if (!foundAll)
49-
{
50-
throw new NoSuchElementException(String.format("Properties %s not found.", propertiesFilesCheck));
51-
}
47+
.ifPresent(podName -> exec(podName, input, propertiesFilesCheck, apiClient));
5248
return null;
5349
});
5450
}
5551

5652

57-
private Boolean exec(final String podName, DataSet input, final Map<String, Map<String, String>> propertiesFilesCheck, final ApiClient apiClient)
53+
private void exec(final String podName, DataSet input, final Map<String, Map<String, String>> propertiesFilesCheck, final ApiClient apiClient)
5854
{
59-
return propertiesFilesCheck.entrySet().stream().allMatch(it -> {
60-
Optional<String> podFileProps = kubernetesExecCommand.exec(podName, String.format(ENV_COMMAND, it.getKey()), apiClient);
55+
propertiesFilesCheck.forEach((propFilePath, propKeys) -> {
56+
Optional<String> podFileProps = kubernetesExecCommand.exec(podName, String.format(ENV_COMMAND, propFilePath), apiClient);
6157
if (podFileProps.isPresent())
6258
{
6359
Properties props = parse(podFileProps.get());
64-
return matchProps(input, it.getKey(), it.getValue(), props);
60+
matchProps(input, propFilePath, propKeys, props);
6561
}
6662
else
6763
{
68-
log.info("Properties not found in {} at POD {}", it.getKey(), podName);
69-
return false;
64+
log.info("Properties file {} not found in POD {}. Properties: {} will be skipped", propFilePath, podName, String.join(",", propKeys.keySet()));
7065
}
7166
});
7267
}
7368

7469

75-
private boolean matchProps(DataSet input, String propFilePath, Map<String, String> propKeys, Properties props)
70+
private void matchProps(DataSet input, String propFilePath, Map<String, String> propKeys, Properties props)
7671
{
77-
return propKeys.entrySet().stream().allMatch(prop -> {
78-
if (props.containsKey(prop.getValue()))
72+
propKeys.forEach((key, value) -> {
73+
if (props.containsKey(value))
7974
{
80-
input.setAdditionalInformation(prop.getKey(), props.get(prop.getValue()));
81-
return true;
75+
input.setAdditionalInformation(key, props.get(value));
8276
}
8377
else
8478
{
85-
log.info("Property Key {}, not found at {}", prop.getValue(), propFilePath);
86-
return false;
79+
log.info("Property Key {}, not found at {}", value, propFilePath);
8780
}
8881
});
8982
}

plugins/kubernetesapi-report/src/test/java/com/freenow/sauron/plugins/KubernetesPropertiesFilesReaderTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class KubernetesPropertiesFilesReaderTest
3434
private static final String PROP_FILE_ENV_VERSION = "dummy.env";
3535
private static final String ENV_ENABLED = "ENV_ENABLED";
3636
private static final String ENV_VERSION = "ENV_VERSION";
37+
private static final String ENV_ANOTHER = "ENV_ANOTHER";
3738
private static final String PROPERTIES_FILES_CHECK = "propertiesFilesCheck";
3839

3940
@Mock
@@ -61,6 +62,23 @@ public void read()
6162
assertNotNull(input);
6263
assertEquals("true", input.getStringAdditionalInformation(ENV_ENABLED).get());
6364
assertEquals("8080", input.getStringAdditionalInformation(ENV_VERSION).get());
65+
assertFalse(input.getStringAdditionalInformation(ENV_ANOTHER).isPresent());
66+
}
67+
68+
@Test
69+
public void twoPropertiesFilesRequestedToCheckButOneIsNotFound()
70+
{
71+
final var objMetaData = createObjMetaData();
72+
final var input = dummyDataSet();
73+
when(kubernetesGetObjectMetaCommand.get(SERVICE_LABEL, POD, SERVICE_NAME, apiClient)).thenReturn(objMetaData);
74+
when(kubernetesExecCommand.exec(objMetaData.get().getName(), String.format(ENV_COMMAND, PROP_FILE_ENV_ENABLED), apiClient)).thenReturn(envProps());
75+
when(kubernetesExecCommand.exec(objMetaData.get().getName(), String.format(ENV_COMMAND, PROP_FILE_ENV_VERSION), apiClient)).thenReturn(Optional.empty());
76+
77+
kubernetesPropertiesFilesReader.read(input, SERVICE_LABEL, getPropertyFilesConfig(), apiClient);
78+
assertNotNull(input);
79+
assertEquals("true", input.getStringAdditionalInformation(ENV_ENABLED).get());
80+
assertFalse(input.getStringAdditionalInformation(ENV_VERSION).isPresent());
81+
assertFalse(input.getStringAdditionalInformation(ENV_ANOTHER).isPresent());
6482
}
6583

6684

@@ -75,6 +93,7 @@ public void noPropertyFileFoundOnPOD()
7593
assertNotNull(input);
7694
assertFalse(input.getStringAdditionalInformation(ENV_ENABLED).isPresent());
7795
assertFalse(input.getStringAdditionalInformation(ENV_VERSION).isPresent());
96+
assertFalse(input.getStringAdditionalInformation(ENV_ANOTHER).isPresent());
7897
}
7998

8099

@@ -106,7 +125,8 @@ private PluginsConfigurationProperties dummyPluginConfig()
106125
Map.of(
107126
"dummy.properties",
108127
Map.of(
109-
"ENV_ENABLED", "env.is.enabled"
128+
"ENV_ENABLED", "env.is.enabled",
129+
"ENV_ANOTHER", "env.is.not.found"
110130
),
111131
"dummy.env", Map.of(
112132
"ENV_VERSION", "ENV_VERSION"

0 commit comments

Comments
 (0)