<p>New to k8s.</p>

<p>Trying to read values from profile based config map. My configmap exists in default namespace. 
But, spring boot is not pickingup the values.</p>

<p>Config map looks like:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
    name: example-configmap-overriding-new-01
data:
    application.properties: |-
        globalkey = global key value
    application-qa.properties: |-
        globalkey = global key qa value    
    application-prod.properties: |-
        globalkey = global key prod value
</code></pre>

<p>The config map is created in default namespace too.</p>

<pre><code>kubectl get configmap -n default 

NAME                                  DATA   AGE
example-configmap-overriding-new-01   3      8d
</code></pre>

<p>My deployment file looks like</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata: 
    name: demo-configmapk8testing
spec:  
    selector:
        matchLabels:
            app: demo-configmapk8testing
replicas: 1
template: 
    metadata:
        labels:
            app: demo-configmapk8testing        
    spec:
        containers:
          - name: demo-configmapk8testing
            image: Path to image
            ports:
            - containerPort: 8080
            args: [
            "--spring.profiles.active=prod",
            "--spring.application.name=example-configmap-overriding-new-01",
            "--spring.cloud.kubernetes.config.name=example-configmap-
            overriding-new-01",
            "--spring.cloud.kubernetes.config.namespace=default",
            "--spring.cloud.kubernetes.config.enabled=true"]
            envFrom:
            - configMapRef:
                name: example-configmap-overriding-new-01
</code></pre>

<p>But the spring boot log says:-</p>

<pre><code>2019-07-02 22:10:38.092  WARN 1 --- [           main] 
o.s.c.k.config.ConfigMapPropertySource   : Can't read configMap with name: 
[example-configmap-overriding-new-01] in namespace:[default]. Ignoring

2019-07-02 22:10:38.331  INFO 1 --- [           main] 
b.c.PropertySourceBootstrapConfiguration : Located property source: 
CompositePropertySource {name='composite-configmap', propertySources= 
[ConfigMapPropertySource {name='configmap.example-configmap-overriding-new- 
01.default'}]}

2019-07-02 22:10:38.420  INFO 1 --- [           main] 
b.c.PropertySourceBootstrapConfiguration : Located property source: 
SecretsPropertySource {name='secrets.example-configmap-overriding-new- 
01.default'}

2019-07-02 22:10:38.692  INFO 1 --- [           main] 
c.e.c.ConfigconsumerApplication          : **The following profiles are 
active: prod**

--some logs--

Injection of autowired dependencies failed; nested exception is 
java.lang.IllegalArgumentException: **Could not resolve placeholder 
'globalkey' in value "${globalkey}"**
</code></pre>

<p>My spring boot config file looks like</p>

<pre><code>@Configuration
public class ConfigConsumerConfig {

    @Value(value = "${globalkey}")
    private String globalkey;

    // with getter and setters 
}
</code></pre>

<p>My pom.xml has the following dependency too.</p>

<pre><code>    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
        &lt;artifactId&gt;spring-cloud-starter-kubernetes-config&lt;/artifactId&gt;
        &lt;version&gt;1.0.2.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
        &lt;optional&gt;true&lt;/optional&gt;
    &lt;/dependency&gt;
</code></pre>

<p>I am running minikube in my local machine. Am I missing something here?</p>

<p>Could someone share some inputs here.</p>

<p><strong>spring-cloud-kubernetes</strong> doesn't have access to the Kubernetes API so it can't read the configMap. Check this docs for more details: <a href="https://github.com/spring-cloud/spring-cloud-kubernetes/blob/master/docs/src/main/asciidoc/security-service-accounts.adoc" rel="noreferrer">https://github.com/spring-cloud/spring-cloud-kubernetes/blob/master/docs/src/main/asciidoc/security-service-accounts.adoc</a>.</p>

<p>In short, apply this configuration and it will work fine:</p>

<pre><code>kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: YOUR-NAME-SPACE
  name: namespace-reader
rules:
  - apiGroups: ["", "extensions", "apps"]
    resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
    verbs: ["get", "list", "watch"]

---

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: namespace-reader-binding
  namespace: YOUR-NAME-SPACE
subjects:
- kind: ServiceAccount
  name: default
  apiGroup: ""
roleRef:
  kind: Role
  name: namespace-reader
  apiGroup: ""
</code></pre>

<p>You can read about Roles and RoleBinding in more detail here: <a href="https://kubernetes.io/docs/reference/access-authn-authz/rbac/" rel="noreferrer">https://kubernetes.io/docs/reference/access-authn-authz/rbac/</a>.</p>

<p>Note: You don't have to create volumes and volume mounts. I would say it is an alternative for it. If you would like to have it this way then you have to specify <code>spring.cloud.kubernetes.config.paths</code> in the spring boot application configuration (I have written it into <strong>bootstrap.yaml</strong> resource file).
E.g.</p>

<pre><code>spring: 
  cloud:
    kubernetes:
      config:
        paths: /etc/config/application.yaml
</code></pre>

<p>And then via Kubernetes Deployment configuration create ConfigMap volume and mount it on that path. In our example the path would be <strong>/etc/config</strong>.</p>

<p>Let me know if it works for you :)</p>
