From 96fe9fb4f836e775c0d4f2feb524f44e60743697 Mon Sep 17 00:00:00 2001 From: wejdross Date: Thu, 10 Aug 2023 11:15:37 +0200 Subject: [PATCH] adjusting code to cooperate withCilium (#54) Co-authored-by: lukasz.widera@vshn.ch --- .../vshn-postgres-func/loadBalancer.go | 96 +++++++------------ .../vshn-postgres-func/loadBalancer_test.go | 9 +- .../loadbalancer/01-LoadBalancerSet.yaml | 2 +- .../02-ServiceObserverPresent.yaml | 10 +- 4 files changed, 49 insertions(+), 68 deletions(-) diff --git a/pkg/comp-functions/functions/vshn-postgres-func/loadBalancer.go b/pkg/comp-functions/functions/vshn-postgres-func/loadBalancer.go index bce5dcd70..57fe4f5df 100644 --- a/pkg/comp-functions/functions/vshn-postgres-func/loadBalancer.go +++ b/pkg/comp-functions/functions/vshn-postgres-func/loadBalancer.go @@ -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 { @@ -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) } @@ -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 } diff --git a/pkg/comp-functions/functions/vshn-postgres-func/loadBalancer_test.go b/pkg/comp-functions/functions/vshn-postgres-func/loadBalancer_test.go index aeae410e2..1a618d9d7 100644 --- a/pkg/comp-functions/functions/vshn-postgres-func/loadBalancer_test.go +++ b/pkg/comp-functions/functions/vshn-postgres-func/loadBalancer_test.go @@ -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) }) @@ -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 diff --git a/test/transforms/vshn-postgres/loadbalancer/01-LoadBalancerSet.yaml b/test/transforms/vshn-postgres/loadbalancer/01-LoadBalancerSet.yaml index 527f21cf6..326435ace 100644 --- a/test/transforms/vshn-postgres/loadbalancer/01-LoadBalancerSet.yaml +++ b/test/transforms/vshn-postgres/loadbalancer/01-LoadBalancerSet.yaml @@ -104,7 +104,7 @@ desired: spec: parameters: network: - serviceType: LoadBalancer + serviceType: ClusterIP service: extensions: - name: ltree diff --git a/test/transforms/vshn-postgres/loadbalancer/02-ServiceObserverPresent.yaml b/test/transforms/vshn-postgres/loadbalancer/02-ServiceObserverPresent.yaml index 4cb577ec0..7a6cce094 100644 --- a/test/transforms/vshn-postgres/loadbalancer/02-ServiceObserverPresent.yaml +++ b/test/transforms/vshn-postgres/loadbalancer/02-ServiceObserverPresent.yaml @@ -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 @@ -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 @@ -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