Skip to content

Commit

Permalink
fix(cmd): move log in init to catch all options
Browse files Browse the repository at this point in the history
Closes #4891
  • Loading branch information
squakez committed Dec 13, 2023
1 parent e6fcd0f commit 1873aa1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 3 deletions.
3 changes: 0 additions & 3 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/util/kubernetes/log/annotation_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pkg/util/kubernetes/log/pod_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pkg/util/kubernetes/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 --.
Expand All @@ -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)
Expand Down

0 comments on commit 1873aa1

Please sign in to comment.