diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 8aee2b6dea..589565ce7c 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 b218a0d116..daec1bfc26 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 765b62df83..c52a771db9 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 c968942760..35ec06a552 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 07051df96d..5dc28d583d 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)