Skip to content

Commit

Permalink
Merge pull request #39 from pohly/contextual-logging
Browse files Browse the repository at this point in the history
fix and test for contextual logging
k8s-ci-robot authored Dec 1, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents efc9428 + dbe91b9 commit be2b8b1
Showing 7 changed files with 118 additions and 13 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -87,6 +87,13 @@ misspell:
vet:
go vet $(MODULE)/...

# Ensure that all log calls support contextual logging.
test: logcheck
.PHONY: logcheck
logcheck:
(cd hack/tools && GOBIN=$(PWD) go install sigs.k8s.io/logtools/logcheck)
./logcheck -check-contextual -check-deprecations ./...

COVERAGE_FILE := coverage.out
test: build cmds
go test -v -coverprofile=$(COVERAGE_FILE) $(MODULE)/...
14 changes: 8 additions & 6 deletions cmd/dra-example-controller/main.go
Original file line number Diff line number Diff line change
@@ -138,7 +138,7 @@ func newApp() *cli.App {
}

if flags.httpEndpoint != "" {
err = SetupHTTPEndpoint(config)
err = SetupHTTPEndpoint(ctx, config)
if err != nil {
return fmt.Errorf("create http endpoint: %v", err)
}
@@ -156,7 +156,9 @@ func newApp() *cli.App {
return app
}

func SetupHTTPEndpoint(config *Config) error {
func SetupHTTPEndpoint(ctx context.Context, config *Config) error {
logger := klog.FromContext(ctx)
logger = klog.LoggerWithName(logger, "http-server")
if config.flags.metricsPath != "" {
// To collect metrics data from the metric handler itself, we
// let it register itself and then collect from that registry.
@@ -169,7 +171,7 @@ func SetupHTTPEndpoint(config *Config) error {
gatherers = append(gatherers, reg)

actualPath := path.Join("/", config.flags.metricsPath)
klog.InfoS("Starting metrics", "path", actualPath)
logger.Info("Starting metrics", "path", actualPath)
// This is similar to k8s.io/component-base/metrics HandlerWithReset
// except that we gather from multiple sources.
config.mux.Handle(actualPath,
@@ -180,7 +182,7 @@ func SetupHTTPEndpoint(config *Config) error {

if config.flags.profilePath != "" {
actualPath := path.Join("/", config.flags.profilePath)
klog.InfoS("Starting profiling", "path", actualPath)
logger.Info("Starting profiling", "path", actualPath)
config.mux.HandleFunc(actualPath, pprof.Index)
config.mux.HandleFunc(path.Join(actualPath, "cmdline"), pprof.Cmdline)
config.mux.HandleFunc(path.Join(actualPath, "profile"), pprof.Profile)
@@ -194,10 +196,10 @@ func SetupHTTPEndpoint(config *Config) error {
}

go func() {
klog.InfoS("Starting HTTP server", "endpoint", config.flags.httpEndpoint)
logger.Info("Starting HTTP server", "endpoint", config.flags.httpEndpoint)
err := http.Serve(listener, config.mux)
if err != nil {
klog.ErrorS(err, "HTTP server failed")
logger.Error(err, "HTTP server failed")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}()
14 changes: 8 additions & 6 deletions cmd/dra-example-kubeletplugin/driver.go
Original file line number Diff line number Diff line change
@@ -96,8 +96,8 @@ func (d *driver) Shutdown(ctx context.Context) error {
}

func (d *driver) NodePrepareResources(ctx context.Context, req *drapbv1.NodePrepareResourcesRequest) (*drapbv1.NodePrepareResourcesResponse, error) {

klog.Infof("NodePrepareResource is called: number of claims: %d", len(req.Claims))
logger := klog.FromContext(ctx)
logger.Info("NodePrepareResource", "numClaims", len(req.Claims))
preparedResources := &drapbv1.NodePrepareResourcesResponse{Claims: map[string]*drapbv1.NodePrepareResourceResponse{}}

// In production version some common operations of d.nodeUnprepareResources
@@ -111,6 +111,7 @@ func (d *driver) NodePrepareResources(ctx context.Context, req *drapbv1.NodePrep
}

func (d *driver) nodePrepareResource(ctx context.Context, claim *drapbv1.Claim) *drapbv1.NodePrepareResourceResponse {
logger := klog.FromContext(ctx)
var err error
var prepared []string
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
@@ -127,7 +128,7 @@ func (d *driver) nodePrepareResource(ctx context.Context, claim *drapbv1.Claim)
err = d.nasclient.Update(ctx, updatedSpec)
if err != nil {
if err := d.state.Unprepare(claim.Uid); err != nil {
klog.Errorf("Failed to unprepare after claim '%v' Update() error: %v", claim.Uid, err)
logger.Error(err, "Failed to unprepare after Update", "claim", claim.Uid)
}
return err
}
@@ -141,12 +142,13 @@ func (d *driver) nodePrepareResource(ctx context.Context, claim *drapbv1.Claim)
}
}

klog.Infof("Prepared devices for claim '%v': %s", claim.Uid, prepared)
klog.FromContext(ctx).Info("Prepared devices", "claim", claim.Uid)
return &drapbv1.NodePrepareResourceResponse{CDIDevices: prepared}
}

func (d *driver) NodeUnprepareResources(ctx context.Context, req *drapbv1.NodeUnprepareResourcesRequest) (*drapbv1.NodeUnprepareResourcesResponse, error) {
klog.Infof("NodeUnprepareResource is called: number of claims: %d", len(req.Claims))
logger := klog.FromContext(ctx)
logger.Info("NodeUnprepareResource", "numClaims", len(req.Claims))
unpreparedResources := &drapbv1.NodeUnprepareResourcesResponse{
Claims: map[string]*drapbv1.NodeUnprepareResourceResponse{},
}
@@ -186,7 +188,7 @@ func (d *driver) nodeUnprepareResource(ctx context.Context, claim *drapbv1.Claim
}
}

klog.Infof("Unprepared devices for claim '%v'", claim.Uid)
klog.FromContext(ctx).Info("Unprepared devices", "claim", claim.Uid)
return &drapbv1.NodeUnprepareResourceResponse{}
}

2 changes: 1 addition & 1 deletion cmd/dra-example-kubeletplugin/main.go
Original file line number Diff line number Diff line change
@@ -159,7 +159,7 @@ func StartPlugin(ctx context.Context, config *Config) error {

err = driver.Shutdown(ctx)
if err != nil {
klog.Errorf("Unable to cleanly shutdown driver: %v", err)
klog.FromContext(ctx).Error(err, "Unable to cleanly shutdown driver")
}

return nil
12 changes: 12 additions & 0 deletions hack/tools/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module sigs.k8s.io/dra-example-driver/hack/tools

go 1.21.0

require sigs.k8s.io/logtools v0.7.0

require (
golang.org/x/exp v0.0.0-20230807204917-050eac23e9de // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/tools v0.12.0 // indirect
)
59 changes: 59 additions & 0 deletions hack/tools/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/exp v0.0.0-20230807204917-050eac23e9de h1:l5Za6utMv/HsBWWqzt4S8X17j+kt1uVETUX5UFhn2rE=
golang.org/x/exp v0.0.0-20230807204917-050eac23e9de/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
sigs.k8s.io/logtools v0.7.0 h1:T1MyHujJGubwMDr2Xd9pc0Pwtcg9x6w04zkUWh9c5Do=
sigs.k8s.io/logtools v0.7.0/go.mod h1:WPITRuV0T26MH6PCQrGKZ6hHoRCOEmsdA1+raufT3E8=
23 changes: 23 additions & 0 deletions hack/tools/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package tools is used to track binary dependencies with go modules
// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
package tools

import (
_ "sigs.k8s.io/logtools/logcheck"
)

0 comments on commit be2b8b1

Please sign in to comment.