Skip to content

Commit

Permalink
Merge pull request #161 from vshn/add/loadbalancer-annotations
Browse files Browse the repository at this point in the history
Inject loadbalancer specific annotations
  • Loading branch information
Kidswiss authored May 3, 2024
2 parents e88ee55 + 242eeb8 commit 3ab6a01
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
24 changes: 16 additions & 8 deletions pkg/comp-functions/functions/vshnpostgres/loadBalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,37 @@ import (
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
"gopkg.in/yaml.v2"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

var serviceName = "primary-service"

func AddLoadBalancerIPToConnectionDetails(ctx context.Context, svc *runtime.ServiceRuntime) *xfnproto.Result {

if !svc.GetBoolFromCompositionConfig("externalDatabaseConnectionsEnabled") {
return nil
}
func AddPrimaryService(ctx context.Context, svc *runtime.ServiceRuntime) *xfnproto.Result {

comp, err := getVSHNPostgreSQL(ctx, svc)

if err != nil {
return runtime.NewFatalResult(fmt.Errorf("Cannot get composite from function io: %w", err))
}

annotations := map[string]string{}
if svc.Config.Data["loadbalancerAnnotations"] != "" && svc.GetBoolFromCompositionConfig("externalDatabaseConnectionsEnabled") {

err := yaml.Unmarshal([]byte(svc.Config.Data["loadbalancerAnnotations"]), annotations)
if err != nil {
svc.Log.Error(err, "cannot unmarshal ingress annotations from input")
svc.AddResult(runtime.NewWarningResult(fmt.Sprintf("cannot unmarshal ingress annotations from input: %s", err)))
}
}

k8sservice := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Namespace: getInstanceNamespace(comp),
Name: serviceName,
Namespace: getInstanceNamespace(comp),
Annotations: annotations,
},
Spec: v1.ServiceSpec{
Selector: map[string]string{
Expand All @@ -51,7 +59,7 @@ func AddLoadBalancerIPToConnectionDetails(ctx context.Context, svc *runtime.Serv
},
}

if comp.Spec.Parameters.Network.ServiceType == "LoadBalancer" {
if comp.Spec.Parameters.Network.ServiceType == "LoadBalancer" && svc.GetBoolFromCompositionConfig("externalDatabaseConnectionsEnabled") {
k8sservice.Spec.Type = v1.ServiceTypeLoadBalancer
} else {
k8sservice.Spec.Type = v1.ServiceTypeClusterIP
Expand Down
18 changes: 8 additions & 10 deletions pkg/comp-functions/functions/vshnpostgres/loadBalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ func TestNothingToDo(t *testing.T) {
svc.Config.Data["externalDatabaseConnectionsEnabled"] = "true"

// When
result := AddLoadBalancerIPToConnectionDetails(ctx, svc)
result := AddPrimaryService(ctx, svc)

desired := svc.GetAllDesired()

if len(desired) != 2 {
t.Fatal("Expected 2 resources in desired, got", len(desired))
}
assert.Len(t, desired, 2)

// Then
assert.NotNil(t, result)
Expand All @@ -42,7 +40,7 @@ func TestLoadBalancerParameterSet(t *testing.T) {
svc.Config.Data["externalDatabaseConnectionsEnabled"] = "true"

// When
result := AddLoadBalancerIPToConnectionDetails(ctx, svc)
result := AddPrimaryService(ctx, svc)

// Then
assert.NotNil(t, result)
Expand All @@ -59,26 +57,26 @@ func TestLoadBalancerServiceObserverCreated(t *testing.T) {
svc.Config.Data["externalDatabaseConnectionsEnabled"] = "true"

// When
result := AddLoadBalancerIPToConnectionDetails(ctx, svc)
result := AddPrimaryService(ctx, svc)

// Then
assert.Nil(t, result)
})
}

// this test normally should return Warning because it's copy of TestLoadBalancerParameterSet()
// but due to disabled LoadBalancer in config it returns Normal
// The primary-service should be deployed even if loadbalancing is disabled.
// However it should not be allowed to be set to type LoadBalancer.
func TestLoadBalancerNotEnabled(t *testing.T) {
ctx := context.Background()

t.Run("Verify composition", func(t *testing.T) {

//Given
svc := commontest.LoadRuntimeFromFile(t, "vshn-postgres/loadbalancer/01-LoadBalancerSet.yaml")
svc := commontest.LoadRuntimeFromFile(t, "vshn-postgres/loadbalancer/02-ServiceObserverPresent.yaml")
svc.Config.Data["externalDatabaseConnectionsEnabled"] = "false"

// When
result := AddLoadBalancerIPToConnectionDetails(ctx, svc)
result := AddPrimaryService(ctx, svc)

// Then
assert.Nil(t, result)
Expand Down
2 changes: 1 addition & 1 deletion pkg/comp-functions/functions/vshnpostgres/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func init() {
},
{
Name: "load-balancer",
Execute: AddLoadBalancerIPToConnectionDetails,
Execute: AddPrimaryService,
},
{
Name: "namespaceQuotas",
Expand Down

0 comments on commit 3ab6a01

Please sign in to comment.