Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devdb: add readinessProbe with TCP check #223

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
40 changes: 27 additions & 13 deletions internal/controller/devdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -132,7 +133,8 @@ func (r *devDBReconciler) devURL(ctx context.Context, sc client.Object, targetUR
return "", errWaitDevDB
// The dev database does not exist, create it.
case apierrors.IsNotFound(err):
deploy, err := deploymentDevDB(key, targetURL)
drv := dbv1alpha1.DriverBySchema(targetURL.Scheme)
deploy, err := deploymentDevDB(key, drv, drv.SchemaBound(targetURL))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -182,8 +184,7 @@ func (r *devDBReconciler) devURL(ctx context.Context, sc client.Object, targetUR
}

// deploymentDevDB returns a deployment for a dev database.
func deploymentDevDB(key types.NamespacedName, targetURL url.URL) (*appsv1.Deployment, error) {
drv := dbv1alpha1.DriverBySchema(targetURL.Scheme)
func deploymentDevDB(key types.NamespacedName, drv dbv1alpha1.Driver, schemaBound bool) (*appsv1.Deployment, error) {
var (
user string
pass string
Expand All @@ -209,7 +210,7 @@ func deploymentDevDB(key types.NamespacedName, targetURL url.URL) (*appsv1.Deplo
// URLs
user, pass, path = "postgres", "pass", "postgres"
q.Set("sslmode", "disable")
if drv.SchemaBound(targetURL) {
if schemaBound {
q.Set("search_path", "public")
}
// Containers
Expand All @@ -230,7 +231,7 @@ func deploymentDevDB(key types.NamespacedName, targetURL url.URL) (*appsv1.Deplo
// URLs
user, pass, path = "sa", "P@ssw0rd0995", ""
q.Set("database", "master")
if !drv.SchemaBound(targetURL) {
if !schemaBound {
q.Set("mode", "DATABASE")
}
// Containers
Expand Down Expand Up @@ -279,7 +280,7 @@ func deploymentDevDB(key types.NamespacedName, targetURL url.URL) (*appsv1.Deplo
c.Env = []corev1.EnvVar{
{Name: "MYSQL_ROOT_PASSWORD", Value: pass},
}
if drv.SchemaBound(targetURL) {
if schemaBound {
path = "dev"
c.Env = append(c.Env, corev1.EnvVar{
Name: "MYSQL_DATABASE", Value: path,
Expand All @@ -305,7 +306,7 @@ func deploymentDevDB(key types.NamespacedName, targetURL url.URL) (*appsv1.Deplo
c.Env = []corev1.EnvVar{
{Name: "MARIADB_ROOT_PASSWORD", Value: pass},
}
if drv.SchemaBound(targetURL) {
if schemaBound {
path = "dev"
c.Env = append(c.Env, corev1.EnvVar{
Name: "MARIADB_DATABASE", Value: path,
Expand All @@ -329,7 +330,7 @@ func deploymentDevDB(key types.NamespacedName, targetURL url.URL) (*appsv1.Deplo
{Name: "CLICKHOUSE_USER", Value: user},
{Name: "CLICKHOUSE_PASSWORD", Value: pass},
}
if drv.SchemaBound(targetURL) {
if schemaBound {
path = "dev"
c.Env = append(c.Env, corev1.EnvVar{
Name: "CLICKHOUSE_DB", Value: path,
Expand All @@ -342,6 +343,14 @@ func deploymentDevDB(key types.NamespacedName, targetURL url.URL) (*appsv1.Deplo
default:
return nil, fmt.Errorf(`devdb: unsupported driver %q. You need to provide the devURL on the resource: https://atlasgo.io/integrations/kubernetes/operator#devurl`, drv)
}
c.ReadinessProbe = &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
TCPSocket: &corev1.TCPSocketAction{
Port: intstr.FromInt32(c.Ports[0].ContainerPort),
},
},
PeriodSeconds: 5,
}
conn := &url.URL{
Scheme: c.Ports[0].Name,
User: url.UserPassword(user, pass),
Expand All @@ -350,11 +359,11 @@ func deploymentDevDB(key types.NamespacedName, targetURL url.URL) (*appsv1.Deplo
RawQuery: q.Encode(),
}
labels := map[string]string{
labelEngine: drv.String(),
labelInstance: key.Name,
"app.kubernetes.io/name": "atlas-dev-db",
"app.kubernetes.io/part-of": "atlas-operator",
"app.kubernetes.io/created-by": "controller-manager",
labelEngine: drv.String(),
labelInstance: key.Name,
"app.kubernetes.io/component": "dev-db",
"app.kubernetes.io/name": "atlas-dev-db",
"app.kubernetes.io/part-of": "atlas-operator",
}
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -386,6 +395,11 @@ func readyPod(pods []corev1.Pod) (*corev1.Pod, error) {
return nil, errors.New("no pods found")
}
idx := slices.IndexFunc(pods, func(p corev1.Pod) bool {
for _, c := range p.Status.ContainerStatuses {
if !c.Ready {
return false
}
}
return slices.ContainsFunc(p.Status.Conditions, func(c corev1.PodCondition) bool {
return c.Type == corev1.PodReady && c.Status == corev1.ConditionTrue
})
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/testscript/migration-mysql.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ spec:
ports:
- containerPort: 3306
name: mysql
readinessProbe:
tcpSocket:
port: 3306
periodSeconds: 5
startupProbe:
exec:
command: [ "mysql", "-ppass", "-h", "127.0.0.1", "-e", "SELECT 1" ]
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/testscript/schema-clickhouse.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ spec:
ports:
- containerPort: 9000
name: clickhouse
readinessProbe:
tcpSocket:
port: 9000
periodSeconds: 5
startupProbe:
exec:
command: [ "clickhouse-client", "-q", "SELECT 1" ]
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/testscript/schema-lint-destructive.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ cmp stdout schema.hcl

kubectl patch -f schema.yaml --type merge --patch-file patch-remove-bio.yaml

# Wait for the controller to detect the change
exec sleep 10

# Ensure the controller is aware of the change
kubectl wait --for=jsonpath='{.status.conditions[*].reason}'=LintPolicyError --timeout=120s AtlasSchemas/mysql
# Check the error message
Expand Down Expand Up @@ -127,6 +124,10 @@ spec:
ports:
- containerPort: 3306
name: mysql
readinessProbe:
tcpSocket:
port: 3306
periodSeconds: 5
startupProbe:
exec:
command: [ "mysql", "-ppass", "-h", "127.0.0.1", "-e", "SELECT 1" ]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/testscript/schema-manual-changes.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ spec:
ports:
- containerPort: 5432
name: postgres
readinessProbe:
tcpSocket:
port: 5432
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand All @@ -171,6 +175,10 @@ spec:
ports:
- containerPort: 5433
name: postgres-dev
readinessProbe:
tcpSocket:
port: 5433
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/testscript/schema-mariadb.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ spec:
ports:
- containerPort: 3306
name: mariadb
readinessProbe:
tcpSocket:
port: 3306
periodSeconds: 5
startupProbe:
exec:
command: [ "mariadb", "-ppass", "-h", "127.0.0.1", "-e", "SELECT 1" ]
Expand All @@ -218,6 +222,10 @@ spec:
ports:
- containerPort: 3307
name: mariadb-dev
readinessProbe:
tcpSocket:
port: 3307
periodSeconds: 5
startupProbe:
exec:
command: [ "mariadb", "-ppass", "-h", "127.0.0.1", "-e", "SELECT 1" ]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/testscript/schema-mysql.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ spec:
ports:
- containerPort: 3306
name: mysql
readinessProbe:
tcpSocket:
port: 3306
periodSeconds: 5
startupProbe:
exec:
command: [ "mysql", "-ppass", "-h", "127.0.0.1", "-e", "SELECT 1" ]
Expand All @@ -218,6 +222,10 @@ spec:
ports:
- containerPort: 3307
name: mysql-dev
readinessProbe:
tcpSocket:
port: 3307
periodSeconds: 5
startupProbe:
exec:
command: [ "mysql", "-ppass", "-h", "127.0.0.1", "-e", "SELECT 1" ]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/testscript/schema-plan-concurrently.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ spec:
ports:
- containerPort: 5432
name: postgres
readinessProbe:
tcpSocket:
port: 5432
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand All @@ -210,6 +214,10 @@ spec:
ports:
- containerPort: 5433
name: postgres-dev
readinessProbe:
tcpSocket:
port: 5433
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/testscript/schema-plan-destructive.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ spec:
ports:
- containerPort: 5432
name: postgres
readinessProbe:
tcpSocket:
port: 5432
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand All @@ -173,6 +177,10 @@ spec:
ports:
- containerPort: 5433
name: postgres-dev
readinessProbe:
tcpSocket:
port: 5433
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/testscript/schema-plan-no-push.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ spec:
ports:
- containerPort: 5432
name: postgres
readinessProbe:
tcpSocket:
port: 5432
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand All @@ -170,6 +174,10 @@ spec:
ports:
- containerPort: 5433
name: postgres-dev
readinessProbe:
tcpSocket:
port: 5433
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/testscript/schema-plan-pre-approved.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ spec:
ports:
- containerPort: 5432
name: postgres
readinessProbe:
tcpSocket:
port: 5432
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand All @@ -182,6 +186,10 @@ spec:
ports:
- containerPort: 5433
name: postgres-dev
readinessProbe:
tcpSocket:
port: 5433
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/testscript/schema-policy-diff.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ spec:
ports:
- containerPort: 3306
name: mysql
readinessProbe:
tcpSocket:
port: 3306
periodSeconds: 5
startupProbe:
exec:
command: [ "mysql", "-ppass", "-h", "127.0.0.1", "-e", "SELECT 1" ]
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/testscript/schema-postgres.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ spec:
ports:
- containerPort: 5432
name: postgres
readinessProbe:
tcpSocket:
port: 5432
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/testscript/schema-registry.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ spec:
ports:
- containerPort: 5432
name: postgres
readinessProbe:
tcpSocket:
port: 5432
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand All @@ -161,6 +165,10 @@ spec:
ports:
- containerPort: 5433
name: postgres-dev
readinessProbe:
tcpSocket:
port: 5433
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/testscript/schema-review-always.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ spec:
ports:
- containerPort: 5432
name: postgres
readinessProbe:
tcpSocket:
port: 5432
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand All @@ -149,6 +153,10 @@ spec:
ports:
- containerPort: 5433
name: postgres-dev
readinessProbe:
tcpSocket:
port: 5433
periodSeconds: 5
startupProbe:
exec:
command: [ "pg_isready" ]
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/testscript/schema-sqlserver.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ spec:
ports:
- containerPort: 1433
name: sqlserver
readinessProbe:
tcpSocket:
port: 1433
periodSeconds: 5
startupProbe:
exec:
command: [
Expand Down
Loading