Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cluster/local/integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ K8S_CLUSTER="${K8S_CLUSTER:-${BUILD_REGISTRY}-inttests}"
PACKAGE_NAME="provider-sql"
MARIADB_ROOT_PW=$(openssl rand -base64 32)
MARIADB_TEST_PW=$(openssl rand -base64 32)
MSSQL_SA_PW="$(openssl rand -base64 16)Aa1!" # MSSQL requires complex password

# cleanup on exit
if [ "$skipcleanup" != true ]; then
Expand All @@ -74,6 +75,13 @@ if [ $? -ne 0 ]; then
exit 1
fi

# shellcheck source="$SCRIPT_DIR/mssqldb_functions.sh"
source "$SCRIPT_DIR/mssqldb_functions.sh"
if [ $? -ne 0 ]; then
echo "mssqldb_functions.sh failed. Exiting."
exit 1
fi

integration_tests_end() {
echo_step "--- CLEAN-UP ---"
cleanup_provider
Expand Down Expand Up @@ -450,4 +458,8 @@ TLS=false API_TYPE="cluster" run_test integration_tests_mariadb
TLS=false API_TYPE="cluster" run_test integration_tests_postgres
TLS=false API_TYPE="namespaced" run_test integration_tests_postgres

# no TLS=false variant - MSSQL uses built-in encryption
TLS=true API_TYPE="cluster" run_test integration_tests_mssql
TLS=true API_TYPE="namespaced" run_test integration_tests_mssql

integration_tests_end
10 changes: 10 additions & 0 deletions cluster/local/mssql.providerconfig.cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: mssql.sql.crossplane.io/v1alpha1
kind: ProviderConfig
metadata:
name: default
spec:
credentials:
source: MSSQLConnectionSecret
connectionSecretRef:
namespace: default
name: mssql-creds
10 changes: 10 additions & 0 deletions cluster/local/mssql.providerconfig.namespaced.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: mssql.sql.m.crossplane.io/v1alpha1
kind: ProviderConfig
metadata:
name: default
namespace: default
spec:
credentials:
source: MSSQLConnectionSecret
connectionSecretRef:
name: mssql-creds
72 changes: 72 additions & 0 deletions cluster/local/mssql.server.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
apiVersion: v1
kind: Service
metadata:
name: mssql
namespace: default
spec:
ports:
- port: 1433
targetPort: 1433
selector:
app: mssql
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mssql
namespace: default
spec:
serviceName: mssql
replicas: 1
selector:
matchLabels:
app: mssql
template:
metadata:
labels:
app: mssql
spec:
containers:
- name: mssql
image: mcr.microsoft.com/mssql/server:2019-CU32-ubuntu-20.04
env:
- name: SA_PASSWORD
valueFrom:
secretKeyRef:
name: mssql-creds
key: password
- name: ACCEPT_EULA
value: "Y"
- name: MSSQL_PID
value: "Developer"
ports:
- containerPort: 1433
volumeMounts:
- name: data
mountPath: /var/opt/mssql
readinessProbe:
exec:
command:
- /bin/bash
- -c
- '/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -C -Q "SELECT 1"'
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
livenessProbe:
exec:
command:
- /bin/bash
- -c
- '/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -C -Q "SELECT 1"'
initialDelaySeconds: 60
periodSeconds: 30
timeoutSeconds: 5
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 2Gi
121 changes: 121 additions & 0 deletions cluster/local/mssqldb_functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/env bash
set -e

setup_mssql() {
echo_step "installing MSSQL Server"

"${KUBECTL}" create secret generic mssql-creds \
--from-literal username="sa" \
--from-literal password="${MSSQL_SA_PW}" \
--from-literal endpoint="mssql.default.svc.cluster.local" \
--from-literal port="1433"

echo_step "Verifying secret creation"
"${KUBECTL}" get secret mssql-creds -o yaml

"${KUBECTL}" apply -f ${scriptdir}/mssql.server.yaml

echo_step "Waiting for MSSQL Server to be ready"
"${KUBECTL}" wait --for=create pod mssql-0
"${KUBECTL}" wait --for=condition=ready pod -l app=mssql --timeout=300s

# Wait a bit more for MSSQL to be fully ready for connections
sleep 30
}

cleanup_mssql() {
echo_step "cleaning up MSSQL server"
"${KUBECTL}" delete -f ${scriptdir}/mssql.server.yaml --ignore-not-found=true
"${KUBECTL}" delete secret mssql-creds --ignore-not-found=true
}

setup_mssql_provider_config() {
echo_step "setting up MSSQL provider config"
"${KUBECTL}" apply -f "${scriptdir}/mssql.providerconfig.${API_TYPE}.yaml"
}

cleanup_mssql_provider_config() {
echo_step "cleaning up MSSQL provider config"
"${KUBECTL}" delete providerconfig.mssql.sql.${APIGROUP_SUFFIX}crossplane.io default --ignore-not-found=true
}

test_create_mssql_database() {
echo_step "test creating MSSQL Database resource"
"${KUBECTL}" apply -f ${projectdir}/examples/${API_TYPE}/mssql/database.yaml

echo_step "Waiting for MSSQL Database to be ready"
"${KUBECTL}" wait --timeout 2m --for condition=Ready -f ${projectdir}/examples/${API_TYPE}/mssql/database.yaml

echo_step_completed
}

test_create_mssql_user() {
echo_step "test creating MSSQL User resource (traditional)"
# Create password secret first
"${KUBECTL}" create secret generic example-pw --from-literal password="Test123!" --dry-run=client -o yaml | "${KUBECTL}" apply -f -

"${KUBECTL}" apply -f ${projectdir}/examples/${API_TYPE}/mssql/user.yaml

echo_step "Waiting for MSSQL User to be ready"
"${KUBECTL}" wait --timeout 2m --for condition=Ready -f ${projectdir}/examples/${API_TYPE}/mssql/user.yaml

echo_step_completed
}

test_update_mssql_user_password() {
echo_step "test updating MSSQL User password"

# Update password secret
"${KUBECTL}" patch secret example-pw -p '{"data":{"password":"'$(echo -n "NewTest123!" | base64)'"}}'

# Force reconcile by adding annotation
"${KUBECTL}" annotate -f ${projectdir}/examples/${API_TYPE}/mssql/user.yaml reconcile=now

# Wait a bit for password update
sleep 10

echo_step_completed
}

test_create_mssql_grant() {
echo_step "test creating MSSQL Grant resource"
"${KUBECTL}" apply -f ${projectdir}/examples/${API_TYPE}/mssql/grant.yaml

echo_step "Waiting for MSSQL Grant to be ready"
"${KUBECTL}" wait --timeout 2m --for condition=Ready -f ${projectdir}/examples/${API_TYPE}/mssql/grant.yaml

echo_step_completed
}

test_mssql_all() {
test_create_mssql_database
test_create_mssql_user
test_update_mssql_user_password
test_create_mssql_grant
}

cleanup_mssql_test_resources() {
echo_step "cleaning up MSSQL test resources"
"${KUBECTL}" delete -f ${projectdir}/examples/${API_TYPE}/mssql/grant.yaml --ignore-not-found=true
"${KUBECTL}" wait --for=delete grant.mssql.sql.${APIGROUP_SUFFIX}crossplane.io/example-grant --timeout=60s

"${KUBECTL}" delete -f ${projectdir}/examples/${API_TYPE}/mssql/user.yaml --ignore-not-found=true
"${KUBECTL}" wait --for=delete user.mssql.sql.${APIGROUP_SUFFIX}crossplane.io/example-user --timeout=60s

"${KUBECTL}" delete -f ${projectdir}/examples/${API_TYPE}/mssql/database.yaml --ignore-not-found=true
"${KUBECTL}" wait --for=delete database.mssql.sql.${APIGROUP_SUFFIX}crossplane.io/example-db --timeout=60s

echo_step "deleting example password secret"
"${KUBECTL}" delete secret example-pw --ignore-not-found=true
}

integration_tests_mssql() {
setup_mssql
setup_mssql_provider_config

test_mssql_all

cleanup_mssql_test_resources
cleanup_mssql_provider_config
cleanup_mssql
}
5 changes: 4 additions & 1 deletion examples/namespaced/mssql/database.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ kind: Database
metadata:
name: example-db
namespace: default
spec: {}
spec:
providerConfigRef:
kind: ProviderConfig
name: default
3 changes: 3 additions & 0 deletions examples/namespaced/mssql/grant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ metadata:
name: example-grant
namespace: default
spec:
providerConfigRef:
kind: ProviderConfig
name: default
forProvider:
permissions:
# CONNECT permission is added by default when user created. So, make sure
Expand Down
3 changes: 3 additions & 0 deletions examples/namespaced/mssql/user.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ metadata:
name: example-user
namespace: default
spec:
providerConfigRef:
kind: ProviderConfig
name: default
forProvider:
databaseRef:
name: example-db
Expand Down