Skip to content

Commit

Permalink
Fix Istio logging to not override all global log configs
Browse files Browse the repository at this point in the history
  • Loading branch information
triffer committed Jun 13, 2024
1 parent df04330 commit 470cff6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
7 changes: 2 additions & 5 deletions cmd/istio-install/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ import (

"istio.io/istio/istioctl/pkg/install/k8sversion"
istio "istio.io/istio/operator/cmd/mesh"
"istio.io/istio/operator/pkg/util/clog"
"istio.io/istio/pkg/kube"
istiolog "istio.io/istio/pkg/log"
"k8s.io/client-go/rest"
)

func main() {
iopFileNames := []string{os.Args[1]}

registeredScope := istiolog.RegisterScope("installation", "installation")
consoleLogger := clog.NewConsoleLogger(os.Stdout, os.Stderr, registeredScope)
consoleLogger := istioclient.CreateIstioLibraryLogger()
printer := istio.NewPrinterForWriter(os.Stdout)

rc, err := kube.DefaultRestConfig("", "", func(config *rest.Config) {
Expand All @@ -43,7 +40,7 @@ func main() {
os.Exit(1)
}

if err := istioclient.ConfigureIstioLog(); err != nil {
if err := istioclient.ConfigureIstioLogScopes(); err != nil {
consoleLogger.LogAndError("Failed to configure Istio log: ", err)
os.Exit(1)
}
Expand Down
11 changes: 3 additions & 8 deletions controllers/istio_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const (
namespace = "kyma-system"
)

func NewController(mgr manager.Manager, reconciliationInterval time.Duration) (*IstioReconciler, error) {
func NewController(mgr manager.Manager, reconciliationInterval time.Duration) *IstioReconciler {
merger := istiooperator.NewDefaultIstioMerger()

statusHandler := status.NewStatusHandler(mgr.GetClient())
Expand All @@ -62,21 +62,16 @@ func NewController(mgr manager.Manager, reconciliationInterval time.Duration) (*
restarter.NewSidecarsRestarter(mgr.GetLogger(), mgr.GetClient(), &merger, sidecars.NewProxyResetter(), []filter.SidecarProxyPredicate{}, statusHandler),
}

istioClient, err := istio.NewIstioClient()
if err != nil {
return nil, err
}

return &IstioReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
istioInstallation: &istio.Installation{Client: mgr.GetClient(), IstioClient: istioClient, Merger: &merger},
istioInstallation: &istio.Installation{Client: mgr.GetClient(), IstioClient: istio.NewIstioClient(), Merger: &merger},
istioResources: istio_resources.NewReconciler(mgr.GetClient()),
restarters: restarters,
log: mgr.GetLogger(),
statusHandler: statusHandler,
reconciliationInterval: reconciliationInterval,
}, nil
}
}

func (r *IstioReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down
24 changes: 14 additions & 10 deletions internal/reconciliations/istio/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ type IstioClient struct {
printer istio.Printer
}

func NewIstioClient() (*IstioClient, error) {
err := ConfigureIstioLog()
if err != nil {
return nil, err
}
const logScope = "istio-library"

func CreateIstioLibraryLogger() *clog.ConsoleLogger {
registeredScope := istiolog.RegisterScope(logScope, logScope)
return clog.NewConsoleLogger(os.Stdout, os.Stderr, registeredScope)
}

registeredScope := istiolog.RegisterScope("installation", "installation")
consoleLogger := clog.NewConsoleLogger(os.Stdout, os.Stderr, registeredScope)
func NewIstioClient() *IstioClient {
consoleLogger := CreateIstioLibraryLogger()
printer := istio.NewPrinterForWriter(os.Stdout)

return &IstioClient{consoleLogger: consoleLogger, printer: printer}, nil
return &IstioClient{consoleLogger: consoleLogger, printer: printer}
}

func installIstioInExternalProcess(mergedIstioOperatorPath string) error {
Expand Down Expand Up @@ -157,9 +158,12 @@ func (c *IstioClient) Uninstall(ctx context.Context) error {
return nil
}

func ConfigureIstioLog() error {
func ConfigureIstioLogScopes() error {
o := istiolog.DefaultOptions()
o.SetDefaultOutputLevel("all", istiolog.WarnLevel)
o.SetDefaultOutputLevel(logScope, istiolog.WarnLevel)
o.SetDefaultOutputLevel("analysis", istiolog.WarnLevel)
o.SetDefaultOutputLevel("translator", istiolog.WarnLevel)
o.SetDefaultOutputLevel("adsc", istiolog.WarnLevel)
// These scopes are too noisy even at warning level
o.SetDefaultOutputLevel("validation", istiolog.ErrorLevel)
o.SetDefaultOutputLevel("processing", istiolog.ErrorLevel)
Expand Down
14 changes: 8 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"github.com/kyma-project/istio/operator/internal/reconciliations/istio"
v1 "k8s.io/api/apps/v1"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -97,20 +98,21 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

mgr, err := createManager(flagVar)
// We configure the Istio logging here to make it visible that global log config is updated instead of hiding it in the scope of istio package.
err := istio.ConfigureIstioLogScopes()
if err != nil {
setupLog.Error(err, "Unable to create manager")
setupLog.Error(err, "Unable to configure Istio log scopes")
os.Exit(1)
}

reconciler, err := controllers.NewController(mgr, flagVar.reconciliationInterval)
mgr, err := createManager(flagVar)
if err != nil {
setupLog.Error(err, "Unable to create controller")
setupLog.Error(err, "Unable to create manager")
os.Exit(1)
}

if err = reconciler.SetupWithManager(mgr, rateLimiter); err != nil {
setupLog.Error(err, "Unable to setup controller", "controller", "Istio")
if err = controllers.NewController(mgr, flagVar.reconciliationInterval).SetupWithManager(mgr, rateLimiter); err != nil {
setupLog.Error(err, "Unable to create controller", "controller", "Istio")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
Expand Down

0 comments on commit 470cff6

Please sign in to comment.