Refactor providers data

This commit is contained in:
Nikolai Rodionov 2024-04-29 18:28:13 +02:00
parent a4c8415449
commit f3e0917471
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
7 changed files with 102 additions and 39 deletions

View File

@ -25,7 +25,7 @@ func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions
data := &controllers.EnvironemntData{
Name: in.GetMetadata().GetName(),
Description: in.GetMetadata().GetDescription(),
Provider: in.GetSpec().GetProvider().String(),
Provider: in.GetSpec().GetProvider(),
Kubernetes: in.GetSpec().GetKubernetes().String(),
}
environment := &controllers.Environemnt{

2
go.mod
View File

@ -67,7 +67,7 @@ require (
)
require (
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.6
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.7
github.com/golang/protobuf v1.5.4
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect

4
go.sum
View File

@ -1,5 +1,5 @@
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.6 h1:ztek91ZtN2pNsq4VCWNYe8oZf5I7WDUMTDtT2GDKHVU=
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.6/go.mod h1:OU+833cHwvecr+gsnPEKQYlAJbpL8bqSJVLobdw63qI=
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.7 h1:l7v4iWm1cjvEg6BqbWAm21t6WmY0yb5TacKdjIT28rA=
git.badhouseplants.net/softplayer/softplayer-go-proto v0.1.7/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=

View File

@ -5,11 +5,12 @@ import (
"errors"
"fmt"
"log"
"strings"
"github.com/joho/godotenv"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers"
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
@ -28,29 +29,38 @@ type Environemnt struct {
type EnvironemntData struct {
Name string
Description string
Provider string
Provider proto.Provider
Kubernetes string
HetznerData HetznerData
HetznerData proto.HetznerOptions
}
type HetznerData struct {
ServerLocation string
ServerType string
}
func (e *EnvironemntData) buildVars() (string, error) {
provider, err := providers.GetProvider(e.Provider)
if err != nil {
return "", err
}
func (e *EnvironemntData) buildVars() string {
vars := fmt.Sprintf("SP_PROVIDER=%s\nSP_KUBERNETES=%s", e.providerFmt(), e.kubernetesFmt())
return vars
}
serverType, err := provider.GetServerType(e.HetznerData.ServerType)
if err != nil {
return "", err
}
func (e *EnvironemntData) providerFmt() string {
res := strings.Replace(e.Provider, "PROVIDER_", "", -1)
return strings.ToLower(res)
}
serverLocation, err := provider.GetServerLocation(e.HetznerData.ServerLocation.String())
if err != nil {
return "", err
}
vars := fmt.Sprintf(`# -- Generated by the softplayer controller
SP_PROVIDER=%s
SP_KUBERNETES=%s
SP_SERVER_TYPE=%s
SP_SERVER_LOCATION=%s`,
e.Provider,
e.Kubernetes,
serverType,
serverLocation,
)
func (e *EnvironemntData) kubernetesFmt() string {
res := strings.Replace(e.Kubernetes, "KUBERNETES_", "", -1)
return strings.ToLower(res)
return vars, nil
}
func (env *Environemnt) isNsVerified(ctx context.Context) error {
@ -74,6 +84,7 @@ func (env *Environemnt) Create(ctx context.Context) error {
log.Println("Can't verify ns")
return err
}
env.Controller.GetClient()
conf := &rest.Config{
Host: "https://kubernetes.default.svc.cluster.local:443",
@ -89,6 +100,10 @@ func (env *Environemnt) Create(ctx context.Context) error {
return err
}
vars, err := env.Data.buildVars()
if err != nil {
return err
}
obj := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: env.Data.Name,
@ -100,7 +115,7 @@ func (env *Environemnt) Create(ctx context.Context) error {
},
Data: map[string]string{
"description": env.Data.Description,
"vars": env.Data.buildVars(),
"vars": vars,
},
}
if err := kube.Create(ctx, controller.GetClient(), &obj, false); err != nil {
@ -125,7 +140,6 @@ func (env *Environemnt) Delete(ctx context.Context) error {
if err != nil {
return err
}
obj := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: env.Data.Name,
@ -134,9 +148,6 @@ func (env *Environemnt) Delete(ctx context.Context) error {
"component": "bootstrap",
},
},
Data: map[string]string{
"vars": env.Data.buildVars(),
},
}
if err := kube.Delete(ctx, controller.GetClient(), &obj, false); err != nil {
return err
@ -194,7 +205,7 @@ func (env *Environemnt) Get(ctx context.Context) error {
return err
}
env.Data.Provider = res["SP_PROVIDER"]
// env.Data.Provider = res["SP_PROVIDER"]
env.Data.Kubernetes = res["SP_KUBERNETES"]
return nil
}

View File

@ -1,8 +1 @@
package kube_test
import (
"testing"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
"github.com/alecthomas/assert/v2"
)

View File

@ -0,0 +1,21 @@
package providers
import (
"fmt"
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
)
type Providers interface {
GetServerType(proto.ServerType) (string, error)
GetServerLocation(string) (string, error)
}
func GetProvider(provider proto.Provider) (Providers, error) {
switch provider {
case proto.Provider_PROVIDER_HETZNER:
return &Hetzner{}, nil
default:
return nil, fmt.Errorf("unknown provider: %s", provider.String())
}
}

View File

@ -1,9 +1,47 @@
package providers
// Hetzner supported regions
const (
HETZNER_REG_FINLAND = "fn"
import (
"fmt"
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
)
type Hetzner struct {
type Hetzner struct{}
// GetServerLocation implements Providers.
func (h *Hetzner) GetServerLocation(location string) (string, error) {
switch location {
case proto.HetznerLocation_HETZNER_LOCATION_ASHBURN.String():
return "ash", nil
case proto.HetznerLocation_HETZNER_LOCATION_HILLSBORO.String():
return "hil", nil
case proto.HetznerLocation_HETZNER_LOCATION_FALKENSTEIN.String():
return "fsn1", nil
case proto.HetznerLocation_HETZNER_LOCATION_NUREMBERG.String():
return "nbg1", nil
case proto.HetznerLocation_HETZNER_LOCATION_HELSINKI.String():
return "hel1", nil
default:
return "", fmt.Errorf("unknown location: %s", location)
}
}
func (h *Hetzner) GetServerType(kind proto.ServerType) (serverType string, err error) {
switch kind {
case proto.ServerType_SERVER_TYPE_STARTER:
serverType = "cpx21"
return
case proto.ServerType_SERVER_TYPE_REGULAR:
serverType = "cpx31"
return
case proto.ServerType_SERVER_TYPE_PLUS:
serverType = "cpx41"
return
case proto.ServerType_SERVER_TYPE_PRO:
serverType = "cpx51"
return
default:
err = fmt.Errorf("unknown server type: %s", kind.String())
return
}
}