diff --git a/api/v1/environments.go b/api/v1/environments.go index 8eb63c7..497389d 100644 --- a/api/v1/environments.go +++ b/api/v1/environments.go @@ -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{ diff --git a/go.mod b/go.mod index d8e008b..6337baf 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index cbaf93e..b30b998 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/controllers/environments.go b/internal/controllers/environments.go index f66e65b..021f474 100644 --- a/internal/controllers/environments.go +++ b/internal/controllers/environments.go @@ -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 } diff --git a/internal/helpers/kube/kube_test.go b/internal/helpers/kube/kube_test.go index 4122d93..d283e01 100644 --- a/internal/helpers/kube/kube_test.go +++ b/internal/helpers/kube/kube_test.go @@ -1,8 +1 @@ package kube_test - -import ( - "testing" - - "git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube" - "github.com/alecthomas/assert/v2" -) diff --git a/internal/providers/common.go b/internal/providers/common.go new file mode 100644 index 0000000..1a652a5 --- /dev/null +++ b/internal/providers/common.go @@ -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()) + } +} diff --git a/internal/providers/hetzner.go b/internal/providers/hetzner.go index 39aedfd..619a99f 100644 --- a/internal/providers/hetzner.go +++ b/internal/providers/hetzner.go @@ -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 + } }