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

deps: convert zap to slog #2825

Merged
merged 54 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
6cc282b
rewrote some logger helper functions
miampf Dec 29, 2023
1d0c6aa
added a test writer
miampf Dec 29, 2023
48d5a15
rewrote doc/description strings in logger
miampf Jan 3, 2024
f16ccf5
rewrote packages
miampf Dec 29, 2023
8dc6ae0
simplified logger package
miampf Jan 8, 2024
3b6feb0
found solution for grpc logger caller skip
miampf Jan 8, 2024
d802001
removed forgotten zap references
miampf Jan 8, 2024
0b0d3f7
remove unused dependencies
miampf Jan 8, 2024
d61f616
Changed TODO comments to reflect new ideas
miampf Jan 8, 2024
e40a99c
fixup after rebase
miampf Jan 10, 2024
0e4d220
added and used LevelHandler
miampf Jan 10, 2024
7cabd03
rewrote documenting comment for logger package
miampf Jan 10, 2024
c116858
dont rewrite operators and dont write replaceAttr function
miampf Jan 10, 2024
69cbd15
fixed some general problems
miampf Jan 10, 2024
69556e8
convert byte array to string to log test correctly
miampf Jan 10, 2024
941889a
fixed final issues
miampf Jan 15, 2024
3f51297
rewrote docs
miampf Jan 15, 2024
5bcd295
bazel tidy
miampf Jan 15, 2024
d6f534d
rewrote disk-mapper integration test
miampf Jan 15, 2024
00a2c98
bazel run //:tidy
miampf Jan 22, 2024
eb69861
use correct handler in disk-mapper benchmark
miampf Jan 22, 2024
944bded
remove mention of nonexistent `Fatal()` from docs
miampf Jan 22, 2024
76ada50
Wrote and applied helper function for tests
miampf Jan 22, 2024
7f0c697
set level instead of logger in middleware
miampf Jan 22, 2024
3e6375e
move LevelHandler to own file
miampf Jan 22, 2024
700d886
use helper functions in grpclog to reduce duplication
miampf Jan 22, 2024
a2ded13
rewrote internal/grpc/grpclog
miampf Jan 22, 2024
35755c6
readded libraries to integration tests
miampf Jan 22, 2024
0c92fa9
updated docs against format strings + correct function `With()`
miampf Jan 22, 2024
d4b4992
reworded for godoc
miampf Jan 22, 2024
0b75b42
use Stderr instead of Stdout for logging
miampf Jan 22, 2024
d80383d
added copyright notice
miampf Jan 22, 2024
972f832
tidy
malt3 Jan 22, 2024
081d357
fixed issues detected by bazel check
miampf Jan 22, 2024
8a246a0
updated conventions.md to more idiomatic suggestion
miampf Jan 24, 2024
f875466
fixed issues from rebase
miampf Jan 24, 2024
62f2e44
log with LevelError if unknown level is used for middlewareLogger
miampf Jan 29, 2024
747af57
rewrote use of `With()` in dev-docs
miampf Jan 29, 2024
e94a04c
Verbosity numbers now correspond to slog log Levels
miampf Jan 29, 2024
8204e06
clean up the middleware logger with `r.Add()`
miampf Jan 29, 2024
4854e97
rewrote apply.go warnLog/warnLogger
miampf Jan 29, 2024
c8b0175
replaced format with msg in needed parameters
miampf Jan 29, 2024
f8a3d65
bazel tidy
miampf Jan 29, 2024
e14f767
fixed usage of Debugf due to rebase
miampf Jan 31, 2024
f8a1d92
go back to old verbosity conversion
miampf Jan 31, 2024
f7d2e1c
added comments to avoid confusion
miampf Jan 31, 2024
1231785
add period to comments
miampf Jan 31, 2024
5d6d3ab
rewrote every slog logger to add source
miampf Jan 31, 2024
e435211
added new point in conventions.md to use provided helper functions
miampf Jan 31, 2024
b0bbea8
also add source to Test logger
miampf Jan 31, 2024
cc980b5
gazelle_generate + tidy
miampf Jan 31, 2024
2cd44b2
fixed issues from last rebase
miampf Feb 8, 2024
348ee1c
removed unused os in benchmark
miampf Feb 8, 2024
aa6d14e
tidy
miampf Feb 8, 2024
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
1 change: 0 additions & 1 deletion bootstrapper/cmd/bootstrapper/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ go_library(
"//internal/versions/components",
"@com_github_spf13_afero//:afero",
"@io_k8s_kubernetes//cmd/kubeadm/app/apis/kubeadm/v1beta3",
"@org_uber_go_zap//:zap",
],
)

Expand Down
33 changes: 20 additions & 13 deletions bootstrapper/cmd/bootstrapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ package main
import (
"context"
"flag"
"fmt"
"io"
"log/slog"
"os"
"strconv"

"github.com/spf13/afero"
"go.uber.org/zap"

"github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubernetes"
"github.com/edgelesssys/constellation/v2/bootstrapper/internal/kubernetes/k8sapi"
Expand Down Expand Up @@ -45,13 +46,12 @@ func main() {
gRPCDebug := flag.Bool("debug", false, "Enable gRPC debug logging")
verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription)
flag.Parse()
log := logger.New(logger.JSONLog, logger.VerbosityFromInt(*verbosity)).Named("bootstrapper")
defer log.Sync()
log := logger.NewJSONLogger(logger.VerbosityFromInt(*verbosity)).WithGroup("bootstrapper")

if *gRPCDebug {
log.Named("gRPC").ReplaceGRPCLogger()
logger.ReplaceGRPCLogger(log.WithGroup("gRPC"))
} else {
log.Named("gRPC").WithIncreasedLevel(zap.WarnLevel).ReplaceGRPCLogger()
logger.ReplaceGRPCLogger(slog.New(logger.NewLevelHandler(slog.LevelWarn, log.Handler())).WithGroup("gRPC"))
}

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -66,18 +66,21 @@ func main() {

attestVariant, err := variant.FromString(os.Getenv(constants.AttestationVariant))
if err != nil {
log.With(zap.Error(err)).Fatalf("Failed to parse attestation variant")
log.With(slog.Any("error", err)).Error("Failed to parse attestation variant")
os.Exit(1)
}
issuer, err := choose.Issuer(attestVariant, log)
if err != nil {
log.With(zap.Error(err)).Fatalf("Failed to select issuer")
log.With(slog.Any("error", err)).Error("Failed to select issuer")
os.Exit(1)
}

switch cloudprovider.FromString(os.Getenv(constellationCSP)) {
case cloudprovider.AWS:
metadata, err := awscloud.New(ctx)
if err != nil {
log.With(zap.Error(err)).Fatalf("Failed to set up AWS metadata API")
log.With(slog.Any("error", err)).Error("Failed to set up AWS metadata API")
os.Exit(1)
}
metadataAPI = metadata

Expand All @@ -91,7 +94,8 @@ func main() {
case cloudprovider.GCP:
metadata, err := gcpcloud.New(ctx)
if err != nil {
log.With(zap.Error(err)).Fatalf("Failed to create GCP metadata client")
log.With(slog.Any("error", err)).Error("Failed to create GCP metadata client")
os.Exit(1)
}
defer metadata.Close()

Expand All @@ -106,11 +110,13 @@ func main() {
case cloudprovider.Azure:
metadata, err := azurecloud.New(ctx)
if err != nil {
log.With(zap.Error(err)).Fatalf("Failed to create Azure metadata client")
log.With(slog.Any("error", err)).Error("Failed to create Azure metadata client")
os.Exit(1)
}

if err := metadata.PrepareControlPlaneNode(ctx, log); err != nil {
log.With(zap.Error(err)).Fatalf("Failed to prepare Azure control plane node")
log.With(slog.Any("error", err)).Error("Failed to prepare Azure control plane node")
os.Exit(1)
}

metadataAPI = metadata
Expand Down Expand Up @@ -138,13 +144,14 @@ func main() {
return tdx.Open()
}
default:
log.Fatalf("Unsupported attestation variant: %s", attestVariant)
log.Error(fmt.Sprintf("Unsupported attestation variant: %s", attestVariant))
}
fs = afero.NewOsFs()
case cloudprovider.OpenStack:
metadata, err := openstackcloud.New(ctx)
if err != nil {
log.With(zap.Error(err)).Fatalf("Failed to create OpenStack metadata client")
log.With(slog.Any("error", err)).Error("Failed to create OpenStack metadata client")
os.Exit(1);
}
clusterInitJoiner = kubernetes.New(
"openstack", k8sapi.NewKubernetesUtil(), &k8sapi.KubdeadmConfiguration{}, kubectl.NewUninitialized(),
Expand Down
27 changes: 16 additions & 11 deletions bootstrapper/cmd/bootstrapper/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ package main

import (
"context"
"fmt"
"log/slog"
"net"
"os"

"github.com/edgelesssys/constellation/v2/bootstrapper/internal/clean"
"github.com/edgelesssys/constellation/v2/bootstrapper/internal/diskencryption"
Expand All @@ -21,39 +24,40 @@ import (
"github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/file"
"github.com/edgelesssys/constellation/v2/internal/grpc/dialer"
"github.com/edgelesssys/constellation/v2/internal/logger"
"go.uber.org/zap"
)

func run(issuer atls.Issuer, openDevice vtpm.TPMOpenFunc, fileHandler file.Handler,
kube clusterInitJoiner, metadata metadataAPI,
bindIP, bindPort string, log *logger.Logger,
bindIP, bindPort string, log *slog.Logger,
) {
log.With(zap.String("version", constants.BinaryVersion().String())).Infof("Starting bootstrapper")
log.With(slog.String("version", constants.BinaryVersion().String())).Info("Starting bootstrapper")

uuid, err := getDiskUUID()
if err != nil {
log.With(zap.Error(err)).Errorf("Failed to get disk UUID")
log.With(slog.Any("error", err)).Error("Failed to get disk UUID")
} else {
log.Infof("Disk UUID: %s", uuid)
log.Info(fmt.Sprintf("Disk UUID: %s", uuid))
}

nodeBootstrapped, err := initialize.IsNodeBootstrapped(openDevice)
if err != nil {
log.With(zap.Error(err)).Fatalf("Failed to check if node was previously bootstrapped")
log.With(slog.Any("error", err)).Error("Failed to check if node was previously bootstrapped")
os.Exit(1)
}

if nodeBootstrapped {
if err := kube.StartKubelet(); err != nil {
log.With(zap.Error(err)).Fatalf("Failed to restart kubelet")
log.With(slog.Any("error", err)).Error("Failed to restart kubelet")
os.Exit(1)
}
return
}

nodeLock := nodelock.New(openDevice)
initServer, err := initserver.New(context.Background(), nodeLock, kube, issuer, fileHandler, metadata, log)
if err != nil {
log.With(zap.Error(err)).Fatalf("Failed to create init server")
log.With(slog.Any("error", err)).Error("Failed to create init server")
os.Exit(1)
}

dialer := dialer.New(issuer, nil, &net.Dialer{})
Expand All @@ -66,10 +70,11 @@ func run(issuer atls.Issuer, openDevice vtpm.TPMOpenFunc, fileHandler file.Handl
joinClient.Start(cleaner)

if err := initServer.Serve(bindIP, bindPort, cleaner); err != nil {
log.With(zap.Error(err)).Fatalf("Failed to serve init server")
log.With(slog.Any("error", err)).Error("Failed to serve init server")
os.Exit(1)
}

log.Infof("bootstrapper done")
log.Info("bootstrapper done")
}

func getDiskUUID() (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions bootstrapper/cmd/bootstrapper/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ package main

import (
"context"
"log/slog"

"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
"github.com/edgelesssys/constellation/v2/internal/logger"
"github.com/edgelesssys/constellation/v2/internal/role"
"github.com/edgelesssys/constellation/v2/internal/versions/components"
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
Expand All @@ -22,13 +22,13 @@ type clusterFake struct{}
// InitCluster fakes bootstrapping a new cluster with the current node being the master, returning the arguments required to join the cluster.
func (c *clusterFake) InitCluster(
context.Context, string, string,
bool, components.Components, []string, string, *logger.Logger,
bool, components.Components, []string, string, *slog.Logger,
) ([]byte, error) {
return []byte{}, nil
}

// JoinCluster will fake joining the current node to an existing cluster.
func (c *clusterFake) JoinCluster(context.Context, *kubeadm.BootstrapTokenDiscovery, role.Role, components.Components, *logger.Logger) error {
func (c *clusterFake) JoinCluster(context.Context, *kubeadm.BootstrapTokenDiscovery, role.Role, components.Components, *slog.Logger) error {
return nil
}

Expand Down
1 change: 0 additions & 1 deletion bootstrapper/internal/initserver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ go_library(
"@org_golang_google_grpc//keepalive",
"@org_golang_google_grpc//status",
"@org_golang_x_crypto//bcrypt",
"@org_uber_go_zap//:zap",
],
)

Expand Down
26 changes: 13 additions & 13 deletions bootstrapper/internal/initserver/initserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net"
"strings"
"sync"
Expand All @@ -43,7 +44,6 @@ import (
"github.com/edgelesssys/constellation/v2/internal/nodestate"
"github.com/edgelesssys/constellation/v2/internal/role"
"github.com/edgelesssys/constellation/v2/internal/versions/components"
"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand All @@ -68,16 +68,16 @@ type Server struct {

kmsURI string

log *logger.Logger
log *slog.Logger

journaldCollector journaldCollection

initproto.UnimplementedAPIServer
}

// New creates a new initialization server.
func New(ctx context.Context, lock locker, kube ClusterInitializer, issuer atls.Issuer, fh file.Handler, metadata MetadataAPI, log *logger.Logger) (*Server, error) {
log = log.Named("initServer")
func New(ctx context.Context, lock locker, kube ClusterInitializer, issuer atls.Issuer, fh file.Handler, metadata MetadataAPI, log *slog.Logger) (*Server, error) {
log = log.WithGroup("initServer")

initSecretHash, err := metadata.InitSecretHash(ctx)
if err != nil {
Expand Down Expand Up @@ -106,7 +106,7 @@ func New(ctx context.Context, lock locker, kube ClusterInitializer, issuer atls.
grpcServer := grpc.NewServer(
grpc.Creds(atlscredentials.New(issuer, nil)),
grpc.KeepaliveParams(keepalive.ServerParameters{Time: 15 * time.Second}),
log.Named("gRPC").GetServerUnaryInterceptor(),
logger.GetServerUnaryInterceptor(log.WithGroup("gRPC")),
)
initproto.RegisterAPIServer(grpcServer, server)

Expand All @@ -122,7 +122,7 @@ func (s *Server) Serve(ip, port string, cleaner cleaner) error {
return fmt.Errorf("failed to listen: %w", err)
}

s.log.Infof("Starting")
s.log.Info("Starting")
return s.grpcServer.Serve(lis)
}

Expand All @@ -132,8 +132,8 @@ func (s *Server) Init(req *initproto.InitRequest, stream initproto.API_InitServe
s.shutdownLock.RLock()
defer s.shutdownLock.RUnlock()

log := s.log.With(zap.String("peer", grpclog.PeerAddrFromContext(stream.Context())))
log.Infof("Init called")
log := s.log.With(slog.String("peer", grpclog.PeerAddrFromContext(stream.Context())))
log.Info("Init called")

s.kmsURI = req.KmsUri

Expand Down Expand Up @@ -174,7 +174,7 @@ func (s *Server) Init(req *initproto.InitRequest, stream initproto.API_InitServe
// init does not make sense, so we just stop.
//
// The server stops itself after the current call is done.
log.Warnf("Node is already in a join process")
log.Warn("Node is already in a join process")

err = status.Error(codes.FailedPrecondition, "node is already being activated")

Expand Down Expand Up @@ -228,7 +228,7 @@ func (s *Server) Init(req *initproto.InitRequest, stream initproto.API_InitServe
return err
}

log.Infof("Init succeeded")
log.Info("Init succeeded")

successMessage := &initproto.InitResponse_InitSuccess{
InitSuccess: &initproto.InitSuccessResponse{
Expand Down Expand Up @@ -287,14 +287,14 @@ func (s *Server) sendLogsWithMessage(stream initproto.API_InitServer, message er

// Stop stops the initialization server gracefully.
func (s *Server) Stop() {
s.log.Infof("Stopping")
s.log.Info("Stopping")

// Make sure to only stop the server if no Init calls are running
s.shutdownLock.Lock()
defer s.shutdownLock.Unlock()
s.grpcServer.GracefulStop()

s.log.Infof("Stopped")
s.log.Info("Stopped")
}

func (s *Server) setupDisk(ctx context.Context, cloudKms kms.CloudKMS) error {
Expand Down Expand Up @@ -342,7 +342,7 @@ type ClusterInitializer interface {
kubernetesComponents components.Components,
apiServerCertSANs []string,
serviceCIDR string,
log *logger.Logger,
log *slog.Logger,
) ([]byte, error)
}

Expand Down
3 changes: 2 additions & 1 deletion bootstrapper/internal/initserver/initserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"errors"
"io"
"log/slog"
"net"
"strings"
"sync"
Expand Down Expand Up @@ -408,7 +409,7 @@ type stubClusterInitializer struct {

func (i *stubClusterInitializer) InitCluster(
context.Context, string, string,
bool, components.Components, []string, string, *logger.Logger,
bool, components.Components, []string, string, *slog.Logger,
) ([]byte, error) {
return i.initClusterKubeconfig, i.initClusterErr
}
Expand Down
2 changes: 0 additions & 2 deletions bootstrapper/internal/joinclient/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ go_library(
"//internal/cloud/metadata",
"//internal/constants",
"//internal/file",
"//internal/logger",
"//internal/nodestate",
"//internal/role",
"//internal/versions/components",
Expand All @@ -23,7 +22,6 @@ go_library(
"@io_k8s_kubernetes//cmd/kubeadm/app/constants",
"@io_k8s_utils//clock",
"@org_golang_google_grpc//:go_default_library",
"@org_uber_go_zap//:zap",
],
)

Expand Down
Loading
Loading