Skip to content

Commit 81e707c

Browse files
committed
add flag for db init routine incl. test asserts as part of cluster ops
1 parent 01d4ac0 commit 81e707c

6 files changed

Lines changed: 86 additions & 34 deletions

File tree

deploy/helm/airflow-operator/crds/crds.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@ spec:
591591
- repo
592592
type: object
593593
type: array
594+
dbInit:
595+
default: true
596+
description: Whether to execute the database initialization routines (a combination of database initialization, upgrade and migration depending on the Airflow version). Defaults to true to be backwward compatible.
597+
type: boolean
594598
exposeConfig:
595599
default: false
596600
description: for internal use only - not for production use.

rust/operator-binary/src/airflow_controller.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,11 @@ fn build_server_rolegroup_statefulset(
951951
.context(GracefulShutdownSnafu)?;
952952

953953
let mut airflow_container_args = Vec::new();
954-
airflow_container_args
955-
.extend(airflow_role.get_commands(authentication_config, resolved_product_image));
954+
airflow_container_args.extend(airflow_role.get_commands(
955+
airflow,
956+
authentication_config,
957+
resolved_product_image,
958+
));
956959

957960
airflow_container
958961
.image_from_product_image(resolved_product_image)

rust/operator-binary/src/crd/mod.rs

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ pub mod versioned {
251251
#[serde(default)]
252252
pub load_examples: bool,
253253

254+
/// Whether to execute the database initialization routines (a combination of database initialization, upgrade and migration depending on the Airflow version). Defaults to true to be backwward compatible.
255+
#[serde(default = "default_db_init")]
256+
pub db_init: bool,
257+
254258
/// Name of the Vector aggregator [discovery ConfigMap](DOCS_BASE_URL_PLACEHOLDER/concepts/service_discovery).
255259
/// It must contain the key `ADDRESS` with the address of the Vector aggregator.
256260
/// Follow the [logging tutorial](DOCS_BASE_URL_PLACEHOLDER/tutorials/logging-vector-aggregator)
@@ -268,7 +272,6 @@ pub mod versioned {
268272
#[schemars(schema_with = "raw_object_list_schema")]
269273
pub volume_mounts: Vec<VolumeMount>,
270274
}
271-
272275
// TODO: move generic version to op-rs?
273276
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
274277
#[serde(rename_all = "camelCase")]
@@ -282,6 +285,10 @@ pub mod versioned {
282285
}
283286
}
284287

288+
fn default_db_init() -> bool {
289+
true
290+
}
291+
285292
impl Default for v1alpha1::WebserverRoleConfig {
286293
fn default() -> Self {
287294
v1alpha1::WebserverRoleConfig {
@@ -547,6 +554,7 @@ impl AirflowRole {
547554
/// if authentication is enabled.
548555
pub fn get_commands(
549556
&self,
557+
airflow: &v1alpha1::AirflowCluster,
550558
auth_config: &AirflowClientAuthenticationDetailsResolved,
551559
resolved_product_image: &ResolvedProductImage,
552560
) -> Vec<String> {
@@ -576,21 +584,30 @@ impl AirflowRole {
576584
"airflow api-server &".to_string(),
577585
]);
578586
}
579-
AirflowRole::Scheduler => command.extend(vec![
580-
"airflow db migrate".to_string(),
581-
"airflow users create \
582-
--username \"$ADMIN_USERNAME\" \
583-
--firstname \"$ADMIN_FIRSTNAME\" \
584-
--lastname \"$ADMIN_LASTNAME\" \
585-
--email \"$ADMIN_EMAIL\" \
586-
--password \"$ADMIN_PASSWORD\" \
587-
--role \"Admin\""
588-
.to_string(),
589-
"prepare_signal_handlers".to_string(),
590-
container_debug_command(),
591-
"airflow dag-processor &".to_string(),
592-
"airflow scheduler &".to_string(),
593-
]),
587+
AirflowRole::Scheduler => {
588+
if airflow.spec.cluster_config.db_init {
589+
tracing::info!("Database initialization...");
590+
command.extend(vec![
591+
"airflow db migrate".to_string(),
592+
"airflow users create \
593+
--username \"$ADMIN_USERNAME\" \
594+
--firstname \"$ADMIN_FIRSTNAME\" \
595+
--lastname \"$ADMIN_LASTNAME\" \
596+
--email \"$ADMIN_EMAIL\" \
597+
--password \"$ADMIN_PASSWORD\" \
598+
--role \"Admin\""
599+
.to_string(),
600+
]);
601+
} else {
602+
tracing::info!("Database initialization routines have been skipped!")
603+
}
604+
command.extend(vec![
605+
"prepare_signal_handlers".to_string(),
606+
container_debug_command(),
607+
"airflow dag-processor &".to_string(),
608+
"airflow scheduler &".to_string(),
609+
]);
610+
}
594611
AirflowRole::Worker => command.extend(vec![
595612
"prepare_signal_handlers".to_string(),
596613
container_debug_command(),
@@ -608,22 +625,31 @@ impl AirflowRole {
608625
"airflow webserver &".to_string(),
609626
]);
610627
}
611-
AirflowRole::Scheduler => command.extend(vec![
612-
// Database initialization is limited to the scheduler, see https://github.com/stackabletech/airflow-operator/issues/259
613-
"airflow db init".to_string(),
614-
"airflow db upgrade".to_string(),
615-
"airflow users create \
616-
--username \"$ADMIN_USERNAME\" \
617-
--firstname \"$ADMIN_FIRSTNAME\" \
618-
--lastname \"$ADMIN_LASTNAME\" \
619-
--email \"$ADMIN_EMAIL\" \
620-
--password \"$ADMIN_PASSWORD\" \
621-
--role \"Admin\""
622-
.to_string(),
623-
"prepare_signal_handlers".to_string(),
624-
container_debug_command(),
625-
"airflow scheduler &".to_string(),
626-
]),
628+
AirflowRole::Scheduler => {
629+
if airflow.spec.cluster_config.db_init {
630+
tracing::info!("Database initialization...");
631+
command.extend(vec![
632+
// Database initialization is limited to the scheduler, see https://github.com/stackabletech/airflow-operator/issues/259
633+
"airflow db init".to_string(),
634+
"airflow db upgrade".to_string(),
635+
"airflow users create \
636+
--username \"$ADMIN_USERNAME\" \
637+
--firstname \"$ADMIN_FIRSTNAME\" \
638+
--lastname \"$ADMIN_LASTNAME\" \
639+
--email \"$ADMIN_EMAIL\" \
640+
--password \"$ADMIN_PASSWORD\" \
641+
--role \"Admin\""
642+
.to_string(),
643+
]);
644+
} else {
645+
tracing::info!("Database initialization routines have been skipped!")
646+
}
647+
command.extend(vec![
648+
"prepare_signal_handlers".to_string(),
649+
container_debug_command(),
650+
"airflow scheduler &".to_string(),
651+
]);
652+
}
627653
AirflowRole::Worker => command.extend(vec![
628654
"prepare_signal_handlers".to_string(),
629655
container_debug_command(),
@@ -981,5 +1007,7 @@ mod tests {
9811007
assert_eq!("KubernetesExecutor", cluster.spec.executor.to_string());
9821008
assert!(cluster.spec.cluster_config.load_examples);
9831009
assert!(cluster.spec.cluster_config.expose_config);
1010+
// defaults to true
1011+
assert!(cluster.spec.cluster_config.db_init);
9841012
}
9851013
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# For this assert we expect the database operation to be logged
3+
apiVersion: kuttl.dev/v1beta1
4+
kind: TestAssert
5+
timeout: 30
6+
commands:
7+
- script: |
8+
kubectl -n $NAMESPACE logs airflow-scheduler-default-0 | grep "Database migrating done!"

tests/templates/kuttl/cluster-operation/30-restart-airflow.yaml.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ spec:
2525
vectorAggregatorConfigMapName: vector-aggregator-discovery
2626
{% endif %}
2727
credentialsSecret: test-airflow-credentials
28+
dbInit: false
2829
webservers:
2930
roleConfig:
3031
listenerClass: external-unstable
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# For this step we expect the database operation to NOT be logged
3+
apiVersion: kuttl.dev/v1beta1
4+
kind: TestAssert
5+
timeout: 30
6+
commands:
7+
- script: |
8+
kubectl -n $NAMESPACE logs airflow-scheduler-default-0 | grep -q "Database migrating done!" && exit 1 || exit 0

0 commit comments

Comments
 (0)