From 98a184a70284ee5367241f55e2a68167d935458e Mon Sep 17 00:00:00 2001 From: Charly Molter Date: Thu, 12 Dec 2024 01:53:05 +0100 Subject: [PATCH] chore(app): add common logging of version, config on startup In a customer investigation it took a while to figure what the version of the dp is. Also the logs between the CP and DP were quite different. Made this similar and log the version and the config in the same line for quicker troubleshooting. Signed-off-by: Charly Molter --- app/kuma-cp/cmd/run.go | 18 +++++------------- app/kuma-dp/cmd/run.go | 9 ++++----- pkg/api-server/config_ws.go | 6 +----- pkg/config/display.go | 10 +++++++--- pkg/kds/envoyadmin/kds_client_test.go | 4 +--- pkg/kds/zone/components.go | 8 ++------ 6 files changed, 20 insertions(+), 35 deletions(-) diff --git a/app/kuma-cp/cmd/run.go b/app/kuma-cp/cmd/run.go index b2d392c5183c..fdd9b7470723 100644 --- a/app/kuma-cp/cmd/run.go +++ b/app/kuma-cp/cmd/run.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "time" "github.com/spf13/cobra" @@ -62,28 +61,21 @@ func newRunCmdWithOpts(opts kuma_cmd.RunCmdOpts) *cobra.Command { runLog.Info(`[WARNING] "standalone" mode is deprecated. Changing it to "zone". Set KUMA_MODE to "zone" as "standalone" will be removed in the future.`) cfg.Mode = config_core.Zone } - kuma_cp.PrintDeprecations(&cfg, cmd.OutOrStdout()) gracefulCtx, ctx, _ := opts.SetupSignalHandler() + // this needs to be done before we log the config as bootstrap may change it. rt, err := bootstrap.Bootstrap(gracefulCtx, cfg) if err != nil { runLog.Error(err, "unable to set up Control Plane runtime") return err } + cfgForDisplay, err := config.ConfigForDisplay(&cfg) if err != nil { - runLog.Error(err, "unable to prepare config for display") - return err + runLog.Error(err, "unable to prepare config for display, log config will be empty") } - cfgBytes, err := config.ToJson(cfgForDisplay) - if err != nil { - runLog.Error(err, "unable to convert config to json") - return err - } - runLog.Info(fmt.Sprintf("Current config %s", cfgBytes)) - runLog.Info(fmt.Sprintf("Running in mode `%s`", cfg.Mode)) - + runLog.Info("starting Control Plane", "version", kuma_version.Build.Version, "mode", cfg.Mode, "config", cfgForDisplay) if err := os.RaiseFileLimit(); err != nil { runLog.Error(err, "unable to raise the open file limit") } @@ -166,7 +158,7 @@ func newRunCmdWithOpts(opts kuma_cmd.RunCmdOpts) *cobra.Command { return err } - runLog.Info("starting Control Plane", "version", kuma_version.Build.Version) + runLog.Info("starting Control Plane runtime") if err := rt.Start(gracefulCtx.Done()); err != nil { runLog.Error(err, "problem running Control Plane") return err diff --git a/app/kuma-dp/cmd/run.go b/app/kuma-dp/cmd/run.go index f894b8bd61b3..c0c6be241653 100644 --- a/app/kuma-dp/cmd/run.go +++ b/app/kuma-dp/cmd/run.go @@ -61,12 +61,11 @@ func newRunCmd(opts kuma_cmd.RunCmdOpts, rootCtx *RootContext) *cobra.Command { kumadp.PrintDeprecations(cfg, cmd.OutOrStdout()) - if conf, err := config.ToJson(cfg); err == nil { - runLog.Info("effective configuration", "config", string(conf)) - } else { - runLog.Error(err, "unable to format effective configuration", "config", cfg) - return err + cfgForDisplay, err := config.ConfigForDisplay(cfg) + if err != nil { + runLog.Error(err, "unable to format effective configuration") } + runLog.Info("starting Data Plane", "version", kuma_version.Build.Version, "config", cfgForDisplay) // Map the resource types that are acceptable depending on the value of the `--proxy-type` flag. proxyTypeMap := map[string]model.ResourceType{ diff --git a/pkg/api-server/config_ws.go b/pkg/api-server/config_ws.go index ab76ad92b281..a09dedf65499 100644 --- a/pkg/api-server/config_ws.go +++ b/pkg/api-server/config_ws.go @@ -14,10 +14,6 @@ func addConfigEndpoints(ws *restful.WebService, access access.ControlPlaneMetada if err != nil { return err } - json, err := config.ToJson(cfgForDisplay) - if err != nil { - return err - } ws.Route(ws.GET("/config").To(func(req *restful.Request, resp *restful.Response) { ctx := req.Request.Context() if err := access.ValidateView(ctx, user.FromCtx(ctx)); err != nil { @@ -25,7 +21,7 @@ func addConfigEndpoints(ws *restful.WebService, access access.ControlPlaneMetada return } resp.AddHeader("content-type", "application/json") - if _, err := resp.Write(json); err != nil { + if _, err := resp.Write([]byte(cfgForDisplay)); err != nil { log.Error(err, "Could not write the index response") } })) diff --git a/pkg/config/display.go b/pkg/config/display.go index 772afe22886f..5a87c3f0b9d3 100644 --- a/pkg/config/display.go +++ b/pkg/config/display.go @@ -8,14 +8,18 @@ import ( "sigs.k8s.io/yaml" ) -func ConfigForDisplay(cfg Config) (Config, error) { +func ConfigForDisplay(cfg Config) (string, error) { // copy config so we don't override values, because nested structs in config are pointers newCfg, err := copyConfig(cfg) if err != nil { - return nil, err + return "", err } newCfg.Sanitize() - return newCfg, nil + b, err := json.Marshal(newCfg) + if err != nil { + return "", err + } + return string(b), nil } func copyConfig(cfg Config) (Config, error) { diff --git a/pkg/kds/envoyadmin/kds_client_test.go b/pkg/kds/envoyadmin/kds_client_test.go index fe3a781109c2..faed91d21f69 100644 --- a/pkg/kds/envoyadmin/kds_client_test.go +++ b/pkg/kds/envoyadmin/kds_client_test.go @@ -11,7 +11,6 @@ import ( mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1" "github.com/kumahq/kuma/api/system/v1alpha1" "github.com/kumahq/kuma/pkg/config" - config_util "github.com/kumahq/kuma/pkg/config" kuma_cp "github.com/kumahq/kuma/pkg/config/app/kuma-cp" store_config "github.com/kumahq/kuma/pkg/config/core/resources/store" core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh" @@ -33,11 +32,10 @@ var _ = Describe("KDS client", func() { cfg := kuma_cp.DefaultConfig() cfg.Store.Type = storeType displayCfg, _ := config.ConfigForDisplay(&cfg) - bytes, _ := config_util.ToJson(displayCfg) zoneInsight.Spec.Subscriptions = []*v1alpha1.KDSSubscription{ { ConnectTime: util_proto.MustTimestampProto(t1), - Config: string(bytes), + Config: displayCfg, }, } return zoneInsight diff --git a/pkg/kds/zone/components.go b/pkg/kds/zone/components.go index f6db21e05630..0cafcc5792a8 100644 --- a/pkg/kds/zone/components.go +++ b/pkg/kds/zone/components.go @@ -54,11 +54,7 @@ func Setup(rt core_runtime.Runtime) error { } kubeFactory := resources_k8s.NewSimpleKubeFactory() cfg := rt.Config() - cfgForDisplay, err := config.ConfigForDisplay(&cfg) - if err != nil { - return errors.Wrap(err, "could not construct config for display") - } - cfgJson, err := config.ToJson(cfgForDisplay) + cfgJson, err := config.ConfigForDisplay(&cfg) if err != nil { return errors.Wrap(err, "could not marshall config to json") } @@ -68,7 +64,7 @@ func Setup(rt core_runtime.Runtime) error { syncClient := kds_client_v2.NewKDSSyncClient( log, reg.ObjectTypes(model.HasKDSFlag(model.GlobalToZoneSelector)), - kds_client_v2.NewDeltaKDSStream(stream, zone, rt, string(cfgJson)), + kds_client_v2.NewDeltaKDSStream(stream, zone, rt, cfgJson), kds_sync_store_v2.ZoneSyncCallback( stream.Context(), rt.KDSContext().Configs,