12/16/2024

Accessing ConfigMap Data in Quarkus

Accessing ConfigMap Data in Quarkus


To retrieve configuration data from a Kubernetes ConfigMap in a Quarkus application, you can utilize the quarkus-kubernetes-config extension, which simplifies the integration of Kubernetes ConfigMaps and Secrets into your application. 
Here’s how you can do it:


Step 1: Add the Extension

First, ensure that you have the quarkus-kubernetes-config extension added to your Quarkus project. You can add it using the following command:
bash
./mvnw quarkus:add-extension -Dextensions="kubernetes-config"


Step 2: Create a ConfigMap

Create a ConfigMap in your Kubernetes cluster that contains the configuration properties you want to use. For example, you can create a ConfigMap named app-config with the following YAML:
yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  greeting: "Hello, Quarkus!"
  port: "8080"
Apply this ConfigMap to your Kubernetes cluster:
bash
kubectl apply -f configmap.yaml


Step 3: Configure Quarkus to Use the ConfigMap

In your application.properties file, you can specify that Quarkus should read configuration values from the ConfigMap. For example:
properties
quarkus.kubernetes-config.config-map=app-config


Step 4: Access the Configuration in Your Application

You can access the values stored in the ConfigMap using the @ConfigProperty annotation in your Quarkus application. Here’s an example of how to do this:
java
import io.quarkus.arc.properties.UnlessBuildProfile;
import io.quarkus.runtime.Startup;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
@Startup
public class GreetingService {

    @ConfigProperty(name = "greeting")
    String greeting;

    @ConfigProperty(name = "port")
    int port;

    public String getGreeting() {
        return greeting;
    }

    public int getPort() {
        return port;
    }
}


Conclusion

By following these steps, you can easily access configuration data from a Kubernetes ConfigMap in your Quarkus application. This approach allows you to manage your application's configuration dynamically, making it more adaptable to different environments without changing the codebase. The use of ConfigMaps enhances the flexibility and maintainability of your applications deployed on Kubernetes.

QUARKUS & GraphQL

 QUARKUS & GraphQL https://www.geeksforgeeks.org/graphql-tutorial/ https://quarkus.io/guides/smallrye-graphql-client https://www.mastert...