Refactor services type structuring

This allows each service to have its own type, which inherits from a
common base service type. Initially, jump host services take advantage
of this to configure an SSH key. The other services may follow the same
pattern in the future if they need their own custom configuration as well.

Signed-off-by: Sean Eagan <seaneagan1@gmail.com>
Change-Id: I4272be541d1b7125241ca791a7152eed91a6b574
Signed-off-by: Sean Eagan <seaneagan1@gmail.com>
This commit is contained in:
Sean Eagan
2021-02-05 16:04:17 -06:00
committed by Sean Eagan
parent 001a28400c
commit 5fa448b575
14 changed files with 400 additions and 292 deletions

View File

@@ -14,10 +14,17 @@
package services
import (
"fmt"
airshipv1 "sipcluster/pkg/api/v1"
)
// ErrAuthTypeNotSupported is returned when wrong AuthType is provided
type ErrInfraServiceNotSupported struct {
Service airshipv1.SIPClusterService
}
func (e ErrInfraServiceNotSupported) Error() string {
return "Invalid Infrastructure Service"
return fmt.Sprintf("Invalid Infrastructure Service: %v", e.Service)
}

View File

@@ -38,11 +38,11 @@ type jumpHost struct {
client client.Client
sipName types.NamespacedName
logger logr.Logger
config airshipv1.InfraConfig
config airshipv1.JumpHostService
machines *airshipvms.MachineList
}
func newJumpHost(name, namespace string, logger logr.Logger, config airshipv1.InfraConfig,
func newJumpHost(name, namespace string, logger logr.Logger, config airshipv1.JumpHostService,
machines *airshipvms.MachineList, client client.Client) InfraService {
return jumpHost{
sipName: types.NamespacedName{
@@ -96,11 +96,6 @@ func (jh jumpHost) Finalize() error {
return nil
}
// Type returns the type of infrastructure service: jumphost.
func (jh jumpHost) Type() airshipv1.InfraService {
return airshipv1.JumpHostService
}
/*
The SIP Cluster operator will manufacture a jump host pod specifically for this

View File

@@ -190,13 +190,13 @@ type loadBalancer struct {
client client.Client
sipName types.NamespacedName
logger logr.Logger
config airshipv1.InfraConfig
config airshipv1.SIPClusterService
machines *airshipvms.MachineList
}
func newLB(name, namespace string,
logger logr.Logger,
config airshipv1.InfraConfig,
config airshipv1.SIPClusterService,
machines *airshipvms.MachineList,
client client.Client) loadBalancer {
return loadBalancer{
@@ -216,11 +216,6 @@ func (lb loadBalancer) Finalize() error {
return nil
}
// Type type of the service
func (lb loadBalancer) Type() airshipv1.InfraService {
return airshipv1.LoadBalancerService
}
func generateTemplate(p proxy) ([]byte, error) {
tmpl, err := template.New("haproxy-config").Parse(defaultTemplate)
if err != nil {

View File

@@ -49,8 +49,9 @@ var _ = Describe("Service Set", func() {
set := services.NewServiceSet(logger, *sip, machineList, k8sClient)
serviceList := set.ServiceList()
serviceList, err := set.ServiceList()
Expect(serviceList).To(HaveLen(2))
Expect(err).To(Succeed())
for _, svc := range serviceList {
err := svc.Deploy()
Expect(err).ToNot(HaveOccurred())

View File

@@ -16,7 +16,6 @@ package services
import (
"context"
"fmt"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
@@ -33,7 +32,6 @@ import (
type InfraService interface {
Deploy() error
Finalize() error
Type() airshipv1.InfraService
}
// ServiceSet provides access to infrastructure services
@@ -42,8 +40,6 @@ type ServiceSet struct {
sip airshipv1.SIPCluster
machines *airshipvms.MachineList
client client.Client
services map[airshipv1.InfraService]InfraService
}
// NewServiceSet returns new instance of ServiceSet
@@ -62,17 +58,6 @@ func NewServiceSet(
}
}
// LoadBalancer returns loadbalancer service
func (ss ServiceSet) LoadBalancer() (InfraService, error) {
lb, ok := ss.services[airshipv1.LoadBalancerService]
if !ok {
ss.logger.Info("sip cluster doesn't have loadbalancer infrastructure service defined")
}
return lb, fmt.Errorf("loadbalancer service is not defined for sip cluster '%s'/'%s'",
ss.sip.GetNamespace(),
ss.sip.GetName())
}
func (ss ServiceSet) Finalize() error {
serviceNamespace := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
@@ -107,31 +92,31 @@ func CreateNS(serviceNamespaceName string, c client.Client) error {
}
// ServiceList returns all services defined in Set
func (ss ServiceSet) ServiceList() []InfraService {
var serviceList []InfraService
for _, serviceConfig := range ss.sip.Spec.InfraServices {
switch serviceConfig.ServiceType {
case airshipv1.LoadBalancerService:
serviceList = append(serviceList,
newLB(ss.sip.GetName(),
ss.sip.Spec.ClusterName,
ss.logger,
serviceConfig,
ss.machines,
ss.client))
case airshipv1.JumpHostService:
serviceList = append(serviceList,
newJumpHost(ss.sip.GetName(),
ss.sip.Spec.ClusterName,
ss.logger,
serviceConfig,
ss.machines,
ss.client))
default:
ss.logger.Info("serviceType unsupported", "serviceType", serviceConfig.ServiceType)
}
func (ss ServiceSet) ServiceList() ([]InfraService, error) {
serviceList := []InfraService{}
services := ss.sip.Spec.Services
for _, svc := range services.LoadBalancer {
serviceList = append(serviceList,
newLB(ss.sip.GetName(),
ss.sip.Spec.ClusterName,
ss.logger,
svc,
ss.machines,
ss.client))
}
return serviceList
for _, svc := range services.Auth {
return nil, ErrInfraServiceNotSupported{svc}
}
for _, svc := range services.JumpHost {
serviceList = append(serviceList,
newJumpHost(ss.sip.GetName(),
ss.sip.Spec.ClusterName,
ss.logger,
svc,
ss.machines,
ss.client))
}
return serviceList, nil
}
func applyRuntimeObject(key client.ObjectKey, obj client.Object, c client.Client) error {