Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: transition to logr (with an ergonomic wrapper) #2073

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/v1alpha1/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewPromotionEventAnnotations(
p *Promotion,
f *Freight,
) map[string]string {
log := logging.LoggerFromContext(ctx)
logger := logging.LoggerFromContext(ctx)

annotations := map[string]string{
AnnotationKeyEventProject: p.GetNamespace(),
Expand All @@ -127,23 +127,23 @@ func NewPromotionEventAnnotations(
if len(f.Commits) > 0 {
data, err := json.Marshal(f.Commits)
if err != nil {
log.WithError(err).Error("marshal freight commits in JSON")
logger.Error(err, "marshal freight commits in JSON")
} else {
annotations[AnnotationKeyEventFreightCommits] = string(data)
}
}
if len(f.Images) > 0 {
data, err := json.Marshal(f.Images)
if err != nil {
log.WithError(err).Error("marshal freight images in JSON")
logger.Error(err, "marshal freight images in JSON")
} else {
annotations[AnnotationKeyEventFreightImages] = string(data)
}
}
if len(f.Charts) > 0 {
data, err := json.Marshal(f.Charts)
if err != nil {
log.WithError(err).Error("marshal freight charts in JSON")
logger.Error(err, "marshal freight charts in JSON")
} else {
annotations[AnnotationKeyEventFreightCharts] = string(data)
}
Expand Down
30 changes: 17 additions & 13 deletions cmd/controlplane/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -25,6 +24,7 @@ import (
rollouts "github.com/akuity/kargo/internal/controller/rollouts/api/v1alpha1"
"github.com/akuity/kargo/internal/kubeclient"
libEvent "github.com/akuity/kargo/internal/kubernetes/event"
"github.com/akuity/kargo/internal/logging"
"github.com/akuity/kargo/internal/os"
versionpkg "github.com/akuity/kargo/internal/version"
)
Expand All @@ -35,12 +35,14 @@ type apiOptions struct {
Host string
Port string

Logger *log.Logger
Logger *logging.Logger
}

func newAPICommand() *cobra.Command {
cmdOpts := &apiOptions{
Logger: log.StandardLogger(),
// During startup, we enforce use of an info-level logger to ensure that
// no important startup messages are missed.
Logger: logging.NewLogger(logging.InfoLevel),
}

cmd := &cobra.Command{
Expand All @@ -67,10 +69,11 @@ func (o *apiOptions) complete() {

func (o *apiOptions) run(ctx context.Context) error {
version := versionpkg.GetVersion()
o.Logger.WithFields(log.Fields{
"version": version.Version,
"commit": version.GitCommit,
}).Info("Starting Kargo API Server")
o.Logger.Info(
"Starting Kargo API Server",
"version", version.Version,
"commit", version.GitCommit,
)

cfg := config.ServerConfigFromEnv()

Expand All @@ -87,7 +90,7 @@ func (o *apiOptions) run(ctx context.Context) error {
case !cfg.RolloutsIntegrationEnabled:
o.Logger.Info("Argo Rollouts integration is disabled")
case !argoRolloutsExists(ctx, clientCfg):
o.Logger.Warn(
o.Logger.Info(
"Argo Rollouts integration was enabled, but no Argo Rollouts " +
"CRDs were found. Proceeding without Argo Rollouts integration.",
)
Expand All @@ -100,11 +103,12 @@ func (o *apiOptions) run(ctx context.Context) error {
o.Logger.Info("admin account is enabled")
}
if cfg.OIDCConfig != nil {
o.Logger.WithFields(log.Fields{
"issuerURL": cfg.OIDCConfig.IssuerURL,
"clientID": cfg.OIDCConfig.ClientID,
"cliClientID": cfg.OIDCConfig.CLIClientID,
}).Info("SSO via OpenID Connect is enabled")
o.Logger.Info(
"SSO via OpenID Connect is enabled",
"issuerURL", cfg.OIDCConfig.IssuerURL,
"clientID", cfg.OIDCConfig.ClientID,
"cliClientID", cfg.OIDCConfig.CLIClientID,
)
}

srv := api.NewServer(
Expand Down
26 changes: 14 additions & 12 deletions cmd/controlplane/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"sync"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -26,6 +25,7 @@ import (
"github.com/akuity/kargo/internal/controller/stages"
"github.com/akuity/kargo/internal/controller/warehouses"
"github.com/akuity/kargo/internal/credentials"
"github.com/akuity/kargo/internal/logging"
"github.com/akuity/kargo/internal/os"
"github.com/akuity/kargo/internal/types"
versionpkg "github.com/akuity/kargo/internal/version"
Expand All @@ -41,12 +41,14 @@ type controllerOptions struct {
ArgoCDKubeConfig string
ArgoCDNamespaceOnly bool

Logger *log.Logger
Logger *logging.Logger
}

func newControllerCommand() *cobra.Command {
cmdOpts := &controllerOptions{
Logger: log.StandardLogger(),
// During startup, we enforce use of an info-level logger to ensure that
// no important startup messages are missed.
Logger: logging.NewLogger(logging.InfoLevel),
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -75,14 +77,14 @@ func (o *controllerOptions) complete() {
func (o *controllerOptions) run(ctx context.Context) error {
version := versionpkg.GetVersion()

startupLogEntry := o.Logger.WithFields(log.Fields{
"version": version.Version,
"commit": version.GitCommit,
})
startupLogger := o.Logger.WithValues(
"version", version.Version,
"commit", version.GitCommit,
)
if o.ShardName != "" {
startupLogEntry = startupLogEntry.WithField("shard", o.ShardName)
startupLogger = startupLogger.WithValues("shard", o.ShardName)
}
startupLogEntry.Info("Starting Kargo Controller")
startupLogger.Info("Starting Kargo Controller")

promotionsReconcilerCfg := promotions.ReconcilerConfigFromEnv()
stagesReconcilerCfg := stages.ReconcilerConfigFromEnv()
Expand Down Expand Up @@ -151,7 +153,7 @@ func (o *controllerOptions) setupKargoManager(
}
if stagesReconcilerCfg.RolloutsIntegrationEnabled {
if argoRolloutsExists(ctx, restCfg) {
log.Info("Argo Rollouts integration is enabled")
o.Logger.Info("Argo Rollouts integration is enabled")
if err = rollouts.AddToScheme(scheme); err != nil {
return nil, stagesReconcilerCfg, fmt.Errorf(
"error adding Argo Rollouts API to Kargo controller manager scheme: %w",
Expand All @@ -161,7 +163,7 @@ func (o *controllerOptions) setupKargoManager(
} else {
// Disable Argo Rollouts integration if the CRDs are not found.
stagesReconcilerCfg.RolloutsIntegrationEnabled = false
log.Warn(
o.Logger.Info(
"Argo Rollouts integration was enabled, but no Argo Rollouts " +
"CRDs were found. Proceeding without Argo Rollouts integration.",
)
Expand Down Expand Up @@ -222,7 +224,7 @@ func (o *controllerOptions) setupArgoCDManager(ctx context.Context) (manager.Man
// Application resources in a single namespace, so we will use that
// namespace when attempting to determine if Argo CD CRDs are installed.
if !argoCDExists(ctx, restCfg, argocdNamespace) {
o.Logger.Warn(
o.Logger.Info(
"Argo CD integration was enabled, but no Argo CD CRDs were found. " +
"Proceeding without Argo CD integration.",
)
Expand Down
17 changes: 10 additions & 7 deletions cmd/controlplane/garbage_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -17,19 +16,22 @@ import (
"github.com/akuity/kargo/internal/api/kubernetes"
"github.com/akuity/kargo/internal/garbage"
"github.com/akuity/kargo/internal/kubeclient"
"github.com/akuity/kargo/internal/logging"
"github.com/akuity/kargo/internal/os"
versionpkg "github.com/akuity/kargo/internal/version"
)

type garbageCollectorOptions struct {
KubeConfig string

Logger *log.Logger
Logger *logging.Logger
}

func newGarbageCollectorCommand() *cobra.Command {
cmdOpts := &garbageCollectorOptions{
Logger: log.StandardLogger(),
// During startup, we enforce use of an info-level logger to ensure that
// no important startup messages are missed.
Logger: logging.NewLogger(logging.InfoLevel),
}

cmd := &cobra.Command{
Expand All @@ -54,10 +56,11 @@ func (o *garbageCollectorOptions) complete() {
func (o *garbageCollectorOptions) run(ctx context.Context) error {
version := versionpkg.GetVersion()

o.Logger.WithFields(log.Fields{
"version": version.Version,
"commit": version.GitCommit,
}).Info("Starting Kargo Garbage Collector")
o.Logger.Info(
"Starting Kargo Garbage Collector",
"version", version.Version,
"commit", version.GitCommit,
)

mgr, err := o.setupManager(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/controlplane/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func main() {
ctx := signals.SetupSignalHandler()
if err := Execute(ctx); err != nil {
logging.LoggerFromContext(ctx).Error(err)
logging.LoggerFromContext(ctx).Error(err, "")
os.Exit(1)
}
}
17 changes: 10 additions & 7 deletions cmd/controlplane/management_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -17,19 +16,22 @@ import (
"github.com/akuity/kargo/internal/api/kubernetes"
"github.com/akuity/kargo/internal/controller/management/namespaces"
"github.com/akuity/kargo/internal/controller/management/projects"
"github.com/akuity/kargo/internal/logging"
"github.com/akuity/kargo/internal/os"
versionpkg "github.com/akuity/kargo/internal/version"
)

type managementControllerOptions struct {
KubeConfig string

Logger *log.Logger
Logger *logging.Logger
}

func newManagementControllerCommand() *cobra.Command {
cmdOpts := &managementControllerOptions{
Logger: log.StandardLogger(),
// During startup, we enforce use of an info-level logger to ensure that
// no important startup messages are missed.
Logger: logging.NewLogger(logging.InfoLevel),
}

cmd := &cobra.Command{
Expand All @@ -54,10 +56,11 @@ func (o *managementControllerOptions) complete() {
func (o *managementControllerOptions) run(ctx context.Context) error {
version := versionpkg.GetVersion()

o.Logger.WithFields(log.Fields{
"version": version.Version,
"commit": version.GitCommit,
}).Info("Starting Kargo Management Controller")
o.Logger.Info(
"Starting Kargo Management Controller",
"version", version.Version,
"commit", version.GitCommit,
)

kargoMgr, err := o.setupManager(ctx)
if err != nil {
Expand Down
17 changes: 10 additions & 7 deletions cmd/controlplane/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
authzv1 "k8s.io/api/authorization/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -17,6 +16,7 @@ import (
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/api/kubernetes"
"github.com/akuity/kargo/internal/kubeclient"
"github.com/akuity/kargo/internal/logging"
"github.com/akuity/kargo/internal/os"
versionpkg "github.com/akuity/kargo/internal/version"
libWebhook "github.com/akuity/kargo/internal/webhook"
Expand All @@ -30,12 +30,14 @@ import (
type webhooksServerOptions struct {
KubeConfig string

Logger *log.Logger
Logger *logging.Logger
}

func newWebhooksServerCommand() *cobra.Command {
cmdOpts := &webhooksServerOptions{
Logger: log.StandardLogger(),
// During startup, we enforce use of an info-level logger to ensure that
// no important startup messages are missed.
Logger: logging.NewLogger(logging.InfoLevel),
}

cmd := &cobra.Command{
Expand All @@ -59,10 +61,11 @@ func (o *webhooksServerOptions) complete() {

func (o *webhooksServerOptions) run(ctx context.Context) error {
version := versionpkg.GetVersion()
o.Logger.WithFields(log.Fields{
"version": version.Version,
"commit": version.GitCommit,
}).Info("Starting Kargo Webhooks Server")
o.Logger.Info(
"Starting Kargo Webhooks Server",
"version", version.Version,
"commit", version.GitCommit,
)

webhookCfg := libWebhook.ConfigFromEnv()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-jose/go-jose/v4 v4.0.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.1
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand Down
5 changes: 4 additions & 1 deletion internal/api/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,10 @@ func GetRestConfig(ctx context.Context, path string) (*rest.Config, error) {
return cfg, nil
}

logger.WithField("path", path).Debug("loading REST config from path")
logger.Debug(
"loading REST config from path",
"path", path,
)
cfg, err := clientcmd.BuildConfigFromFlags("", path)
if err != nil {
return cfg, fmt.Errorf("error loading REST config from %q: %w", path, err)
Expand Down
Loading
Loading