Create an env using api
This commit is contained in:
parent
b9a1fcd02a
commit
affabc3c03
@ -78,4 +78,3 @@ func populateAccount(data *controllers.AccountData, controller ctrl.Manager) *co
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
|
||||
proto_email "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
)
|
||||
@ -15,13 +17,14 @@ type EmailServer struct {
|
||||
proto_email.UnimplementedEmailValidationServer
|
||||
emailConfig email.EmailConf
|
||||
controller ctrl.Manager
|
||||
|
||||
devMode bool
|
||||
}
|
||||
|
||||
func InitEmailServer(controller ctrl.Manager, emailConfig *email.EmailConf) *EmailServer {
|
||||
func InitEmailServer(controller ctrl.Manager, emailConfig *email.EmailConf, devMode bool) *EmailServer {
|
||||
return &EmailServer{
|
||||
controller: controller,
|
||||
emailConfig: *emailConfig,
|
||||
devMode: devMode,
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,11 +35,22 @@ func (c *EmailServer) SendRequest(ctx context.Context, in *proto_email.RequestVa
|
||||
},
|
||||
EmailConfig: c.emailConfig,
|
||||
Controller: c.controller,
|
||||
DevMode: c.devMode,
|
||||
}
|
||||
err := emailSvc.SendVerification(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.devMode {
|
||||
header := metadata.Pairs("code", emailSvc.Data.Code)
|
||||
if err := grpc.SendHeader(ctx, header); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
trailer := metadata.Pairs("trailer-key", "val")
|
||||
if err := grpc.SetTrailer(ctx, trailer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
|
@ -2,28 +2,51 @@ package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
||||
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
"google.golang.org/grpc/metadata"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
)
|
||||
|
||||
func NewapiGrpcImpl() *EnvironmentsServer {
|
||||
return &EnvironmentsServer{}
|
||||
func NewapiGrpcImpl(controller ctrl.Manager) *EnvironmentsServer {
|
||||
return &EnvironmentsServer{
|
||||
controller: controller,
|
||||
}
|
||||
}
|
||||
|
||||
type EnvironmentsServer struct {
|
||||
proto.UnimplementedEnvironmentsServer
|
||||
controller ctrl.Manager
|
||||
}
|
||||
|
||||
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.EnvironmentData) (*proto.EnvironmentFull, error) {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return nil, errors.New("metadata is not provided")
|
||||
}
|
||||
token, ok := md["token"]
|
||||
if !ok {
|
||||
return nil, errors.New("token is not sent via metadata")
|
||||
}
|
||||
|
||||
uuid, ok := md["uuid"]
|
||||
if !ok {
|
||||
return nil, errors.New("used id is not sent via metadata")
|
||||
}
|
||||
|
||||
data := &controllers.EnvironemntData{
|
||||
Name: in.GetName(),
|
||||
Provider: in.GetProvider().String(),
|
||||
Kubernetes: in.GetKubernetes().String(),
|
||||
}
|
||||
environment := &controllers.Environemnt{
|
||||
Controller: nil,
|
||||
UserID: uuid[0],
|
||||
Controller: e.controller,
|
||||
Data: data,
|
||||
Token: token[0],
|
||||
}
|
||||
err := environment.Create(ctx)
|
||||
if err != nil {
|
||||
|
4
dataexample.yaml
Normal file
4
dataexample.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
provider: hetzner
|
||||
servers:
|
||||
- name: some name
|
||||
kind: cax11
|
6
go.mod
6
go.mod
@ -9,6 +9,7 @@ require (
|
||||
golang.org/x/crypto v0.21.0
|
||||
k8s.io/api v0.29.3
|
||||
k8s.io/apimachinery v0.29.3
|
||||
k8s.io/client-go v0.29.3
|
||||
sigs.k8s.io/controller-runtime v0.17.2
|
||||
)
|
||||
|
||||
@ -55,7 +56,6 @@ require (
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
k8s.io/apiextensions-apiserver v0.29.3 // indirect
|
||||
k8s.io/client-go v0.29.3 // indirect
|
||||
k8s.io/component-base v0.29.3 // indirect
|
||||
k8s.io/klog/v2 v2.120.1 // indirect
|
||||
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
|
||||
@ -66,12 +66,12 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.2
|
||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.3
|
||||
github.com/golang/protobuf v1.5.4
|
||||
golang.org/x/net v0.22.0 // indirect
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
|
||||
google.golang.org/grpc v1.62.1
|
||||
google.golang.org/protobuf v1.33.0 // indirect
|
||||
google.golang.org/protobuf v1.33.0
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -1,5 +1,5 @@
|
||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.2 h1:kKL9lOWIRzzkf9mJCVQWOCj6AAodwm/IeOaIDBs5J+8=
|
||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.2/go.mod h1:OU+833cHwvecr+gsnPEKQYlAJbpL8bqSJVLobdw63qI=
|
||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.3 h1:c0q0nz9bT1TXUaMI7G+RII6mzZI1Tg0xWn2ToKekh4c=
|
||||
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.3/go.mod h1:OU+833cHwvecr+gsnPEKQYlAJbpL8bqSJVLobdw63qI=
|
||||
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
|
||||
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
||||
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=
|
||||
|
9
internal/consts/consts.go
Normal file
9
internal/consts/consts.go
Normal file
@ -0,0 +1,9 @@
|
||||
package consts
|
||||
|
||||
const (
|
||||
USERNAME_LABEL_KEY = "username"
|
||||
EMAIL_VERIFIED_LABEL_KEY = "email-verified"
|
||||
EMAIL_VERIFIED_LABEL_TRUE = "true"
|
||||
EMAIL_VERIFIED_LABEL_FALSE = "false"
|
||||
SOFTPLAYER_ACCOUNTS_NAMESPACE = "softplayer-accounts"
|
||||
)
|
@ -113,7 +113,6 @@ func (acc *Account) Create(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
rb := &rbacv1.RoleBinding{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: acc.Data.UUID,
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
|
||||
@ -20,6 +21,7 @@ type EmailSvc struct {
|
||||
Controller ctrl.Manager
|
||||
Data EmailData
|
||||
EmailConfig email.EmailConf
|
||||
DevMode bool
|
||||
}
|
||||
|
||||
type EmailData struct {
|
||||
@ -48,11 +50,14 @@ func (svc *EmailSvc) SendVerification(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
number := encodeToString(6)
|
||||
svc.Data.Code = number
|
||||
if !svc.DevMode {
|
||||
emailContent := "Subject: Softplayer verification code\r\n" + "\r\n" + fmt.Sprintf("Your verification code is %s", number)
|
||||
email := string(accountData.Data["email"])
|
||||
if err := svc.EmailConfig.SendEmail(email, number); err != nil {
|
||||
if err := svc.EmailConfig.SendEmail(email, emailContent); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
emailCode := corev1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "email-verification-code",
|
||||
@ -94,6 +99,7 @@ func (svc *EmailSvc) ConfirmVerification(ctx context.Context) error {
|
||||
}, userns); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
userns.Labels["email-verified"] = "true"
|
||||
if err := client.Update(ctx, userns); err != nil {
|
||||
return err
|
||||
|
@ -2,26 +2,104 @@ package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/rest"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
)
|
||||
|
||||
type Environemnt struct {
|
||||
Controller ctrl.Manager
|
||||
UserID string
|
||||
Data *EnvironemntData
|
||||
Token string
|
||||
}
|
||||
|
||||
type EnvironemntData struct {
|
||||
Name string
|
||||
Provider string
|
||||
Kubernetes string
|
||||
HetznerData HetznerData
|
||||
}
|
||||
|
||||
func (env *Environemnt) Create(ctx context.Context) error {
|
||||
log.Printf("%s", env.Data.Name)
|
||||
log.Printf("%s", env.Data.Provider)
|
||||
type HetznerData struct {
|
||||
ServerLocation string
|
||||
ServerType string
|
||||
}
|
||||
|
||||
func (e *EnvironemntData) buildVars() string {
|
||||
vars := fmt.Sprintf("SP_PROVIDER=%s\nSP_KUBERNETES=%s", e.providerFmt(), e.kubernetesFmt())
|
||||
return vars
|
||||
}
|
||||
|
||||
func (e *EnvironemntData) providerFmt() string {
|
||||
res := strings.Replace(e.Provider, "PROVIDER_", "", -1)
|
||||
return strings.ToLower(res)
|
||||
}
|
||||
|
||||
func (e *EnvironemntData) kubernetesFmt() string {
|
||||
res := strings.Replace(e.Kubernetes, "KUBERNETES", "", -1)
|
||||
return strings.ToLower(res)
|
||||
}
|
||||
|
||||
func (env *Environemnt) isNsVerified(ctx context.Context) error {
|
||||
client := env.Controller.GetClient()
|
||||
ns := &corev1.Namespace{}
|
||||
if err := client.Get(ctx, types.NamespacedName{Name: env.UserID}, ns); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
val, ok := ns.GetLabels()["email-verified"]
|
||||
if !ok || val == "false" {
|
||||
return errors.New("User email is not verified, can't create an new env")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if err := env.isNsVerified(ctx); err != nil {
|
||||
log.Println("Can't verify ns")
|
||||
return err
|
||||
}
|
||||
env.Controller.GetClient()
|
||||
conf := &rest.Config{
|
||||
Host: "https://kubernetes.default.svc.cluster.local:443",
|
||||
BearerToken: env.Token,
|
||||
TLSClientConfig: rest.TLSClientConfig{
|
||||
Insecure: true,
|
||||
},
|
||||
}
|
||||
|
||||
controller, err := ctrl.NewManager(conf, ctrl.Options{})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
obj := corev1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: env.Data.Name,
|
||||
Namespace: env.UserID,
|
||||
Labels: map[string]string{
|
||||
"component": "bootstrap",
|
||||
},
|
||||
},
|
||||
Data: map[string]string{
|
||||
"vars": env.Data.buildVars(),
|
||||
},
|
||||
}
|
||||
if err := kube.Create(ctx, controller.GetClient(), &obj, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create a configmap
|
||||
//
|
||||
return nil
|
||||
}
|
||||
|
@ -52,4 +52,3 @@ func WaitUntilCreated(ctx context.Context, client client.Client, obj client.Obje
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,8 @@
|
||||
package kube_test
|
||||
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
|
||||
"github.com/alecthomas/assert/v2"
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
9
internal/providers/hetzner.go
Normal file
9
internal/providers/hetzner.go
Normal file
@ -0,0 +1,9 @@
|
||||
package providers
|
||||
|
||||
// Hetzner supported regions
|
||||
const (
|
||||
HETZNER_REG_FINLAND = "fn"
|
||||
)
|
||||
|
||||
type Hetzner struct {
|
||||
}
|
11
main.go
11
main.go
@ -8,8 +8,8 @@ import (
|
||||
v1 "git.badhouseplants.net/softplayer/softplayer-backend/api/v1"
|
||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
|
||||
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/accounts"
|
||||
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
|
||||
email_proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email"
|
||||
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
|
||||
"github.com/alecthomas/kong"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
@ -17,10 +17,11 @@ import (
|
||||
)
|
||||
|
||||
type Serve struct {
|
||||
Port int16 `short:"p" env:"SOFTPLAYER_PORT" default:"4020"`
|
||||
Port int64 `short:"p" env:"SOFTPLAYER_PORT" default:"4020"`
|
||||
Host string `env:"SOFTPLAYER_HOST" default:"0.0.0.0"`
|
||||
HashCost int16 `env:"SOFTPLAYER_HASH_COST" default:"1"`
|
||||
Reflection bool `env:"SOFTPLAYER_REFLECTION" default:"false"`
|
||||
DevMode bool `env:"SOFTPLAYER_DEV_MODE" default:"false"`
|
||||
SmtpHost string `env:"SOFTPLAYER_SMTP_HOST"`
|
||||
SmtpPort string `env:"SOFTPLAYER_SMTP_PORT" default:"587"`
|
||||
SmtpFrom string `env:"SOFTPLAYER_SMTP_FROM" default:"overlord@badhouseplants.net"`
|
||||
@ -62,6 +63,7 @@ func server(params Serve) error {
|
||||
SmtpHost: params.SmtpHost,
|
||||
SmtpPort: params.SmtpPort,
|
||||
}
|
||||
|
||||
address := fmt.Sprintf("%s:%d", params.Host, params.Port)
|
||||
lis, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
@ -72,10 +74,9 @@ func server(params Serve) error {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
|
||||
|
||||
environments.RegisterEnvironmentsServer(grpcServer, v1.NewapiGrpcImpl())
|
||||
environments.RegisterEnvironmentsServer(grpcServer, v1.NewapiGrpcImpl(controller))
|
||||
accounts.RegisterAccountsServer(grpcServer, v1.NewAccountRPCImpl(controller, params.HashCost))
|
||||
email_proto.RegisterEmailValidationServer(grpcServer, v1.InitEmailServer(controller,&emailConfig))
|
||||
email_proto.RegisterEmailValidationServer(grpcServer, v1.InitEmailServer(controller, &emailConfig, params.DevMode))
|
||||
if err := grpcServer.Serve(lis); err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user