Skip to content

Commit c2bcaf9

Browse files
authored
Support enabling verbose client-go logging (#1806)
* Support enabling verbose client-go logging - Can set via extraEnvVars in helm chart - Allows debugging of k8s client things * Fix linting error
1 parent a1a2591 commit c2bcaf9

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

cmd/clusters-service/app/server.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"crypto/x509/pkix"
1212
"encoding/pem"
1313
"errors"
14+
"flag"
1415
"fmt"
1516
"math/big"
1617
"net"
@@ -31,7 +32,7 @@ import (
3132
"github.com/go-logr/logr"
3233
grpc_runtime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
3334
"github.com/spf13/cobra"
34-
flag "github.com/spf13/pflag"
35+
"github.com/spf13/pflag"
3536
"github.com/spf13/viper"
3637
gitopsv1alpha1 "github.com/weaveworks/cluster-controller/api/v1alpha1"
3738
"github.com/weaveworks/go-checkpoint"
@@ -81,6 +82,7 @@ import (
8182
"k8s.io/client-go/informers"
8283
"k8s.io/client-go/kubernetes"
8384
k8scache "k8s.io/client-go/tools/cache"
85+
"k8s.io/klog/v2"
8486
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
8587
ctrl "sigs.k8s.io/controller-runtime"
8688
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -143,6 +145,7 @@ type Params struct {
143145
CostEstimationFilters string `mapstructure:"cost-estimation-filters"`
144146
CostEstimationAPIRegion string `mapstructure:"cost-estimation-api-region"`
145147
CostEstimationFilename string `mapstructure:"cost-estimation-csv-file"`
148+
KLogVerbosity string `mapstructure:"klog-verbosity"`
146149
}
147150

148151
type OIDCAuthenticationOptions struct {
@@ -227,12 +230,13 @@ func NewAPIServerCommand(log logr.Logger, tempDir string) *cobra.Command {
227230
cmdFlags.Bool("use-k8s-cached-clients", true, "Enables the use of cached clients")
228231
cmdFlags.String("ui-config", "", "UI configuration, JSON encoded")
229232
cmdFlags.String("pipeline-controller-address", pipelines.DefaultPipelineControllerAddress, "Pipeline controller address")
233+
cmdFlags.String("klog-verbosity", "0", "Set the logging of the klog library")
230234

231235
cmdFlags.String("cost-estimation-filters", "", "Cost estimation filters")
232236
cmdFlags.String("cost-estimation-api-region", "", "API region for cost estimation queries")
233237
cmdFlags.String("cost-estimation-csv-file", "", "Filename to parse as Cost Estimation data")
234238

235-
cmdFlags.VisitAll(func(fl *flag.Flag) {
239+
cmdFlags.VisitAll(func(fl *pflag.Flag) {
236240
if strings.HasPrefix(fl.Name, "cost-estimation") {
237241
cobra.CheckErr(cmdFlags.MarkHidden(fl.Name))
238242
}
@@ -306,10 +310,33 @@ func initializeConfig(cmd *cobra.Command) error {
306310
return nil
307311
}
308312

309-
func StartServer(ctx context.Context, log logr.Logger, tempDir string, p Params) error {
313+
// configureKlogVerbosity sets the klog verbosity level.
314+
// This log is used by the client-go k8s libraries and can be useful for debugging
315+
// client-go's network requests.
316+
//
317+
// v=5 - log CRD cache things?
318+
// v=6 - log requests (e.g. GET url)
319+
// v=7 - log req/res headers
320+
// v=8 - log res body
321+
func configureKLogVerbosity(v string) error {
322+
klog.InitFlags(nil)
323+
err := flag.Set("v", v)
324+
if err != nil {
325+
return fmt.Errorf("could not set klog verbosity: %w", err)
326+
}
327+
flag.Parse()
328+
return nil
329+
}
310330

331+
func StartServer(ctx context.Context, log logr.Logger, tempDir string, p Params) error {
311332
featureflags.SetFromEnv(os.Environ())
312333

334+
log.Info("Setting klog verbosity", "verbosity", p.KLogVerbosity)
335+
err := configureKLogVerbosity(p.KLogVerbosity)
336+
if err != nil {
337+
return fmt.Errorf("could not configure klog verbosity: %w", err)
338+
}
339+
313340
if p.CAPITemplatesNamespace == "" {
314341
return errors.New("CAPI templates namespace not set")
315342
}
@@ -327,7 +354,7 @@ func StartServer(ctx context.Context, log logr.Logger, tempDir string, p Params)
327354
schemeBuilder = append(schemeBuilder, capiv1.AddToScheme)
328355
}
329356

330-
err := schemeBuilder.AddToScheme(scheme)
357+
err = schemeBuilder.AddToScheme(scheme)
331358
if err != nil {
332359
return err
333360
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ require (
326326
gopkg.in/yaml.v2 v2.4.0
327327
k8s.io/apiserver v0.25.4 // indirect
328328
k8s.io/component-base v0.25.4 // indirect
329-
k8s.io/klog/v2 v2.80.1 // indirect
329+
k8s.io/klog/v2 v2.80.1
330330
k8s.io/kube-openapi v0.0.0-20221110221610-a28e98eb7c70 // indirect
331331
k8s.io/kubectl v0.25.4 // indirect
332332
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 // indirect

tools/dev-values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ extraEnvVars:
2020
value: ""
2121
- name: WEAVE_GITOPS_FEATURE_TELEMETRY
2222
value: "false"
23+
# Can bump this up to 4+ to get more verbose logging
24+
- name: KLOG_VERBOSITY
25+
value: "0"
2326

2427
extraEnvVarsSecret: ""
2528

0 commit comments

Comments
 (0)