Start implementing a logger

This commit is contained in:
Nikolai Rodionov 2024-05-06 11:39:28 +02:00
parent 0cb8295df1
commit bcd62301e3
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
4 changed files with 33 additions and 6 deletions

View File

@ -7,23 +7,30 @@ import (
"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers/infra"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers/kubernetes"
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
"github.com/go-logr/logr"
"github.com/golang/protobuf/ptypes/empty"
ctrl "sigs.k8s.io/controller-runtime"
)
func NewapiGrpcImpl(controller ctrl.Manager) *EnvironmentsServer {
func NewapiGrpcImpl(controller ctrl.Manager, log logr.Logger) *EnvironmentsServer {
return &EnvironmentsServer{
controller: controller,
controller: controller,
logInstance: log,
}
}
type EnvironmentsServer struct {
proto.UnimplementedEnvironmentsServer
controller ctrl.Manager
controller ctrl.Manager
logInstance logr.Logger
}
// Create an environment
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.EnvironmentFull, error) {
log := e.logInstance
log.WithValues("user_id", in.GetOwnerId().GetUuid())
ctx = logr.NewContext(ctx, log)
provider, err := infra.GetProvider(in.GetSpec().GetProvider().String())
if err != nil {
return nil, err

6
go.mod
View File

@ -5,9 +5,12 @@ go 1.22.1
require (
github.com/alecthomas/assert/v2 v2.8.1
github.com/alecthomas/kong v0.9.0
github.com/go-logr/logr v1.4.1
github.com/go-logr/zapr v1.3.0
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/sirupsen/logrus v1.9.3
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.22.0
gopkg.in/yaml.v2 v2.4.0
helm.sh/helm/v3 v3.14.4
@ -52,7 +55,6 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
@ -122,7 +124,7 @@ require (
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/zap v1.27.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect

View File

@ -7,6 +7,7 @@ import (
"log"
"strings"
"github.com/go-logr/logr"
"github.com/google/uuid"
"github.com/joho/godotenv"
@ -225,6 +226,11 @@ func (env *Environemnt) Delete(ctx context.Context) error {
}
func (env *Environemnt) ListEnvs(ctx context.Context, searchString string) ([]*Environemnt, error) {
log, err := logr.FromContext(ctx)
if err != nil {
return nil, err
}
log.Info("Test a new logger")
env.Controller.GetClient()
conf := &rest.Config{
Host: "https://kubernetes.default.svc.cluster.local:443",

14
main.go
View File

@ -13,6 +13,9 @@ import (
email_proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email"
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
"github.com/alecthomas/kong"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
ctrl "sigs.k8s.io/controller-runtime"
@ -53,6 +56,15 @@ func server(params Serve) error {
return err
}
// Init a logger
var log logr.Logger
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log = zapr.NewLogger(zapLog)
controller, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
if err != nil {
return err
@ -82,7 +94,7 @@ func server(params Serve) error {
reflection.Register(grpcServer)
}
environments.RegisterEnvironmentsServer(grpcServer, v1.NewapiGrpcImpl(controller))
environments.RegisterEnvironmentsServer(grpcServer, v1.NewapiGrpcImpl(controller, log))
accounts.RegisterAccountsServer(grpcServer, v1.NewAccountRPCImpl(controller, params.HashCost))
email_proto.RegisterEmailValidationServer(grpcServer, v1.InitEmailServer(controller, &emailConfig, params.DevMode))
applications_proto.RegisterApplicationsServer(grpcServer, v1.NewApplicationsGrpcImpl(controller))