Start implementing a logger

This commit is contained in:
Nikolai Rodionov 2024-05-06 18:00:31 +02:00
parent 557b4dce44
commit da0f3da7e3
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
3 changed files with 100 additions and 30 deletions

View File

@ -202,7 +202,7 @@ func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environmen
Token: in.GetToken().GetToken(),
}
envs, err := environment.ListEnvs(ctx, in.GetSearchString())
envs, err := environment.List(ctx, in.GetSearchString())
if err != nil {
return err
}

View File

@ -1,5 +1,10 @@
package consts
import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
USERNAME_LABEL_KEY = "username"
EMAIL_VERIFIED_LABEL_KEY = "email-verified"
@ -7,3 +12,7 @@ const (
EMAIL_VERIFIED_LABEL_FALSE = "false"
SOFTPLAYER_ACCOUNTS_NAMESPACE = "softplayer-accounts"
)
var (
ErrSystemError = status.Error(codes.Internal, "a system error occured, we will try to fix it as soon as possible")
)

View File

@ -4,13 +4,17 @@ import (
"context"
"errors"
"fmt"
"log"
"strings"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/google/uuid"
"github.com/joho/godotenv"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/consts"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -57,13 +61,24 @@ SP_DISK_SIZE=%d`,
// Check whether used has passed the email verification
func (env *Environemnt) isNsVerified(ctx context.Context) error {
log, err := logr.FromContext(ctx)
if err != nil {
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log = zapr.NewLogger(zapLog)
}
clientset, err := kubernetes.NewForConfig(env.Config)
if err != nil {
return err
log.Error(err, "Couldn't create a new clientset")
return consts.ErrSystemError
}
ns, err := clientset.CoreV1().Namespaces().Get(ctx, env.UserID, metav1.GetOptions{})
if err != nil {
return err
log.Error("Couldn't get a user's namespace")
return consts.ErrSystemError
}
val, ok := ns.GetLabels()["email-verified"]
@ -77,9 +92,17 @@ func (env *Environemnt) isNsVerified(ctx context.Context) error {
// Create environment should create a new configmap in the user's namespace
// using a token that belongs to the user.
func (env *Environemnt) Create(ctx context.Context) error {
log, err := logr.FromContext(ctx)
if err != nil {
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log = zapr.NewLogger(zapLog)
}
if err := env.isNsVerified(ctx); err != nil {
log.Println("Can't verify ns")
return err
return status.Error(codes.Unauthenticated, err.Error())
}
// Prepare a new ID for a enironment
@ -95,14 +118,15 @@ func (env *Environemnt) Create(ctx context.Context) error {
}
controller, err := ctrl.NewManager(conf, ctrl.Options{})
if err != nil {
return err
log.Error(err, "Couldn't init a controller")
return consts.ErrSystemError
}
vars, err := env.Data.buildVars()
if err != nil {
return err
log.Error(err, "Couldn't build the environment's dotenv file", "environment_id", env.Data.UUID)
return consts.ErrSystemError
}
obj := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
@ -120,16 +144,21 @@ func (env *Environemnt) Create(ctx context.Context) error {
},
}
if err := kube.Create(ctx, controller.GetClient(), &obj, false); err != nil {
return err
log.Error(err, "Couln't create the environment's configmap", "environment_id", env.Data.UUID)
return consts.ErrSystemError
}
return nil
}
func (env *Environemnt) Update(ctx context.Context) error {
if err := env.isNsVerified(ctx); err != nil {
log.Println("Can't verify ns")
return err
log, err := logr.FromContext(ctx)
if err != nil {
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log = zapr.NewLogger(zapLog)
}
env.Controller.GetClient()
@ -142,9 +171,9 @@ func (env *Environemnt) Update(ctx context.Context) error {
}
controller, err := ctrl.NewManager(conf, ctrl.Options{})
if err != nil {
return err
log.Error(err, "Couldn't init a controller")
return consts.ErrSystemError
}
oldEnv := &Environemnt{
Controller: env.Controller,
@ -156,7 +185,8 @@ func (env *Environemnt) Update(ctx context.Context) error {
}
if err := oldEnv.Get(ctx); err != nil {
return err
log.Error(err, "Couldn't get environment's configmap", "environment_id", env.Data.UUID)
return consts.ErrSystemError
}
// Check whter immutable fields are changed
@ -170,7 +200,8 @@ func (env *Environemnt) Update(ctx context.Context) error {
vars, err := env.Data.buildVars()
if err != nil {
return err
log.Error(err, "Couldn't build the environment's dotenv file", "environment_id", env.Data.UUID)
return consts.ErrSystemError
}
obj := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
@ -189,12 +220,22 @@ func (env *Environemnt) Update(ctx context.Context) error {
}
if err := kube.Update(ctx, controller.GetClient(), &obj); err != nil {
return err
log.Error(err, "Couln't update the environment's configmap", "environment_id", env.Data.UUID)
return consts.ErrSystemError
}
return nil
}
func (env *Environemnt) Delete(ctx context.Context) error {
log, err := logr.FromContext(ctx)
if err != nil {
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log = zapr.NewLogger(zapLog)
}
env.Controller.GetClient()
conf := &rest.Config{
Host: "https://kubernetes.default.svc.cluster.local:443",
@ -205,9 +246,9 @@ func (env *Environemnt) Delete(ctx context.Context) error {
}
controller, err := ctrl.NewManager(conf, ctrl.Options{})
if err != nil {
return err
log.Error(err, "couldn't init a controller")
return consts.ErrSystemError
}
obj := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
@ -219,18 +260,23 @@ func (env *Environemnt) Delete(ctx context.Context) error {
},
}
if err := kube.Delete(ctx, controller.GetClient(), &obj, false); err != nil {
return err
log.Error(err, "Couln't remove environment's configmap", "environment_id", env.Data.UUID)
return consts.ErrSystemError
}
return nil
}
func (env *Environemnt) ListEnvs(ctx context.Context, searchString string) ([]*Environemnt, error) {
func (env *Environemnt) List(ctx context.Context, searchString string) ([]*Environemnt, error) {
log, err := logr.FromContext(ctx)
if err != nil {
return nil, err
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log.Info("Test a new logger")
log = zapr.NewLogger(zapLog)
}
env.Controller.GetClient()
conf := &rest.Config{
Host: "https://kubernetes.default.svc.cluster.local:443",
@ -241,11 +287,13 @@ func (env *Environemnt) ListEnvs(ctx context.Context, searchString string) ([]*E
}
clientset, err := kubernetes.NewForConfig(conf)
if err != nil {
return nil, err
log.Error(err, "Couldn't create a new clientset")
return nil, consts.ErrSystemError
}
configmaps, err := clientset.CoreV1().ConfigMaps(env.UserID).List(ctx, metav1.ListOptions{LabelSelector: "kind=environment"})
if err != nil {
return nil, err
log.Error(err, "Couldn't list configmaps")
return nil, consts.ErrSystemError
}
result := []*Environemnt{}
@ -259,7 +307,8 @@ func (env *Environemnt) ListEnvs(ctx context.Context, searchString string) ([]*E
i.Data = data
i.Controller = env.Controller
if err := i.Get(ctx); err != nil {
return nil, err
log.Error(err, "Couldn't get an environment", "environment_id", i.Data.UUID)
return nil, consts.ErrSystemError
}
if len(searchString) > 0 {
if strings.Contains(i.Data.Name, searchString) {
@ -276,6 +325,15 @@ func (env *Environemnt) ListEnvs(ctx context.Context, searchString string) ([]*E
}
func (env *Environemnt) Get(ctx context.Context) error {
log, err := logr.FromContext(ctx)
if err != nil {
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log = zapr.NewLogger(zapLog)
}
env.Controller.GetClient()
conf := &rest.Config{
Host: "https://kubernetes.default.svc.cluster.local:443",
@ -286,16 +344,19 @@ func (env *Environemnt) Get(ctx context.Context) error {
}
clientset, err := kubernetes.NewForConfig(conf)
if err != nil {
return err
log.Error(err, "Couldn't create a new clientset")
return consts.ErrSystemError
}
envData, err := clientset.CoreV1().ConfigMaps(env.UserID).Get(ctx, env.Data.UUID, metav1.GetOptions{})
if err != nil {
return err
log.Error(err, "Couldn't get an environment's configmap", "environment_id", env.Data.UUID)
return consts.ErrSystemError
}
res, err := godotenv.Unmarshal(envData.Data["vars"])
if err != nil {
return err
log.Error(err, "Couldn't parse environment's dotenv file from a configmap", "environment_id", env.Data.UUID)
return consts.ErrSystemError
}
if val, ok := envData.Data["name"]; ok {