cleanup: Avoid usage of numbers

Add seperate functions to handle all
levels and types of logging.

Signed-off-by: Yug <yuggupta27@gmail.com>
This commit is contained in:
Yug
2020-07-09 20:18:24 +05:30
committed by mergify[bot]
parent 8dc4ab6b1b
commit 1490daed7e
26 changed files with 186 additions and 114 deletions

View File

@ -18,8 +18,6 @@ package util
import (
"errors"
klog "k8s.io/klog/v2"
)
// ForAllFunc is a unary predicate for visiting all cache entries
@ -42,13 +40,13 @@ type CachePersister interface {
// NewCachePersister returns CachePersister based on store
func NewCachePersister(metadataStore, pluginPath string) (CachePersister, error) {
if metadataStore == "k8s_configmap" {
klog.V(4).Infof("cache-perister: using kubernetes configmap as metadata cache persister")
DebugLogMsg("cache-perister: using kubernetes configmap as metadata cache persister")
k8scm := &K8sCMCache{}
k8scm.Client = NewK8sClient()
k8scm.Namespace = GetK8sNamespace()
return k8scm, nil
} else if metadataStore == "node" {
klog.V(4).Infof("cache-persister: using node as metadata cache persister")
DebugLogMsg("cache-persister: using node as metadata cache persister")
nc := &NodeCache{}
nc.BasePath = pluginPath
nc.CacheDir = "controller"

View File

@ -148,7 +148,7 @@ func GetCryptoPassphrase(ctx context.Context, volumeID string, kms EncryptionKMS
return passphrase, nil
}
if _, ok := err.(MissingPassphrase); ok {
klog.V(4).Infof(Log(ctx, "Encryption passphrase is missing for %s. Generating a new one"),
DebugLog(ctx, "Encryption passphrase is missing for %s. Generating a new one",
volumeID)
passphrase, err = generateNewEncryptionPassphrase()
if err != nil {
@ -183,7 +183,7 @@ func VolumeMapper(volumeID string) (mapperFile, mapperFilePath string) {
// EncryptVolume encrypts provided device with LUKS
func EncryptVolume(ctx context.Context, devicePath, passphrase string) error {
klog.V(4).Infof(Log(ctx, "Encrypting device %s with LUKS"), devicePath)
DebugLog(ctx, "Encrypting device %s with LUKS", devicePath)
if _, _, err := LuksFormat(devicePath, passphrase); err != nil {
return fmt.Errorf("failed to encrypt device %s with LUKS: %w", devicePath, err)
}
@ -192,14 +192,14 @@ func EncryptVolume(ctx context.Context, devicePath, passphrase string) error {
// OpenEncryptedVolume opens volume so that it can be used by the client
func OpenEncryptedVolume(ctx context.Context, devicePath, mapperFile, passphrase string) error {
klog.V(4).Infof(Log(ctx, "Opening device %s with LUKS on %s"), devicePath, mapperFile)
DebugLog(ctx, "Opening device %s with LUKS on %s", devicePath, mapperFile)
_, _, err := LuksOpen(devicePath, mapperFile, passphrase)
return err
}
// CloseEncryptedVolume closes encrypted volume so it can be detached
func CloseEncryptedVolume(ctx context.Context, mapperFile string) error {
klog.V(4).Infof(Log(ctx, "Closing LUKS device %s"), mapperFile)
DebugLog(ctx, "Closing LUKS device %s", mapperFile)
_, _, err := LuksClose(mapperFile)
return err
}
@ -220,7 +220,7 @@ func DeviceEncryptionStatus(ctx context.Context, devicePath string) (mappedDevic
mapPath := strings.TrimPrefix(devicePath, mapperFilePathPrefix+"/")
stdout, _, err := LuksStatus(mapPath)
if err != nil {
klog.V(4).Infof(Log(ctx, "device %s is not an active LUKS device: %v"), devicePath, err)
DebugLog(ctx, "device %s is not an active LUKS device: %v", devicePath, err)
return devicePath, "", nil
}
lines := strings.Split(string(stdout), "\n")

View File

@ -122,7 +122,7 @@ func (k8scm *K8sCMCache) ForAll(pattern string, destObj interface{}, f ForAllFun
func (k8scm *K8sCMCache) Create(identifier string, data interface{}) error {
cm, err := k8scm.getMetadataCM(identifier)
if cm != nil && err == nil {
klog.V(4).Infof("k8s-cm-cache: configmap %s already exists, skipping configmap creation", identifier)
DebugLogMsg("k8s-cm-cache: configmap %s already exists, skipping configmap creation", identifier)
return nil
}
dataJSON, err := json.Marshal(data)
@ -144,13 +144,13 @@ func (k8scm *K8sCMCache) Create(identifier string, data interface{}) error {
_, err = k8scm.Client.CoreV1().ConfigMaps(k8scm.Namespace).Create(context.TODO(), cm, metav1.CreateOptions{})
if err != nil {
if apierrs.IsAlreadyExists(err) {
klog.V(4).Infof("k8s-cm-cache: configmap %s already exists", identifier)
DebugLogMsg("k8s-cm-cache: configmap %s already exists", identifier)
return nil
}
return fmt.Errorf("k8s-cm-cache: couldn't persist %s metadata as configmap: %w", identifier, err)
}
klog.V(4).Infof("k8s-cm-cache: configmap %s successfully created", identifier)
DebugLogMsg("k8s-cm-cache: configmap %s successfully created", identifier)
return nil
}
@ -176,12 +176,12 @@ func (k8scm *K8sCMCache) Delete(identifier string) error {
err := k8scm.Client.CoreV1().ConfigMaps(k8scm.Namespace).Delete(context.TODO(), identifier, metav1.DeleteOptions{})
if err != nil {
if apierrs.IsNotFound(err) {
klog.V(4).Infof("k8s-cm-cache: cannot delete missing metadata configmap %s, assuming it's already deleted", identifier)
DebugLogMsg("k8s-cm-cache: cannot delete missing metadata configmap %s, assuming it's already deleted", identifier)
return nil
}
return fmt.Errorf("k8s-cm-cache: couldn't delete metadata configmap %s: %w", identifier, err)
}
klog.V(4).Infof("k8s-cm-cache: successfully deleted metadata configmap %s", identifier)
DebugLogMsg("k8s-cm-cache: successfully deleted metadata configmap %s", identifier)
return nil
}

View File

@ -86,7 +86,7 @@ func decodeObj(fpath, pattern string, file os.FileInfo, destObj interface{}) err
// #nosec
fp, err := os.Open(path.Join(fpath, file.Name()))
if err != nil {
klog.V(4).Infof("node-cache: open file: %s err %v", file.Name(), err)
DebugLogMsg("node-cache: open file: %s err %v", file.Name(), err)
return errDec
}
decoder := json.NewDecoder(fp)
@ -117,7 +117,7 @@ func (nc *NodeCache) Create(identifier string, data interface{}) error {
if err = encoder.Encode(data); err != nil {
return fmt.Errorf("node-cache: failed to encode metadata for file: %s: %w", file, err)
}
klog.V(4).Infof("node-cache: successfully saved metadata into file: %s\n", file)
DebugLogMsg("node-cache: successfully saved metadata into file: %s\n", file)
return nil
}
@ -154,12 +154,12 @@ func (nc *NodeCache) Delete(identifier string) error {
err := os.Remove(file)
if err != nil {
if os.IsNotExist(err) {
klog.V(4).Infof("node-cache: cannot delete missing metadata storage file %s, assuming it's already deleted", file)
DebugLogMsg("node-cache: cannot delete missing metadata storage file %s, assuming it's already deleted", file)
return nil
}
return fmt.Errorf("node-cache: error removing file %s: %w", file, err)
}
klog.V(4).Infof("node-cache: successfully deleted metadata storage file at: %+v\n", file)
DebugLogMsg("node-cache: successfully deleted metadata storage file at: %+v\n", file)
return nil
}

View File

@ -36,6 +36,15 @@ import (
"k8s.io/utils/mount"
)
// enum defining logging levels.
const (
Default klog.Level = iota + 1
Useful
Extended
Debug
Trace
)
// RoundOffVolSize rounds up given quantity upto chunks of MiB/GiB
func RoundOffVolSize(size int64) int64 {
size = RoundOffBytes(size)
@ -337,3 +346,75 @@ func contains(s []string, key string) bool {
return false
}
// DefaultLog helps in logging with klog.level 1
func DefaultLog(message string, args ...interface{}) {
logMessage := fmt.Sprintf(message, args...)
// If logging is disabled, don't evaluate the arguments
if klog.V(Default).Enabled() {
klog.InfoDepth(1, logMessage)
}
}
// UsefulLog helps in logging with klog.level 2
func UsefulLog(ctx context.Context, message string, args ...interface{}) {
logMessage := fmt.Sprintf(Log(ctx, message), args...)
// If logging is disabled, don't evaluate the arguments
if klog.V(Useful).Enabled() {
klog.InfoDepth(1, logMessage)
}
}
// ExtendedLogMsg helps in logging a message with klog.level 3
func ExtendedLogMsg(message string, args ...interface{}) {
logMessage := fmt.Sprintf(message, args...)
// If logging is disabled, don't evaluate the arguments
if klog.V(Extended).Enabled() {
klog.InfoDepth(1, logMessage)
}
}
// ExtendedLog helps in logging with klog.level 3
func ExtendedLog(ctx context.Context, message string, args ...interface{}) {
logMessage := fmt.Sprintf(Log(ctx, message), args...)
// If logging is disabled, don't evaluate the arguments
if klog.V(Extended).Enabled() {
klog.InfoDepth(1, logMessage)
}
}
// DebugLogMsg helps in logging a message with klog.level 4
func DebugLogMsg(message string, args ...interface{}) {
logMessage := fmt.Sprintf(message, args...)
// If logging is disabled, don't evaluate the arguments
if klog.V(Debug).Enabled() {
klog.InfoDepth(1, logMessage)
}
}
// DebugLog helps in logging with klog.level 4
func DebugLog(ctx context.Context, message string, args ...interface{}) {
logMessage := fmt.Sprintf(Log(ctx, message), args...)
// If logging is disabled, don't evaluate the arguments
if klog.V(Debug).Enabled() {
klog.InfoDepth(1, logMessage)
}
}
// TraceLogMsg helps in logging a message with klog.level 5
func TraceLogMsg(message string, args ...interface{}) {
logMessage := fmt.Sprintf(message, args...)
// If logging is disabled, don't evaluate the arguments
if klog.V(Trace).Enabled() {
klog.InfoDepth(1, logMessage)
}
}
// TraceLog helps in logging with klog.level 5
func TraceLog(ctx context.Context, message string, args ...interface{}) {
logMessage := fmt.Sprintf(Log(ctx, message), args...)
// If logging is disabled, don't evaluate the arguments
if klog.V(Trace).Enabled() {
klog.InfoDepth(1, logMessage)
}
}