diff --git a/pkg/instance/build.go b/pkg/instance/build.go index 82d04f7..a15b1ba 100644 --- a/pkg/instance/build.go +++ b/pkg/instance/build.go @@ -8,6 +8,7 @@ import ( "time" "github.com/google/uuid" + "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "github.com/celestiaorg/knuu/pkg/builder" @@ -135,7 +136,10 @@ func (b *build) SetUser(user string) error { if err := b.builderFactory.SetUser(user); err != nil { return ErrSettingUser.WithParams(user, b.instance.name).Wrap(err) } - b.instance.Logger.Debugf("Set user '%s' for instance '%s'", user, b.instance.name) + b.instance.Logger.WithFields(logrus.Fields{ + "instance": b.instance.name, + "user": user, + }).Debugf("Set user for instance") return nil } @@ -148,7 +152,10 @@ func (b *build) Commit(ctx context.Context) error { if !b.builderFactory.Changed() { b.imageName = b.builderFactory.ImageNameFrom() - b.instance.Logger.Debugf("No need to build and push image for instance '%s'", b.instance.name) + b.instance.Logger.WithFields(logrus.Fields{ + "instance": b.instance.name, + "image": b.imageName, + }).Debugf("no need to build and push image for instance") b.instance.SetState(StateCommitted) return nil @@ -169,18 +176,31 @@ func (b *build) Commit(ctx context.Context) error { cachedImageName, exists := b.checkImageHashInCache(imageHash) if exists { b.imageName = cachedImageName - b.instance.Logger.Debugf("Using cached image for instance '%s'", b.instance.name) - } else { - b.instance.Logger.Debugf("Cannot use any cached image for instance '%s'", b.instance.name) - err = b.builderFactory.PushBuilderImage(ctx, imageName) - if err != nil { - return ErrPushingImage.WithParams(b.instance.name).Wrap(err) - } - b.updateImageCacheWithHash(imageHash, imageName) - b.imageName = imageName - b.instance.Logger.Debugf("Pushed new image for instance '%s'", b.instance.name) + + b.instance.Logger.WithFields(logrus.Fields{ + "instance": b.instance.name, + "image": b.imageName, + }).Debugf("using cached image for instance") + + b.instance.SetState(StateCommitted) + return nil } + b.instance.Logger.WithFields(logrus.Fields{ + "instance": b.instance.name, + }).Debugf("cannot use any cached image for instance") + err = b.builderFactory.PushBuilderImage(ctx, imageName) + if err != nil { + return ErrPushingImage.WithParams(b.instance.name).Wrap(err) + } + b.updateImageCacheWithHash(imageHash, imageName) + b.imageName = imageName + + b.instance.Logger.WithFields(logrus.Fields{ + "instance": b.instance.name, + "image": b.imageName, + }).Debugf("pushed new image for instance") + b.instance.SetState(StateCommitted) return nil } @@ -220,11 +240,15 @@ func (b *build) SetEnvironmentVariable(key, value string) error { if !b.instance.IsInState(StatePreparing, StateCommitted) { return ErrSettingEnvNotAllowed.WithParams(b.instance.state.String()) } - b.instance.Logger.Debugf("Setting environment variable '%s' in instance '%s'", key, b.instance.name) + b.instance.Logger.WithFields(logrus.Fields{ + "instance": b.instance.name, + "key": key, + // value is not logged to avoid leaking sensitive information + }).Debugf("Setting environment variable") + if b.instance.state == StatePreparing { return b.builderFactory.SetEnvVar(key, value) } - b.env[key] = value return nil } diff --git a/pkg/instance/execution.go b/pkg/instance/execution.go index 948c505..bd390ac 100644 --- a/pkg/instance/execution.go +++ b/pkg/instance/execution.go @@ -73,7 +73,10 @@ func (e *execution) StartWithCallback(ctx context.Context, callback func()) erro go func() { err := e.WaitInstanceIsRunning(ctx) if err != nil { - e.instance.Logger.Errorf("Error waiting for instance '%s' to be running: %s", e.instance.k8sName, err) + e.instance.Logger.WithFields(logrus.Fields{ + "instance": e.instance.k8sName, + "error": err, + }).Errorf("waiting for instance to be running") return } callback() @@ -117,10 +120,8 @@ func (e *execution) StartAsync(ctx context.Context) error { return ErrDeployingPodForInstance.WithParams(e.instance.k8sName).Wrap(err) } - e.instance.state = StateStarted + e.instance.SetState(StateStarted) e.instance.sidecars.setStateForSidecars(StateStarted) - e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.k8sName, e.instance.state.String()) - return nil } @@ -212,10 +213,9 @@ func (e *execution) Stop(ctx context.Context) error { if err := e.destroyPod(ctx); err != nil { return ErrDestroyingPod.WithParams(e.instance.k8sName).Wrap(err) } - e.instance.state = StateStopped - e.instance.sidecars.setStateForSidecars(StateStopped) - e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.k8sName, e.instance.state.String()) + e.instance.SetState(StateStopped) + e.instance.sidecars.setStateForSidecars(StateStopped) return nil } @@ -252,17 +252,18 @@ func (e *execution) Destroy(ctx context.Context) error { err := e.instance.sidecars.applyFunctionToSidecars( func(sidecar SidecarManager) error { - e.instance.Logger.Debugf("Destroying sidecar resources from '%s'", sidecar.Instance().k8sName) + e.instance.Logger.WithFields(logrus.Fields{ + "instance": e.instance.k8sName, + "sidecar": sidecar.Instance().k8sName, + }).Debugf("destroying sidecar resources") return sidecar.Instance().resources.destroyResources(ctx) }) if err != nil { return ErrDestroyingResourcesForSidecars.WithParams(e.instance.k8sName).Wrap(err) } - e.instance.state = StateDestroyed + e.instance.SetState(StateDestroyed) e.instance.sidecars.setStateForSidecars(StateDestroyed) - e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.k8sName, e.instance.state.String()) - return nil } @@ -344,9 +345,7 @@ func (e *execution) deployPod(ctx context.Context) error { e.instance.kubernetesReplicaSet = replicaSet // Log the deployment of the pod - e.instance.Logger.Debugf("Started statefulSet '%s'", e.instance.k8sName) - e.instance.Logger.Debugf("Set state of instance '%s' to '%s'", e.instance.k8sName, e.instance.state.String()) - + e.instance.Logger.WithField("instance", e.instance.k8sName).Debugf("started statefulSet") return nil } diff --git a/pkg/instance/monitoring.go b/pkg/instance/monitoring.go index 14eb68c..08dc9be 100644 --- a/pkg/instance/monitoring.go +++ b/pkg/instance/monitoring.go @@ -1,6 +1,9 @@ package instance -import v1 "k8s.io/api/core/v1" +import ( + "github.com/sirupsen/logrus" + v1 "k8s.io/api/core/v1" +) type monitoring struct { instance *Instance @@ -22,7 +25,10 @@ func (m *monitoring) SetLivenessProbe(livenessProbe *v1.Probe) error { return err } m.livenessProbe = livenessProbe - m.instance.Logger.Debugf("Set liveness probe to '%s' in instance '%s'", livenessProbe, m.instance.name) + m.instance.Logger.WithFields(logrus.Fields{ + "instance": m.instance.name, + "liveness_probe": livenessProbe, + }).Debug("set liveness probe") return nil } @@ -35,7 +41,10 @@ func (m *monitoring) SetReadinessProbe(readinessProbe *v1.Probe) error { return err } m.readinessProbe = readinessProbe - m.instance.Logger.Debugf("Set readiness probe to '%s' in instance '%s'", readinessProbe, m.instance.name) + m.instance.Logger.WithFields(logrus.Fields{ + "instance": m.instance.name, + "readiness_probe": readinessProbe, + }).Debug("set readiness probe") return nil } @@ -48,7 +57,10 @@ func (m *monitoring) SetStartupProbe(startupProbe *v1.Probe) error { return err } m.startupProbe = startupProbe - m.instance.Logger.Debugf("Set startup probe to '%s' in instance '%s'", startupProbe, m.instance.name) + m.instance.Logger.WithFields(logrus.Fields{ + "instance": m.instance.name, + "startup_probe": startupProbe, + }).Debug("set startup probe") return nil } diff --git a/pkg/instance/network.go b/pkg/instance/network.go index 91caee1..ddbca44 100644 --- a/pkg/instance/network.go +++ b/pkg/instance/network.go @@ -5,6 +5,7 @@ import ( "net" "time" + "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" ) @@ -34,7 +35,10 @@ func (n *network) AddPortTCP(port int) error { } n.portsTCP = append(n.portsTCP, port) - n.instance.Logger.Debugf("Added TCP port '%d' to instance '%s'", port, n.instance.name) + n.instance.Logger.WithFields(logrus.Fields{ + "instance": n.instance.name, + "port": port, + }).Debug("added tcp port to instance") return nil } @@ -79,7 +83,14 @@ func (n *network) PortForwardTCP(ctx context.Context, port int) (int, error) { if attempt == maxRetries { return -1, ErrForwardingPort.WithParams(maxRetries) } - n.instance.Logger.Debugf("Forwarding port %d failed, cause: %v, retrying after %v (retry %d/%d)", port, err, retryInterval, attempt, maxRetries) + n.instance.Logger.WithFields(logrus.Fields{ + "instance": n.instance.name, + "port": port, + "error": err, + "attempt": attempt, + "max": maxRetries, + "retry_interval": retryInterval.String(), + }).Debug("forwarding port failed, retrying") } return localPort, nil } @@ -99,7 +110,10 @@ func (n *network) AddPortUDP(port int) error { } n.portsUDP = append(n.portsUDP, port) - n.instance.Logger.Debugf("Added UDP port '%d' to instance '%s'", port, n.instance.k8sName) + n.instance.Logger.WithFields(logrus.Fields{ + "instance": n.instance.name, + "port": port, + }).Debug("added udp port to instance") return nil } @@ -152,7 +166,10 @@ func (n *network) deployService(ctx context.Context, portsTCP, portsUDP []int) e return ErrDeployingService.WithParams(n.instance.k8sName).Wrap(err) } n.kubernetesService = srv - n.instance.Logger.Debugf("Started service '%s'", n.instance.k8sName) + n.instance.Logger.WithFields(logrus.Fields{ + "instance": n.instance.name, + "service": serviceName, + }).Debug("started service") return nil } @@ -174,7 +191,10 @@ func (n *network) patchService(ctx context.Context, portsTCP, portsUDP []int) er return ErrPatchingService.WithParams(serviceName).Wrap(err) } n.kubernetesService = srv - n.instance.Logger.Debugf("Patched service '%s'", serviceName) + n.instance.Logger.WithFields(logrus.Fields{ + "instance": n.instance.name, + "service": serviceName, + }).Debug("patched service") return nil } @@ -257,7 +277,7 @@ func (n *network) deployOrPatchService(ctx context.Context, portsTCP, portsUDP [ return nil } - n.instance.Logger.Debugf("Ports not empty, deploying service for instance '%s'", n.instance.k8sName) + n.instance.Logger.WithField("instance", n.instance.name).Debug("ports not empty, deploying service") svc, _ := n.instance.K8sClient.GetService(ctx, n.instance.k8sName) if svc == nil { if err := n.deployService(ctx, portsTCP, portsUDP); err != nil { @@ -275,7 +295,10 @@ func (n *network) deployOrPatchService(ctx context.Context, portsTCP, portsUDP [ func (n *network) enableIfDisabled(ctx context.Context) error { disableNetwork, err := n.IsDisabled(ctx) if err != nil { - n.instance.Logger.Errorf("error checking network status for instance") + n.instance.Logger.WithFields(logrus.Fields{ + "instance": n.instance.name, + "error": err, + }).Error("error checking network status for instance") return ErrCheckingNetworkStatusForInstance.WithParams(n.instance.k8sName).Wrap(err) } @@ -283,7 +306,10 @@ func (n *network) enableIfDisabled(ctx context.Context) error { return nil } if err := n.Enable(ctx); err != nil { - n.instance.Logger.Errorf("error enabling network for instance") + n.instance.Logger.WithFields(logrus.Fields{ + "instance": n.instance.name, + "error": err, + }).Error("error enabling network for instance") return ErrEnablingNetworkForInstance.WithParams(n.instance.k8sName).Wrap(err) } return nil diff --git a/pkg/instance/resources.go b/pkg/instance/resources.go index 1c54aa4..4b633f7 100644 --- a/pkg/instance/resources.go +++ b/pkg/instance/resources.go @@ -3,6 +3,7 @@ package instance import ( "context" + "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/runtime/schema" ) @@ -26,7 +27,11 @@ func (r *resources) SetMemory(request, limit resource.Quantity) error { } r.memoryRequest = request r.memoryLimit = limit - r.instance.Logger.Debugf("Set memory to '%s' and limit to '%s' in instance '%s'", request.String(), limit.String(), r.instance.name) + r.instance.Logger.WithFields(logrus.Fields{ + "instance": r.instance.name, + "memory_request": request.String(), + "memory_limit": limit.String(), + }).Debug("set memory for instance") return nil } @@ -37,7 +42,10 @@ func (r *resources) SetCPU(request resource.Quantity) error { return ErrSettingCPUNotAllowed.WithParams(r.instance.state.String()) } r.cpuRequest = request - r.instance.Logger.Debugf("Set cpu to '%s' in instance '%s'", request.String(), r.instance.name) + r.instance.Logger.WithFields(logrus.Fields{ + "instance": r.instance.name, + "cpu_request": request.String(), + }).Debug("set cpu for instance") return nil } diff --git a/pkg/instance/security.go b/pkg/instance/security.go index 8ff9857..f2d954e 100644 --- a/pkg/instance/security.go +++ b/pkg/instance/security.go @@ -1,6 +1,9 @@ package instance import ( + "strings" + + "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" ) @@ -40,7 +43,10 @@ func (s *security) SetPrivileged(privileged bool) error { return ErrSettingPrivilegedNotAllowed.WithParams(s.instance.state.String()) } s.privileged = privileged - s.instance.Logger.Debugf("Set privileged to '%t' for instance '%s'", privileged, s.instance.name) + s.instance.Logger.WithFields(logrus.Fields{ + "instance": s.instance.name, + "privileged": privileged, + }).Debug("set privileged for instance") return nil } @@ -51,7 +57,10 @@ func (s *security) AddKubernetesCapability(capability string) error { return ErrAddingCapabilityNotAllowed.WithParams(s.instance.state.String()) } s.capabilitiesAdd = append(s.capabilitiesAdd, capability) - s.instance.Logger.Debugf("Added capability '%s' to instance '%s'", capability, s.instance.name) + s.instance.Logger.WithFields(logrus.Fields{ + "instance": s.instance.name, + "capability": capability, + }).Debug("added capability to instance") return nil } @@ -61,10 +70,12 @@ func (s *security) AddKubernetesCapabilities(capabilities []string) error { if !s.instance.IsInState(StatePreparing, StateCommitted) { return ErrAddingCapabilitiesNotAllowed.WithParams(s.instance.state.String()) } - for _, capability := range capabilities { - s.capabilitiesAdd = append(s.capabilitiesAdd, capability) - s.instance.Logger.Debugf("Added capability '%s' to instance '%s'", capability, s.instance.name) - } + s.capabilitiesAdd = append(s.capabilitiesAdd, capabilities...) + + s.instance.Logger.WithFields(logrus.Fields{ + "instance": s.instance.name, + "capabilities": strings.Join(capabilities, ", "), + }).Debug("added capabilities to instance") return nil } diff --git a/pkg/instance/sidecars.go b/pkg/instance/sidecars.go index bdd235a..bd0d3db 100644 --- a/pkg/instance/sidecars.go +++ b/pkg/instance/sidecars.go @@ -3,6 +3,8 @@ package instance import ( "context" + "github.com/sirupsen/logrus" + "github.com/celestiaorg/knuu/pkg/system" ) @@ -58,7 +60,10 @@ func (s *sidecars) Add(ctx context.Context, sc SidecarManager) error { s.sidecars = append(s.sidecars, sc) sc.Instance().parentInstance = s.instance - s.instance.Logger.Debugf("Added sidecar '%s' to instance '%s'", sc.Instance().Name(), s.instance.name) + s.instance.Logger.WithFields(logrus.Fields{ + "sidecar": sc.Instance().Name(), + "instance": s.instance.name, + }).Debug("added sidecar to instance") return nil } diff --git a/pkg/instance/state.go b/pkg/instance/state.go index 30aa0fd..2aebcc4 100644 --- a/pkg/instance/state.go +++ b/pkg/instance/state.go @@ -1,5 +1,7 @@ package instance +import "github.com/sirupsen/logrus" + // InstanceState represents the state of the instance type InstanceState int @@ -33,7 +35,10 @@ func (i *Instance) IsInState(states ...InstanceState) bool { func (i *Instance) SetState(state InstanceState) { i.state = state - i.Logger.Debugf("Set state of instance '%s' to '%s'", i.name, i.state.String()) + i.Logger.WithFields(logrus.Fields{ + "instance": i.name, + "state": i.state.String(), + }).Debug("set state of instance") } func (i *Instance) IsState(state InstanceState) bool { diff --git a/pkg/instance/storage.go b/pkg/instance/storage.go index 89b7006..4cee01b 100644 --- a/pkg/instance/storage.go +++ b/pkg/instance/storage.go @@ -9,9 +9,10 @@ import ( "strconv" "strings" - "github.com/celestiaorg/knuu/pkg/k8s" - + "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/api/resource" + + "github.com/celestiaorg/knuu/pkg/k8s" ) type storage struct { @@ -51,7 +52,12 @@ func (s *storage) AddFile(src string, dest string, chown string) error { return s.addFileToInstance(dstPath, dest, chown) } - s.instance.Logger.Debugf("Added file '%s' to instance '%s'", dest, s.instance.name) + s.instance.Logger.WithFields(logrus.Fields{ + "file": dest, + "instance": s.instance.name, + "state": s.instance.state, + "build_dir": s.instance.build.getBuildDir(), + }).Debug("added file") return nil } @@ -98,7 +104,12 @@ func (s *storage) AddFolder(src string, dest string, chown string) error { return ErrCopyingFolderToInstance.WithParams(src, s.instance.name).Wrap(err) } - s.instance.Logger.Debugf("Added folder '%s' to instance '%s'", dest, s.instance.name) + s.instance.Logger.WithFields(logrus.Fields{ + "folder": dest, + "instance": s.instance.name, + "state": s.instance.state, + "build_dir": s.instance.build.getBuildDir(), + }).Debug("added folder") return nil } @@ -132,7 +143,10 @@ func (s *storage) AddFileBytes(bytes []byte, dest string, chown string) error { func (s *storage) AddVolume(path string, size resource.Quantity) error { // temporary feat, we will remove it once we can add multiple volumes if len(s.volumes) > 0 { - s.instance.Logger.Debugf("Maximum volumes exceeded for instance '%s', volumes: %d", s.instance.name, len(s.volumes)) + s.instance.Logger.WithFields(logrus.Fields{ + "instance": s.instance.name, + "volumes": len(s.volumes), + }).Debug("maximum volumes exceeded") return ErrMaximumVolumesExceeded.WithParams(s.instance.name) } return s.AddVolumeWithOwner(path, size, 0) @@ -146,12 +160,20 @@ func (s *storage) AddVolumeWithOwner(path string, size resource.Quantity, owner } // temporary feat, we will remove it once we can add multiple volumes if len(s.volumes) > 0 { - s.instance.Logger.Debugf("Maximum volumes exceeded for instance '%s', volumes: %d", s.instance.name, len(s.volumes)) + s.instance.Logger.WithFields(logrus.Fields{ + "instance": s.instance.name, + "volumes": len(s.volumes), + }).Debug("maximum volumes exceeded") return ErrMaximumVolumesExceeded.WithParams(s.instance.name) } volume := s.instance.K8sClient.NewVolume(path, size, owner) s.volumes = append(s.volumes, volume) - s.instance.Logger.Debugf("Added volume '%s' with size '%s' and owner '%d' to instance '%s'", path, size.String(), owner, s.instance.name) + s.instance.Logger.WithFields(logrus.Fields{ + "volume": path, + "size": size.String(), + "owner": owner, + "instance": s.instance.name, + }).Debug("added volume") return nil } @@ -284,7 +306,10 @@ func (s *storage) deployVolume(ctx context.Context) error { totalSize.Add(volume.Size) } s.instance.K8sClient.CreatePersistentVolumeClaim(ctx, s.instance.k8sName, s.instance.execution.Labels(), totalSize) - s.instance.Logger.Debugf("Deployed persistent volume '%s'", s.instance.k8sName) + s.instance.Logger.WithFields(logrus.Fields{ + "total_size": totalSize.String(), + "instance": s.instance.name, + }).Debug("deployed persistent volume") return nil } @@ -295,7 +320,7 @@ func (s *storage) destroyVolume(ctx context.Context) error { if err != nil { return ErrFailedToDeletePersistentVolumeClaim.Wrap(err) } - s.instance.Logger.Debugf("Destroyed persistent volume '%s'", s.instance.k8sName) + s.instance.Logger.WithField("instance", s.instance.name).Debug("destroyed persistent volume") return nil } @@ -330,7 +355,7 @@ func (s *storage) deployFiles(ctx context.Context) error { return ErrFailedToCreateConfigMap.Wrap(err) } - s.instance.Logger.Debugf("Deployed configmap '%s'", s.instance.k8sName) + s.instance.Logger.WithField("configmap", s.instance.k8sName).Debug("deployed configmap") return nil } @@ -341,7 +366,7 @@ func (s *storage) destroyFiles(ctx context.Context) error { return ErrFailedToDeleteConfigMap.Wrap(err) } - s.instance.Logger.Debugf("Destroyed configmap '%s'", s.instance.k8sName) + s.instance.Logger.WithField("configmap", s.instance.k8sName).Debug("destroyed configmap") return nil } diff --git a/pkg/knuu/knuu.go b/pkg/knuu/knuu.go index 06ce179..898b512 100644 --- a/pkg/knuu/knuu.go +++ b/pkg/knuu/knuu.go @@ -88,7 +88,7 @@ func (k *Knuu) HandleStopSignal(ctx context.Context) { <-stop k.Logger.Info("Received signal to stop, cleaning up resources...") if err := k.CleanUp(ctx); err != nil { - k.Logger.Errorf("Error deleting namespace: %v", err) + k.Logger.WithField("error", err).Error("deleting namespace") } }() } @@ -120,7 +120,7 @@ func (k *Knuu) handleTimeout(ctx context.Context) error { k.Scope, k.K8sClient.Namespace(), instance.TimeoutHandlerInstance.String(), k.K8sClient.Namespace())) // Delete the namespace as it was created by knuu. - k.Logger.Debugf("The namespace generated [%s] will be deleted", k.K8sClient.Namespace()) + k.Logger.WithField("namespace", k.K8sClient.Namespace()).Debug("the namespace will be deleted") commands = append(commands, fmt.Sprintf("kubectl delete namespace %s", k.K8sClient.Namespace())) // Delete all labeled resources within the namespace. @@ -131,7 +131,7 @@ func (k *Knuu) handleTimeout(ctx context.Context) error { // Run the command if err := inst.Build().SetStartCommand("sh", "-c", finalCmd); err != nil { - k.Logger.Debugf("The full command generated is [%s]", finalCmd) + k.Logger.WithField("command", finalCmd).Error("cannot set start command") return ErrCannotSetStartCommand.Wrap(err) } @@ -225,6 +225,6 @@ func setupProxy(ctx context.Context, k *Knuu) error { if err != nil { return ErrCannotGetTraefikEndpoint.Wrap(err) } - k.Logger.Debugf("Proxy endpoint: %s", endpoint) + k.Logger.WithField("endpoint", endpoint).Debug("proxy endpoint") return nil } diff --git a/pkg/log/logger.go b/pkg/log/logger.go index 94dbcc0..7fba226 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -31,10 +31,17 @@ func DefaultLogger() *logrus.Logger { if customLevel := os.Getenv(envLogLevel); customLevel != "" { err := logger.Level.UnmarshalText([]byte(customLevel)) if err != nil { - logger.Warnf("Failed to parse %s: %v, defaulting to INFO", envLogLevel, err) + logger.WithFields(logrus.Fields{ + "env_log_level": envLogLevel, + "error": err, + }). + Warn("Failed to parse env var LOG_LEVEL, defaulting to INFO") } } - logger.Infof("%s: %s", envLogLevel, logger.GetLevel()) + logger.WithFields(logrus.Fields{ + "env_log_level": envLogLevel, + "log_level": logger.GetLevel(), + }).Info("Current log level") return logger } diff --git a/pkg/minio/minio.go b/pkg/minio/minio.go index c5c3600..ad06303 100644 --- a/pkg/minio/minio.go +++ b/pkg/minio/minio.go @@ -104,7 +104,10 @@ func (m *Minio) Push(ctx context.Context, localReader io.Reader, minioFilePath, return ErrMinioFailedToUploadData.Wrap(err) } - m.Logger.Debugf("Data uploaded successfully to %s in bucket %s", uploadInfo.Key, bucketName) + m.Logger.WithFields(logrus.Fields{ + "key": uploadInfo.Key, + "bucket": bucketName, + }).Debug("Data uploaded successfully") return nil } @@ -125,7 +128,10 @@ func (m *Minio) Delete(ctx context.Context, minioFilePath, bucketName string) er return ErrMinioFailedToDeleteFile.Wrap(err) } - m.Logger.Debugf("File %s deleted successfully from bucket %s", minioFilePath, bucketName) + m.Logger.WithFields(logrus.Fields{ + "key": minioFilePath, + "bucket": bucketName, + }).Debug("File deleted successfully") return nil } @@ -301,12 +307,12 @@ func (m *Minio) createOrUpdateService(ctx context.Context) error { // Check if Minio service already exists existingService, err := serviceClient.Get(ctx, ServiceName, metav1.GetOptions{}) if err == nil { - m.Logger.Debugf("Service `%s` already exists, updating.", ServiceName) + m.Logger.WithField("service", ServiceName).Debug("Service already exists, updating.") minioService.ResourceVersion = existingService.ResourceVersion // Retain the existing resource version if _, err := serviceClient.Update(ctx, minioService, metav1.UpdateOptions{}); err != nil { return ErrMinioFailedToUpdateService.Wrap(err) } - m.Logger.Debugf("Service %s updated successfully.", ServiceName) + m.Logger.WithField("service", ServiceName).Debug("Service updated successfully.") return nil } @@ -315,7 +321,7 @@ func (m *Minio) createOrUpdateService(ctx context.Context) error { return ErrMinioFailedToCreateService.Wrap(err) } - m.Logger.Debugf("Service %s created successfully.", ServiceName) + m.Logger.WithField("service", ServiceName).Debug("Service created successfully.") return nil } @@ -347,7 +353,7 @@ func (m *Minio) createBucketIfNotExists(ctx context.Context, bucketName string) if err := m.client.MakeBucket(ctx, bucketName, miniogo.MakeBucketOptions{}); err != nil { return err } - m.Logger.Debugf("Bucket `%s` created successfully.", bucketName) + m.Logger.WithField("bucket", bucketName).Debug("Bucket created successfully.") return nil } @@ -414,7 +420,7 @@ func (m *Minio) createPVC(ctx context.Context, pvcName string, storageSize resou // Check if PVC already exists _, err := pvcClient.Get(ctx, pvcName, metav1.GetOptions{}) if err == nil { - m.Logger.Debugf("PersistentVolumeClaim `%s` already exists.", pvcName) + m.Logger.WithField("pvc", pvcName).Debug("PersistentVolumeClaim already exists.") return nil } @@ -455,7 +461,7 @@ func (m *Minio) createPVC(ctx context.Context, pvcName string, storageSize resou return ErrMinioFailedToCreatePersistentVolume.Wrap(err) } } - m.Logger.Debugf("PersistentVolume `%s` created successfully.", existingPV.Name) + m.Logger.WithField("pv", existingPV.Name).Debug("PersistentVolume created successfully.") // Create PVC with the existing or newly created PV pvc := &v1.PersistentVolumeClaim{ @@ -478,6 +484,6 @@ func (m *Minio) createPVC(ctx context.Context, pvcName string, storageSize resou return ErrMinioFailedToCreatePersistentVolumeClaim.Wrap(err) } - m.Logger.Debugf("PersistentVolumeClaim `%s` created successfully.", pvcName) + m.Logger.WithField("pvc", pvcName).Debug("PersistentVolumeClaim created successfully.") return nil } diff --git a/pkg/traefik/traefik.go b/pkg/traefik/traefik.go index 3f4b19a..9606879 100644 --- a/pkg/traefik/traefik.go +++ b/pkg/traefik/traefik.go @@ -256,7 +256,7 @@ func (t *Traefik) createService(ctx context.Context) error { return ErrTraefikFailedToCreateService.Wrap(err) } - t.Logger.Debugf("Service %s created successfully.", traefikServiceName) + t.Logger.WithField("service", traefikServiceName).Debug("Service created successfully.") return nil } @@ -352,7 +352,7 @@ func (t *Traefik) createIngressRoute( func (t *Traefik) IsTraefikAPIAvailable(ctx context.Context) bool { apiResourceList, err := t.K8sClient.Clientset().Discovery().ServerResourcesForGroupVersion(traefikAPIGroupVersion) if err != nil { - t.Logger.Errorf("Failed to discover Traefik API resources: %v", err) + t.Logger.WithField("error", err).Error("Failed to discover Traefik API resources") return false } @@ -371,6 +371,6 @@ func (t *Traefik) IsTraefikAPIAvailable(ctx context.Context) bool { return true } - t.Logger.Warnf("Missing Traefik API resources: %v", requiredResources) + t.Logger.WithField("missing_resources", requiredResources).Warn("Missing Traefik API resources") return false }