Skip to content

Commit

Permalink
Dump redacted Pod and PodOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
e-sumin committed Nov 14, 2023
1 parent 4759dde commit 083430a
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion pkg/kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func CreatePod(ctx context.Context, cli kubernetes.Interface, opts *PodOptions)

pod, err = cli.CoreV1().Pods(pod.Namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
log.Error().WithContext(ctx).WithError(err).Print("Failed to create pod.", field.M{"pod": pod, "options": opts})
log.Error().WithContext(ctx).WithError(err).Print("Failed to create pod.", field.M{"pod": getRedactedPod(pod), "options": getRedactedOptions(opts)})
return nil, errors.Wrapf(err, "Failed to create pod. Namespace: %s, NameFmt: %s", opts.Namespace, opts.GenerateName)
}
return pod, nil
Expand Down Expand Up @@ -489,3 +489,59 @@ func GetPodReadyWaitTimeout() time.Duration {

return DefaultPodReadyWaitTimeout
}

// getRedactedEnvVariables returns array of variables with removed values
func getRedactedEnvVariables(env []v1.EnvVar) []v1.EnvVar {
if len(env) == 0 {
return nil
}

result := make([]v1.EnvVar, len(env))
for i, ev := range env {
result[i] = v1.EnvVar{
Name: ev.Name,
Value: "XXXXX",
}
}

return result
}

// getRedactedPod hides all values of env variables from pod, so that it should be safely logged
func getRedactedPod(pod *v1.Pod) *v1.Pod {
if pod == nil {
return nil
}

result := *pod // Make shallow copy

getSanitizedContainers := func(containers []v1.Container) []v1.Container {
if len(containers) == 0 {
return nil
}

result := make([]v1.Container, len(containers))
for i, c := range containers {
result[i] = c
result[i].Env = getRedactedEnvVariables(c.Env)
}
return result
}

result.Spec.Containers = getSanitizedContainers(result.Spec.Containers)
result.Spec.InitContainers = getSanitizedContainers(result.Spec.InitContainers)

return &result
}

// getRedactedOptions hides all values of env variables from pod options, so that they should be safely logged
func getRedactedOptions(opts *PodOptions) *PodOptions {
if opts == nil {
return nil
}

result := *opts // Make shallow copy

result.EnvironmentVariables = getRedactedEnvVariables(result.EnvironmentVariables)
return &result
}

0 comments on commit 083430a

Please sign in to comment.