I have a spring boot web app which simply prints a property that is passed in a Kubernetes' ConfigMap.
This is my main class:
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class DemoApplication {
private MyConfig config;
private DiscoveryClient discoveryClient;
@Autowired
public DemoApplication(MyConfig config, DiscoveryClient discoveryClient) {
this.config = config;
this.discoveryClient = discoveryClient;
}
@RequestMapping("/")
public String info() {
return config.getMessage();
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/services")
public String services() {
StringBuilder b = new StringBuilder();
discoveryClient.getServices().forEach((s) -> b.append(s).append(" , "));
return b.toString();
}
}
and the MyConfig
class is:
@Configuration
@ConfigurationProperties(prefix = "bean")
public class MyConfig {
private String message = "a message that can be changed live";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Basically, by invoking root resource I always get:
a message that can be changed live
And invoking /services I actually get a list of Kubernetes services.
I'm creating the ConfigMap with kubectl create -f configmap-demo.yml
being the content:
apiVersion: v1
kind: ConfigMap
metadata:
name: demo
data:
bean.message: This is an info from k8
And the deployment with kubecetl create -f deploy-demo.yml
and the content is:
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo
labels:
app: demo
spec:
replicas: 1
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
# this service account was created according to
# https://kubernetes.io/docs/reference/access-authn-authz/rbac/#service-account-permissions
# point 5 - Grant super-user access to all service accounts cluster-wide (strongly discouraged)
serviceAccountName: i18n-spring-k8
containers:
- name: demo
image: aribeiro/sck-demo
imagePullPolicy: Never
env:
- name: JAVA_OPTS
value:
ports:
- containerPort: 8080
volumes:
- name: demo
configMap:
name: demo
The problem is that when accessing the root resource /
I always get the default hardcoded value and never what is defined in Kubernetes' ConfigMap.
Example project also with yaml files and Docker file available at https://drive.google.com/open?id=107IcwnYIbVpmwVgdgi8Dhx4nHEFAVxV8 .
Also checked the startup DEBUG logs and I don't see any error or clue why it should not work.