skip to Main Content

If I have a java configuration bean, saying:

package com.mycompany.app.configuration;

// whatever imports

public class MyConfiguration {

  private String someConfigurationValue = "defaultValue"; 

  // getters and setters etc
}

If I set that using jetty for local testing I can do so using a config.xml file in the following form:

  <myConfiguration class="com.mycompany.app.configuration.MyConfiguration" context="SomeContextAttribute">
    <someConfigurationValue>http://localhost:8080</someConfigurationValue>
  </myConfiguration>

However in the deployed environment in which I need to test, I will need to use docker to set these configuration values, we use jboss.

Is there a way to directly set these JNDI values? I’ve been looking for examples for quite a while but cannot find any. This would be in the context of a yaml file which is used to configure a k8 cluster. Apologies for the psuedocode, I would post the real code but it’s all proprietary so I can’t.

What I have so far for the overrides.yaml snippet is of the form:

env:
    'MyConfig.SomeContextAttribute':
      class_name: 'com.mycompany.app.configuration.MyConfiguration'
      someConfigurationValue: 'http://localhost:8080'

However this is a complete guess.

2

Answers


  1. Chosen as BEST ANSWER

    The way to do this is as follows:

    If you are attempting to set a value that looks like this in terms of fully qualified name:

    com.mycompany.app.configuration.MyConfiguration#someConfigurationValue 
    

    Then that will look like the following in a yaml file:

    com_mycompany_app_configuration_MyConfiguration_someConfigurationValue: 'blahValue'
    

    It really is that simple. It does need to be set as an environment variable in the yaml, but I'm not sure whether it needs to be under env: or if that's specific to us.

    I don't think there's a way of setting something in YAML that in XML would be an attribute, however. I've tried figuring that part out, but I haven't been able to.


  2. You can achieve it by using ConfigMap.

    A ConfigMap is an API object used to store non-confidential data in key-value pairs. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume.

    First what you need to create ConfigMap from your file using command as below:

    kubectl create configmap <map-name> <data-source>
    

    Where <map-name> is the name you want to assign to the ConfigMap and <data-source> is the directory, file, or literal value to draw the data from. You can read more about it here.

    Here is an example:

    1. Download the sample file:
    wget https://kubernetes.io/examples/configmap/game.properties
    

    You can check what is inside this file using cat command:

    cat game.properties 
    

    You will see that there are some variables in this file:

    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30r
    
    1. Create the ConfigMap from this file:
    kubectl create configmap game-config --from-file=game.properties
    

    You should see output that ConfigMap has been created:

    configmap/game-config created
    

    You can display details of the ConfigMap using command below:

    kubectl describe configmaps game-config
    

    You will see output as below:

    Name:         game-config
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    Data
    ====
    game.properties:
    ----
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
    

    You can also see how yaml of this ConfigMap will look using:

    kubectl get configmaps game-config -o yaml
    

    The output will be similar:

    apiVersion: v1
    data:
      game.properties: |-
        enemies=aliens
        lives=3
        enemies.cheat=true
        enemies.cheat.level=noGoodRotten
        secret.code.passphrase=UUDDLRLRBABAS
        secret.code.allowed=true
        secret.code.lives=30
    kind: ConfigMap
    metadata:
      creationTimestamp: "2022-01-28T12:33:33Z"
      name: game-config
      namespace: default
      resourceVersion: "2692045"
      uid: 5eed4d9d-0d38-42af-bde2-5c7079a48518
    

    Next goal is connecting ConfigMap to Pod. It could be added in yaml file of Podconfiguration.
    As you can see under containersthere is envFrom section. As name is a name of ConfigMapwhich I created in previous step. You can read about envFrom here

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
        - name: test-container
          image: nginx
          envFrom:
          - configMapRef:
              name: game-config
    

    Create a Pod from yaml file using:

    kubectl apply -f <name-of-your-file>.yaml
    

    Final step is checking environment variables in this Pod using below command:

    kubectl exec -it test-pod -- env
    

    As you can see below, there are environment variables from simple file which I downloaded in the first step:

    game.properties=enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search