This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/master by this push: new 0d3c881 Use consistent logging style #362 0d3c881 is described below commit 0d3c88164bec8d0f8e75041b5bd2a579fdf09fd3 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Wed Jan 23 15:38:23 2019 +0100 Use consistent logging style #362 --- Gopkg.lock | 1 + pkg/builder/builder.go | 33 ++-- pkg/cmd/log.go | 4 +- pkg/cmd/run.go | 7 +- pkg/controller/integration/action.go | 9 ++ pkg/controller/integration/build_context.go | 5 +- pkg/controller/integration/build_image.go | 14 +- .../integration/build_image_failure_recovery.go | 7 +- pkg/controller/integration/deploy.go | 4 +- pkg/controller/integration/initialize.go | 9 +- .../integration/integration_controller.go | 17 +- pkg/controller/integration/{action.go => log.go} | 30 +--- pkg/controller/integration/monitor.go | 5 +- pkg/controller/integrationcontext/action.go | 9 ++ pkg/controller/integrationcontext/build.go | 16 +- .../integrationcontext/build_failure_recovery.go | 7 +- pkg/controller/integrationcontext/initialize.go | 5 +- .../integrationcontext_controller.go | 17 +- .../integrationcontext/log.go} | 26 +-- pkg/controller/integrationcontext/monitor.go | 5 +- pkg/controller/integrationplatform/action.go | 9 ++ pkg/controller/integrationplatform/create.go | 10 +- pkg/controller/integrationplatform/initialize.go | 10 +- .../integrationplatform_controller.go | 17 +- .../integrationplatform/log.go} | 26 +-- pkg/controller/integrationplatform/start.go | 5 +- pkg/metadata/metadata.go | 4 +- pkg/trait/catalog.go | 6 +- pkg/trait/knative_service.go | 3 +- pkg/trait/types.go | 10 ++ .../{ => kubernetes}/log/annotation_scraper.go | 16 +- pkg/util/{ => kubernetes}/log/pod_scraper.go | 14 +- pkg/util/{ => kubernetes}/log/util.go | 0 pkg/util/log/log.go | 179 +++++++++++++++++++++ pkg/util/maven/maven.go | 14 +- pkg/util/openshift/register.go | 4 +- pkg/util/sync/file.go | 4 +- pkg/util/watch/watch.go | 6 +- test/log_scrape_integration_test.go | 2 +- 39 files changed, 368 insertions(+), 201 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index a7bbf83..e063b62 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1056,6 +1056,7 @@ input-imports = [ "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1", "github.com/fatih/structs", + "github.com/go-logr/logr", "github.com/jpillora/backoff", "github.com/knative/eventing/pkg/apis/eventing/v1alpha1", "github.com/knative/serving/pkg/apis/serving/v1alpha1", diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go index d9ec9e3..25d18d6 100644 --- a/pkg/builder/builder.go +++ b/pkg/builder/builder.go @@ -20,6 +20,7 @@ package builder import ( "context" "errors" + "fmt" "io/ioutil" "os" "sort" @@ -31,7 +32,7 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" - "github.com/sirupsen/logrus" + "github.com/apache/camel-k/pkg/util/log" ) // ******************************** @@ -46,7 +47,7 @@ type buildTask struct { } type defaultBuilder struct { - log *logrus.Entry + log log.Logger ctx context.Context client client.Client tasks chan buildTask @@ -59,7 +60,7 @@ type defaultBuilder struct { // New -- func New(ctx context.Context, c client.Client, namespace string) Builder { m := defaultBuilder{ - log: logrus.WithField("logger", "builder"), + log: log.WithName("builder"), ctx: ctx, client: c, tasks: make(chan buildTask), @@ -134,7 +135,18 @@ func (b *defaultBuilder) loop() { func (b *defaultBuilder) process(request Request, handler func(*Result)) { result, present := b.request.Load(request.Meta.Name) if !present || result == nil { - b.log.Panicf("no info found for: %+v", request.Meta.Name) + + r := result.(Result) + r.Status = StatusError + r.Error = fmt.Errorf("no info found for: %s/%s", request.Meta.Namespace, request.Meta.Name) + + b.log.Error(r.Error, "error processing request") + + if handler != nil { + handler(&r) + } + + return } // update the status @@ -153,7 +165,8 @@ func (b *defaultBuilder) process(request Request, handler func(*Result)) { } builderPath, err := ioutil.TempDir(buildDir, "builder-") if err != nil { - logrus.Warning("Unexpected error while creating a temporary dir ", err) + log.Error(err, "Unexpected error while creating a temporary dir") + r.Status = StatusError r.Error = err } @@ -216,11 +229,11 @@ func (b *defaultBuilder) process(request Request, handler func(*Result)) { case <-request.C.Done(): r.Status = StatusInterrupted default: - l := b.log.WithFields(logrus.Fields{ - "step": step.ID(), - "phase": step.Phase(), - "context": request.Meta.Name, - }) + l := b.log.WithValues( + "step", step.ID(), + "phase", step.Phase(), + "context", request.Meta.Name, + ) l.Infof("executing step") diff --git a/pkg/cmd/log.go b/pkg/cmd/log.go index b847c49..7a285f1 100644 --- a/pkg/cmd/log.go +++ b/pkg/cmd/log.go @@ -21,7 +21,7 @@ import ( "fmt" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/apache/camel-k/pkg/util/log" + k8slog "github.com/apache/camel-k/pkg/util/kubernetes/log" "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -81,7 +81,7 @@ func (o *logCmdOptions) run(cmd *cobra.Command, args []string) error { if err := c.Get(o.Context, key, &integration); err != nil { return err } - if err := log.Print(o.Context, c, &integration); err != nil { + if err := k8slog.Print(o.Context, c, &integration); err != nil { return err } diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index b4c34a2..9c83fda 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -37,11 +37,10 @@ import ( "github.com/apache/camel-k/pkg/trait" "github.com/apache/camel-k/pkg/util" "github.com/apache/camel-k/pkg/util/kubernetes" - "github.com/apache/camel-k/pkg/util/log" + k8slog "github.com/apache/camel-k/pkg/util/kubernetes/log" "github.com/apache/camel-k/pkg/util/sync" "github.com/apache/camel-k/pkg/util/watch" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -192,7 +191,7 @@ func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error { } } if o.Logs || o.Dev { - err = log.Print(o.Context, c, integration) + err = k8slog.Print(o.Context, c, integration) if err != nil { return err } @@ -244,7 +243,7 @@ func (o *runCmdOptions) syncIntegration(c client.Client, sources []string) error case <-changes: _, err := o.updateIntegrationCode(c, sources) if err != nil { - logrus.Error("Unable to sync integration: ", err) + fmt.Println("Unable to sync integration: ", err.Error()) } } } diff --git a/pkg/controller/integration/action.go b/pkg/controller/integration/action.go index eebadef..1fb2a02 100644 --- a/pkg/controller/integration/action.go +++ b/pkg/controller/integration/action.go @@ -22,6 +22,7 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" + "github.com/apache/camel-k/pkg/util/log" ) // Action -- @@ -36,12 +37,20 @@ type Action interface { // executes the handling function Handle(ctx context.Context, integration *v1alpha1.Integration) error + + // Inject integration logger + InjectLogger(log.Logger) } type baseAction struct { client client.Client + L log.Logger } func (action *baseAction) InjectClient(client client.Client) { action.client = client } + +func (action *baseAction) InjectLogger(log log.Logger) { + action.L = log +} diff --git a/pkg/controller/integration/build_context.go b/pkg/controller/integration/build_context.go index c78c588..c7f0b0f 100644 --- a/pkg/controller/integration/build_context.go +++ b/pkg/controller/integration/build_context.go @@ -26,7 +26,6 @@ import ( "github.com/apache/camel-k/pkg/util" "github.com/apache/camel-k/pkg/util/digest" "github.com/rs/xid" - "github.com/sirupsen/logrus" ) // NewBuildContextAction create an action that handles integration context build @@ -81,7 +80,7 @@ func (action *buildContextAction) Handle(ctx context.Context, integration *v1alp return err } - logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase) + action.L.Info("Integration state transition", "phase", target.Status.Phase) return action.client.Status().Update(ctx, target) } @@ -102,7 +101,7 @@ func (action *buildContextAction) Handle(ctx context.Context, integration *v1alp return err } - logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase) + action.L.Info("Integration state transition", "phase", target.Status.Phase) return action.client.Status().Update(ctx, target) } diff --git a/pkg/controller/integration/build_image.go b/pkg/controller/integration/build_image.go index 6145259..1d4270a 100644 --- a/pkg/controller/integration/build_image.go +++ b/pkg/controller/integration/build_image.go @@ -33,8 +33,6 @@ import ( "github.com/apache/camel-k/pkg/trait" "github.com/apache/camel-k/pkg/util/digest" "github.com/apache/camel-k/pkg/util/kubernetes" - "github.com/sirupsen/logrus" - corev1 "k8s.io/api/core/v1" ) @@ -80,7 +78,7 @@ func (action *buildImageAction) handleBuildImageRunning(ctx context.Context, int } if b.IsBuilding(integration.ObjectMeta) { - logrus.Infof("Build for integration %s is running", integration.Name) + action.L.Info("Build running") } return nil @@ -139,7 +137,7 @@ func (action *buildImageAction) handleBuildImageSubmitted(ctx context.Context, i // this function is invoked synchronously for every state change // if err := action.handleBuildStateChange(result.Request.C, result); err != nil { - logrus.Warnf("Error while building integration image %s, reason: %s", ictx.Name, err.Error()) + action.L.Error(err, "Error while building integration image") } }) } @@ -158,11 +156,11 @@ func (action *buildImageAction) handleBuildStateChange(ctx context.Context, res switch res.Status { case builder.StatusSubmitted: - logrus.Info("Build submitted") + action.L.Info("Build submitted") case builder.StatusStarted: target.Status.Phase = v1alpha1.IntegrationPhaseBuildImageRunning - logrus.Infof("Integration %s transitioning to state %s", target.Name, target.Status.Phase) + action.L.Info("Integration state transition", "phase", target.Status.Phase) return action.client.Status().Update(ctx, target) case builder.StatusError: @@ -179,7 +177,7 @@ func (action *buildImageAction) handleBuildStateChange(ctx context.Context, res } } - logrus.Infof("Integration %s transitioning to state %s, reason: %s", target.Name, target.Status.Phase, res.Error.Error()) + action.L.Error(res.Error, "Integration state transition", "phase", target.Status.Phase) return action.client.Status().Update(ctx, target) case builder.StatusCompleted: @@ -197,7 +195,7 @@ func (action *buildImageAction) handleBuildStateChange(ctx context.Context, res target.Status.Digest = dgst - logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase) + action.L.Info("Integration state transition", "phase", target.Status.Phase) if err := action.client.Status().Update(ctx, target); err != nil { return err diff --git a/pkg/controller/integration/build_image_failure_recovery.go b/pkg/controller/integration/build_image_failure_recovery.go index 0accf10..235eae0 100644 --- a/pkg/controller/integration/build_image_failure_recovery.go +++ b/pkg/controller/integration/build_image_failure_recovery.go @@ -25,7 +25,6 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/platform" - "github.com/sirupsen/logrus" ) // NewErrorRecoveryAction creates a new error recovering handling action for the integration @@ -58,7 +57,7 @@ func (action *errorRecoveryAction) Handle(ctx context.Context, integration *v1al // The integration platform needs to be initialized before starting handle // context in status error if _, err := platform.GetCurrentPlatform(ctx, action.client, integration.Namespace); err != nil { - logrus.Info("Waiting for a integration platform to be initialized") + action.L.Info("Waiting for a integration platform to be initialized") return nil } @@ -66,7 +65,7 @@ func (action *errorRecoveryAction) Handle(ctx context.Context, integration *v1al target := integration.DeepCopy() target.Status.Phase = v1alpha1.IntegrationPhaseError - logrus.Infof("Max recovery attempt reached for integration %s, transition to phase %s", integration.Name, string(target.Status.Phase)) + action.L.Info("Max recovery attempt reached, transition to error phase") return action.client.Status().Update(ctx, target) } @@ -91,7 +90,7 @@ func (action *errorRecoveryAction) Handle(ctx context.Context, integration *v1al target.Status.Failure.Recovery.Attempt = integration.Status.Failure.Recovery.Attempt + 1 target.Status.Failure.Recovery.AttemptTime = time.Now() - logrus.Infof("Recovery attempt for integration %s (%d/%d)", + action.L.Info("Recovery attempt (%d/%d)", integration.Name, target.Status.Failure.Recovery.Attempt, target.Status.Failure.Recovery.AttemptMax, diff --git a/pkg/controller/integration/deploy.go b/pkg/controller/integration/deploy.go index f53c37e..7329e27 100644 --- a/pkg/controller/integration/deploy.go +++ b/pkg/controller/integration/deploy.go @@ -24,7 +24,6 @@ import ( "github.com/apache/camel-k/pkg/trait" "github.com/apache/camel-k/pkg/util/kubernetes" "github.com/pkg/errors" - "github.com/sirupsen/logrus" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -73,7 +72,8 @@ func (action *deployAction) Handle(ctx context.Context, integration *v1alpha1.In target := integration.DeepCopy() target.Status.Phase = v1alpha1.IntegrationPhaseRunning - logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase) + + action.L.Info("Integration state transition", "phase", target.Status.Phase) return action.client.Status().Update(ctx, target) } diff --git a/pkg/controller/integration/initialize.go b/pkg/controller/integration/initialize.go index 1ec852c..ff89589 100644 --- a/pkg/controller/integration/initialize.go +++ b/pkg/controller/integration/initialize.go @@ -26,10 +26,9 @@ import ( "github.com/apache/camel-k/pkg/platform" "github.com/apache/camel-k/pkg/trait" "github.com/apache/camel-k/pkg/util/digest" - "github.com/sirupsen/logrus" ) -// NewInitializeAction creates a new inititialize action +// NewInitializeAction creates a new initialize action func NewInitializeAction() Action { return &initializeAction{} } @@ -54,13 +53,13 @@ func (action *initializeAction) Handle(ctx context.Context, integration *v1alpha // The integration platform needs to be ready before starting to create integrations if err != nil || pl.Status.Phase != v1alpha1.IntegrationPlatformPhaseReady { - logrus.Info("Waiting for a integration platform to be ready") + action.L.Info("Waiting for the integration platform to be initialized") if integration.Status.Phase != v1alpha1.IntegrationPhaseWaitingForPlatform { target := integration.DeepCopy() target.Status.Phase = v1alpha1.IntegrationPhaseWaitingForPlatform - logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase) + action.L.Info("Integration state transition", "phase", target.Status.Phase) return action.client.Status().Update(ctx, target) } @@ -107,7 +106,7 @@ func (action *initializeAction) Handle(ctx context.Context, integration *v1alpha target.Status.Context = integration.Spec.Context target.Status.Image = "" - logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase) + action.L.Info("Integration state transition", "phase", target.Status.Phase) return action.client.Status().Update(ctx, target) } diff --git a/pkg/controller/integration/integration_controller.go b/pkg/controller/integration/integration_controller.go index 80760da..06550b2 100644 --- a/pkg/controller/integration/integration_controller.go +++ b/pkg/controller/integration/integration_controller.go @@ -6,7 +6,6 @@ import ( camelv1alpha1 "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" - "github.com/sirupsen/logrus" appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" @@ -14,12 +13,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" - logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" "sigs.k8s.io/controller-runtime/pkg/source" ) -var log = logf.Log.WithName("controller_integration") - /** * USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller * business logic. Delete these comments after modifying this file.* @@ -37,7 +33,10 @@ func Add(mgr manager.Manager) error { // newReconciler returns a new reconcile.Reconciler func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler { - return &ReconcileIntegration{client: c, scheme: mgr.GetScheme()} + return &ReconcileIntegration{ + client: c, + scheme: mgr.GetScheme(), + } } // add adds a new Controller to mgr with r as the reconcile.Reconciler @@ -82,8 +81,8 @@ type ReconcileIntegration struct { // The Controller will requeue the Request to be processed again if the returned error is non-nil or // Result.Requeue is true, otherwise upon completion it will remove the work from the queue. func (r *ReconcileIntegration) Reconcile(request reconcile.Request) (reconcile.Result, error) { - reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) - reqLogger.Info("Reconciling Integration") + rlog := Log.WithValues("request-namespace", request.Namespace, "request-name", request.Name) + rlog.Info("Reconciling Integration") ctx := context.TODO() @@ -110,10 +109,12 @@ func (r *ReconcileIntegration) Reconcile(request reconcile.Request) (reconcile.R NewMonitorAction(), } + ilog := rlog.ForIntegration(instance) for _, a := range integrationActionPool { a.InjectClient(r.client) + a.InjectLogger(ilog) if a.CanHandle(instance) { - logrus.Debug("Invoking action ", a.Name(), " on integration ", instance.Name) + ilog.Infof("Invoking action %s", a.Name()) if err := a.Handle(ctx, instance); err != nil { return reconcile.Result{}, err } diff --git a/pkg/controller/integration/action.go b/pkg/controller/integration/log.go similarity index 57% copy from pkg/controller/integration/action.go copy to pkg/controller/integration/log.go index eebadef..621d233 100644 --- a/pkg/controller/integration/action.go +++ b/pkg/controller/integration/log.go @@ -17,31 +17,7 @@ limitations under the License. package integration -import ( - "context" +import "github.com/apache/camel-k/pkg/util/log" - "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/apache/camel-k/pkg/client" -) - -// Action -- -type Action interface { - client.Injectable - - // a user friendly name for the action - Name() string - - // returns true if the action can handle the integration - CanHandle(integration *v1alpha1.Integration) bool - - // executes the handling function - Handle(ctx context.Context, integration *v1alpha1.Integration) error -} - -type baseAction struct { - client client.Client -} - -func (action *baseAction) InjectClient(client client.Client) { - action.client = client -} +// Log -- +var Log = log.Log.WithName("controller").WithName("integration") diff --git a/pkg/controller/integration/monitor.go b/pkg/controller/integration/monitor.go index 8ef911b..6a630da 100644 --- a/pkg/controller/integration/monitor.go +++ b/pkg/controller/integration/monitor.go @@ -22,7 +22,6 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/util/digest" - "github.com/sirupsen/logrus" ) // NewMonitorAction creates a new monitoring action for an integration @@ -51,13 +50,13 @@ func (action *monitorAction) Handle(ctx context.Context, integration *v1alpha1.I } if hash != integration.Status.Digest { - logrus.Info("Integration ", integration.Name, " needs a rebuild") + action.L.Info("Integration needs a rebuild") target := integration.DeepCopy() target.Status.Digest = hash target.Status.Phase = "" - logrus.Info("Integration ", target.Name, " transitioning to state ", target.Status.Phase) + action.L.Info("Integration state transition", "phase", target.Status.Phase) return action.client.Status().Update(ctx, target) } diff --git a/pkg/controller/integrationcontext/action.go b/pkg/controller/integrationcontext/action.go index 6d1369a..7c74194 100644 --- a/pkg/controller/integrationcontext/action.go +++ b/pkg/controller/integrationcontext/action.go @@ -22,6 +22,7 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" + "github.com/apache/camel-k/pkg/util/log" ) // Action -- @@ -36,12 +37,20 @@ type Action interface { // executes the handling function Handle(ctx context.Context, integration *v1alpha1.IntegrationContext) error + + // Inject integration logger + InjectLogger(log.Logger) } type baseAction struct { client client.Client + L log.Logger } func (action *baseAction) InjectClient(client client.Client) { action.client = client } + +func (action *baseAction) InjectLogger(log log.Logger) { + action.L = log +} diff --git a/pkg/controller/integrationcontext/build.go b/pkg/controller/integrationcontext/build.go index bbc6d29..0ec16b3 100644 --- a/pkg/controller/integrationcontext/build.go +++ b/pkg/controller/integrationcontext/build.go @@ -30,8 +30,6 @@ import ( "github.com/apache/camel-k/pkg/builder" "github.com/apache/camel-k/pkg/platform" - "github.com/sirupsen/logrus" - k8sclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -77,7 +75,7 @@ func (action *buildAction) handleBuildRunning(ctx context.Context, ictx *v1alpha } if b.IsBuilding(ictx.ObjectMeta) { - logrus.Infof("Build for context %s is running", ictx.Name) + action.L.Info("Build running") } return nil @@ -124,7 +122,7 @@ func (action *buildAction) handleBuildSubmitted(ctx context.Context, ictx *v1alp // for a compatible/base image // if err := action.handleBuildStateChange(result.Request.C, result); err != nil { - logrus.Warnf("Error while building context %s, reason: %s", ictx.Name, err.Error()) + action.L.Error(err, "Error while building integration context") } }) } @@ -143,11 +141,11 @@ func (action *buildAction) handleBuildStateChange(ctx context.Context, res *buil switch res.Status { case builder.StatusSubmitted: - logrus.Infof("Build submitted for IntegrationContext %s", target.Name) + action.L.Info("Build submitted") case builder.StatusStarted: target.Status.Phase = v1alpha1.IntegrationContextPhaseBuildRunning - logrus.Infof("Context %s transitioning to state %s", target.Name, target.Status.Phase) + action.L.Info("IntegrationContext state transition", "phase", target.Status.Phase) return action.client.Update(ctx, target) case builder.StatusError: @@ -179,7 +177,7 @@ func (action *buildAction) handleBuildStateChange(ctx context.Context, res *buil } } - logrus.Infof("Context %s transitioning to state %s, reason: %s", target.Name, target.Status.Phase, res.Error.Error()) + action.L.Error(res.Error, "Context state transition", "phase", target.Status.Phase) return action.client.Update(ctx, target) case builder.StatusCompleted: @@ -212,12 +210,12 @@ func (action *buildAction) handleBuildStateChange(ctx context.Context, res *buil }) } - logrus.Info("Context ", target.Name, " transitioning to state ", target.Status.Phase) + action.L.Info("IntegrationContext state transition", "phase", target.Status.Phase) if err := action.client.Update(ctx, target); err != nil { return err } - logrus.Infof("Inform integrations about context %s state change", target.Name) + action.L.Info("Inform integrations about context state change") if err := action.informIntegrations(ctx, target); err != nil { return err } diff --git a/pkg/controller/integrationcontext/build_failure_recovery.go b/pkg/controller/integrationcontext/build_failure_recovery.go index 7b1cd54..162a028 100644 --- a/pkg/controller/integrationcontext/build_failure_recovery.go +++ b/pkg/controller/integrationcontext/build_failure_recovery.go @@ -25,7 +25,6 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/platform" - "github.com/sirupsen/logrus" ) // NewErrorRecoveryAction creates a new error recovering handling action for the context @@ -58,7 +57,7 @@ func (action *errorRecoveryAction) Handle(ctx context.Context, ictx *v1alpha1.In // The integration platform needs to be initialized before starting handle // context in status error if _, err := platform.GetCurrentPlatform(ctx, action.client, ictx.Namespace); err != nil { - logrus.Info("Waiting for a integration platform to be initialized") + action.L.Info("Waiting for a integration platform to be initialized") return nil } @@ -66,7 +65,7 @@ func (action *errorRecoveryAction) Handle(ctx context.Context, ictx *v1alpha1.In target := ictx.DeepCopy() target.Status.Phase = v1alpha1.IntegrationContextPhaseError - logrus.Infof("Max recovery attempt reached for context %s, transition to phase %s", ictx.Name, string(target.Status.Phase)) + action.L.Info("Max recovery attempt reached, transition to error phase") return action.client.Update(ctx, target) } @@ -91,7 +90,7 @@ func (action *errorRecoveryAction) Handle(ctx context.Context, ictx *v1alpha1.In target.Status.Failure.Recovery.Attempt = ictx.Status.Failure.Recovery.Attempt + 1 target.Status.Failure.Recovery.AttemptTime = time.Now() - logrus.Infof("Recovery attempt for context %s (%d/%d)", + action.L.Info("Recovery attempt (%d/%d)", ictx.Name, target.Status.Failure.Recovery.Attempt, target.Status.Failure.Recovery.AttemptMax, diff --git a/pkg/controller/integrationcontext/initialize.go b/pkg/controller/integrationcontext/initialize.go index 5a75986..4fad950 100644 --- a/pkg/controller/integrationcontext/initialize.go +++ b/pkg/controller/integrationcontext/initialize.go @@ -24,7 +24,6 @@ import ( "github.com/apache/camel-k/pkg/platform" "github.com/apache/camel-k/pkg/trait" "github.com/apache/camel-k/pkg/util/digest" - "github.com/sirupsen/logrus" ) // NewInitializeAction creates a new initialization handling action for the context @@ -47,7 +46,7 @@ func (action *initializeAction) CanHandle(ictx *v1alpha1.IntegrationContext) boo func (action *initializeAction) Handle(ctx context.Context, ictx *v1alpha1.IntegrationContext) error { // The integration platform needs to be initialized before starting to create contexts if _, err := platform.GetCurrentPlatform(ctx, action.client, ictx.Namespace); err != nil { - logrus.Info("Waiting for a integration platform to be initialized") + action.L.Info("Waiting for the integration platform to be initialized") return nil } @@ -76,5 +75,7 @@ func (action *initializeAction) Handle(ctx context.Context, ictx *v1alpha1.Integ } target.Status.Digest = dgst + action.L.Info("IntegrationContext state transition", "phase", target.Status.Phase) + return action.client.Update(ctx, target) } diff --git a/pkg/controller/integrationcontext/integrationcontext_controller.go b/pkg/controller/integrationcontext/integrationcontext_controller.go index 27fa3e1..fc71a76 100644 --- a/pkg/controller/integrationcontext/integrationcontext_controller.go +++ b/pkg/controller/integrationcontext/integrationcontext_controller.go @@ -6,19 +6,15 @@ import ( camelv1alpha1 "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" - "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" - logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" "sigs.k8s.io/controller-runtime/pkg/source" ) -var log = logf.Log.WithName("controller_integrationcontext") - // Add creates a new IntegrationContext Controller and adds it to the Manager. The Manager will set fields on the Controller // and Start it when the Manager is Started. func Add(mgr manager.Manager) error { @@ -31,7 +27,10 @@ func Add(mgr manager.Manager) error { // newReconciler returns a new reconcile.Reconciler func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler { - return &ReconcileIntegrationContext{client: c, scheme: mgr.GetScheme()} + return &ReconcileIntegrationContext{ + client: c, + scheme: mgr.GetScheme(), + } } // add adds a new Controller to mgr with r as the reconcile.Reconciler @@ -67,8 +66,8 @@ type ReconcileIntegrationContext struct { // The Controller will requeue the Request to be processed again if the returned error is non-nil or // Result.Requeue is true, otherwise upon completion it will remove the work from the queue. func (r *ReconcileIntegrationContext) Reconcile(request reconcile.Request) (reconcile.Result, error) { - reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) - reqLogger.Info("Reconciling IntegrationContext") + rlog := Log.WithValues("request-namespace", request.Namespace, "request-name", request.Name) + rlog.Info("Reconciling IntegrationContext") ctx := context.TODO() @@ -93,10 +92,12 @@ func (r *ReconcileIntegrationContext) Reconcile(request reconcile.Request) (reco NewMonitorAction(), } + ilog := rlog.ForIntegrationContext(instance) for _, a := range integrationContextActionPool { a.InjectClient(r.client) + a.InjectLogger(ilog) if a.CanHandle(instance) { - logrus.Debug("Invoking action ", a.Name(), " on integration context ", instance.Name) + ilog.Infof("Invoking action %s", a.Name()) if err := a.Handle(ctx, instance); err != nil { return reconcile.Result{}, err } diff --git a/pkg/util/log/util.go b/pkg/controller/integrationcontext/log.go similarity index 56% copy from pkg/util/log/util.go copy to pkg/controller/integrationcontext/log.go index 2dc91fc..798e4e4 100644 --- a/pkg/util/log/util.go +++ b/pkg/controller/integrationcontext/log.go @@ -15,27 +15,9 @@ See the License for the specific language governing permissions and limitations under the License. */ -package log +package integrationcontext -import ( - "context" - "fmt" - "io" - "io/ioutil" - "os" +import "github.com/apache/camel-k/pkg/util/log" - "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "k8s.io/client-go/kubernetes" -) - -// Print prints integrations logs to the stdout -func Print(ctx context.Context, client kubernetes.Interface, integration *v1alpha1.Integration) error { - scraper := NewSelectorScraper(client, integration.Namespace, integration.Name,"camel.apache.org/integration="+integration.Name) - reader := scraper.Start(ctx) - - if _, err := io.Copy(os.Stdout, ioutil.NopCloser(reader)); err != nil { - fmt.Println(err.Error()) - } - - return nil -} +// Log -- +var Log = log.Log.WithName("controller").WithName("integrationcontext") diff --git a/pkg/controller/integrationcontext/monitor.go b/pkg/controller/integrationcontext/monitor.go index 7d56f70..1052deb 100644 --- a/pkg/controller/integrationcontext/monitor.go +++ b/pkg/controller/integrationcontext/monitor.go @@ -22,7 +22,6 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/util/digest" - "github.com/sirupsen/logrus" ) // NewMonitorAction creates a new monitoring handling action for the context @@ -48,13 +47,13 @@ func (action *monitorAction) Handle(ctx context.Context, ictx *v1alpha1.Integrat return err } if hash != ictx.Status.Digest { - logrus.Info("IntegrationContext ", ictx.Name, " needs a rebuild") + action.L.Info("IntegrationContext needs a rebuild") target := ictx.DeepCopy() target.Status.Digest = hash target.Status.Phase = v1alpha1.IntegrationContextPhaseBuildSubmitted - logrus.Info("Context ", target.Name, " transitioning to state ", target.Status.Phase) + action.L.Info("IntegrationContext state transition", "phase", target.Status.Phase) return action.client.Update(ctx, target) } diff --git a/pkg/controller/integrationplatform/action.go b/pkg/controller/integrationplatform/action.go index fccc36c..3d4464a 100644 --- a/pkg/controller/integrationplatform/action.go +++ b/pkg/controller/integrationplatform/action.go @@ -22,6 +22,7 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" + "github.com/apache/camel-k/pkg/util/log" ) // Action -- @@ -36,12 +37,20 @@ type Action interface { // executes the handling function Handle(ctx context.Context, platform *v1alpha1.IntegrationPlatform) error + + // Inject integration logger + InjectLogger(log.Logger) } type baseAction struct { client client.Client + L log.Logger } func (action *baseAction) InjectClient(client client.Client) { action.client = client } + +func (action *baseAction) InjectLogger(log log.Logger) { + action.L = log +} diff --git a/pkg/controller/integrationplatform/create.go b/pkg/controller/integrationplatform/create.go index fd2ddec..45a1206 100644 --- a/pkg/controller/integrationplatform/create.go +++ b/pkg/controller/integrationplatform/create.go @@ -25,8 +25,6 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/install" p "github.com/apache/camel-k/pkg/platform" - - "github.com/sirupsen/logrus" ) // NewCreateAction returns a action that creates resources needed by the platform @@ -68,21 +66,21 @@ func (action *createAction) Handle(ctx context.Context, platform *v1alpha1.Integ } if len(res) > 0 { - logrus.Info("Installing custom platform resources") + action.L.Info("Installing custom platform resources") err := install.Resources(ctx, action.client, platform.Namespace, res...) if err != nil { return err } } } else { - logrus.Info("Installing default platform resources") + action.L.Info("Installing default platform resources") err := install.Resources(ctx, action.client, platform.Namespace, p.DefaultContexts...) if err != nil { return err } if platform.Spec.Profile == v1alpha1.TraitProfileKnative { - logrus.Info("Installing knative resources") + action.L.Info("Installing knative resources") err := install.Resources(ctx, action.client, platform.Namespace, p.KnativeContexts...) if err != nil { return err @@ -92,7 +90,7 @@ func (action *createAction) Handle(ctx context.Context, platform *v1alpha1.Integ target := platform.DeepCopy() target.Status.Phase = v1alpha1.IntegrationPlatformPhaseStarting - logrus.Info("Platform ", target.Name, " transitioning to state ", target.Status.Phase) + action.L.Info("IntegrationPlatform state transition", "phase", target.Status.Phase) return action.client.Update(ctx, target) } diff --git a/pkg/controller/integrationplatform/initialize.go b/pkg/controller/integrationplatform/initialize.go index e4a329e..865ecc3 100644 --- a/pkg/controller/integrationplatform/initialize.go +++ b/pkg/controller/integrationplatform/initialize.go @@ -23,7 +23,6 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" platformutils "github.com/apache/camel-k/pkg/platform" "github.com/apache/camel-k/pkg/util/openshift" - "github.com/sirupsen/logrus" ) // NewInitializeAction returns a action that initializes the platform configuration when not provided by the user @@ -54,8 +53,10 @@ func (action *initializeAction) Handle(ctx context.Context, platform *v1alpha1.I // another platform already present in the namespace if platform.Status.Phase != v1alpha1.IntegrationPlatformPhaseDuplicate { target := platform.DeepCopy() - logrus.Info("Platform ", target.Name, " transitioning to state ", v1alpha1.IntegrationPlatformPhaseDuplicate) target.Status.Phase = v1alpha1.IntegrationPlatformPhaseDuplicate + + action.L.Info("IntegrationPlatform state transition", "phase", target.Status.Phase) + return action.client.Update(ctx, target) } return nil @@ -84,7 +85,7 @@ func (action *initializeAction) Handle(ctx context.Context, platform *v1alpha1.I } if target.Spec.Build.PublishStrategy == v1alpha1.IntegrationPlatformBuildPublishStrategyKaniko && target.Spec.Build.Registry == "" { - logrus.Warning("No registry specified for publishing images") + action.L.Info("No registry specified for publishing images") } if target.Spec.Profile == "" { @@ -92,8 +93,9 @@ func (action *initializeAction) Handle(ctx context.Context, platform *v1alpha1.I } // next status - logrus.Info("Platform ", target.Name, " transitioning to state ", v1alpha1.IntegrationPlatformPhaseCreating) target.Status.Phase = v1alpha1.IntegrationPlatformPhaseCreating + + action.L.Info("IntegrationPlatform state transition", "phase", target.Status.Phase) return action.client.Update(ctx, target) } diff --git a/pkg/controller/integrationplatform/integrationplatform_controller.go b/pkg/controller/integrationplatform/integrationplatform_controller.go index 0859f95..3d574a0 100644 --- a/pkg/controller/integrationplatform/integrationplatform_controller.go +++ b/pkg/controller/integrationplatform/integrationplatform_controller.go @@ -6,19 +6,15 @@ import ( camelv1alpha1 "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" - "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" - logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" "sigs.k8s.io/controller-runtime/pkg/source" ) -var log = logf.Log.WithName("controller_integrationplatform") - // Add creates a new IntegrationPlatform Controller and adds it to the Manager. The Manager will set fields on the Controller // and Start it when the Manager is Started. func Add(mgr manager.Manager) error { @@ -31,7 +27,10 @@ func Add(mgr manager.Manager) error { // newReconciler returns a new reconcile.Reconciler func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler { - return &ReconcileIntegrationPlatform{client: c, scheme: mgr.GetScheme()} + return &ReconcileIntegrationPlatform{ + client: c, + scheme: mgr.GetScheme(), + } } // add adds a new Controller to mgr with r as the reconcile.Reconciler @@ -67,8 +66,8 @@ type ReconcileIntegrationPlatform struct { // The Controller will requeue the Request to be processed again if the returned error is non-nil or // Result.Requeue is true, otherwise upon completion it will remove the work from the queue. func (r *ReconcileIntegrationPlatform) Reconcile(request reconcile.Request) (reconcile.Result, error) { - reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) - reqLogger.Info("Reconciling IntegrationPlatform") + rlog := Log.WithValues("request-namespace", request.Namespace, "request-name", request.Name) + rlog.Info("Reconciling IntegrationPlatform") ctx := context.TODO() @@ -92,10 +91,12 @@ func (r *ReconcileIntegrationPlatform) Reconcile(request reconcile.Request) (rec NewStartAction(), } + ilog := rlog.ForIntegrationPlatform(instance) for _, a := range integrationPlatformActionPool { a.InjectClient(r.client) + a.InjectLogger(ilog) if a.CanHandle(instance) { - logrus.Debug("Invoking action ", a.Name(), " on integration platform ", instance.Name) + ilog.Infof("Invoking action %s", a.Name()) if err := a.Handle(ctx, instance); err != nil { return reconcile.Result{}, err } diff --git a/pkg/util/log/util.go b/pkg/controller/integrationplatform/log.go similarity index 56% copy from pkg/util/log/util.go copy to pkg/controller/integrationplatform/log.go index 2dc91fc..8640d08 100644 --- a/pkg/util/log/util.go +++ b/pkg/controller/integrationplatform/log.go @@ -15,27 +15,9 @@ See the License for the specific language governing permissions and limitations under the License. */ -package log +package integrationplatform -import ( - "context" - "fmt" - "io" - "io/ioutil" - "os" +import "github.com/apache/camel-k/pkg/util/log" - "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "k8s.io/client-go/kubernetes" -) - -// Print prints integrations logs to the stdout -func Print(ctx context.Context, client kubernetes.Interface, integration *v1alpha1.Integration) error { - scraper := NewSelectorScraper(client, integration.Namespace, integration.Name,"camel.apache.org/integration="+integration.Name) - reader := scraper.Start(ctx) - - if _, err := io.Copy(os.Stdout, ioutil.NopCloser(reader)); err != nil { - fmt.Println(err.Error()) - } - - return nil -} +// Log -- +var Log = log.Log.WithName("controller").WithName("integrationplatform") diff --git a/pkg/controller/integrationplatform/start.go b/pkg/controller/integrationplatform/start.go index 6d69bf6..e9974ed 100644 --- a/pkg/controller/integrationplatform/start.go +++ b/pkg/controller/integrationplatform/start.go @@ -21,7 +21,6 @@ import ( "context" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/labels" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -50,8 +49,10 @@ func (action *startAction) Handle(ctx context.Context, platform *v1alpha1.Integr } if platform.Status.Phase != aggregatePhase { target := platform.DeepCopy() - logrus.Info("Platform ", target.Name, " transitioning to state ", aggregatePhase) target.Status.Phase = aggregatePhase + + action.L.Info("IntegrationPlatform state transition", "phase", target.Status.Phase) + return action.client.Update(ctx, target) } // wait diff --git a/pkg/metadata/metadata.go b/pkg/metadata/metadata.go index cec6c07..aa38b6b 100644 --- a/pkg/metadata/metadata.go +++ b/pkg/metadata/metadata.go @@ -22,8 +22,8 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/gzip" + "github.com/apache/camel-k/pkg/util/log" src "github.com/apache/camel-k/pkg/util/source" - "github.com/sirupsen/logrus" ) // ExtractAll returns metadata information from all listed source codes @@ -73,7 +73,7 @@ func Extract(source v1alpha1.SourceSpec) IntegrationMetadata { var err error source, err = uncompress(source) if err != nil { - logrus.Errorf("unable to uncompress source %s: %v", source.Name, err) + log.Errorf(err, "unable to uncompress source %s: %v", source.Name, err) } language := source.InferLanguage() diff --git a/pkg/trait/catalog.go b/pkg/trait/catalog.go index 93596e7..2311462 100644 --- a/pkg/trait/catalog.go +++ b/pkg/trait/catalog.go @@ -24,12 +24,13 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" + "github.com/apache/camel-k/pkg/util/log" "github.com/fatih/structs" - "github.com/sirupsen/logrus" ) // Catalog collects all information about traits in one place type Catalog struct { + L log.Logger tDebug Trait tDependencies Trait tDeployment Trait @@ -53,6 +54,7 @@ type Catalog struct { // NewCatalog creates a new trait Catalog func NewCatalog(ctx context.Context, c client.Client) *Catalog { catalog := Catalog{ + L: log.Log.WithName("trait"), tDebug: newDebugTrait(), tRest: newRestTrait(), tKnative: newKnativeTrait(), @@ -179,7 +181,7 @@ func (c *Catalog) apply(environment *Environment) error { } if enabled { - logrus.Infof("Apply trait: %s", trait.ID()) + c.L.Infof("Apply trait: %s", trait.ID()) err = trait.Apply(environment) if err != nil { diff --git a/pkg/trait/knative_service.go b/pkg/trait/knative_service.go index 08a3e91..cb4a603 100644 --- a/pkg/trait/knative_service.go +++ b/pkg/trait/knative_service.go @@ -26,7 +26,6 @@ import ( "github.com/apache/camel-k/pkg/metadata" "github.com/apache/camel-k/pkg/util/envvar" serving "github.com/knative/serving/pkg/apis/serving/v1alpha1" - "github.com/sirupsen/logrus" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -136,7 +135,7 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) (*serving.Service, e sources := make([]string, 0, len(e.Integration.Spec.Sources)) for i, s := range sourcesSpecs { if s.Content == "" { - logrus.Warnf("Source %s has empty content", s.Name) + t.L.Debug("Source %s has and empty content", s.Name) } envName := fmt.Sprintf("CAMEL_K_ROUTE_%03d", i) diff --git a/pkg/trait/types.go b/pkg/trait/types.go index 4f37bf7..4f7470d 100644 --- a/pkg/trait/types.go +++ b/pkg/trait/types.go @@ -20,6 +20,8 @@ package trait import ( "context" + "github.com/apache/camel-k/pkg/util/log" + "github.com/apache/camel-k/pkg/util/source" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" @@ -57,12 +59,20 @@ type Trait interface { /* Base trait */ +func newBaseTrait(id string) BaseTrait { + return BaseTrait{ + id: ID(id), + L: log.Log.WithName("traits").WithValues("trait", id), + } +} + // BaseTrait is the root trait with noop implementations for hooks type BaseTrait struct { id ID Enabled *bool `property:"enabled"` client client.Client ctx context.Context + L log.Logger } // ID returns the identifier of the trait diff --git a/pkg/util/log/annotation_scraper.go b/pkg/util/kubernetes/log/annotation_scraper.go similarity index 90% rename from pkg/util/log/annotation_scraper.go rename to pkg/util/kubernetes/log/annotation_scraper.go index 697c20e..0636148 100644 --- a/pkg/util/log/annotation_scraper.go +++ b/pkg/util/kubernetes/log/annotation_scraper.go @@ -26,7 +26,7 @@ import ( "sync/atomic" "time" - "github.com/sirupsen/logrus" + klog "github.com/apache/camel-k/pkg/util/log" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -40,6 +40,7 @@ type SelectorScraper struct { labelSelector string podScrapers sync.Map counter uint64 + L klog.Logger } // NewSelectorScraper creates a new SelectorScraper @@ -49,6 +50,7 @@ func NewSelectorScraper(client kubernetes.Interface, namespace string, defaultCo namespace: namespace, defaultContainerName: defaultContainerName, labelSelector: labelSelector, + L: klog.WithName("scraper").WithName("label").WithValues("selector", labelSelector), } } @@ -68,7 +70,7 @@ func (s *SelectorScraper) Start(ctx context.Context) *bufio.Reader { func (s *SelectorScraper) periodicSynchronize(ctx context.Context, out *bufio.Writer, clientCloser func() error) { err := s.synchronize(ctx, out) if err != nil { - logrus.Warn("Could not synchronize log by label " + s.labelSelector) + s.L.Info("Could not synchronize log") } select { case <-ctx.Done(): @@ -81,7 +83,7 @@ func (s *SelectorScraper) periodicSynchronize(ctx context.Context, out *bufio.Wr return true }) if err := clientCloser(); err != nil { - logrus.Warn("Unable to close the client", err) + s.L.Error(err, "Unable to close the client") } case <-time.After(2 * time.Second): go s.periodicSynchronize(ctx, out, clientCloser) @@ -124,7 +126,7 @@ func (s *SelectorScraper) synchronize(ctx context.Context, out *bufio.Writer) er return nil } -func (s *SelectorScraper) addPodScraper(ctx context.Context, podName string, out *bufio.Writer) { +func (s *SelectorScraper) addPodScraper(ctx context.Context, podName string, out *bufio.Writer) { podScraper := NewPodScraper(s.client, s.namespace, podName, s.defaultContainerName) podCtx, podCancel := context.WithCancel(ctx) id := atomic.AddUint64(&s.counter, 1) @@ -135,7 +137,7 @@ func (s *SelectorScraper) addPodScraper(ctx context.Context, podName string, ou defer podCancel() if _, err := out.WriteString(prefix + "Monitoring pod " + podName); err != nil { - logrus.Error("Cannot write to output: ", err) + s.L.Error(err, "Cannot write to output") return } for { @@ -143,11 +145,11 @@ func (s *SelectorScraper) addPodScraper(ctx context.Context, podName string, ou if err == io.EOF { return } else if err != nil { - logrus.Error("Cannot read from pod stream: ", err) + s.L.Error(err, "Cannot read from pod stream") return } if _, err := out.WriteString(prefix + str); err != nil { - logrus.Error("Cannot write to output: ", err) + s.L.Error(err, "Cannot write to output") return } out.Flush() diff --git a/pkg/util/log/pod_scraper.go b/pkg/util/kubernetes/log/pod_scraper.go similarity index 92% rename from pkg/util/log/pod_scraper.go rename to pkg/util/kubernetes/log/pod_scraper.go index 93ebc18..40ee5d5 100644 --- a/pkg/util/log/pod_scraper.go +++ b/pkg/util/kubernetes/log/pod_scraper.go @@ -23,8 +23,8 @@ import ( "io" "time" + klog "github.com/apache/camel-k/pkg/util/log" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -45,6 +45,7 @@ type PodScraper struct { podName string defaultContainerName string client kubernetes.Interface + L klog.Logger } // NewPodScraper creates a new pod scraper @@ -54,6 +55,7 @@ func NewPodScraper(c kubernetes.Interface, namespace string, podName string, def podName: podName, defaultContainerName: defaultContainerName, client: c, + L: klog.WithName("scraper").WithName("pod").WithValues("name", podName), } } @@ -108,24 +110,24 @@ func (s *PodScraper) doScrape(ctx context.Context, out *bufio.Writer, clientClos func (s *PodScraper) handleAndRestart(ctx context.Context, err error, wait time.Duration, out *bufio.Writer, clientCloser func() error) { if err != nil { - logrus.Warn(errors.Wrap(err, "error caught during log scraping for pod "+s.podName)) + s.L.Error(err, "error caught during log scraping") } if ctx.Err() != nil { - logrus.Debug("Pod ", s.podName, " will no longer be monitored") + s.L.Debug("Pod will no longer be monitored") if err := clientCloser(); err != nil { - logrus.Warn("Unable to close the client", err) + s.L.Error(err, "Unable to close the client") } return } - logrus.Debug("Retrying to scrape pod ", s.podName, " logs in ", wait.Seconds(), " seconds...") + s.L.Debugf("Retrying to scrape pod logs in %f seconds...", wait.Seconds()) select { case <-time.After(wait): break case <-ctx.Done(): if err := clientCloser(); err != nil { - logrus.Warn("Unable to close the client", err) + s.L.Error(err, "Unable to close the client") } return } diff --git a/pkg/util/log/util.go b/pkg/util/kubernetes/log/util.go similarity index 100% rename from pkg/util/log/util.go rename to pkg/util/kubernetes/log/util.go diff --git a/pkg/util/log/log.go b/pkg/util/log/log.go new file mode 100644 index 0000000..d11b295 --- /dev/null +++ b/pkg/util/log/log.go @@ -0,0 +1,179 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package log + +import ( + "fmt" + + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" + + "github.com/go-logr/logr" + logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" +) + +// Log -- +var Log Logger + +func init() { + Log = Logger{ + delegate: logf.Log.WithName("camel-k"), + } +} + +// Logger -- +type Logger struct { + delegate logr.Logger +} + +// Debugf -- +func (l Logger) Debugf(format string, args ...interface{}) { + l.delegate.V(1).Info(fmt.Sprintf(format, args...)) +} + +// Infof -- +func (l Logger) Infof(format string, args ...interface{}) { + l.delegate.Info(fmt.Sprintf(format, args...)) +} + +// Errorf -- +func (l Logger) Errorf(err error, format string, args ...interface{}) { + l.delegate.Error(err, fmt.Sprintf(format, args...)) +} + +// Debug -- +func (l Logger) Debug(msg string, keysAndValues ...interface{}) { + l.delegate.V(1).Info(msg, keysAndValues...) +} + +// Info -- +func (l Logger) Info(msg string, keysAndValues ...interface{}) { + l.delegate.Info(msg, keysAndValues...) +} + +// Error -- +func (l Logger) Error(err error, msg string, keysAndValues ...interface{}) { + l.delegate.Error(err, msg, keysAndValues...) +} + +// WithName -- +func (l Logger) WithName(name string) Logger { + return Logger{ + delegate: l.delegate.WithName(name), + } +} + +// WithValues -- +func (l Logger) WithValues(keysAndValues ...interface{}) Logger { + return Logger{ + delegate: l.delegate.WithValues(keysAndValues...), + } +} + +// ForIntegration -- +func (l Logger) ForIntegration(target *v1alpha1.Integration) Logger { + return l.WithValues( + "type", target.TypeMeta.Kind, + "ns", target.Namespace, + "name", target.Name, + ) +} + +// ForIntegrationContext -- +func (l Logger) ForIntegrationContext(target *v1alpha1.IntegrationContext) Logger { + return l.WithValues( + "type", target.TypeMeta.Kind, + "ns", target.Namespace, + "name", target.Name, + ) +} + +// ForIntegrationPlatform -- +func (l Logger) ForIntegrationPlatform(target *v1alpha1.IntegrationPlatform) Logger { + return l.WithValues( + "type", target.TypeMeta.Kind, + "ns", target.Namespace, + "name", target.Name, + ) +} + +// *********************************** +// +// Helpers +// +// *********************************** + +// WithName -- +func WithName(name string) Logger { + return Log.WithName(name) +} + +// WithValues -- +func WithValues(keysAndValues ...interface{}) Logger { + return Log.WithValues(keysAndValues...) +} + +// ForIntegration -- +func ForIntegration(target *v1alpha1.Integration) Logger { + return Log.ForIntegration(target) +} + +// ForIntegrationContext -- +func ForIntegrationContext(target *v1alpha1.IntegrationContext) Logger { + return Log.ForIntegrationContext(target) +} + +// ForIntegrationPlatform -- +func ForIntegrationPlatform(target *v1alpha1.IntegrationPlatform) Logger { + return Log.ForIntegrationPlatform(target) +} + +// *********************************** +// +// +// +// *********************************** + +// Debugf -- +func Debugf(format string, args ...interface{}) { + Log.Debugf(format, args...) +} + +// Infof -- +func Infof(format string, args ...interface{}) { + Log.Infof(format, args...) +} + +// Errorf -- +func Errorf(err error, format string, args ...interface{}) { + Log.Errorf(err, format, args...) +} + +// Debug -- +func Debug(msg string, keysAndValues ...interface{}) { + Log.Debug(msg, keysAndValues...) +} + +// Info -- +func Info(msg string, keysAndValues ...interface{}) { + Log.Info(msg, keysAndValues...) +} + +// Error -- +func Error(err error, msg string, keysAndValues ...interface{}) { + Log.Error(err, msg, keysAndValues...) +} diff --git a/pkg/util/maven/maven.go b/pkg/util/maven/maven.go index 782cb83..cb8db2d 100644 --- a/pkg/util/maven/maven.go +++ b/pkg/util/maven/maven.go @@ -27,10 +27,12 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/util" - - "github.com/sirupsen/logrus" + "github.com/apache/camel-k/pkg/util/log" ) +// Log -- +var Log = log.WithName("maven") + // BuildResult -- type BuildResult struct { Classpath []v1alpha1.Artifact @@ -54,7 +56,7 @@ func GeneratePomContent(project Project) (string, error) { // CreateStructure -- func CreateStructure(buildDir string, project Project) error { - logrus.Infof("write project: %+v", project) + Log.Infof("write project: %+v", project) pom, err := GeneratePomContent(project) if err != nil { @@ -76,16 +78,12 @@ func Run(buildDir string, args ...string) error { mvnCmd = c } - l := logrus.WithFields(logrus.Fields{ - "logger": "maven", - }) - cmd := exec.Command(mvnCmd, args...) cmd.Dir = buildDir cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - l.Infof("execute: %s", strings.Join(cmd.Args, " ")) + Log.Infof("execute: %s", strings.Join(cmd.Args, " ")) return cmd.Run() } diff --git a/pkg/util/openshift/register.go b/pkg/util/openshift/register.go index 3e00ffe..c089046 100644 --- a/pkg/util/openshift/register.go +++ b/pkg/util/openshift/register.go @@ -18,13 +18,13 @@ limitations under the License. package openshift import ( + "github.com/apache/camel-k/pkg/util/log" apps "github.com/openshift/api/apps/v1" authorization "github.com/openshift/api/authorization/v1" build "github.com/openshift/api/build/v1" image "github.com/openshift/api/image/v1" route "github.com/openshift/api/route/v1" template "github.com/openshift/api/template/v1" - "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/runtime" ) @@ -49,7 +49,7 @@ func AddToScheme(scheme *runtime.Scheme) error { func doAdd(addToScheme registerFunction, scheme *runtime.Scheme, err error) error { callErr := addToScheme(scheme) if callErr != nil { - logrus.Error("Error while registering Openshift types", callErr) + log.Error(callErr, "Error while registering OpenShift types") } if err == nil { diff --git a/pkg/util/sync/file.go b/pkg/util/sync/file.go index d40ebe2..f362d04 100644 --- a/pkg/util/sync/file.go +++ b/pkg/util/sync/file.go @@ -22,8 +22,8 @@ import ( "context" "time" + "github.com/apache/camel-k/pkg/util/log" "github.com/radovskyb/watcher" - "github.com/sirupsen/logrus" ) // File returns a channel that signals each time the content of the file changes @@ -48,7 +48,7 @@ func File(ctx context.Context, path string) (<-chan bool, error) { go func() { if err := w.Start(200 * time.Millisecond); err != nil { - logrus.Error("Error while starting watcher: ", err) + log.Error(err, "Error while starting watcher") close(out) } }() diff --git a/pkg/util/watch/watch.go b/pkg/util/watch/watch.go index f1bcd84..c5c1cf1 100644 --- a/pkg/util/watch/watch.go +++ b/pkg/util/watch/watch.go @@ -21,10 +21,10 @@ import ( "context" "github.com/apache/camel-k/pkg/util/kubernetes" + "github.com/apache/camel-k/pkg/util/log" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/util/kubernetes/customclient" - "github.com/sirupsen/logrus" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/json" @@ -78,7 +78,7 @@ func HandleIntegrationStateChanges(ctx context.Context, integration *v1alpha1.In copy := integration.DeepCopy() err = json.Unmarshal(jsondata, copy) if err != nil { - logrus.Error("Unexpected error detected when watching resource", err) + log.Error(err, "Unexpected error detected when watching resource") return nil } @@ -142,7 +142,7 @@ func HandlePlatformStateChanges(ctx context.Context, platform *v1alpha1.Integrat copy := platform.DeepCopy() err = json.Unmarshal(jsondata, copy) if err != nil { - logrus.Error("Unexpected error detected when watching resource", err) + log.Error(err, "Unexpected error detected when watching resource") return nil } diff --git a/test/log_scrape_integration_test.go b/test/log_scrape_integration_test.go index f233e29..2124982 100644 --- a/test/log_scrape_integration_test.go +++ b/test/log_scrape_integration_test.go @@ -27,7 +27,7 @@ import ( "testing" "time" - "github.com/apache/camel-k/pkg/util/log" + "github.com/apache/camel-k/pkg/util/kubernetes/log" "github.com/stretchr/testify/assert" )