You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2023/12/13 11:02:41 UTC

(camel-k) branch main updated: fix(cmd): move log in init to catch all options

This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new 1873aa161 fix(cmd): move log in init to catch all options
1873aa161 is described below

commit 1873aa1619ec78049111d981326da0e53caa5144
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Tue Dec 5 18:01:55 2023 +0100

    fix(cmd): move log in init to catch all options
    
    Closes #4891
---
 pkg/cmd/run.go                                | 3 ---
 pkg/util/kubernetes/log/annotation_scraper.go | 1 +
 pkg/util/kubernetes/log/pod_scraper.go        | 1 +
 pkg/util/kubernetes/portforward.go            | 1 +
 pkg/util/log/log.go                           | 6 ++++++
 5 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index 8aee2b6de..589565ce7 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -55,10 +55,8 @@ import (
 	"k8s.io/apimachinery/pkg/util/yaml"
 	"k8s.io/cli-runtime/pkg/printers"
 	"k8s.io/client-go/kubernetes/scheme"
-	logf "sigs.k8s.io/controller-runtime/pkg/log"
 
 	ctrl "sigs.k8s.io/controller-runtime/pkg/client"
-	"sigs.k8s.io/controller-runtime/pkg/log/zap"
 
 	v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/v2/pkg/client"
@@ -337,7 +335,6 @@ func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error {
 		signal.Notify(cs, os.Interrupt, syscall.SIGTERM)
 		go func() {
 			<-cs
-			logf.SetLogger(zap.New(zap.UseDevMode(true)))
 			if o.Context.Err() != nil {
 				// Context canceled
 				return
diff --git a/pkg/util/kubernetes/log/annotation_scraper.go b/pkg/util/kubernetes/log/annotation_scraper.go
index b218a0d11..daec1bfc2 100644
--- a/pkg/util/kubernetes/log/annotation_scraper.go
+++ b/pkg/util/kubernetes/log/annotation_scraper.go
@@ -49,6 +49,7 @@ type SelectorScraper struct {
 
 // NewSelectorScraper creates a new SelectorScraper.
 func NewSelectorScraper(client kubernetes.Interface, namespace string, defaultContainerName string, labelSelector string, tailLines *int64) *SelectorScraper {
+	klog.InitForCmd()
 	return &SelectorScraper{
 		client:               client,
 		namespace:            namespace,
diff --git a/pkg/util/kubernetes/log/pod_scraper.go b/pkg/util/kubernetes/log/pod_scraper.go
index 765b62df8..c52a771db 100644
--- a/pkg/util/kubernetes/log/pod_scraper.go
+++ b/pkg/util/kubernetes/log/pod_scraper.go
@@ -54,6 +54,7 @@ type PodScraper struct {
 
 // NewPodScraper creates a new pod scraper.
 func NewPodScraper(c kubernetes.Interface, namespace string, podName string, defaultContainerName string, tailLines *int64) *PodScraper {
+	klog.InitForCmd()
 	return &PodScraper{
 		namespace:            namespace,
 		podName:              podName,
diff --git a/pkg/util/kubernetes/portforward.go b/pkg/util/kubernetes/portforward.go
index c96894276..35ec06a55 100644
--- a/pkg/util/kubernetes/portforward.go
+++ b/pkg/util/kubernetes/portforward.go
@@ -36,6 +36,7 @@ import (
 )
 
 func PortForward(ctx context.Context, c client.Client, ns, labelSelector string, localPort, remotePort uint, stdOut, stdErr io.Writer) error {
+	log.InitForCmd()
 	list, err := c.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{
 		LabelSelector: labelSelector,
 	})
diff --git a/pkg/util/log/log.go b/pkg/util/log/log.go
index 07051df96..4c47aa610 100644
--- a/pkg/util/log/log.go
+++ b/pkg/util/log/log.go
@@ -24,6 +24,7 @@ import (
 	"github.com/apache/camel-k/v2/pkg/apis/camel/v1alpha1"
 	"github.com/go-logr/logr"
 	logf "sigs.k8s.io/controller-runtime/pkg/log"
+	"sigs.k8s.io/controller-runtime/pkg/log/zap"
 )
 
 // Log --.
@@ -35,6 +36,11 @@ func init() {
 	}
 }
 
+// InitForCmd is required to avoid nil pointer exceptions from command line.
+func InitForCmd() {
+	logf.SetLogger(zap.New(zap.UseDevMode(true)))
+}
+
 // Injectable identifies objects that can receive a Logger.
 type Injectable interface {
 	InjectLogger(Logger)