Skip to content

Commit

Permalink
deps: convert zap to slog (#2825)
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf authored Feb 8, 2024
1 parent 3765cb0 commit 54cce77
Show file tree
Hide file tree
Showing 182 changed files with 1,474 additions and 1,509 deletions.
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

0 comments on commit 54cce77

Please sign in to comment.