coolbeevip opened a new issue #2341:
URL: https://github.com/apache/bookkeeper/issues/2341


   **FEATURE REQUEST**
   
   1. Please describe the feature you are requesting.
   
   In docker, the environment variable parameters are used 
applying-config-from-env.py to modify bk_server.conf. This inconsistency can be 
confusing for non-container deployments
   
   2. Indicate the importance of this issue to you (blocker, must-have, 
should-have, nice-to-have).
      Are you currently using any workarounds to address this issue?
   
   I think you can add a new parse method parseEnvironment and this method is 
executed after parseCommandLine. Environment variable parameters take 
precedence over command line parameters
   
   3. Provide any additional detail on your proposed use case for this feature.
   
    code snippet is shown below, any suggestions?
   
   Main.java
   ```
           // 0. parse command line
           try {
               conf = parseCommandLine(args);
           } catch (IllegalArgumentException iae) {
               return ExitCode.INVALID_CONF;
           }
   
           try {
               conf = parseEnvironment(conf);
           } catch (IllegalArgumentException iae) {
               return ExitCode.INVALID_CONF;
           }
   ```
   
   Read the variable with the prefix BK_ from the environment variable, and 
overwrite the variable value read from the command line through reflection
   
   ```
       private static ServerConfiguration parseEnvironment(ServerConfiguration 
conf) {
           System.getenv().entrySet().stream().filter(entry -> 
entry.getKey().startsWith("BK_"))
               .forEach(entry -> {
                   String name = entry.getKey().substring("BK_".length());
                   String value = entry.getValue();
                   Arrays.stream(conf.getClass().getDeclaredMethods())
                       .filter(m -> m.getName().startsWith("set")).forEach(m -> 
{
                       try {
                           if (m.getName().toLowerCase().equals("set" + 
name.toLowerCase())
                               && m.getParameterCount() == 1) {
                               if (m.getParameterTypes()[0] == boolean.class) {
                                   m.invoke(conf, 
Boolean.valueOf(value).booleanValue());
                               } else if (m.getParameterTypes()[0] == 
int.class) {
                                   m.invoke(conf, 
Integer.valueOf(value).intValue());
                               } else if (m.getParameterTypes()[0] == 
long.class) {
                                   m.invoke(conf, 
Long.valueOf(value).longValue());
                               } else if (m.getParameterTypes()[0] == 
double.class) {
                                   m.invoke(conf, 
Double.valueOf(value).doubleValue());
                               } else if (m.getParameterTypes()[0] == 
float.class) {
                                   m.invoke(conf, 
Float.valueOf(value).floatValue());
                               } else if (m.getParameterTypes()[0] == 
String.class) {
                                   m.invoke(conf, value);
                               }
                           }
                       } catch (Exception e) {
                           log.error(
                               "Error parsing environment " + entry.getKey() + 
"=" + entry.getValue()
                                   + " : ", e);
                       }
                   });
               });
           return conf;
       }
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to