-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ConfigProvider SPI and K8s implementation to load ConfigMap resou…
…rces (#80) * Load configMap properties at start up * Add config provider to clean up dependencies * Address feedback - change service loader interface, removing system props, and load configs per driver * Move field expansion out of SPI
- Loading branch information
Showing
19 changed files
with
163 additions
and
34 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: hoptimator-configmap | ||
data: | ||
# https://kubernetes.io/docs/concepts/configuration/configmap/ | ||
|
||
# property-like keys; each key maps to a simple value | ||
player_initial_lives: "3" | ||
ui_properties_file_name: "user-interface.properties" | ||
|
||
# file-like keys | ||
game.properties: | | ||
enemy.types=aliens,monsters | ||
player.maximum-lives=5 | ||
user-interface.properties: | | ||
color.good=purple | ||
color.bad=yellow | ||
allow.textmode=true |
8 changes: 8 additions & 0 deletions
8
hoptimator-api/src/main/java/com/linkedin/hoptimator/ConfigProvider.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,8 @@ | ||
package com.linkedin.hoptimator; | ||
|
||
import java.util.Properties; | ||
|
||
public interface ConfigProvider { | ||
|
||
Properties loadConfig() throws Exception; | ||
} |
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
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
12 changes: 12 additions & 0 deletions
12
...mator-jdbc/src/main/java/com/linkedin/hoptimator/jdbc/SystemPropertiesConfigProvider.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,12 @@ | ||
package com.linkedin.hoptimator.jdbc; | ||
|
||
import java.util.Properties; | ||
|
||
import com.linkedin.hoptimator.ConfigProvider; | ||
|
||
public class SystemPropertiesConfigProvider implements ConfigProvider { | ||
|
||
public Properties loadConfig() { | ||
return System.getProperties(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
hoptimator-jdbc/src/main/resources/META-INF/services/com.linkedin.hoptimator.ConfigProvider
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 @@ | ||
com.linkedin.hoptimator.jdbc.SystemPropertiesConfigProvider |
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
29 changes: 29 additions & 0 deletions
29
hoptimator-k8s/src/main/java/com/linkedin/hoptimator/k8s/K8sConfigProvider.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,29 @@ | ||
package com.linkedin.hoptimator.k8s; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import io.kubernetes.client.openapi.models.V1ConfigMap; | ||
import io.kubernetes.client.openapi.models.V1ConfigMapList; | ||
|
||
import com.linkedin.hoptimator.ConfigProvider; | ||
|
||
|
||
public class K8sConfigProvider implements ConfigProvider { | ||
|
||
public static final String HOPTIMATOR_CONFIG_MAP = "hoptimator-configmap"; | ||
|
||
public Properties loadConfig() throws SQLException { | ||
Map<String, String> topLevelConfigs = loadTopLevelConfig(HOPTIMATOR_CONFIG_MAP); | ||
Properties p = new Properties(); | ||
p.putAll(topLevelConfigs); | ||
return p; | ||
} | ||
|
||
// Load top-level config map properties | ||
private Map<String, String> loadTopLevelConfig(String configMapName) throws SQLException { | ||
K8sApi<V1ConfigMap, V1ConfigMapList> configMapApi = new K8sApi<>(K8sContext.currentContext(), K8sApiEndpoints.CONFIG_MAPS); | ||
return configMapApi.get(configMapName).getData(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
hoptimator-k8s/src/main/resources/META-INF/services/com.linkedin.hoptimator.ConfigProvider
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 @@ | ||
com.linkedin.hoptimator.k8s.K8sConfigProvider |
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
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
44 changes: 44 additions & 0 deletions
44
hoptimator-util/src/main/java/com/linkedin/hoptimator/util/ConfigService.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,44 @@ | ||
package com.linkedin.hoptimator.util; | ||
|
||
import java.io.StringReader; | ||
import java.util.Properties; | ||
import java.util.ServiceLoader; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.linkedin.hoptimator.ConfigProvider; | ||
|
||
public final class ConfigService { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ConfigService.class); | ||
|
||
private ConfigService() { | ||
} | ||
|
||
// Loads top level configs and expands input fields as file-like properties | ||
// Ex: | ||
// log.properties: | | ||
// level=INFO | ||
public static Properties config(String... expansionFields) { | ||
ServiceLoader<ConfigProvider> loader = ServiceLoader.load(ConfigProvider.class); | ||
Properties properties = new Properties(); | ||
for (ConfigProvider provider : loader) { | ||
try { | ||
Properties loadedProperties = provider.loadConfig(); | ||
log.debug("Loaded properties={} from provider={}", loadedProperties, provider); | ||
properties.putAll(loadedProperties); | ||
for (String expansionField : expansionFields) { | ||
if (loadedProperties == null || !loadedProperties.containsKey(expansionField)) { | ||
log.warn("provider={} does not contain field={}", provider, expansionField); | ||
continue; | ||
} | ||
properties.load(new StringReader(loadedProperties.getProperty(expansionField))); | ||
} | ||
} catch (Exception e) { | ||
log.warn("Could not load properties for provider={}", provider, e); | ||
} | ||
} | ||
return properties; | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.