Not getting ConfigMapKeyRef for env entry in AdmissionController

8/12/2019

I'm working on a MutatingAdmissionWebhook monitoring Deployment objects in Go. The webhook is running and receives the request correctly.

I am trying to read through the Deployment->Spec->Container->env List.

I am able to get the env list, but I have ConfigMapKeyRef defined for these env vars, which is coming in as nil.

When I dumped the ValueFrom here is what I got

%!(EXTRA *v1.EnvVarSource=&EnvVarSource{FieldRef:nil,ResourceFieldRef:nil,
ConfigMapKeyRef:nil,SecretKeyRef:&SecretKeySelector{
LocalObjectReference:LocalObjectReference{Name:myuser,},Key:username,Optional:nil,},})

I am expecting to find a full name of the ConfigMap in the ConfigMapKeyRef.

I'm running Kubernetes Client v1.14.0

Any help to solve this?

Thanks,

-Sreeni

-- Sreeni
kubernetes
webhooks

2 Answers

8/20/2019

It means that object reference is not set to an instance of an object.

Here is similar problem: NilReferenceException.

Tips to Prevent Nil Reference Exceptions

  1. Initialize variables with valid values.

  2. If a variable can be null, then check for nill and handle it appropriately

  3. Use the ? operator on methods when possible. stringvar?.ToUpper();

  4. Use tools like Resharper to help point out potential nill reference exceptions

Useful article: admission-webhooks.

Admission Controller documentation: admission-controller.

-- MaggieO
Source: StackOverflow

9/10/2019

Tested with:
- validating admission Webhook (node-js)
- Kubernetes v1.15.2
- Pod spec:

spec:
  containers:
  - image: nginx
    name: nginx-with-env
    env:
    - name: PASSWORD
      value: fail
    - name: PASSWORD
      value: fail
    - name: SPECIAL_LEVEL_KEY
      valueFrom:
        configMapKeyRef:
          name: special-config
          key: SPECIAL_LEVEL
    - name: SPECIAL_TYPE_KEY
      valueFrom:
        configMapKeyRef:
          name: special-config
          key: SPECIAL_TYPE

I don't have any issue with reading this value ("full name of the ConfigMap in the ConfigMapKeyRef") out of AdmissionReview request object.

when I dump the value of one of such envs using ConfigMap's data, e.g: container.env[2].valueFrom, I'm getting JSON like object:

{"configMapKeyRef":{"name":"special-config","key":"SPECIAL_LEVEL"}}.

In Go lang you should refer to data in context with following syntax:

env.ValueFrom.ConfigMapKeyRef.Name
-- Nepomucen
Source: StackOverflow