Skip to content

Commit

Permalink
Add pod and container name to log context (#291)
Browse files Browse the repository at this point in the history
* Add pod and container name to context

* Address reviews

* nit

* refactor
  • Loading branch information
SupriyaKasten committed Sep 17, 2019
1 parent 0812700 commit d9fcee5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
6 changes: 5 additions & 1 deletion pkg/function/backup_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

kanister "github.com/kanisterio/kanister/pkg"
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/consts"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/format"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/param"
Expand Down Expand Up @@ -106,6 +108,8 @@ func (*backupDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
if err = OptArg(args, BackupDataEncryptionKeyArg, &encryptionKey, restic.GeneratePassword()); err != nil {
return nil, err
}
ctx = field.Context(ctx, consts.PodNameKey, pod)

This comment has been minimized.

Copy link
@julio-lopez

julio-lopez Sep 18, 2019

Contributor

We added field.AddMapToContext(ctx, ...) specifically to simplify this use case.
Suppose that we add a type alias in the pkg/function package of the form

type M = field.M

Then, this could be simplified as:

ctx = field.AddMapToContext(ctx, M{consts.PodNameKey: pod, consts.ContainerNameKey: container})

or without the type alias:

ctx = field.AddMapToContext(ctx, field.M{consts.PodNameKey: pod, consts.ContainerNameKey: container})

Same in other calls in this package, and similarly in other packages.

ctx = field.Context(ctx, consts.ContainerNameKey, container)
// Validate the Profile
if err = validateProfile(tp.Profile); err != nil {
return nil, errors.Wrapf(err, "Failed to validate Profile")
Expand Down Expand Up @@ -170,7 +174,7 @@ func getPodWriter(cli kubernetes.Interface, ctx context.Context, namespace, podN
func cleanUpCredsFile(ctx context.Context, pw *kube.PodWriter, namespace, podName, containerName string) {
if pw != nil {
if err := pw.Remove(ctx, namespace, podName, containerName); err != nil {
log.Errorf("Could not delete the temp file")
log.WithContext(ctx).Error("Could not delete the temp file")
}
}
}
4 changes: 4 additions & 0 deletions pkg/function/backup_data_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"k8s.io/client-go/kubernetes"

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/consts"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/param"
"github.com/kanisterio/kanister/pkg/restic"
Expand Down Expand Up @@ -85,6 +87,7 @@ func (*backupDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, arg
if err = OptArg(args, BackupDataAllEncryptionKeyArg, &encryptionKey, restic.GeneratePassword()); err != nil {
return nil, err
}
ctx = field.Context(ctx, consts.ContainerNameKey, container)
// Validate the Profile
if err = validateProfile(tp.Profile); err != nil {
return nil, errors.Wrapf(err, "Failed to validate Profile")
Expand Down Expand Up @@ -121,6 +124,7 @@ func backupDataAll(ctx context.Context, cli kubernetes.Interface, namespace stri
// Run the command
for _, pod := range ps {
go func(pod string, container string) {
ctx = field.Context(ctx, consts.PodNameKey, pod)
backupID, backupTag, err := backupData(ctx, cli, namespace, pod, container, fmt.Sprintf("%s/%s", backupArtifactPrefix, pod), includePath, encryptionKey, tp)
errChan <- errors.Wrapf(err, "Failed to backup data for pod %s", pod)
outChan <- BackupInfo{PodName: pod, BackupID: backupID, BackupTag: backupTag}
Expand Down
5 changes: 4 additions & 1 deletion pkg/function/kube_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/pkg/errors"

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/consts"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/format"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/output"
Expand Down Expand Up @@ -89,7 +91,8 @@ func (kef *kubeExecFunc) Exec(ctx context.Context, tp param.TemplateParams, args
if err = Arg(args, KubeExecCommandArg, &cmd); err != nil {
return nil, err
}

ctx = field.Context(ctx, consts.PodNameKey, pod)
ctx = field.Context(ctx, consts.ContainerNameKey, container)
stdout, stderr, err := kube.Exec(cli, namespace, pod, container, cmd, nil)
format.Log(pod, container, stdout)
format.Log(pod, container, stderr)
Expand Down
8 changes: 6 additions & 2 deletions pkg/function/kube_exec_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"k8s.io/client-go/kubernetes"

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/consts"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/format"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/param"
Expand Down Expand Up @@ -69,21 +71,23 @@ func (*kubeExecAllFunc) Exec(ctx context.Context, tp param.TemplateParams, args
}
ps := strings.Fields(pods)
cs := strings.Fields(containers)
return execAll(cli, namespace, ps, cs, cmd)
return execAll(ctx, cli, namespace, ps, cs, cmd)
}

func (*kubeExecAllFunc) RequiredArgs() []string {
return []string{KubeExecAllNamespaceArg, KubeExecAllPodsNameArg, KubeExecAllContainersNameArg, KubeExecAllCommandArg}
}

func execAll(cli kubernetes.Interface, namespace string, ps []string, cs []string, cmd []string) (map[string]interface{}, error) {
func execAll(ctx context.Context, cli kubernetes.Interface, namespace string, ps []string, cs []string, cmd []string) (map[string]interface{}, error) {
numContainers := len(ps) * len(cs)
errChan := make(chan error, numContainers)
output := ""
// Run the command
for _, p := range ps {
for _, c := range cs {
go func(p string, c string) {
ctx = field.Context(ctx, consts.PodNameKey, p)
ctx = field.Context(ctx, consts.ContainerNameKey, c)
stdout, stderr, err := kube.Exec(cli, namespace, p, c, cmd, nil)
format.Log(p, c, stdout)
format.Log(p, c, stderr)
Expand Down
3 changes: 3 additions & 0 deletions pkg/function/kube_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"k8s.io/client-go/kubernetes"

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/consts"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/output"
"github.com/kanisterio/kanister/pkg/param"
Expand Down Expand Up @@ -76,6 +78,7 @@ func kubeTaskPodFunc(cli kubernetes.Interface) func(ctx context.Context, pod *v1
if err := kube.WaitForPodReady(ctx, cli, pod.Namespace, pod.Name); err != nil {
return nil, errors.Wrapf(err, "Failed while waiting for Pod %s to complete", pod.Name)
}
ctx = field.Context(ctx, consts.PodNameKey, pod.Name)
// Fetch logs from the pod
r, err := kube.StreamPodLogs(ctx, cli, pod.Namespace, pod.Name)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/kube/pod_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"

"github.com/kanisterio/kanister/pkg/consts"
"github.com/kanisterio/kanister/pkg/field"
)

// PodRunner specifies Kubernetes Client and PodOptions needed for creating Pod
Expand Down Expand Up @@ -48,6 +51,8 @@ func (p *PodRunner) Run(ctx context.Context, fn func(context.Context, *v1.Pod) (
if err != nil {
return nil, errors.Wrapf(err, "Failed to create pod")
}
ctx = field.Context(ctx, consts.PodNameKey, pod.Name)
ctx = field.Context(ctx, consts.ContainerNameKey, pod.Spec.Containers[0].Name)
go func() {
<-ctx.Done()
err := DeletePod(context.Background(), p.cli, pod)
Expand Down

0 comments on commit d9fcee5

Please sign in to comment.