mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
fix go lint errors in util
This commit is contained in:
parent
95e0077ba1
commit
c134a16c36
@ -31,14 +31,16 @@ func init() {
|
|||||||
flag.Set("logtostderr", "true")
|
flag.Set("logtostderr", "true")
|
||||||
}
|
}
|
||||||
|
|
||||||
// This serves as a bridge between the standard log package and the glog package.
|
// GlogWriter serves as a bridge between the standard log package and the glog package.
|
||||||
type GlogWriter struct{}
|
type GlogWriter struct{}
|
||||||
|
|
||||||
|
// Write implements the io.Writer interface.
|
||||||
func (writer GlogWriter) Write(data []byte) (n int, err error) {
|
func (writer GlogWriter) Write(data []byte) (n int, err error) {
|
||||||
glog.Info(string(data))
|
glog.Info(string(data))
|
||||||
return len(data), nil
|
return len(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InitLogs initializes logs the way we want for kubernetes.
|
||||||
func InitLogs() {
|
func InitLogs() {
|
||||||
log.SetOutput(GlogWriter{})
|
log.SetOutput(GlogWriter{})
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
@ -46,10 +48,12 @@ func InitLogs() {
|
|||||||
go Forever(glog.Flush, *logFlushFreq)
|
go Forever(glog.Flush, *logFlushFreq)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FlushLogs flushes logs immediately.
|
||||||
func FlushLogs() {
|
func FlushLogs() {
|
||||||
glog.Flush()
|
glog.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewLogger creates a new log.Logger which sends logs to glog.Info.
|
||||||
func NewLogger(prefix string) *log.Logger {
|
func NewLogger(prefix string) *log.Logger {
|
||||||
return log.New(GlogWriter{}, prefix, 0)
|
return log.New(GlogWriter{}, prefix, 0)
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ import (
|
|||||||
// For testing, bypass HandleCrash.
|
// For testing, bypass HandleCrash.
|
||||||
var ReallyCrash bool
|
var ReallyCrash bool
|
||||||
|
|
||||||
// Simply catches a crash and logs an error. Meant to be called via defer.
|
// HandleCrash simply catches a crash and logs an error. Meant to be called via defer.
|
||||||
func HandleCrash() {
|
func HandleCrash() {
|
||||||
if ReallyCrash {
|
if ReallyCrash {
|
||||||
return
|
return
|
||||||
@ -48,7 +48,7 @@ func HandleCrash() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loops forever running f every d. Catches any panics, and keeps going.
|
// Forever loops forever running f every d. Catches any panics, and keeps going.
|
||||||
func Forever(f func(), period time.Duration) {
|
func Forever(f func(), period time.Duration) {
|
||||||
for {
|
for {
|
||||||
func() {
|
func() {
|
||||||
@ -59,9 +59,9 @@ func Forever(f func(), period time.Duration) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns o marshalled as a JSON string, ignoring any errors.
|
// MakeJSONString returns obj marshalled as a JSON string, ignoring any errors.
|
||||||
func MakeJSONString(o interface{}) string {
|
func MakeJSONString(obj interface{}) string {
|
||||||
data, _ := json.Marshal(o)
|
data, _ := json.Marshal(obj)
|
||||||
return string(data)
|
return string(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,13 +75,15 @@ type IntOrString struct {
|
|||||||
StrVal string
|
StrVal string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IntstrKind represents the stored type of IntOrString.
|
||||||
type IntstrKind int
|
type IntstrKind int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
IntstrInt IntstrKind = iota
|
IntstrInt IntstrKind = iota // The IntOrString holds an int.
|
||||||
IntstrString
|
IntstrString // The IntOrString holds a string.
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SetYAML implements the yaml.Setter interface.
|
||||||
func (intstr *IntOrString) SetYAML(tag string, value interface{}) bool {
|
func (intstr *IntOrString) SetYAML(tag string, value interface{}) bool {
|
||||||
if intVal, ok := value.(int); ok {
|
if intVal, ok := value.(int); ok {
|
||||||
intstr.Kind = IntstrInt
|
intstr.Kind = IntstrInt
|
||||||
@ -96,6 +98,7 @@ func (intstr *IntOrString) SetYAML(tag string, value interface{}) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetYAML implements the yaml.Getter interface.
|
||||||
func (intstr IntOrString) GetYAML() (tag string, value interface{}) {
|
func (intstr IntOrString) GetYAML() (tag string, value interface{}) {
|
||||||
switch intstr.Kind {
|
switch intstr.Kind {
|
||||||
case IntstrInt:
|
case IntstrInt:
|
||||||
@ -108,6 +111,7 @@ func (intstr IntOrString) GetYAML() (tag string, value interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements the json.Unmarshaller interface.
|
||||||
func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
|
func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
|
||||||
if value[0] == '"' {
|
if value[0] == '"' {
|
||||||
intstr.Kind = IntstrString
|
intstr.Kind = IntstrString
|
||||||
@ -117,6 +121,7 @@ func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
|
|||||||
return json.Unmarshal(value, &intstr.IntVal)
|
return json.Unmarshal(value, &intstr.IntVal)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalJSON implements the json.Marshaller interface.
|
||||||
func (intstr IntOrString) MarshalJSON() ([]byte, error) {
|
func (intstr IntOrString) MarshalJSON() ([]byte, error) {
|
||||||
switch intstr.Kind {
|
switch intstr.Kind {
|
||||||
case IntstrInt:
|
case IntstrInt:
|
||||||
|
Loading…
Reference in New Issue
Block a user