/spring-boot-configmap-booster

Quickstart to expose a REST Greeting endpoint using Spring Boot where the message is defined as a Kubernetes Config Map property

Primary LanguageJavaApache License 2.0Apache-2.0

Introduction

This project exposes a simple HTTP endpoint where the service greeting is available at this address http://hostname:port/greeting and returns a json Greeting message

{
    "content": "Hello, World!",
    "id": 1
}

The id of the message is incremented for each request. To customize the message, you can pass as parameter the name of the person that you want to send your greeting.

When the Spring Boot application is deployed on OpenShift, Kubernetes Config Map is used to externalize the message to be returned to the client when it will call the HTTP endpoint. The message is declared within the application.properties file as a key. The application.properties file is declared as a property of the ConfigMap under the section data.

During the deployment of the application, the following configuration will be used to install the Config Map

apiVersion: "v1"
kind: "ConfigMap"
metadata:
  name: "spring-boot-configmap"
data:
  application.properties: "message: Hello, %s from Kubernetes ConfigMap !\n"

Remark : In order to tell to SpringBoot how it could find the ConfigMop, the springboot.application.name property must be assigned with the value of the configmap

spring.application.name=spring-boot-configmap

You can perform this task in three different ways:

  1. Build and launch using Spring Boot.
  2. Build and deploy using OpenShift.
  3. Build, deploy, and authenticate using OpenShift Online.

Prerequisites

To get started with these quickstarts you'll need the following prerequisites:

Name Description Version
java Java JDK 8
maven Apache Maven 3.3.9
oc OpenShift Client v3.3.x
git Git version management 2.x

In order to build and deploy this project, you must have an account on an OpenShift Online (OSO): https://console.dev-preview-int.openshift.com/ instance.

Launch and test

  1. Run the following command to start the maven goal of Spring Boot:

    mvn clean spring-boot:run
    
  2. If the application launched without error, use the following command to access the HTTP endpoint exposed using curl or httpie tool:

    http http://localhost:8080/api/greeting
    curl http://localhost:8080/api/greeting
    
  3. To pass a parameter for the Greeting Service, use the following HTTP request:

    http http://localhost:8080/api/greeting name==Charles
    curl http://localhost:8080/api/greeting -d name=Bruno
    

OpenShift Online

  1. Go to OpenShift Online to get the token used by the oc client for authentication and project access.

  2. On the oc client, execute the following command to replace MYTOKEN with the one from the Web Console:

    oc login https://api.dev-preview-int.openshift.com --token=MYTOKEN
    
  3. To allow the Spring Boot application running as a pod to access the Kubernetes Api to retrieve the Config Map associated to the application name of the project spring-boot-configmap, the view role must be assigned to the default service account in the current project:

    oc policy add-role-to-user view -n $(oc project -q) -z default
    
  4. Use the Fabric8 Maven Plugin to launch the S2I process on the OpenShift Online machine & start the pod.

    mvn clean fabric8:deploy -Popenshift
    
  5. Get the route url.

    oc get route/spring-boot-configmap
    NAME              HOST/PORT                                          PATH      SERVICE                TERMINATION   LABELS
    spring-boot-configmap   <HOST_PORT_ADDRESS>             spring-boot-configmap:8080
    
  6. Use the Host or Port address to access the HTTP endpoint.

    http http://<HOST_PORT_ADDRESS>/api/greeting
    http http://<HOST_PORT_ADDRESS>/api/greeting name==Bruno
    
    or 
    
    curl http://<HOST_PORT_ADDRESS>/api/greeting
    curl http://<HOST_PORT_ADDRESS>/api/greeting name==Bruno
    
  7. Validate that you get the message Hello, World from Kubernetes ConfigMap! as call's response from the HTTP endpoint