Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Templating can't access all of the pod's fields #32

@LCaparelli

Description

@LCaparelli

Hey folks, thanks for the work!

This project looks like a great fit for the project my team is working on, but there are some things we only know at the time the mutation is being applied, more specifically the pod's name and namespace.

Now, I understand that there is a templating feature, but it doesn't seem to work for this:

# output omitted
env:
  - name: foo
    value: '{% .ObjectMeta.Name %}.{% .ObjectMeta.Namespace %}.rest.of.my.string'

Looking around the code it seems this is the culprit:

sidecarConfig, err := sidecarconfig.RenderTemplate(corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: pod.Annotations,
},
Spec: corev1.PodSpec{
ServiceAccountName: pod.Spec.ServiceAccountName,
},
}, whsvr.sidecarConfigTemplate)

It passes on a new pod object that only carries the original's Service Account and Annotations, but not the rest of the object (such as .ObjectMeta.Name and .ObjectMeta.Namespace.

Is there a particular reason not to pass the entire Pod that is already received on the admission review request?

// mutate method for mutation webhook
func (whsvr *WebhookServer) mutate(ar *v1beta1.AdmissionReview) (admissionResponse *v1beta1.AdmissionResponse, statusForMutations map[string]mutationStatus) {
	req := ar.Request
	var pod corev1.Pod
	if err := json.Unmarshal(req.Object.Raw, &pod); err != nil {
		glog.Errorf("api=mutate, reason=json.Unmarshal, message=invalid raw object, err=%v", err)
		return &v1beta1.AdmissionResponse{
			Result: &metav1.Status{
				Message: err.Error(),
			},
		}, nil
	}
        
// some lines later

        sidecarConfig, err := sidecarconfig.RenderTemplate(pod, whsvr.sidecarConfigTemplate)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions