Skip to content

Commit

Permalink
Added Shared DB functionality
Browse files Browse the repository at this point in the history
* Removed the imageList and Image(already deprecated) from the spec
  No code should break because we have supplied a hard coded list
  instead. Also removed DatabaseImage struct as it is no longer used.
* Added new SharedDBAppName to reference the app that we would like
  to share the DB from.
* Added new helper called GetAppForDBInSameEnv in ClowdApp types, which
  finds the correct app spec and ensures the DB name matches.
* Added new helper called GetAppInSameEnv which, given an app, returns
  all other ClowdApp objects in the same Env.
* Kuttl test timeout increased to 400s to allow the kafka test to
  finish - on rare occasions it takes longer than 300s and this causes
  the test to fail when really the test passes fine.
* Updated all tests to drop the usage of imageList and image.
* Updated all documentation to drop the usage imageList and image.
* Updated all CRDs to drop the usage of imageList and image.
* Updated local and app-interface modes to allow the SharedDBAppName
  to be respected.
  • Loading branch information
psav authored and kylape committed Jan 25, 2021
1 parent bc30a29 commit 669ca4d
Show file tree
Hide file tree
Showing 31 changed files with 299 additions and 151 deletions.
36 changes: 36 additions & 0 deletions apis/cloud.redhat.com/v1alpha1/clowdapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ limitations under the License.
package v1alpha1

import (
"context"
"errors"
"fmt"

"cloud.redhat.com/clowder/v2/apis/cloud.redhat.com/v1alpha1/common"
Expand All @@ -21,6 +23,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// InitContainer is a struct defining a k8s init container. This will be
Expand Down Expand Up @@ -50,6 +53,9 @@ type DatabaseSpec struct {
// name of the logical database inside the database server in (*_local_*) mode
// and the name of the secret to be used for Database configuration in (*_app-interface_*) mode.
Name string `json:"name,omitempty"`

// Defines the Name of the app to share a database from
SharedDBAppName string `json:"sharedDbAppName,omitempty"`
}

// Job defines either a Job to be used in creating a Job via external means, or
Expand Down Expand Up @@ -378,3 +384,33 @@ func Labels(labels map[string]string) omfunc {
o.SetLabels(labels)
}
}

func GetAppInSameEnv(pClient client.Client, ctx context.Context, app *ClowdApp, appList *ClowdAppList) error {
err := pClient.List(ctx, appList, client.MatchingFields{"spec.envName": app.Spec.EnvName})

if err != nil {
return err
// return errors.New("Could not get app list")
}

return nil
}

func GetAppForDBInSameEnv(pClient client.Client, ctx context.Context, app *ClowdApp) (*ClowdApp, error) {
appList := &ClowdAppList{}
var refApp ClowdApp

err := GetAppInSameEnv(pClient, ctx, app, appList)

if err != nil {
return nil, err
}

for _, iapp := range appList.Items {
if iapp.Name == app.Spec.Database.SharedDBAppName {
refApp = iapp
return &refApp, nil
}
}
return nil, errors.New("Could not get app for db in env")
}
16 changes: 0 additions & 16 deletions apis/cloud.redhat.com/v1alpha1/clowdenvironment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,9 @@ type DatabaseConfig struct {
// where the provider will spin up a local instance of the database.
Mode DatabaseMode `json:"mode"`

// DEPRECATED In (*_local_*) mode, the Image field is used to define the database image
// for local database instances.
Image string `json:"image,omitempty"`

// If using the (*_local_*) mode and PVC is set to true, this instructs the local
// Database instance to use a PVC instead of emptyDir for its volumes.
PVC bool `json:"pvc,omitempty"`

// Allows the definition of multiple versions of database images, if Image
// is present ImageList is ignored.
ImageList []DatabaseImage `json:"imageList,omitempty"`
}

type DatabaseImage struct {
// Defines the major version of the postgres version to use.
Version int32 `json:"version"`

// Defines the image name to be deployed.
Image string `json:"image"`
}

// TODO: Other potential modes: splunk, kafka
Expand Down
2 changes: 1 addition & 1 deletion bundle/tests/scorecard/kuttl/kuttl-test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: kudo.dev/v1alpha1
kind: TestSuite
startControlPlane: false
timeout: 300
timeout: 400
parallel: 4
1 change: 0 additions & 1 deletion bundle/tests/scorecard/kuttl/test-basic-app/01-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
clusterName: my-cluster
mode: none
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down
1 change: 0 additions & 1 deletion bundle/tests/scorecard/kuttl/test-cron-job/01-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
clusterName: my-cluster
mode: none
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down
5 changes: 0 additions & 5 deletions bundle/tests/scorecard/kuttl/test-cyndi-local-db/01-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ spec:
clusterName: my-cluster
mode: none
db:
imageList:
- version: 12
image: "quay.io/cloudservices/postgresql-rds:12-1"
- version: 10
image: "quay.io/cloudservices/postgresql-rds:10-1"
mode: local
logging:
mode: none
Expand Down
1 change: 0 additions & 1 deletion bundle/tests/scorecard/kuttl/test-elasticache/01-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
clusterName: my-cluster
mode: none
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down
1 change: 0 additions & 1 deletion bundle/tests/scorecard/kuttl/test-kafka-app/01-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
clusterName: my-cluster
mode: local
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
clusterName: my-cluster
mode: operator
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down
3 changes: 1 addition & 2 deletions bundle/tests/scorecard/kuttl/test-local-kafka/01-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ spec:
clusterName: my-cluster
mode: local
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: local
mode: none
logging:
mode: none
objectStore:
Expand Down
1 change: 0 additions & 1 deletion bundle/tests/scorecard/kuttl/test-minio-app/01-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
clusterName: my-cluster
mode: none
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Namespace
metadata:
name: test-multi-app-interface-db
spec:
finalizers:
- kubernetes
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
apiVersion: cloud.redhat.com/v1alpha1
kind: ClowdEnvironment
metadata:
name: test-multi-app-interface-db
spec:
targetNamespace: test-multi-app-interface-db
providers:
web:
port: 8000
mode: operator
metrics:
port: 9000
mode: operator
path: "/metrics"
kafka:
namespace: kafka
clusterName: my-cluster
mode: none
db:
mode: app-interface
logging:
mode: none
objectStore:
mode: none
inMemoryDb:
mode: none
resourceDefaults:
limits:
cpu: 400m
memory: 1024Mi
requests:
cpu: 30m
memory: 512Mi
---
apiVersion: cloud.redhat.com/v1alpha1
kind: ClowdApp
metadata:
name: app-b
namespace: test-multi-app-interface-db
spec:
envName: test-multi-app-interface-db
deployments:
- name: processor
podSpec:
image: quay.io/psav/clowder-hello
database:
name: app-b
version: 10
---
apiVersion: cloud.redhat.com/v1alpha1
kind: ClowdApp
metadata:
name: app-c
namespace: test-multi-app-interface-db
spec:
envName: test-multi-app-interface-db
deployments:
- name: processor
podSpec:
image: quay.io/psav/clowder-hello
database:
sharedDbAppName: app-b
version: 10
dependencies:
- app-b
---
apiVersion: v1
kind: Secret
metadata:
name: app-b-readonly-db
namespace: test-multi-app-interface-db
type: Opaque
data:
db.host: YXBwLWItc3RhZ2UucmRzLmV4YW1wbGUuY29t # app-b-stage.rds.example.com
db.name: ZGJuYW1l # dbname
db.port: NTQzMg== # 5432
db.user: dXNlcg== # user
db.password: cGFzc3dvcmQxMjM= # password123
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
apiVersion: kudo.dev/v1alpha1
kind: TestStep
commands:
- script: sleep 5
- script: kubectl get secret --namespace=test-multi-app-interface-db app-c -o json > /tmp/test-multi-app-interface-db-c
- script: jq -r '.data["cdappconfig.json"]' < /tmp/test-multi-app-interface-db-c | base64 -d > /tmp/test-multi-app-interface-db-json-c

- script: jq -r '.database.hostname == "app-b-stage.rds.example.com"' -e < /tmp/test-multi-app-interface-db-json-c

- script: kubectl get secret --namespace=test-multi-app-interface-db app-b -o json > /tmp/test-multi-app-interface-db-b
- script: jq -r '.data["cdappconfig.json"]' < /tmp/test-multi-app-interface-db-b | base64 -d > /tmp/test-multi-app-interface-db-json-b

- script: jq -r '.database.hostname == "app-b-stage.rds.example.com"' -e < /tmp/test-multi-app-interface-db-json-b
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
apiVersion: kudo.dev/v1alpha1
kind: TestStep
delete:
- apiVersion: v1
kind: Namespace
name: test-multi-app-interface-db
- apiVersion: cloud.redhat.com/v1alpha1
kind: ClowdEnvironment
name: test-multi-app-interface-db
4 changes: 2 additions & 2 deletions bundle/tests/scorecard/kuttl/test-multi-db/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
template:
spec:
containers:
- image: registry.redhat.io/rhel8/postgresql-12:1-36
- image: quay.io/cloudservices/postgresql-rds:12-1
---
apiVersion: apps/v1
kind: Deployment
Expand All @@ -19,4 +19,4 @@ spec:
template:
spec:
containers:
- image: registry.redhat.io/rhel8/postgresql-10:1-118
- image: quay.io/cloudservices/postgresql-rds:10-1
23 changes: 17 additions & 6 deletions bundle/tests/scorecard/kuttl/test-multi-db/01-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ spec:
clusterName: my-cluster
mode: none
db:
imageList:
- version: 12
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
- version: 10
image: "registry.redhat.io/rhel8/postgresql-10:1-118"
mode: local
logging:
mode: none
Expand Down Expand Up @@ -66,4 +61,20 @@ spec:
image: quay.io/psav/clowder-hello
database:
name: app-b
version: 10
version: 10
---
apiVersion: cloud.redhat.com/v1alpha1
kind: ClowdApp
metadata:
name: app-c
namespace: test-multi-db
spec:
envName: test-multi-db
pods:
- name: processor
image: quay.io/psav/clowder-hello
database:
sharedDbAppName: app-b
version: 10
dependencies:
- app-b
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: kudo.dev/v1alpha1
kind: TestStep
commands:
- script: sleep 1
- script: kubectl get secret --namespace=test-multi-db app-c -o json > /tmp/test-multi-db
- script: jq -r '.data["cdappconfig.json"]' < /tmp/test-multi-db | base64 -d > /tmp/test-multi-db-json

- script: jq -r '.database.hostname == "app-b-db.test-multi-db.svc"' -e < /tmp/test-multi-db-json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
clusterName: my-cluster
mode: none
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down Expand Up @@ -55,7 +54,6 @@ spec:
clusterName: my-cluster
mode: none
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down
1 change: 0 additions & 1 deletion bundle/tests/scorecard/kuttl/test-rename-pod/01-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spec:
clusterName: my-cluster
mode: none
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ spec:
clusterName: my-cluster
mode: none
db:
image: "registry.redhat.io/rhel8/postgresql-12:1-36"
mode: none
logging:
mode: none
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/cloud.redhat.com_clowdapps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ spec:
server in (*_local_*) mode and the name of the secret to be used
for Database configuration in (*_app-interface_*) mode.
type: string
sharedDbAppName:
description: Defines the Name of the app to share a database from
type: string
version:
description: Defines the Version of the PostGreSQL database, defaults
to 12.
Expand Down
Loading

0 comments on commit 669ca4d

Please sign in to comment.