Skip to content

Commit

Permalink
Merge pull request #2181 from mrkm4ntr/log-from-context
Browse files Browse the repository at this point in the history
 ✨  Use log from context
  • Loading branch information
k8s-ci-robot authored May 18, 2021
2 parents 53b7bf2 + 1ae1686 commit e9e789d
Show file tree
Hide file tree
Showing 22 changed files with 34 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ package {{ if and .MultiGroup .Resource.Group }}{{ .Resource.PackageName }}{{ el
import (
"context"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
{{ if not (isEmptyStr .Resource.Path) -}}
{{ .Resource.ImportAlias }} "{{ .Resource.Path }}"
{{- end }}
Expand All @@ -80,7 +80,6 @@ import (
// {{ .Resource.Kind }}Reconciler reconciles a {{ .Resource.Kind }} object
type {{ .Resource.Kind }}Reconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}
Expand All @@ -98,7 +97,7 @@ type {{ .Resource.Kind }}Reconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@{{ .ControllerRuntimeVersion }}/pkg/reconcile
func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("{{ .Resource.Kind | lower }}", req.NamespacedName)
_ = log.FromContext(ctx)
// your logic here
Expand Down
6 changes: 2 additions & 4 deletions pkg/plugins/golang/v3/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ const (
`
reconcilerSetupCodeFragment = `if err = (&controllers.%sReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("%s"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "%s")
Expand All @@ -108,7 +107,6 @@ const (
`
multiGroupReconcilerSetupCodeFragment = `if err = (&%scontrollers.%sReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("%s").WithName("%s"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "%s")
Expand Down Expand Up @@ -157,10 +155,10 @@ func (f *MainUpdater) GetCodeFragments() machinery.CodeFragmentsMap {
if f.WireController {
if !f.MultiGroup || f.Resource.Group == "" {
setup = append(setup, fmt.Sprintf(reconcilerSetupCodeFragment,
f.Resource.Kind, f.Resource.Kind, f.Resource.Kind))
f.Resource.Kind, f.Resource.Kind))
} else {
setup = append(setup, fmt.Sprintf(multiGroupReconcilerSetupCodeFragment,
f.Resource.PackageName(), f.Resource.Kind, f.Resource.Group, f.Resource.Kind, f.Resource.Kind))
f.Resource.PackageName(), f.Resource.Kind, f.Resource.Kind))
}
}
if f.WireWebhook {
Expand Down
3 changes: 0 additions & 3 deletions testdata/project-v3-addon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,20 @@ func main() {

if err = (&controllers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Captain")
os.Exit(1)
}
if err = (&controllers.FirstMateReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FirstMate"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FirstMate")
os.Exit(1)
}
if err = (&controllers.AdmiralReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Admiral"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Admiral")
Expand Down
5 changes: 2 additions & 3 deletions testdata/project-v3-config/controllers/admiral_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package controllers
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v3-config/api/v1"
)

// AdmiralReconciler reconciles a Admiral object
type AdmiralReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -48,7 +47,7 @@ type AdmiralReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *AdmiralReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("admiral", req.NamespacedName)
_ = log.FromContext(ctx)

// your logic here

Expand Down
5 changes: 2 additions & 3 deletions testdata/project-v3-config/controllers/captain_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package controllers
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v3-config/api/v1"
)

// CaptainReconciler reconciles a Captain object
type CaptainReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -48,7 +47,7 @@ type CaptainReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *CaptainReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("captain", req.NamespacedName)
_ = log.FromContext(ctx)

// your logic here

Expand Down
5 changes: 2 additions & 3 deletions testdata/project-v3-config/controllers/laker_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ package controllers
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// LakerReconciler reconciles a Laker object
type LakerReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -46,7 +45,7 @@ type LakerReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *LakerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("laker", req.NamespacedName)
_ = log.FromContext(ctx)

// your logic here

Expand Down
5 changes: 0 additions & 5 deletions testdata/project-v3-config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,13 @@ func main() {

if err = (&controllers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Captain")
os.Exit(1)
}
if err = (&controllers.CaptainReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Captain"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Captain")
Expand All @@ -100,7 +98,6 @@ func main() {
}
if err = (&controllers.FirstMateReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("FirstMate"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FirstMate")
Expand All @@ -112,7 +109,6 @@ func main() {
}
if err = (&controllers.AdmiralReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Admiral"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Admiral")
Expand All @@ -124,7 +120,6 @@ func main() {
}
if err = (&controllers.LakerReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Laker"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Laker")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ package apps
import (
"context"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// DeploymentReconciler reconciles a Deployment object
type DeploymentReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -47,7 +46,7 @@ type DeploymentReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *DeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("deployment", req.NamespacedName)
_ = log.FromContext(ctx)

// your logic here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package crew
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

crewv1 "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/apis/crew/v1"
)

// CaptainReconciler reconciles a Captain object
type CaptainReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -48,7 +47,7 @@ type CaptainReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *CaptainReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("captain", req.NamespacedName)
_ = log.FromContext(ctx)

// your logic here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package foopolicy
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

foopolicyv1 "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/apis/foo.policy/v1"
)

// HealthCheckPolicyReconciler reconciles a HealthCheckPolicy object
type HealthCheckPolicyReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -48,7 +47,7 @@ type HealthCheckPolicyReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *HealthCheckPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("healthcheckpolicy", req.NamespacedName)
_ = log.FromContext(ctx)

// your logic here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package controllers
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

testprojectorgv1 "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/apis/v1"
)

// LakersReconciler reconciles a Lakers object
type LakersReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -48,7 +47,7 @@ type LakersReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *LakersReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("lakers", req.NamespacedName)
_ = log.FromContext(ctx)

// your logic here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package seacreatures
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

seacreaturesv1beta1 "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/apis/sea-creatures/v1beta1"
)

// KrakenReconciler reconciles a Kraken object
type KrakenReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -48,7 +47,7 @@ type KrakenReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *KrakenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("kraken", req.NamespacedName)
_ = log.FromContext(ctx)

// your logic here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package seacreatures
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

seacreaturesv1beta2 "sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup/apis/sea-creatures/v1beta2"
)

// LeviathanReconciler reconciles a Leviathan object
type LeviathanReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand All @@ -48,7 +47,7 @@ type LeviathanReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *LeviathanReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("leviathan", req.NamespacedName)
_ = log.FromContext(ctx)

// your logic here

Expand Down
Loading

0 comments on commit e9e789d

Please sign in to comment.