-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to configure multiple PostgreSQL instances
- Loading branch information
Showing
7 changed files
with
501 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
pkg/comp-functions/functions/vshn-postgres-func/replication.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package vshnpostgres | ||
|
||
import ( | ||
"context" | ||
|
||
stackgresv1 "github.com/vshn/appcat/apis/stackgres/v1" | ||
vshnv1 "github.com/vshn/appcat/apis/vshn/v1" | ||
"github.com/vshn/appcat/pkg/comp-functions/runtime" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
// ConfigureReplication configures the stackgres replication based on the claim | ||
func ConfigureReplication(ctx context.Context, iof *runtime.Runtime) runtime.Result { | ||
comp := &vshnv1.VSHNPostgreSQL{} | ||
err := iof.Desired.GetComposite(ctx, comp) | ||
if err != nil { | ||
return runtime.NewFatalErr(ctx, "Cannot get composite from function io", err) | ||
} | ||
cluster := &stackgresv1.SGCluster{} | ||
err = iof.Desired.GetFromObject(ctx, cluster, "cluster") | ||
if err != nil { | ||
return runtime.NewFatalErr(ctx, "not able to get cluster", err) | ||
} | ||
|
||
cluster = configureReplication(ctx, comp, cluster) | ||
|
||
err = iof.Desired.PutIntoObject(ctx, cluster, "cluster") | ||
if err != nil { | ||
return runtime.NewFatalErr(ctx, "cannot save cluster to functionIO", err) | ||
} | ||
return runtime.NewNormal() | ||
} | ||
|
||
func configureReplication(ctx context.Context, comp *vshnv1.VSHNPostgreSQL, cluster *stackgresv1.SGCluster) *stackgresv1.SGCluster { | ||
cluster.Spec.Replication = &stackgresv1.SGClusterSpecReplication{ | ||
Mode: pointer.String("async"), | ||
SyncInstances: pointer.Int(1), | ||
} | ||
cluster.Spec.Instances = comp.Spec.Parameters.Instances | ||
if comp.Spec.Parameters.Instances > 1 && comp.Spec.Parameters.Replication.Mode != "async" && comp.Spec.Parameters.Replication.Mode != "" { | ||
cluster.Spec.Replication.Mode = pointer.String(comp.Spec.Parameters.Replication.Mode) | ||
cluster.Spec.Replication.SyncInstances = pointer.Int(comp.Spec.Parameters.Instances - 1) | ||
} | ||
return cluster | ||
} |
143 changes: 143 additions & 0 deletions
143
pkg/comp-functions/functions/vshn-postgres-func/replication_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package vshnpostgres | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
stackgresv1 "github.com/vshn/appcat/apis/stackgres/v1" | ||
vshnv1 "github.com/vshn/appcat/apis/vshn/v1" | ||
"github.com/vshn/appcat/pkg/comp-functions/functions/commontest" | ||
"github.com/vshn/appcat/pkg/comp-functions/runtime" | ||
) | ||
|
||
func Test_ConfigureReplication(t *testing.T) { | ||
ctx := context.TODO() | ||
iof := commontest.LoadRuntimeFromFile(t, "vshn-postgres/replication/01-GivenMulitInstance.yaml") | ||
|
||
res := ConfigureReplication(ctx, iof) | ||
assert.Equal(t, runtime.NewNormal(), res) | ||
|
||
cluster := &stackgresv1.SGCluster{} | ||
assert.NoError(t, iof.Desired.GetFromObject(ctx, cluster, "cluster")) | ||
assert.Equal(t, 3, cluster.Spec.Instances) | ||
assert.Equal(t, "sync", *cluster.Spec.Replication.Mode) | ||
assert.Equal(t, 2, *cluster.Spec.Replication.SyncInstances) | ||
} | ||
|
||
func Test_configureReplication_SingleInstance(t *testing.T) { | ||
ctx := context.Background() | ||
comp := &vshnv1.VSHNPostgreSQL{ | ||
Spec: vshnv1.VSHNPostgreSQLSpec{ | ||
Parameters: vshnv1.VSHNPostgreSQLParameters{ | ||
Instances: 1, | ||
Replication: vshnv1.VSHNPostgreSQLReplicationStrategy{}, | ||
}, | ||
}, | ||
} | ||
cluster := &stackgresv1.SGCluster{} | ||
|
||
cluster = configureReplication(ctx, comp, cluster) | ||
|
||
assert.Equal(t, 1, cluster.Spec.Instances) | ||
assert.Equal(t, "async", *cluster.Spec.Replication.Mode) | ||
} | ||
|
||
func Test_configureReplication_SingleInstance_Sync(t *testing.T) { | ||
ctx := context.Background() | ||
comp := &vshnv1.VSHNPostgreSQL{ | ||
Spec: vshnv1.VSHNPostgreSQLSpec{ | ||
Parameters: vshnv1.VSHNPostgreSQLParameters{ | ||
Instances: 1, | ||
Replication: vshnv1.VSHNPostgreSQLReplicationStrategy{ | ||
Mode: "sync", | ||
}, | ||
}, | ||
}, | ||
} | ||
cluster := &stackgresv1.SGCluster{} | ||
|
||
cluster = configureReplication(ctx, comp, cluster) | ||
|
||
assert.Equal(t, 1, cluster.Spec.Instances) | ||
assert.Equal(t, "async", *cluster.Spec.Replication.Mode) | ||
} | ||
|
||
func Test_configureReplication_MultiInstance_Async(t *testing.T) { | ||
ctx := context.Background() | ||
comp := &vshnv1.VSHNPostgreSQL{ | ||
Spec: vshnv1.VSHNPostgreSQLSpec{ | ||
Parameters: vshnv1.VSHNPostgreSQLParameters{ | ||
Instances: 2, | ||
Replication: vshnv1.VSHNPostgreSQLReplicationStrategy{ | ||
Mode: "async", | ||
}, | ||
}, | ||
}, | ||
} | ||
cluster := &stackgresv1.SGCluster{} | ||
|
||
cluster = configureReplication(ctx, comp, cluster) | ||
|
||
assert.Equal(t, 2, cluster.Spec.Instances) | ||
assert.Equal(t, "async", *cluster.Spec.Replication.Mode) | ||
} | ||
|
||
func Test_configureReplication_MultiInstance_Sync(t *testing.T) { | ||
ctx := context.Background() | ||
comp := &vshnv1.VSHNPostgreSQL{ | ||
Spec: vshnv1.VSHNPostgreSQLSpec{ | ||
Parameters: vshnv1.VSHNPostgreSQLParameters{ | ||
Instances: 2, | ||
Replication: vshnv1.VSHNPostgreSQLReplicationStrategy{ | ||
Mode: "sync", | ||
}, | ||
}, | ||
}, | ||
} | ||
cluster := &stackgresv1.SGCluster{} | ||
|
||
cluster = configureReplication(ctx, comp, cluster) | ||
|
||
assert.Equal(t, 2, cluster.Spec.Instances) | ||
assert.Equal(t, "sync", *cluster.Spec.Replication.Mode) | ||
assert.Equal(t, 1, *cluster.Spec.Replication.SyncInstances) | ||
} | ||
|
||
func Test_configureReplication_MultiInstance_StrictSync(t *testing.T) { | ||
ctx := context.Background() | ||
comp := &vshnv1.VSHNPostgreSQL{ | ||
Spec: vshnv1.VSHNPostgreSQLSpec{ | ||
Parameters: vshnv1.VSHNPostgreSQLParameters{ | ||
Instances: 3, | ||
Replication: vshnv1.VSHNPostgreSQLReplicationStrategy{ | ||
Mode: "strict-sync", | ||
}, | ||
}, | ||
}, | ||
} | ||
cluster := &stackgresv1.SGCluster{} | ||
|
||
cluster = configureReplication(ctx, comp, cluster) | ||
|
||
assert.Equal(t, 3, cluster.Spec.Instances) | ||
assert.Equal(t, "strict-sync", *cluster.Spec.Replication.Mode) | ||
assert.Equal(t, 2, *cluster.Spec.Replication.SyncInstances) | ||
} | ||
|
||
func Test_configureReplication_MultiInstance_Default(t *testing.T) { | ||
ctx := context.Background() | ||
comp := &vshnv1.VSHNPostgreSQL{ | ||
Spec: vshnv1.VSHNPostgreSQLSpec{ | ||
Parameters: vshnv1.VSHNPostgreSQLParameters{ | ||
Instances: 3, | ||
}, | ||
}, | ||
} | ||
cluster := &stackgresv1.SGCluster{} | ||
|
||
cluster = configureReplication(ctx, comp, cluster) | ||
|
||
assert.Equal(t, 3, cluster.Spec.Instances) | ||
assert.Equal(t, "async", *cluster.Spec.Replication.Mode) | ||
} |
Oops, something went wrong.