cleanup: move log functions to new internal/util/log package

Moving the log functions into its own internal/util/log package makes it
possible to split out the humongous internal/util packages in further
smaller pieces. This reduces the inter-dependencies between utility
functions and components, preventing circular dependencies which are not
allowed in Go.

Updates: #852
Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos
2021-08-24 17:03:25 +02:00
committed by mergify[bot]
parent 2036b587d7
commit 6d00b39886
41 changed files with 505 additions and 467 deletions

View File

@ -23,6 +23,8 @@ import (
"fmt"
"os/exec"
"github.com/ceph/ceph-csi/internal/util/log"
"github.com/ceph/go-ceph/rados"
)
@ -50,14 +52,14 @@ func ExecCommand(ctx context.Context, program string, args ...string) (string, s
if err != nil {
err = fmt.Errorf("an error (%w) occurred while running %s args: %v", err, program, sanitizedArgs)
if ctx != context.TODO() {
UsefulLog(ctx, "%s", err)
log.UsefulLog(ctx, "%s", err)
}
return stdout, stderr, err
}
if ctx != context.TODO() {
UsefulLog(ctx, "command succeeded: %s %v", program, sanitizedArgs)
log.UsefulLog(ctx, "command succeeded: %s %v", program, sanitizedArgs)
}
return stdout, stderr, nil
@ -151,7 +153,7 @@ func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolNam
if errors.Is(err, rados.ErrObjectExists) {
return JoinErrors(ErrObjectExists, err)
} else if err != nil {
ErrorLog(ctx, "failed creating omap (%s) in pool (%s): (%v)", objectName, poolName, err)
log.ErrorLog(ctx, "failed creating omap (%s) in pool (%s): (%v)", objectName, poolName, err)
return err
}
@ -187,7 +189,7 @@ func RemoveObject(ctx context.Context, monitors string, cr *Credentials, poolNam
if errors.Is(err, rados.ErrNotFound) {
return JoinErrors(ErrObjectNotFound, err)
} else if err != nil {
ErrorLog(ctx, "failed removing omap (%s) in pool (%s): (%v)", oMapName, poolName, err)
log.ErrorLog(ctx, "failed removing omap (%s) in pool (%s): (%v)", oMapName, poolName, err)
return err
}

View File

@ -25,6 +25,8 @@ import (
"path"
"strconv"
"strings"
"github.com/ceph/ceph-csi/internal/util/log"
)
const (
@ -262,7 +264,7 @@ func VolumeMapper(volumeID string) (mapperFile, mapperFilePath string) {
// EncryptVolume encrypts provided device with LUKS.
func EncryptVolume(ctx context.Context, devicePath, passphrase string) error {
DebugLog(ctx, "Encrypting device %s with LUKS", devicePath)
log.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)
}
@ -272,10 +274,10 @@ 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 {
DebugLog(ctx, "Opening device %s with LUKS on %s", devicePath, mapperFile)
log.DebugLog(ctx, "Opening device %s with LUKS on %s", devicePath, mapperFile)
_, stderr, err := LuksOpen(devicePath, mapperFile, passphrase)
if err != nil {
ErrorLog(ctx, "failed to open LUKS device %q: %s", devicePath, stderr)
log.ErrorLog(ctx, "failed to open LUKS device %q: %s", devicePath, stderr)
}
return err
@ -283,10 +285,10 @@ func OpenEncryptedVolume(ctx context.Context, devicePath, mapperFile, passphrase
// ResizeEncryptedVolume resizes encrypted volume so that it can be used by the client.
func ResizeEncryptedVolume(ctx context.Context, mapperFile string) error {
DebugLog(ctx, "Resizing LUKS device %s", mapperFile)
log.DebugLog(ctx, "Resizing LUKS device %s", mapperFile)
_, stderr, err := LuksResize(mapperFile)
if err != nil {
ErrorLog(ctx, "failed to resize LUKS device %s: %s", mapperFile, stderr)
log.ErrorLog(ctx, "failed to resize LUKS device %s: %s", mapperFile, stderr)
}
return err
@ -294,7 +296,7 @@ func ResizeEncryptedVolume(ctx context.Context, mapperFile string) error {
// CloseEncryptedVolume closes encrypted volume so it can be detached.
func CloseEncryptedVolume(ctx context.Context, mapperFile string) error {
DebugLog(ctx, "Closing LUKS device %s", mapperFile)
log.DebugLog(ctx, "Closing LUKS device %s", mapperFile)
_, _, err := LuksClose(mapperFile)
return err
@ -317,7 +319,7 @@ func DeviceEncryptionStatus(ctx context.Context, devicePath string) (mappedDevic
mapPath := strings.TrimPrefix(devicePath, mapperFilePathPrefix+"/")
stdout, _, err := LuksStatus(mapPath)
if err != nil {
DebugLog(ctx, "device %s is not an active LUKS device: %v", devicePath, err)
log.DebugLog(ctx, "device %s is not an active LUKS device: %v", devicePath, err)
return devicePath, "", nil
}

View File

@ -8,6 +8,8 @@ import (
runtime_pprof "runtime/pprof"
"strconv"
"github.com/ceph/ceph-csi/internal/util/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
@ -24,13 +26,13 @@ func StartMetricsServer(c *Config) {
http.Handle(c.MetricsPath, promhttp.Handler())
err := http.ListenAndServe(addr, nil)
if err != nil {
FatalLogMsg("failed to listen on address %v: %s", addr, err)
log.FatalLogMsg("failed to listen on address %v: %s", addr, err)
}
}
func addPath(name string, handler http.Handler) {
http.Handle(name, handler)
DebugLogMsg("DEBUG: registered profiling handler on /debug/pprof/%s\n", name)
log.DebugLogMsg("DEBUG: registered profiling handler on /debug/pprof/%s\n", name)
}
// EnableProfiling enables golang profiling.

View File

@ -17,6 +17,8 @@ import (
"fmt"
"sync"
"github.com/ceph/ceph-csi/internal/util/log"
"k8s.io/apimachinery/pkg/util/sets"
)
@ -240,6 +242,6 @@ func (ol *OperationLock) release(op operation, volumeID string) {
}
}
default:
ErrorLogMsg("%v operation not supported", op)
log.ErrorLogMsg("%v operation not supported", op)
}
}

View File

@ -19,6 +19,8 @@ package util
import (
"os"
"github.com/ceph/ceph-csi/internal/util/log"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
@ -32,17 +34,17 @@ func NewK8sClient() *k8s.Clientset {
if cPath != "" {
cfg, err = clientcmd.BuildConfigFromFlags("", cPath)
if err != nil {
FatalLogMsg("Failed to get cluster config with error: %v\n", err)
log.FatalLogMsg("Failed to get cluster config with error: %v\n", err)
}
} else {
cfg, err = rest.InClusterConfig()
if err != nil {
FatalLogMsg("Failed to get cluster config with error: %v\n", err)
log.FatalLogMsg("Failed to get cluster config with error: %v\n", err)
}
}
client, err := k8s.NewForConfig(cfg)
if err != nil {
FatalLogMsg("Failed to create client with error: %v\n", err)
log.FatalLogMsg("Failed to create client with error: %v\n", err)
}
return client

View File

@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package util
package log
import (
"context"

View File

@ -22,6 +22,8 @@ import (
"fmt"
"strings"
"github.com/ceph/ceph-csi/internal/util/log"
"github.com/container-storage-interface/spec/lib/go/csi"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@ -59,7 +61,7 @@ func GetTopologyFromDomainLabels(domainLabels, nodeName, driverName string) (map
// Convert passed in labels to a map, and check for uniqueness
labelsToRead := strings.SplitN(domainLabels, labelSeparator, -1)
DefaultLog("passed in node labels for processing: %+v", labelsToRead)
log.DefaultLog("passed in node labels for processing: %+v", labelsToRead)
labelsIn := make(map[string]bool)
labelCount := 0
@ -106,7 +108,7 @@ func GetTopologyFromDomainLabels(domainLabels, nodeName, driverName string) (map
return nil, fmt.Errorf("missing domain labels %v on node %q", missingLabels, nodeName)
}
DefaultLog("list of domains processed: %+v", domainMap)
log.DefaultLog("list of domains processed: %+v", domainMap)
topology := make(map[string]string)
for domain, value := range domainMap {

View File

@ -26,6 +26,8 @@ import (
"strings"
"time"
"github.com/ceph/ceph-csi/internal/util/log"
"golang.org/x/sys/unix"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/cloud-provider/volume/helpers"
@ -216,7 +218,7 @@ func parseKernelRelease(release string) (int, int, int, int, error) {
func CheckKernelSupport(release string, supportedVersions []KernelVersion) bool {
version, patchlevel, sublevel, extraversion, err := parseKernelRelease(release)
if err != nil {
ErrorLogMsg("%v", err)
log.ErrorLogMsg("%v", err)
return false
}
@ -242,7 +244,7 @@ func CheckKernelSupport(release string, supportedVersions []KernelVersion) bool
}
}
}
ErrorLogMsg("kernel %s does not support required features", release)
log.ErrorLogMsg("kernel %s does not support required features", release)
return false
}