Skip to content

Commit

Permalink
Convert lib/cloud to use slog
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy committed Dec 17, 2024
1 parent 7384627 commit d4d6d7d
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 58 deletions.
33 changes: 19 additions & 14 deletions lib/cloud/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package aws

import (
"context"
"log/slog"
"slices"
"strings"

Expand All @@ -27,9 +29,9 @@ import (
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/redshift"
"github.com/coreos/go-semver/semver"
log "github.com/sirupsen/logrus"

"github.com/gravitational/teleport/lib/services"
logutils "github.com/gravitational/teleport/lib/utils/log"
)

// IsResourceAvailable checks if the input status indicates the resource is
Expand All @@ -38,7 +40,7 @@ import (
// Note that this function checks some common values but not necessarily covers
// everything. For types that have other known status values, separate
// functions (e.g. IsDBClusterAvailable) can be implemented.
func IsResourceAvailable(r interface{}, status *string) bool {
func IsResourceAvailable(r any, status *string) bool {
switch strings.ToLower(aws.StringValue(status)) {
case "available", "modifying", "snapshotting", "active":
return true
Expand All @@ -47,7 +49,10 @@ func IsResourceAvailable(r interface{}, status *string) bool {
return false

default:
log.WithField("aws_resource", r).Warnf("Unknown status type: %q. Assuming the AWS resource %T is available.", aws.StringValue(status), r)
slog.WarnContext(context.Background(), "Assuming that AWS resource with an unknown status is available",
"status", aws.StringValue(status),
"resource", logutils.TypeAttr(r),
)
return true
}
}
Expand Down Expand Up @@ -89,7 +94,7 @@ func IsRDSInstanceSupported(instance *rds.DBInstance) bool {
// MariaDB follows semver schema: https://mariadb.org/about/
ver, err := semver.NewVersion(aws.StringValue(instance.EngineVersion))
if err != nil {
log.Errorf("Failed to parse RDS MariaDB version: %s", aws.StringValue(instance.EngineVersion))
slog.ErrorContext(context.Background(), "Failed to parse RDS MariaDB version", "version", aws.StringValue(instance.EngineVersion))
return false
}

Expand Down Expand Up @@ -152,7 +157,7 @@ func AuroraMySQLVersion(cluster *rds.DBCluster) string {
func IsDocumentDBClusterSupported(cluster *rds.DBCluster) bool {
ver, err := semver.NewVersion(aws.StringValue(cluster.EngineVersion))
if err != nil {
log.Errorf("Failed to parse DocumentDB engine version: %s", aws.StringValue(cluster.EngineVersion))
slog.ErrorContext(context.Background(), "Failed to parse DocumentDB engine version", "version", aws.StringValue(cluster.EngineVersion))
return false
}

Expand Down Expand Up @@ -201,9 +206,9 @@ func IsRDSInstanceAvailable(instanceStatus, instanceIdentifier *string) bool {
return false

default:
log.Warnf("Unknown status type: %q. Assuming RDS instance %q is available.",
aws.StringValue(instanceStatus),
aws.StringValue(instanceIdentifier),
slog.WarnContext(context.Background(), "Assuming RDS instance with unknown status is available",
"status", aws.StringValue(instanceStatus),
"instance", aws.StringValue(instanceIdentifier),
)
return true
}
Expand All @@ -230,9 +235,9 @@ func IsDBClusterAvailable(clusterStatus, clusterIndetifier *string) bool {
return false

default:
log.Warnf("Unknown status type: %q. Assuming Aurora cluster %q is available.",
aws.StringValue(clusterStatus),
aws.StringValue(clusterIndetifier),
slog.WarnContext(context.Background(), "Assuming Aurora cluster with unknown status is available",
"status", aws.StringValue(clusterStatus),
"cluster", aws.StringValue(clusterIndetifier),
)
return true
}
Expand Down Expand Up @@ -264,9 +269,9 @@ func IsRedshiftClusterAvailable(cluster *redshift.Cluster) bool {
return false

default:
log.Warnf("Unknown status type: %q. Assuming Redshift cluster %q is available.",
aws.StringValue(cluster.ClusterStatus),
aws.StringValue(cluster.ClusterIdentifier),
slog.WarnContext(context.Background(), "Assuming Redshift cluster with unknown status is available",
"status", aws.StringValue(cluster.ClusterStatus),
"cluster", aws.StringValue(cluster.ClusterIdentifier),
)
return true
}
Expand Down
17 changes: 13 additions & 4 deletions lib/cloud/aws/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package aws
import (
"context"
"encoding/json"
"log/slog"
"net/url"
"slices"
"sort"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/aws/aws-sdk-go-v2/service/iam"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"

awsutils "github.com/gravitational/teleport/lib/utils/aws"
)
Expand Down Expand Up @@ -517,7 +517,10 @@ func (p *policies) Upsert(ctx context.Context, policy *Policy) (string, error) {
return "", trace.Wrap(err)
}

log.Debugf("Created new policy %q with ARN %q", policy.Name, policyARN)
slog.DebugContext(ctx, "Created new policy",
"policy_name", policy.Name,
"policy_arn", policyARN,
)
return policyARN, nil
}

Expand All @@ -543,7 +546,10 @@ func (p *policies) Upsert(ctx context.Context, policy *Policy) (string, error) {
return "", trace.Wrap(err)
}

log.Debugf("Max policy versions reached for policy %q, deleted policy version %q", policyARN, policyVersionID)
slog.DebugContext(ctx, "Max policy versions reached for policy, deleted non-default policy version",
"policy_arn", policyARN,
"policy_version", policyVersionID,
)
}

// Create new policy version.
Expand All @@ -552,7 +558,10 @@ func (p *policies) Upsert(ctx context.Context, policy *Policy) (string, error) {
return "", trace.Wrap(err)
}

log.Debugf("Created new policy version %q for %q", versionID, policyARN)
slog.DebugContext(ctx, "Created new policy version",
"policy_version", versionID,
"policy_arn", policyARN,
)
return policyARN, nil
}

Expand Down
5 changes: 3 additions & 2 deletions lib/cloud/aws/tags_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package aws

import (
"context"
"log/slog"
"slices"

ec2TypesV2 "github.com/aws/aws-sdk-go-v2/service/ec2/types"
Expand All @@ -32,7 +34,6 @@ import (
"github.com/aws/aws-sdk-go/service/redshift"
"github.com/aws/aws-sdk-go/service/redshiftserverless"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/sirupsen/logrus"
"golang.org/x/exp/maps"

"github.com/gravitational/teleport/api/types"
Expand Down Expand Up @@ -67,7 +68,7 @@ func TagsToLabels[Tag ResourceTag](tags []Tag) map[string]string {
if types.IsValidLabelKey(key) {
labels[key] = value
} else {
logrus.Debugf("Skipping AWS resource tag %q, not a valid label key.", key)
slog.DebugContext(context.Background(), "Skipping AWS resource tag with invalid label key", "key", key)
}
}
return labels
Expand Down
10 changes: 6 additions & 4 deletions lib/cloud/azure/db_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
package azure

import (
"context"
"log/slog"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/mysql/armmysql"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/postgresql/armpostgresql"
log "github.com/sirupsen/logrus"

"github.com/gravitational/teleport/lib/defaults"
)
Expand Down Expand Up @@ -120,9 +122,9 @@ func (s *DBServer) IsAvailable() bool {
case "Inaccessible", "Dropping", "Disabled":
return false
default:
log.Warnf("Unknown server state: %q. Assuming Azure DB server %q is available.",
s.Properties.UserVisibleState,
s.Name,
slog.WarnContext(context.Background(), "Assuming Azure DB server with unknown server state is available",
"state", s.Properties.UserVisibleState,
"server", s.Name,
)
return true
}
Expand Down
12 changes: 9 additions & 3 deletions lib/cloud/azure/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package azure
import (
"context"
"fmt"
"log/slog"
"strings"
"time"

Expand All @@ -33,7 +34,6 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v6"
"github.com/golang-jwt/jwt/v4"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/rbac/v1"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -195,7 +195,10 @@ func (c *aksClient) ListAll(ctx context.Context) ([]*AKSCluster, error) {
for _, s := range page.Value {
cluster, err := AKSClusterFromManagedCluster(s)
if err != nil {
logrus.WithError(err).Debugf("Failed to convert discovered AKS cluster %q to Teleport internal representation.", StringVal(s.Name))
slog.DebugContext(ctx, "Failed to convert discovered AKS cluster to Teleport internal representation",
"cluster", StringVal(s.Name),
"error", err,
)
continue
}
servers = append(servers, cluster)
Expand All @@ -217,7 +220,10 @@ func (c *aksClient) ListWithinGroup(ctx context.Context, group string) ([]*AKSCl
for _, s := range page.Value {
cluster, err := AKSClusterFromManagedCluster(s)
if err != nil {
logrus.WithError(err).Debugf("Failed to convert discovered AKS cluster %q to Teleport internal representation.", StringVal(s.Name))
slog.DebugContext(ctx, "Failed to convert discovered AKS cluster to Teleport internal representation",
"cluster", StringVal(s.Name),
"error", err,
)
continue
}
servers = append(servers, cluster)
Expand Down
8 changes: 4 additions & 4 deletions lib/cloud/azure/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package azure

import (
"context"
"log/slog"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/mysql/armmysql"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
)

var _ DBServersClient = (*mySQLClient)(nil)
Expand Down Expand Up @@ -87,9 +87,9 @@ func isMySQLVersionSupported(s *DBServer) bool {
case armmysql.ServerVersionFive6:
return false
default:
log.Warnf("Unknown server version: %q. Assuming Azure DB server %q is supported.",
s.Properties.Version,
s.Name,
slog.WarnContext(context.Background(), "Assuming Azure DB server with unknown server version is supported",
"version", s.Properties.Version,
"server", s.Name,
)
return true
}
Expand Down
2 changes: 0 additions & 2 deletions lib/cloud/azure/mysql_flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/mysql/armmysqlflexibleservers"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
)

// armMySQLFlexServersClient is an interface that defines a subset of functions of armmysqlflexibleservers.ServersClient.
Expand All @@ -44,7 +43,6 @@ var _ MySQLFlexServersClient = (*mySQLFlexServersClient)(nil)

// NewMySQLFlexServersClient creates a new Azure MySQL Flexible server client by subscription and credentials.
func NewMySQLFlexServersClient(subID string, cred azcore.TokenCredential, opts *arm.ClientOptions) (MySQLFlexServersClient, error) {
logrus.Debug("Initializing Azure MySQL Flexible servers client.")
api, err := armmysqlflexibleservers.NewServersClient(subID, cred, opts)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
2 changes: 0 additions & 2 deletions lib/cloud/azure/postgres_flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/postgresql/armpostgresqlflexibleservers"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
)

type armPostgresFlexServersClient interface {
Expand All @@ -44,7 +43,6 @@ var _ PostgresFlexServersClient = (*postgresFlexServersClient)(nil)

// NewPostgresFlexServersClient creates a new Azure PostgreSQL Flexible server client by subscription and credentials.
func NewPostgresFlexServersClient(subID string, cred azcore.TokenCredential, opts *arm.ClientOptions) (PostgresFlexServersClient, error) {
logrus.Debug("Initializing Azure PostgreSQL Flexible servers client.")
api, err := armpostgresqlflexibleservers.NewServersClient(subID, cred, opts)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
2 changes: 0 additions & 2 deletions lib/cloud/azure/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/redis/armredis/v3"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
)

// armRedisClient is an interface defines a subset of functions of armredis.Client.
Expand All @@ -43,7 +42,6 @@ type redisClient struct {

// NewRedisClient creates a new Azure Redis client by subscription and credentials.
func NewRedisClient(subscription string, cred azcore.TokenCredential, options *arm.ClientOptions) (RedisClient, error) {
logrus.Debug("Initializing Azure Redis client.")
api, err := armredis.NewClient(subscription, cred, options)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
12 changes: 8 additions & 4 deletions lib/cloud/azure/redis_enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ package azure
import (
"context"
"fmt"
"log/slog"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/redisenterprise/armredisenterprise"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
)

// armRedisEnterpriseDatabaseClient is an interface defines a subset of
Expand All @@ -53,7 +53,6 @@ type redisEnterpriseClient struct {
// NewRedisEnterpriseClient creates a new Azure Redis Enterprise client by
// subscription and credentials.
func NewRedisEnterpriseClient(subscription string, cred azcore.TokenCredential, options *arm.ClientOptions) (RedisEnterpriseClient, error) {
logrus.Debug("Initializing Azure Redis Enterprise client.")
clusterAPI, err := armredisenterprise.NewClient(subscription, cred, options)
if err != nil {
return nil, trace.Wrap(err)
Expand Down Expand Up @@ -160,9 +159,14 @@ func (c *redisEnterpriseClient) listDatabasesByClusters(ctx context.Context, clu
databases, err := c.listDatabasesByCluster(ctx, cluster)
if err != nil {
if trace.IsAccessDenied(err) || trace.IsNotFound(err) {
logrus.Debugf("Failed to listDatabasesByCluster on Redis Enterprise cluster %v: %v.", StringVal(cluster.Name), err.Error())
slog.DebugContext(ctx, "Failed to listDatabasesByCluster on Redis Enterprise cluster",
"cluster", StringVal(cluster.Name),
"error", err)
} else {
logrus.Warnf("Failed to listDatabasesByCluster on Redis Enterprise cluster %v: %v.", StringVal(cluster.Name), err.Error())
slog.WarnContext(ctx, "Failed to listDatabasesByCluster on Redis Enterprise cluster",
"cluster", StringVal(cluster.Name),
"error", err,
)
}
continue
}
Expand Down
Loading

0 comments on commit d4d6d7d

Please sign in to comment.