Skip to content

Commit

Permalink
SQUASH: Move ForResource to its own file
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
  • Loading branch information
sttts committed Sep 2, 2023
1 parent 9ef9737 commit 74b3726
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
43 changes: 43 additions & 0 deletions pkg/logging/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2023 The Crossplane 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 logging

import (
"fmt"

"sigs.k8s.io/controller-runtime/pkg/client"
)

// ForResource returns logging values for a resource.
func ForResource(object client.Object) []string {
ret := make([]string, 0, 10)
gvk := object.GetObjectKind().GroupVersionKind()
if gvk.Kind == "" {
gvk.Kind = fmt.Sprintf("%T", object) // best effort for native Go types
}
ret = append(ret,
"name", object.GetName(),
"kind", gvk.Kind,
"version", gvk.Version,
"group", gvk.Group,
)
if ns := object.GetNamespace(); ns != "" {
ret = append(ret, "namespace", ns)
}

return ret
}
23 changes: 0 additions & 23 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ limitations under the License.
package logging

import (
"fmt"

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

// A Logger logs messages. Messages may be supplemented by structured data.
Expand Down Expand Up @@ -95,23 +92,3 @@ func (l logrLogger) Debug(msg string, keysAndValues ...any) {
func (l logrLogger) WithValues(keysAndValues ...any) Logger {
return logrLogger{log: l.log.WithValues(keysAndValues...)} //nolint:logrlint // False positive - logrlint thinks there's an odd number of args.
}

// ForResource returns logging values for a resource.
func ForResource(object client.Object) []string {
ret := make([]string, 0, 10)
gvk := object.GetObjectKind().GroupVersionKind()
if gvk.Kind == "" {
gvk.Kind = fmt.Sprintf("%T", object) // best effort for native Go types
}
ret = append(ret,
"name", object.GetName(),
"kind", gvk.Kind,
"version", gvk.Version,
"group", gvk.Group,
)
if ns := object.GetNamespace(); ns != "" {
ret = append(ret, "namespace", ns)
}

return ret
}

0 comments on commit 74b3726

Please sign in to comment.