-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for override defaults file #871
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
SingularityService/src/main/java/com/hubspot/singularity/config/MergingSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.hubspot.singularity.config; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Iterator; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
|
||
import io.dropwizard.configuration.ConfigurationSourceProvider; | ||
|
||
public class MergingSourceProvider implements ConfigurationSourceProvider { | ||
private final ConfigurationSourceProvider delegate; | ||
private final String defaultConfigurationPath; | ||
private final ObjectMapper objectMapper; | ||
private final YAMLFactory yamlFactory; | ||
|
||
public MergingSourceProvider(ConfigurationSourceProvider delegate, String defaultConfigurationPath, ObjectMapper objectMapper, YAMLFactory yamlFactory) { | ||
this.delegate = delegate; | ||
this.defaultConfigurationPath = defaultConfigurationPath; | ||
this.objectMapper = objectMapper; | ||
this.yamlFactory = yamlFactory; | ||
} | ||
|
||
@Override | ||
public InputStream open(String path) throws IOException { | ||
final JsonNode originalNode = objectMapper.readTree(yamlFactory.createParser(delegate.open(defaultConfigurationPath))); | ||
final JsonNode overrideNode = objectMapper.readTree(yamlFactory.createParser(delegate.open(path))); | ||
|
||
if (!(originalNode instanceof ObjectNode && overrideNode instanceof ObjectNode)) { | ||
throw new SingularityConfigurationMergeException(String.format("Both %s and %s need to be YAML objects", defaultConfigurationPath, path)); | ||
} | ||
|
||
merge((ObjectNode)originalNode, (ObjectNode)overrideNode); | ||
|
||
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
objectMapper.writeTree(yamlFactory.createGenerator(baos), originalNode); | ||
|
||
return new ByteArrayInputStream(baos.toByteArray()); | ||
} | ||
|
||
private static void merge(ObjectNode to, ObjectNode from) { | ||
Iterator<String> newFieldNames = from.fieldNames(); | ||
|
||
while (newFieldNames.hasNext()) { | ||
String newFieldName = newFieldNames.next(); | ||
JsonNode oldVal = to.get(newFieldName); | ||
JsonNode newVal = from.get(newFieldName); | ||
|
||
if (oldVal == null || oldVal.isNull()) { | ||
to.put(newFieldName, newVal); | ||
} else if (oldVal.isArray() && newVal.isArray()) { | ||
((ArrayNode) oldVal).removeAll(); | ||
((ArrayNode) oldVal).addAll((ArrayNode) newVal); | ||
} else if (oldVal.isObject() && newVal.isObject()) { | ||
merge((ObjectNode) oldVal, (ObjectNode) newVal); | ||
} else if (!(newVal == null || newVal.isNull())) { | ||
to.put(newFieldName, newVal); | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
.../src/main/java/com/hubspot/singularity/config/SingularityConfigurationMergeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.hubspot.singularity.config; | ||
|
||
public class SingularityConfigurationMergeException extends RuntimeException { | ||
public SingularityConfigurationMergeException(String message) { | ||
super(message); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...larityService/src/test/java/com/hubspot/singularity/config/MergingSourceProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.hubspot.singularity.config; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import com.google.inject.Inject; | ||
import com.hubspot.singularity.SingularityTestBaseNoDb; | ||
|
||
import io.dropwizard.configuration.ConfigurationSourceProvider; | ||
|
||
public class MergingSourceProviderTest extends SingularityTestBaseNoDb { | ||
private static final String DEFAULT_PATH = "/configs/default.yaml"; | ||
private static final String OVERRIDE_PATH = "/configs/override.yaml"; | ||
private static final String JUST_A_STRING_PATH = "/configs/just_a_string.yaml"; | ||
private static final String DOESNT_EXIST_PATH = "/configs/doesnt_exist.yaml"; | ||
|
||
private static final YAMLFactory YAML_FACTORY = new YAMLFactory(); | ||
|
||
@Inject | ||
private ObjectMapper objectMapper; | ||
|
||
private ConfigurationSourceProvider buildConfigurationSourceProvider(String baseFilename) { | ||
final Class<?> klass = getClass(); | ||
|
||
return new MergingSourceProvider(new ConfigurationSourceProvider() { | ||
@Override | ||
public InputStream open(String path) throws IOException { | ||
final InputStream stream = klass.getResourceAsStream(path); | ||
if (stream == null) { | ||
throw new FileNotFoundException("File " + path + " not found in test resources directory"); | ||
} | ||
return stream; | ||
} | ||
}, baseFilename, objectMapper, YAML_FACTORY); | ||
} | ||
|
||
@Test | ||
public void testMergedConfigs() throws Exception { | ||
final InputStream mergedConfigStream = buildConfigurationSourceProvider(DEFAULT_PATH).open(OVERRIDE_PATH); | ||
final SingularityConfiguration mergedConfig = objectMapper.readValue(YAML_FACTORY.createParser(mergedConfigStream), SingularityConfiguration.class); | ||
|
||
assertEquals(10000, mergedConfig.getCacheTasksMaxSize()); | ||
assertEquals(500, mergedConfig.getCacheTasksInitialSize()); | ||
assertEquals(100, mergedConfig.getCheckDeploysEverySeconds()); | ||
assertEquals("baseuser", mergedConfig.getDatabaseConfiguration().get().getUser()); | ||
assertEquals("overridepassword", mergedConfig.getDatabaseConfiguration().get().getPassword()); | ||
} | ||
|
||
@Test( expected = SingularityConfigurationMergeException.class ) | ||
public void testNonObjectFails() throws Exception { | ||
buildConfigurationSourceProvider(DEFAULT_PATH).open(JUST_A_STRING_PATH); | ||
} | ||
|
||
@Test( expected = FileNotFoundException.class) | ||
public void testFileNoExistFail() throws Exception { | ||
buildConfigurationSourceProvider(DEFAULT_PATH).open(DOESNT_EXIST_PATH); | ||
buildConfigurationSourceProvider(DOESNT_EXIST_PATH).open(OVERRIDE_PATH); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cacheTasksMaxSize: 10000 # override.yaml will not touch this | ||
cacheTasksInitialSize: 200 # override.yaml will override this to 500 | ||
# override.yaml will override checkDeploysEverySeconds to 100 | ||
|
||
database: # override.yaml will override some of this | ||
user: "baseuser" |
1 change: 1 addition & 0 deletions
1
SingularityService/src/test/resources/configs/just_a_string.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"this is just a string!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
cacheTasksInitialSize: 500 | ||
checkDeploysEverySeconds: 100 | ||
|
||
database: | ||
password: "overridepassword" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this use-case do you think the behavior should be to append or to overwrite?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good eye -- probably overwrite