Skip to content

Commit

Permalink
adjusting code to cooperate withCilium (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: lukasz.widera@vshn.ch <lukasz.widera@vshn.ch>
  • Loading branch information
wejdross and lukasz.widera@vshn.ch authored Aug 10, 2023
1 parent 8ac2bf6 commit 96fe9fb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 68 deletions.
96 changes: 36 additions & 60 deletions pkg/comp-functions/functions/vshn-postgres-func/loadBalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package vshnpostgres

import (
"context"
"fmt"

"github.com/crossplane/crossplane/apis/apiextensions/fn/io/v1alpha1"
stackgresv1 "github.com/vshn/appcat/apis/stackgres/v1"
vshnv1 "github.com/vshn/appcat/apis/vshn/v1"
"github.com/vshn/appcat/pkg/comp-functions/runtime"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"k8s.io/apimachinery/pkg/util/intstr"
)

var serviceObserverName = "loadbalancer-observer"
var serviceName = "postgresql-master"

func AddLoadBalancerIPToConnectionDetails(ctx context.Context, iof *runtime.Runtime) runtime.Result {

Expand All @@ -27,37 +25,53 @@ func AddLoadBalancerIPToConnectionDetails(ctx context.Context, iof *runtime.Runt
return runtime.NewFatalErr(ctx, "Cannot get composite from function io", err)
}

if comp.Spec.Parameters.Network.ServiceType != "LoadBalancer" {
return runtime.NewNormal()
k8sservice := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
Namespace: getInstanceNamespace(comp),
},
Spec: v1.ServiceSpec{
Selector: map[string]string{
"role": "master",
},
Ports: []v1.ServicePort{
{
Name: "pgport",
Port: 5432,
Protocol: v1.ProtocolTCP,
TargetPort: intstr.IntOrString{
Type: intstr.String,
StrVal: "pgport",
},
},
},
},
}

cluster, err := getSGCluster(ctx, iof)
if err != nil {
return runtime.NewFatalErr(ctx, "Cannot get cluster from function io", err)
}
if err := updateClusterForLoadBalancer(cluster, ctx, iof); err != nil {
return runtime.NewFatal(ctx, "Cannot put cluster into function io")
if comp.Spec.Parameters.Network.ServiceType == "LoadBalancer" {
k8sservice.Spec.Type = v1.ServiceTypeLoadBalancer
} else {
k8sservice.Spec.Type = v1.ServiceTypeClusterIP
}

serviceToApply, err := createServiceForLoadBalancer(comp)
if err != nil {
return runtime.NewFatalErr(ctx, "Cannot create load balancer service", err)
if err := iof.Desired.PutIntoObject(ctx, k8sservice, serviceName); err != nil {
return runtime.NewFatalErr(ctx, "Cannot put service into function io", err)
}

if err := addServiceObserverToDesiredState(ctx, iof, serviceToApply); err != nil {
return runtime.NewFatalErr(ctx, "Cannot add load balancer observer", err)
if comp.Spec.Parameters.Network.ServiceType == "ClusterIP" {
return runtime.NewNormal()
}

serviceToApply, err = getObservedService(ctx, iof, serviceToApply)
k8sservice, err = getObservedService(ctx, iof, k8sservice)
if err != nil {
return runtime.NewWarning(ctx, "Cannot yet get service observer object")
return runtime.NewWarning(ctx, "Cannot yet get service object")
}

if serviceToApply.Status.LoadBalancer.Ingress == nil {
if k8sservice.Status.LoadBalancer.Ingress == nil {
return runtime.NewWarning(ctx, "LoadBalancerIP is not ready yet")
}

if err := updateConnectionSecretWithLoadBalancerIP(ctx, iof, serviceToApply); err != nil {
if err := updateConnectionSecretWithLoadBalancerIP(ctx, iof, k8sservice); err != nil {
return runtime.NewFatalErr(ctx, "Cannot update connection secret", err)
}

Expand All @@ -70,46 +84,8 @@ func getVSHNPostgreSQL(ctx context.Context, iof *runtime.Runtime) (*vshnv1.VSHNP
return comp, err
}

func getSGCluster(ctx context.Context, iof *runtime.Runtime) (*stackgresv1.SGCluster, error) {
cluster := &stackgresv1.SGCluster{}
err := iof.Desired.GetFromObject(ctx, cluster, "cluster")
return cluster, err
}

func updateClusterForLoadBalancer(cluster *stackgresv1.SGCluster, ctx context.Context, iof *runtime.Runtime) error {
cluster.Spec.PostgresServices = &stackgresv1.SGClusterSpecPostgresServices{
Primary: &stackgresv1.SGClusterSpecPostgresServicesPrimary{
Type: pointer.String("LoadBalancer"),
},
}
err := iof.Desired.PutIntoObject(ctx, cluster, "cluster")
if err != nil {
return err
}
return nil
}

func createServiceForLoadBalancer(comp *vshnv1.VSHNPostgreSQL) (*v1.Service, error) {
serviceToApply := &v1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: comp.GetName(),
Namespace: getInstanceNamespace(comp),
},
}

return serviceToApply, nil
}

func addServiceObserverToDesiredState(ctx context.Context, iof *runtime.Runtime, service *v1.Service) error {
return iof.Desired.PutIntoObserveOnlyObject(ctx, service, fmt.Sprintf("%s-%s", service.Name, serviceObserverName))
}

func getObservedService(ctx context.Context, iof *runtime.Runtime, service *v1.Service) (*v1.Service, error) {
err := iof.Observed.GetFromObject(ctx, service, fmt.Sprintf("%s-%s", service.Name, serviceObserverName))
err := iof.Observed.GetFromObject(ctx, service, service.Name)
return service, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ func TestNothingToDo(t *testing.T) {
t.Run("NothingToDo", func(t *testing.T) {

//Given
iof := commontest.LoadRuntimeFromFile(t, "vshn-postgres/maintenance/01-GivenSchedule.yaml")
iof := commontest.LoadRuntimeFromFile(t, "vshn-postgres/loadbalancer/01-LoadBalancerSet.yaml")
iof.Config.Data["externalDatabaseConnectionsEnabled"] = "true"

// When
result := AddLoadBalancerIPToConnectionDetails(ctx, iof)

if len(iof.Desired.List(ctx)) != 2 {
t.Fatal("Expected 2 resources in desired, got", len(iof.Desired.List(ctx)))
}

// Then
assert.Equal(t, expectResult, result)
})
Expand All @@ -30,12 +34,13 @@ func TestNothingToDo(t *testing.T) {
func TestLoadBalancerParameterSet(t *testing.T) {
ctx := context.Background()
// it need another reconciliation to get the service observer object
expectResult := runtime.NewWarning(ctx, "Cannot yet get service observer object")
expectResult := runtime.NewNormal()

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

//Given
iof := commontest.LoadRuntimeFromFile(t, "vshn-postgres/loadbalancer/01-LoadBalancerSet.yaml")

iof.Config.Data["externalDatabaseConnectionsEnabled"] = "true"

// When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ desired:
spec:
parameters:
network:
serviceType: LoadBalancer
serviceType: ClusterIP
service:
extensions:
- name: ltree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ observed:
name: postgresql-prod-n97jr-connection
- apiVersion: kubernetes.crossplane.io/v1alpha1
kind: Object
name: postgresql-prod-n97jr-loadbalancer-observer
name: postgresql-master
- apiVersion: kubernetes.crossplane.io/v1alpha1
kind: Object
name: postgresql-prod-n97jr-localca
Expand Down Expand Up @@ -780,16 +780,16 @@ observed:
status: "True"
type: Synced
connectionDetails: []
- name: postgresql-prod-n97jr-loadbalancer-observer
- name: postgresql-master
resource:
apiVersion: kubernetes.crossplane.io/v1alpha1
kind: Object
metadata:
annotations:
crossplane.io/composition-resource-name: postgresql-prod-n97jr-loadbalancer-observer
crossplane.io/composition-resource-name: postgresql-master
crossplane.io/external-create-pending: 2023-07-28T09:05:56Z
crossplane.io/external-create-succeeded: 2023-07-28T09:05:57Z
crossplane.io/external-name: postgresql-prod-n97jr-loadbalancer-observer
crossplane.io/external-name: postgresql-master
creationTimestamp: 2023-07-28T09:05:55Z
finalizers:
- finalizer.managedresource.crossplane.io
Expand Down Expand Up @@ -907,7 +907,7 @@ observed:
operation: Update
subresource: status
time: 2023-07-28T09:06:03Z
name: postgresql-prod-n97jr-loadbalancer-observer
name: postgresql-master
ownerReferences:
- apiVersion: vshn.appcat.vshn.io/v1
blockOwnerDeletion: true
Expand Down

0 comments on commit 96fe9fb

Please sign in to comment.