1

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

Amit Kumar Gupta
  • 13,775
  • 5
  • 38
  • 55
Sreeni
  • 143
  • 1
  • 8

2 Answers2

0

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.

Malgorzata
  • 3,842
  • 1
  • 4
  • 17
0

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
  • 2,354
  • 1
  • 3
  • 13