Create an env using api
This commit is contained in:
parent
b9a1fcd02a
commit
affabc3c03
@ -21,7 +21,7 @@ func NewAccountRPCImpl(contoller ctrl.Manager, hashCost int16) *AccountsServer {
|
|||||||
type AccountsServer struct {
|
type AccountsServer struct {
|
||||||
accounts.UnimplementedAccountsServer
|
accounts.UnimplementedAccountsServer
|
||||||
Controller ctrl.Manager
|
Controller ctrl.Manager
|
||||||
Params *controllers.AccountParams
|
Params *controllers.AccountParams
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AccountsServer) SignUp(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
|
func (a *AccountsServer) SignUp(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
|
||||||
@ -78,4 +78,3 @@ func populateAccount(data *controllers.AccountData, controller ctrl.Manager) *co
|
|||||||
Data: data,
|
Data: data,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,47 +7,61 @@ import (
|
|||||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
||||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
|
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
|
||||||
proto_email "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/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"
|
"google.golang.org/protobuf/types/known/emptypb"
|
||||||
ctrl "sigs.k8s.io/controller-runtime"
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EmailServer struct {
|
type EmailServer struct {
|
||||||
proto_email.UnimplementedEmailValidationServer
|
proto_email.UnimplementedEmailValidationServer
|
||||||
emailConfig email.EmailConf
|
emailConfig email.EmailConf
|
||||||
controller ctrl.Manager
|
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{
|
return &EmailServer{
|
||||||
controller: controller,
|
controller: controller,
|
||||||
emailConfig: *emailConfig,
|
emailConfig: *emailConfig,
|
||||||
|
devMode: devMode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EmailServer) SendRequest(ctx context.Context, in *proto_email.RequestValidation) (*emptypb.Empty, error) {
|
func (c *EmailServer) SendRequest(ctx context.Context, in *proto_email.RequestValidation) (*emptypb.Empty, error) {
|
||||||
emailSvc := controllers.EmailSvc {
|
emailSvc := controllers.EmailSvc{
|
||||||
Data: controllers.EmailData{
|
Data: controllers.EmailData{
|
||||||
UserID: in.GetUserId(),
|
UserID: in.GetUserId(),
|
||||||
},
|
},
|
||||||
EmailConfig: c.emailConfig,
|
EmailConfig: c.emailConfig,
|
||||||
Controller: c.controller,
|
Controller: c.controller,
|
||||||
|
DevMode: c.devMode,
|
||||||
}
|
}
|
||||||
err := emailSvc.SendVerification(ctx)
|
err := emailSvc.SendVerification(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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
|
return &emptypb.Empty{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EmailServer) ValidateEmail(ctx context.Context, in *proto_email.ConfirmValidation) (*emptypb.Empty, error) {
|
func (c *EmailServer) ValidateEmail(ctx context.Context, in *proto_email.ConfirmValidation) (*emptypb.Empty, error) {
|
||||||
emailSvc := controllers.EmailSvc {
|
emailSvc := controllers.EmailSvc{
|
||||||
Data: controllers.EmailData{
|
Data: controllers.EmailData{
|
||||||
UserID: in.GetUserId(),
|
UserID: in.GetUserId(),
|
||||||
Code: fmt.Sprintf("%d", in.GetCode()),
|
Code: fmt.Sprintf("%d", in.GetCode()),
|
||||||
},
|
},
|
||||||
EmailConfig: c.emailConfig,
|
EmailConfig: c.emailConfig,
|
||||||
Controller: c.controller,
|
Controller: c.controller,
|
||||||
}
|
}
|
||||||
err := emailSvc.ConfirmVerification(ctx)
|
err := emailSvc.ConfirmVerification(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -2,28 +2,51 @@ package v1
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
||||||
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
|
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
|
||||||
"github.com/golang/protobuf/ptypes/empty"
|
"github.com/golang/protobuf/ptypes/empty"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewapiGrpcImpl() *EnvironmentsServer {
|
func NewapiGrpcImpl(controller ctrl.Manager) *EnvironmentsServer {
|
||||||
return &EnvironmentsServer{}
|
return &EnvironmentsServer{
|
||||||
|
controller: controller,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type EnvironmentsServer struct {
|
type EnvironmentsServer struct {
|
||||||
proto.UnimplementedEnvironmentsServer
|
proto.UnimplementedEnvironmentsServer
|
||||||
|
controller ctrl.Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.EnvironmentData) (*proto.EnvironmentFull, error) {
|
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{
|
data := &controllers.EnvironemntData{
|
||||||
Name: in.GetName(),
|
Name: in.GetName(),
|
||||||
Provider: in.GetProvider().String(),
|
Provider: in.GetProvider().String(),
|
||||||
|
Kubernetes: in.GetKubernetes().String(),
|
||||||
}
|
}
|
||||||
environment := &controllers.Environemnt{
|
environment := &controllers.Environemnt{
|
||||||
Controller: nil,
|
UserID: uuid[0],
|
||||||
|
Controller: e.controller,
|
||||||
Data: data,
|
Data: data,
|
||||||
|
Token: token[0],
|
||||||
}
|
}
|
||||||
err := environment.Create(ctx)
|
err := environment.Create(ctx)
|
||||||
if err != nil {
|
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
|
golang.org/x/crypto v0.21.0
|
||||||
k8s.io/api v0.29.3
|
k8s.io/api v0.29.3
|
||||||
k8s.io/apimachinery v0.29.3
|
k8s.io/apimachinery v0.29.3
|
||||||
|
k8s.io/client-go v0.29.3
|
||||||
sigs.k8s.io/controller-runtime v0.17.2
|
sigs.k8s.io/controller-runtime v0.17.2
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -55,7 +56,6 @@ require (
|
|||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
k8s.io/apiextensions-apiserver v0.29.3 // 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/component-base v0.29.3 // indirect
|
||||||
k8s.io/klog/v2 v2.120.1 // indirect
|
k8s.io/klog/v2 v2.120.1 // indirect
|
||||||
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
|
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
|
||||||
@ -66,12 +66,12 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
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
|
github.com/golang/protobuf v1.5.4
|
||||||
golang.org/x/net v0.22.0 // indirect
|
golang.org/x/net v0.22.0 // indirect
|
||||||
golang.org/x/sys v0.18.0 // indirect
|
golang.org/x/sys v0.18.0 // indirect
|
||||||
golang.org/x/text v0.14.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/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
|
||||||
google.golang.org/grpc v1.62.1
|
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.3 h1:c0q0nz9bT1TXUaMI7G+RII6mzZI1Tg0xWn2ToKekh4c=
|
||||||
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/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 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
|
||||||
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
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=
|
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"
|
||||||
|
)
|
@ -47,13 +47,13 @@ func (acc *Account) Create(ctx context.Context) error {
|
|||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: acc.Data.UUID,
|
Name: acc.Data.UUID,
|
||||||
Labels: map[string]string{
|
Labels: map[string]string{
|
||||||
"username": acc.Data.Username,
|
"username": acc.Data.Username,
|
||||||
"email-verified": "false",
|
"email-verified": "false",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := kube.Create(ctx,client, namespace, true); err != nil {
|
if err := kube.Create(ctx, client, namespace, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ func (acc *Account) Create(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare RBAC resources for the account
|
// Prepare RBAC resources for the account
|
||||||
role := &rbacv1.Role{
|
role := &rbacv1.Role{
|
||||||
ObjectMeta: metav1.ObjectMeta{Name: acc.Data.Username, Namespace: acc.Data.UUID},
|
ObjectMeta: metav1.ObjectMeta{Name: acc.Data.Username, Namespace: acc.Data.UUID},
|
||||||
@ -105,7 +105,7 @@ func (acc *Account) Create(ctx context.Context) error {
|
|||||||
Namespace: acc.Data.UUID,
|
Namespace: acc.Data.UUID,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := client.Create(ctx, sa); err != nil {
|
if err := client.Create(ctx, sa); err != nil {
|
||||||
if err := client.Delete(ctx, namespace); err != nil {
|
if err := client.Delete(ctx, namespace); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -113,7 +113,6 @@ func (acc *Account) Create(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
rb := &rbacv1.RoleBinding{
|
rb := &rbacv1.RoleBinding{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: acc.Data.UUID,
|
Name: acc.Data.UUID,
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
@ -17,21 +18,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type EmailSvc struct {
|
type EmailSvc struct {
|
||||||
Controller ctrl.Manager
|
Controller ctrl.Manager
|
||||||
Data EmailData
|
Data EmailData
|
||||||
EmailConfig email.EmailConf
|
EmailConfig email.EmailConf
|
||||||
|
DevMode bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmailData struct {
|
type EmailData struct {
|
||||||
UserID string
|
UserID string
|
||||||
Code string
|
Code string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *EmailSvc) SendVerification(ctx context.Context) error {
|
func (svc *EmailSvc) SendVerification(ctx context.Context) error {
|
||||||
client := svc.Controller.GetClient()
|
client := svc.Controller.GetClient()
|
||||||
userns := &corev1.Namespace{}
|
userns := &corev1.Namespace{}
|
||||||
if err := client.Get(ctx, types.NamespacedName{
|
if err := client.Get(ctx, types.NamespacedName{
|
||||||
Name: svc.Data.UserID,
|
Name: svc.Data.UserID,
|
||||||
}, userns); err != nil {
|
}, userns); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -48,17 +50,20 @@ func (svc *EmailSvc) SendVerification(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
number := encodeToString(6)
|
number := encodeToString(6)
|
||||||
email := string(accountData.Data["email"])
|
svc.Data.Code = number
|
||||||
if err := svc.EmailConfig.SendEmail(email, number); err != nil {
|
if !svc.DevMode {
|
||||||
return err
|
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, emailContent); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
emailCode := corev1.ConfigMap{
|
emailCode := corev1.ConfigMap{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "email-verification-code",
|
Name: "email-verification-code",
|
||||||
Namespace: svc.Data.UserID,
|
Namespace: svc.Data.UserID,
|
||||||
},
|
},
|
||||||
Data: map[string]string{
|
Data: map[string]string{
|
||||||
"code": number,
|
"code": number,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -66,7 +71,7 @@ func (svc *EmailSvc) SendVerification(ctx context.Context) error {
|
|||||||
if err := kube.Create(ctx, client, &emailCode, true); err != nil {
|
if err := kube.Create(ctx, client, &emailCode, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *EmailSvc) ConfirmVerification(ctx context.Context) error {
|
func (svc *EmailSvc) ConfirmVerification(ctx context.Context) error {
|
||||||
@ -74,7 +79,7 @@ func (svc *EmailSvc) ConfirmVerification(ctx context.Context) error {
|
|||||||
emailCode := &corev1.ConfigMap{}
|
emailCode := &corev1.ConfigMap{}
|
||||||
if err := client.Get(ctx, types.NamespacedName{
|
if err := client.Get(ctx, types.NamespacedName{
|
||||||
Namespace: svc.Data.UserID,
|
Namespace: svc.Data.UserID,
|
||||||
Name: "email-verification-code",
|
Name: "email-verification-code",
|
||||||
}, emailCode); err != nil {
|
}, emailCode); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -90,10 +95,11 @@ func (svc *EmailSvc) ConfirmVerification(ctx context.Context) error {
|
|||||||
|
|
||||||
userns := &corev1.Namespace{}
|
userns := &corev1.Namespace{}
|
||||||
if err := client.Get(ctx, types.NamespacedName{
|
if err := client.Get(ctx, types.NamespacedName{
|
||||||
Name: svc.Data.UserID,
|
Name: svc.Data.UserID,
|
||||||
}, userns); err != nil {
|
}, userns); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
userns.Labels["email-verified"] = "true"
|
userns.Labels["email-verified"] = "true"
|
||||||
if err := client.Update(ctx, userns); err != nil {
|
if err := client.Update(ctx, userns); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -102,15 +108,15 @@ func (svc *EmailSvc) ConfirmVerification(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func encodeToString(max int) string {
|
func encodeToString(max int) string {
|
||||||
b := make([]byte, max)
|
b := make([]byte, max)
|
||||||
n, err := io.ReadAtLeast(rand.Reader, b, max)
|
n, err := io.ReadAtLeast(rand.Reader, b, max)
|
||||||
if n != max {
|
if n != max {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
for i := 0; i < len(b); i++ {
|
for i := 0; i < len(b); i++ {
|
||||||
b[i] = table[int(b[i])%len(table)]
|
b[i] = table[int(b[i])%len(table)]
|
||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
var table = [...]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}
|
var table = [...]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}
|
||||||
|
@ -2,26 +2,104 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"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"
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Environemnt struct {
|
type Environemnt struct {
|
||||||
Controller ctrl.Manager
|
Controller ctrl.Manager
|
||||||
|
UserID string
|
||||||
Data *EnvironemntData
|
Data *EnvironemntData
|
||||||
|
Token string
|
||||||
}
|
}
|
||||||
|
|
||||||
type EnvironemntData struct {
|
type EnvironemntData struct {
|
||||||
Name string
|
Name string
|
||||||
Provider string
|
Provider string
|
||||||
|
Kubernetes string
|
||||||
|
HetznerData HetznerData
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *Environemnt) Create(ctx context.Context) error {
|
type HetznerData struct {
|
||||||
log.Printf("%s", env.Data.Name)
|
ServerLocation string
|
||||||
log.Printf("%s", env.Data.Provider)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ func Create(ctx context.Context, client client.Client, obj client.Object, wait b
|
|||||||
if err := client.Create(ctx, obj); err != nil {
|
if err := client.Create(ctx, obj); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if wait{
|
if wait {
|
||||||
if err := WaitUntilCreated(ctx, client, obj, 10, time.Millisecond*50); err != nil {
|
if err := WaitUntilCreated(ctx, client, obj, 10, time.Millisecond*50); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -23,13 +23,13 @@ func Create(ctx context.Context, client client.Client, obj client.Object, wait b
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SetOwnerRef(ctx context.Context, client client.Client, obj client.Object, owner client.Object) client.Object {
|
func SetOwnerRef(ctx context.Context, client client.Client, obj client.Object, owner client.Object) client.Object {
|
||||||
apiVersion := fmt.Sprintf("%s/%s", owner.GetObjectKind().GroupVersionKind().Group, owner.GetObjectKind().GroupVersionKind().Version)
|
apiVersion := fmt.Sprintf("%s/%s", owner.GetObjectKind().GroupVersionKind().Group, owner.GetObjectKind().GroupVersionKind().Version)
|
||||||
ownerReference := []metav1.OwnerReference{
|
ownerReference := []metav1.OwnerReference{
|
||||||
{
|
{
|
||||||
APIVersion: apiVersion,
|
APIVersion: apiVersion,
|
||||||
Kind: owner.GetObjectKind().GroupVersionKind().GroupKind().Kind,
|
Kind: owner.GetObjectKind().GroupVersionKind().GroupKind().Kind,
|
||||||
Name: owner.GetName(),
|
Name: owner.GetName(),
|
||||||
UID: owner.GetUID(),
|
UID: owner.GetUID(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
obj.SetOwnerReferences(ownerReference)
|
obj.SetOwnerReferences(ownerReference)
|
||||||
@ -52,4 +52,3 @@ func WaitUntilCreated(ctx context.Context, client client.Client, obj client.Obje
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
package kube_test
|
package kube_test
|
||||||
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
|
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
|
||||||
"github.com/alecthomas/assert/v2"
|
"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"
|
v1 "git.badhouseplants.net/softplayer/softplayer-backend/api/v1"
|
||||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
|
"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/accounts"
|
||||||
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
|
|
||||||
email_proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email"
|
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/alecthomas/kong"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/reflection"
|
"google.golang.org/grpc/reflection"
|
||||||
@ -17,10 +17,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Serve struct {
|
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"`
|
Host string `env:"SOFTPLAYER_HOST" default:"0.0.0.0"`
|
||||||
HashCost int16 `env:"SOFTPLAYER_HASH_COST" default:"1"`
|
HashCost int16 `env:"SOFTPLAYER_HASH_COST" default:"1"`
|
||||||
Reflection bool `env:"SOFTPLAYER_REFLECTION" default:"false"`
|
Reflection bool `env:"SOFTPLAYER_REFLECTION" default:"false"`
|
||||||
|
DevMode bool `env:"SOFTPLAYER_DEV_MODE" default:"false"`
|
||||||
SmtpHost string `env:"SOFTPLAYER_SMTP_HOST"`
|
SmtpHost string `env:"SOFTPLAYER_SMTP_HOST"`
|
||||||
SmtpPort string `env:"SOFTPLAYER_SMTP_PORT" default:"587"`
|
SmtpPort string `env:"SOFTPLAYER_SMTP_PORT" default:"587"`
|
||||||
SmtpFrom string `env:"SOFTPLAYER_SMTP_FROM" default:"overlord@badhouseplants.net"`
|
SmtpFrom string `env:"SOFTPLAYER_SMTP_FROM" default:"overlord@badhouseplants.net"`
|
||||||
@ -62,6 +63,7 @@ func server(params Serve) error {
|
|||||||
SmtpHost: params.SmtpHost,
|
SmtpHost: params.SmtpHost,
|
||||||
SmtpPort: params.SmtpPort,
|
SmtpPort: params.SmtpPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
address := fmt.Sprintf("%s:%d", params.Host, params.Port)
|
address := fmt.Sprintf("%s:%d", params.Host, params.Port)
|
||||||
lis, err := net.Listen("tcp", address)
|
lis, err := net.Listen("tcp", address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -72,10 +74,9 @@ func server(params Serve) error {
|
|||||||
reflection.Register(grpcServer)
|
reflection.Register(grpcServer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
environments.RegisterEnvironmentsServer(grpcServer, v1.NewapiGrpcImpl(controller))
|
||||||
environments.RegisterEnvironmentsServer(grpcServer, v1.NewapiGrpcImpl())
|
|
||||||
accounts.RegisterAccountsServer(grpcServer, v1.NewAccountRPCImpl(controller, params.HashCost))
|
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 {
|
if err := grpcServer.Serve(lis); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user