Skip to content

Commit

Permalink
Change logrus calls in pkg/ and cmd/ to use log.Entry(ctx) (#…
Browse files Browse the repository at this point in the history
…6419)

* helper function and context setting

* update logrus calls to use log.Entry()

* fix imports in integration/

* revert unneeded changes

* revert more unnecessary changes

* fix wrapper_windows.go
  • Loading branch information
MarlonGamez authored Aug 16, 2021
1 parent 9535940 commit e26e868
Show file tree
Hide file tree
Showing 148 changed files with 729 additions and 642 deletions.
23 changes: 12 additions & 11 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation/prompt"
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/server"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/survey"
Expand Down Expand Up @@ -107,9 +108,9 @@ func NewSkaffoldCommand(out, errOut io.Writer) *cobra.Command {
// Print version
versionInfo := version.Get()
version.SetClient(opts.User)
logrus.Infof("Skaffold %+v", versionInfo)
log.Entry(context.Background()).Infof("Skaffold %+v", versionInfo)
if !isHouseKeepingMessagesAllowed(cmd) {
logrus.Debugf("Disable housekeeping messages for command explicitly")
log.Entry(context.Background()).Debug("Disable housekeeping messages for command explicitly")
return nil
}
s = survey.New(opts.GlobalConfig, opts.ConfigurationFile, opts.Command)
Expand All @@ -128,7 +129,7 @@ func NewSkaffoldCommand(out, errOut io.Writer) *cobra.Command {
select {
case msg := <-updateMsg:
if err := config.UpdateMsgDisplayed(opts.GlobalConfig); err != nil {
logrus.Debugf("could not update the 'last-prompted' config for 'update-config' section due to %s", err)
log.Entry(context.Background()).Debugf("could not update the 'last-prompted' config for 'update-config' section due to %s", err)
}
fmt.Fprintf(cmd.OutOrStderr(), "%s\n", msg)
default:
Expand Down Expand Up @@ -235,7 +236,7 @@ func setFlagsFromEnvVariables(rootCmd *cobra.Command) {
// special case for backward compatibility.
if f.Name == "namespace" {
if val, present := os.LookupEnv("SKAFFOLD_DEPLOY_NAMESPACE"); present {
logrus.Warnln("Using SKAFFOLD_DEPLOY_NAMESPACE env variable is deprecated. Please use SKAFFOLD_NAMESPACE instead.")
log.Entry(context.Background()).Warn("Using SKAFFOLD_DEPLOY_NAMESPACE env variable is deprecated. Please use SKAFFOLD_NAMESPACE instead.")
cmd.Flags().Set(f.Name, val)
}
}
Expand All @@ -262,7 +263,7 @@ func setUpLogs(stdErr io.Writer, level string, timestamp bool) error {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: timestamp,
})
logrus.AddHook(event.NewLogHook(constants.DevLoop, event.SubtaskIDNone))
logrus.AddHook(event.NewLogHook(constants.DevLoop, constants.SubtaskIDNone))
return nil
}

Expand Down Expand Up @@ -310,13 +311,13 @@ func preReleaseVersion(s string) bool {
func isQuietMode() bool {
switch {
case !interactive:
logrus.Debug("Update check prompt, survey prompt and telemetry prompt disabled in non-interactive mode")
log.Entry(context.Background()).Debug("Update check prompt, survey prompt and telemetry prompt disabled in non-interactive mode")
return true
case quietFlag:
logrus.Debug("Update check prompt, survey prompt and telemetry prompt disabled in quiet mode")
log.Entry(context.Background()).Debug("Update check prompt, survey prompt and telemetry prompt disabled in quiet mode")
return true
case analyze:
logrus.Debug("Update check prompt, survey prompt and telemetry prompt disabled when running `init --analyze`")
log.Entry(context.Background()).Debug("Update check prompt, survey prompt and telemetry prompt disabled when running `init --analyze`")
return true
default:
return false
Expand All @@ -334,16 +335,16 @@ func apiServerShutdownHook(err error) error {

func updateCheckForReleasedVersionsIfNotDisabled(s string) string {
if preReleaseVersion(s) {
logrus.Debug("Skipping update check for pre-release version")
log.Entry(context.Background()).Debug("Skipping update check for pre-release version")
return ""
}
if !update.EnableCheck {
logrus.Debug("Skipping update check for flag `--update-check` set to false")
log.Entry(context.Background()).Debug("Skipping update check for flag `--update-check` set to false")
return ""
}
msg, err := updateCheck(opts.GlobalConfig)
if err != nil {
logrus.Infof("update check failed: %s", err)
log.Entry(context.Background()).Infof("update check failed: %s", err)
}
return msg
}
12 changes: 6 additions & 6 deletions cmd/skaffold/app/cmd/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ limitations under the License.
package config

import (
"context"
"fmt"

"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
kctx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

Expand All @@ -31,13 +31,13 @@ func resolveKubectlContext() {
return
}

config, err := context.CurrentConfig()
config, err := kctx.CurrentConfig()
switch {
case err != nil:
logrus.Warn("unable to retrieve current kubectl context, using global values")
log.Entry(context.Background()).Warn("unable to retrieve current kubectl context, using global values")
global = true
case config.CurrentContext == "":
logrus.Infof("no kubectl context currently set, using global values")
log.Entry(context.Background()).Info("no kubectl context currently set, using global values")
global = true
default:
kubecontext = config.CurrentContext
Expand Down
6 changes: 3 additions & 3 deletions cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"errors"
"io"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
Expand Down Expand Up @@ -73,15 +73,15 @@ func runDev(ctx context.Context, out io.Writer) error {
if r.HasDeployed() {
cleanup = func() {
if err := r.Cleanup(context.Background(), out); err != nil {
logrus.Warnln("deployer cleanup:", err)
log.Entry(ctx).Warn("deployer cleanup:", err)
}
}
}

if r.HasBuilt() {
prune = func() {
if err := r.Prune(context.Background(), out); err != nil {
logrus.Warnln("builder cleanup:", err)
log.Entry(ctx).Warn("builder cleanup:", err)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/skaffold/app/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ limitations under the License.
package cmd

import (
"context"
"fmt"
"reflect"
"time"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/flags"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
)

var (
Expand Down Expand Up @@ -660,7 +661,7 @@ func setDefaultValues(v interface{}, fl *Flag, cmdName string) {
} else if val, ok := v.(pflag.Value); ok {
val.Set(fmt.Sprintf("%v", d))
} else {
logrus.Fatalf("%s --%s: unhandled value type: %v (%T)", cmdName, fl.Name, v, v)
log.Entry(context.Background()).Fatalf("%s --%s: unhandled value type: %v (%T)", cmdName, fl.Name, v, v)
}
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/skaffold/app/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"fmt"
"io"

"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
Expand All @@ -32,6 +30,7 @@ import (
initConfig "github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
Expand Down Expand Up @@ -153,10 +152,10 @@ func setDefaultDeployer(configs parser.SkaffoldConfigSet) {
func warnIfUpdateIsAvailable() {
warning, err := update.CheckVersionOnError(opts.GlobalConfig)
if err != nil {
logrus.Infof("update check failed: %s", err)
log.Entry(context.Background()).Infof("update check failed: %s", err)
return
}
if warning != "" {
logrus.Warn(warning)
log.Entry(context.Background()).Warn(warning)
}
}
4 changes: 2 additions & 2 deletions cmd/skaffold/app/skaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"os"

shell "github.com/kballard/go-shellquote"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
)

func Run(out, stderr io.Writer) error {
Expand All @@ -41,7 +41,7 @@ func Run(out, stderr io.Writer) error {
return fmt.Errorf("SKAFFOLD_CMDLINE is invalid: %w", err)
}
// XXX logged before logrus.SetLevel is called in NewSkaffoldCommand's PersistentPreRunE
logrus.Debugf("Retrieving command line from SKAFFOLD_CMDLINE: %q", parsed)
log.Entry(ctx).Debugf("Retrieving command line from SKAFFOLD_CMDLINE: %q", parsed)
c.SetArgs(parsed)
}
err := c.ExecuteContext(ctx)
Expand Down
7 changes: 3 additions & 4 deletions cmd/skaffold/skaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ package main
import (
"context"
"errors"
"log"
"os"

"cloud.google.com/go/profiler"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
)

Expand All @@ -44,13 +43,13 @@ func main() {
ServiceVersion: version.Get().Version,
})
if err != nil {
log.Fatalf("failed to start the profiler: %v", err)
log.Entry(context.Background()).Fatalf("failed to start the profiler: %v", err)
}
}
var code int
if err := app.Run(os.Stdout, os.Stderr); err != nil {
if errors.Is(err, context.Canceled) {
logrus.Debugln("ignore error since context is cancelled:", err)
log.Entry(context.Background()).Debugln("ignore error since context is cancelled:", err)
} else {
// As we allow some color setup using CLI flags for the main run, we can't run SetupColors()
// for the entire skaffold run here. It's possible SetupColors() was never called, so call it again
Expand Down
2 changes: 1 addition & 1 deletion hack/versions/pkg/schema/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func RunSchemaCheckOnChangedFiles() error {
continue
}

logrus.Warnf("Detected changes to the latest config. Checking on Github if it's released...")
logrus.Warn("Detected changes to the latest config. Checking on Github if it's released...")
latestVersion, isReleased := GetLatestVersion()
if !isReleased {
logrus.Infof("Schema %q is not yet released. Changes are ok.", latestVersion)
Expand Down
22 changes: 11 additions & 11 deletions pkg/diag/validator/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"regexp"
"strings"

"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -33,6 +32,7 @@ import (
deploymentutil "k8s.io/kubectl/pkg/util/deployment"

"github.com/GoogleContainerTools/skaffold/pkg/diag/recommender"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/proto/v1"
)

Expand Down Expand Up @@ -89,10 +89,10 @@ func NewPodValidator(k kubernetes.Interface, d appsv1.Deployment) *PodValidator
func (p *PodValidator) Validate(ctx context.Context, ns string, opts metav1.ListOptions) ([]Resource, error) {
_, _, controller, err := getReplicaSet(&p.depObj, p.k.AppsV1())
if err != nil {
logrus.Debugf("could not fetch deployment replica set %s", err)
log.Entry(ctx).Debugf("could not fetch deployment replica set %s", err)
return []Resource{}, err
} else if controller == nil {
logrus.Debugf("deployment replica set not created yet.")
log.Entry(ctx).Debugf("deployment replica set not created yet.")
return []Resource{}, nil
}

Expand Down Expand Up @@ -144,14 +144,14 @@ func getPodStatus(pod *v1.Pod) (proto.StatusCode, []string, error) {
}
// If the event type PodScheduled with status False is found then we check if it is due to taints and tolerations.
if c, ok := isPodNotScheduled(pod); ok {
logrus.Debugf("Pod %q not scheduled: checking tolerations", pod.Name)
log.Entry(context.Background()).Debugf("Pod %q not scheduled: checking tolerations", pod.Name)
sc, err := getUntoleratedTaints(c.Reason, c.Message)
return sc, nil, err
}
// we can check the container status if the pod has been scheduled successfully. This can be determined by having the event
// PodScheduled with status True, or a ContainerReady or PodReady event with status False.
if isPodScheduledButNotReady(pod) {
logrus.Debugf("Pod %q scheduled but not ready: checking container statuses", pod.Name)
log.Entry(context.Background()).Debugf("Pod %q scheduled but not ready: checking container statuses", pod.Name)
// TODO(dgageot): Add EphemeralContainerStatuses
cs := append(pod.Status.InitContainerStatuses, pod.Status.ContainerStatuses...)
// See https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-states
Expand All @@ -171,11 +171,11 @@ func getPodStatus(pod *v1.Pod) (proto.StatusCode, []string, error) {
}

if c, ok := isPodStatusUnknown(pod); ok {
logrus.Debugf("Pod %q condition status of type %s is unknown", pod.Name, c.Type)
log.Entry(context.Background()).Debugf("Pod %q condition status of type %s is unknown", pod.Name, c.Type)
return proto.StatusCode_STATUSCHECK_UNKNOWN, nil, fmt.Errorf(c.Message)
}

logrus.Debugf("Unable to determine current service state of pod %q", pod.Name)
log.Entry(context.Background()).Debugf("Unable to determine current service state of pod %q", pod.Name)
return proto.StatusCode_STATUSCHECK_UNKNOWN, nil, fmt.Errorf("unable to determine current service state of pod %q", pod.Name)
}

Expand Down Expand Up @@ -288,13 +288,13 @@ func processPodEvents(e corev1.EventInterface, pod v1.Pod, ps *podStatus) {
if _, ok := unknownConditionsOrSuccess[ps.ae.ErrCode]; !ok {
return
}
logrus.Debugf("Fetching events for pod %q", pod.Name)
log.Entry(context.Background()).Debugf("Fetching events for pod %q", pod.Name)
// Get pod events.
scheme := runtime.NewScheme()
scheme.AddKnownTypes(v1.SchemeGroupVersion, &pod)
events, err := e.Search(scheme, &pod)
if err != nil {
logrus.Debugf("Could not fetch events for resource %q due to %v", pod.Name, err)
log.Entry(context.Background()).Debugf("Could not fetch events for resource %q due to %v", pod.Name, err)
return
}
// find the latest failed event.
Expand Down Expand Up @@ -385,7 +385,7 @@ func extractErrorMessageFromWaitingContainerStatus(po *v1.Pod, c v1.ContainerSta
return proto.StatusCode_STATUSCHECK_RUN_CONTAINER_ERR, nil, fmt.Errorf("container %s in error: %s", c.Name, trimSpace(match[3]))
}
}
logrus.Debugf("Unknown waiting reason for container %q: %v", c.Name, c.State)
log.Entry(context.Background()).Debugf("Unknown waiting reason for container %q: %v", c.Name, c.State)
return proto.StatusCode_STATUSCHECK_CONTAINER_WAITING_UNKNOWN, nil, fmt.Errorf("container %s in error: %v", c.Name, c.State.Waiting)
}

Expand All @@ -405,7 +405,7 @@ func trimSpace(msg string) string {
}

func getPodLogs(po *v1.Pod, c string, sc proto.StatusCode) (proto.StatusCode, []string) {
logrus.Debugf("Fetching logs for container %s/%s", po.Name, c)
log.Entry(context.Background()).Debugf("Fetching logs for container %s/%s", po.Name, c)
logCommand := []string{"kubectl", "logs", po.Name, "-n", po.Namespace, "-c", c}
logs, err := runCli(logCommand[0], logCommand[1:])
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions pkg/skaffold/build/bazel/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import (
"sync"
"time"

"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)
Expand All @@ -49,7 +48,7 @@ func GetDependencies(ctx context.Context, dir string, a *latestV1.BazelArtifact)

go func() {
<-timer.C
once.Do(func() { logrus.Warnln("Retrieving Bazel dependencies can take a long time the first time") })
once.Do(func() { log.Entry(ctx).Warn("Retrieving Bazel dependencies can take a long time the first time") })
}()

topLevelFolder, err := findWorkspace(dir)
Expand Down Expand Up @@ -95,7 +94,7 @@ func GetDependencies(ctx context.Context, dir string, a *latestV1.BazelArtifact)
}
deps = append(deps, rel)

logrus.Debugf("Found dependencies for bazel artifact: %v", deps)
log.Entry(ctx).Debugf("Found dependencies for bazel artifact: %v", deps)

return deps, nil
}
Expand Down
Loading

0 comments on commit e26e868

Please sign in to comment.