sigs.k8s.io/cluster-api@v1.7.1/exp/runtime/topologymutation/logRef.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package topologymutation
    18  
    19  import "strings"
    20  
    21  // logRef is used to correctly render a reference with GroupVersionKind, Namespace and Name for both JSON and text logging.
    22  type logRef struct {
    23  	Group     string `json:"group,omitempty"`
    24  	Version   string `json:"version,omitempty"`
    25  	Kind      string `json:"kind,omitempty"`
    26  	Namespace string `json:"namespace,omitempty"`
    27  	Name      string `json:"name,omitempty"`
    28  }
    29  
    30  func (l logRef) String() string {
    31  	var parts []string
    32  	for _, s := range []string{l.Group, l.Version, l.Kind, l.Namespace, l.Name} {
    33  		if strings.TrimSpace(s) != "" {
    34  			parts = append(parts, s)
    35  		}
    36  	}
    37  	return strings.Join(parts, "/")
    38  }
    39  
    40  type logRefWithoutStringFunc logRef
    41  
    42  // MarshalLog ensures that loggers with support for structured output will log
    43  // as a struct by removing the String method via a custom type.
    44  func (l logRef) MarshalLog() interface{} {
    45  	return logRefWithoutStringFunc(l)
    46  }