Skip to content

Commit

Permalink
Silence PodSecurityPolicy deprecation warnings (and more) (#2333)
Browse files Browse the repository at this point in the history
* set klog (kubernetes/client-go's logger) level to error

* add warning if unable to create logger
  • Loading branch information
kubasobon authored Jul 31, 2024
1 parent 7b79ec0 commit a19868c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
github.com/elastic/elastic-agent-libs v0.9.15
github.com/elastic/go-licenser v0.4.2
github.com/elastic/go-ucfg v0.8.8
github.com/go-logr/zapr v1.3.0
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a
github.com/gofrs/uuid v4.4.0+incompatible
github.com/googleapis/gax-go/v2 v2.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,8 @@ github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jT
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-logr/zapr v1.2.3/go.mod h1:eIauM6P8qSvTw5o2ez6UEAfGjQKrxQTl5EoK+Qa2oG4=
github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ=
github.com/go-logr/zapr v1.3.0/go.mod h1:YKepepNBd1u/oyhd/yQmtjVXmm9uML4IXUgMOwR8/Gg=
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab h1:xveKWz2iaueeTaUgdetzel+U7exyigDYBryyVfV/rZk=
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
Expand Down
11 changes: 11 additions & 0 deletions internal/dataprovider/providers/k8s/client_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import (

"github.com/elastic/elastic-agent-autodiscover/kubernetes"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/go-logr/zapr"
"go.uber.org/zap"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)

type ClientGetterAPI interface {
Expand All @@ -32,6 +35,14 @@ type ClientGetterAPI interface {
type ClientGetter struct{}

func (ClientGetter) GetClient(log *logp.Logger, kubeConfig string, options kubernetes.KubeClientOptions) (k8s.Interface, error) {
// Prevent klog from writing anything other than errors to stderr
if replacementLogger, err := zap.NewProduction(); err != nil {
replacementLogger = replacementLogger.WithOptions(zap.IncreaseLevel(zap.ErrorLevel))
klog.SetLogger(zapr.NewLogger(replacementLogger))
} else {
log.Warnf("could not suppress klog: %+v", err)
}

client, err := kubernetes.GetKubernetesClient(kubeConfig, options)
if err != nil {
if kubernetes.IsInCluster(kubeConfig) {
Expand Down
9 changes: 9 additions & 0 deletions internal/processor/add_cluster_id/add_cluster_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor"
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
agentconfig "github.com/elastic/elastic-agent-libs/config"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"k8s.io/klog/v2"
)

const (
Expand All @@ -49,6 +52,12 @@ func New(agentCfg *agentconfig.C) (beat.Processor, error) {
return nil, makeErrConfigUnpack(err)
}

// Prevent klog from writing anything other than errors to stderr
if replacementLogger, err := zap.NewProduction(); err != nil {
replacementLogger = replacementLogger.WithOptions(zap.IncreaseLevel(zap.ErrorLevel))
klog.SetLogger(zapr.NewLogger(replacementLogger))
}

client, err := kubernetes.GetKubernetesClient("", kubernetes.KubeClientOptions{})
if err != nil {
return nil, err
Expand Down

0 comments on commit a19868c

Please sign in to comment.