Skip to content

Commit

Permalink
fix: use generate name in logs if pod name is empty (#706)
Browse files Browse the repository at this point in the history
Signed-off-by: Anish Ramasekar <[email protected]>
  • Loading branch information
aramase committed Jan 18, 2023
1 parent a915e40 commit 8372568
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func (m *podMutator) Handle(ctx context.Context, req admission.Request) (respons
return admission.Errored(http.StatusBadRequest, err)
}

podName := pod.GetName()
if podName == "" {
podName = pod.GetGenerateName() + " (prefix)"
}
// for daemonset/deployment pods the namespace field is not set in objectMeta
// explicitly set the namespace to request namespace
pod.Namespace = req.Namespace
Expand All @@ -110,7 +114,7 @@ func (m *podMutator) Handle(ctx context.Context, req admission.Request) (respons
serviceAccountName = "default"
}

logger := log.Log.WithName("handler").WithValues("pod", pod.Name, "namespace", pod.Namespace, "service-account", serviceAccountName)
logger := log.Log.WithName("handler").WithValues("pod", podName, "namespace", pod.Namespace, "service-account", serviceAccountName)
// get service account associated with the pod
serviceAccount := &corev1.ServiceAccount{}
if err = m.client.Get(ctx, types.NamespacedName{Name: serviceAccountName, Namespace: pod.Namespace}, serviceAccount); err != nil {
Expand Down

0 comments on commit 8372568

Please sign in to comment.