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
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
Initialize variables with valid values.
If a variable can be null, then check for nill and handle it appropriately
Use the ? operator on methods when possible. stringvar?.ToUpper();
Useful article: admission-webhooks.
Admission Controller documentation: admission-controller.
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