Refactor providers data
This commit is contained in:
		@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1 @@
 | 
			
		||||
package kube_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
 | 
			
		||||
	"github.com/alecthomas/assert/v2"
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								internal/providers/common.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								internal/providers/common.go
									
									
									
									
									
										Normal 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())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user