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

fix: align generation field with k8s api #279

Merged
merged 1 commit into from May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion authentication-service/src/lib.rs
Expand Up @@ -8,8 +8,8 @@ use drogue_cloud_service_api::{
health::HealthChecked,
webapp::{self as actix_web, prom::PrometheusMetricsBuilder},
};
use drogue_cloud_service_common::app::run_main;
use drogue_cloud_service_common::{
app::run_main,
defaults,
health::HealthServerConfig,
openid::{Authenticator, AuthenticatorConfig},
Expand Down
6 changes: 6 additions & 0 deletions authentication-service/tests/sql/10-basic/up.sql
Expand Up @@ -8,13 +8,15 @@ INSERT INTO APPLICATIONS (
CREATION_TIMESTAMP,
RESOURCE_VERSION,
GENERATION,
REVISION,
DATA
) VALUES (
'app1',
'4e185ea6-7c26-11eb-a319-d45d6455d210',
'2020-01-01 00:00:00',
'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11',
0,
0,
'{}'::JSONB
);

Expand All @@ -39,6 +41,7 @@ INSERT INTO DEVICES (
CREATION_TIMESTAMP,
RESOURCE_VERSION,
GENERATION,
REVISION,
DATA
) VALUES (
'app1',
Expand All @@ -47,6 +50,7 @@ INSERT INTO DEVICES (
'2020-01-01 00:00:00',
'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11',
0,
0,
'{
"spec": {
"credentials": {
Expand Down Expand Up @@ -85,6 +89,7 @@ INSERT INTO DEVICES (
CREATION_TIMESTAMP,
RESOURCE_VERSION,
GENERATION,
REVISION,
DATA
) VALUES (
'app1',
Expand All @@ -93,6 +98,7 @@ INSERT INTO DEVICES (
'2020-01-01 00:00:00',
'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11',
0,
0,
'{
"spec": {
"credentials": {
Expand Down
4 changes: 4 additions & 0 deletions authentication-service/tests/sql/20-x509/up.sql
Expand Up @@ -8,13 +8,15 @@ INSERT INTO APPLICATIONS (
CREATION_TIMESTAMP,
RESOURCE_VERSION,
GENERATION,
REVISION,
DATA
) VALUES (
'app2',
'4e185ea6-7c26-11eb-a319-d45d6455d220',
'2020-01-01 00:00:00',
'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11',
0,
0,
'{
"spec": {
"trustAnchors": {
Expand Down Expand Up @@ -69,6 +71,7 @@ INSERT INTO DEVICES (
CREATION_TIMESTAMP,
RESOURCE_VERSION,
GENERATION,
REVISION,
DATA
) VALUES (
'app2',
Expand All @@ -77,6 +80,7 @@ INSERT INTO DEVICES (
'2020-01-01 00:00:00',
'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11',
0,
0,
'{
"spec": {
"credentials": {}
Expand Down
8 changes: 8 additions & 0 deletions authentication-service/tests/sql/30-hashed/up.sql
Expand Up @@ -8,13 +8,15 @@ INSERT INTO APPLICATIONS (
CREATION_TIMESTAMP,
RESOURCE_VERSION,
GENERATION,
REVISION,
DATA
) VALUES (
'app3',
'4cf9607e-c7ad-11eb-8d69-d45d6455d2cc',
'2021-01-01 00:00:00',
'547531d4-c7ad-11eb-abee-d45d6455d2cc',
0,
0,
'{}'::JSONB
);

Expand All @@ -39,6 +41,7 @@ INSERT INTO DEVICES (
CREATION_TIMESTAMP,
RESOURCE_VERSION,
GENERATION,
REVISION,
DATA
) VALUES (
'app3',
Expand All @@ -47,6 +50,7 @@ INSERT INTO DEVICES (
'2020-01-01 00:00:00',
'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11',
0,
0,
'{
"spec": {
"credentials": {
Expand Down Expand Up @@ -82,6 +86,7 @@ INSERT INTO DEVICES (
CREATION_TIMESTAMP,
RESOURCE_VERSION,
GENERATION,
REVISION,
DATA
) VALUES (
'app3',
Expand All @@ -90,6 +95,7 @@ INSERT INTO DEVICES (
'2020-01-01 00:00:00',
'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11',
0,
0,
'{
"spec": {
"credentials": {
Expand Down Expand Up @@ -125,6 +131,7 @@ INSERT INTO DEVICES (
CREATION_TIMESTAMP,
RESOURCE_VERSION,
GENERATION,
REVISION,
DATA
) VALUES (
'app3',
Expand All @@ -133,6 +140,7 @@ INSERT INTO DEVICES (
'2020-01-01 00:00:00',
'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11',
0,
0,
'{
"spec": {
"credentials": {
Expand Down
13 changes: 13 additions & 0 deletions database-common/migrations/20220511145200_add_revision/down.sql
@@ -0,0 +1,13 @@
ALTER TABLE applications
DROP COLUMN REVISION
;

ALTER TABLE devices
DROP COLUMN REVISION
;

ALTER TABLE outbox
RENAME COLUMN REVISION TO GENERATION;

ALTER TABLE WORKQUEUE
RENAME COLUMN REV TO GEN;
35 changes: 35 additions & 0 deletions database-common/migrations/20220511145200_add_revision/up.sql
@@ -0,0 +1,35 @@
BEGIN;

-- applications

ALTER TABLE applications
ADD COLUMN REVISION BIGINT NOT NULL DEFAULT 0
;

UPDATE applications SET REVISION = GENERATION;

ALTER TABLE applications
ALTER COLUMN REVISION DROP DEFAULT
;

-- devices

ALTER TABLE devices
ADD COLUMN REVISION BIGINT NOT NULL DEFAULT 0
;

UPDATE devices SET REVISION = GENERATION;

ALTER TABLE devices
ALTER COLUMN REVISION DROP DEFAULT
;

-- commit

COMMIT;

ALTER TABLE outbox
RENAME COLUMN GENERATION TO REVISION;

ALTER TABLE WORKQUEUE
RENAME COLUMN GEN TO REV;
6 changes: 3 additions & 3 deletions database-common/src/error.rs
@@ -1,4 +1,4 @@
use crate::models::GenerationError;
use crate::models::AdvanceError;
use deadpool_postgres::PoolError;
use drogue_cloud_service_api::webapp::{HttpResponse, ResponseError};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -27,8 +27,8 @@ pub enum ServiceError {
OptimisticLockFailed,
}

impl From<GenerationError> for ServiceError {
fn from(err: GenerationError) -> Self {
impl From<AdvanceError> for ServiceError {
fn from(err: AdvanceError) -> Self {
ServiceError::Internal(err.to_string())
}
}
Expand Down
25 changes: 19 additions & 6 deletions database-common/src/models/app.rs
Expand Up @@ -8,7 +8,7 @@ use crate::{
sql::{slice_iter, SelectBuilder},
Lock, TypedAlias,
},
update_aliases, Client,
revision, update_aliases, Client,
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
Expand All @@ -35,8 +35,12 @@ pub struct Application {
pub labels: HashMap<String, String>,
pub annotations: HashMap<String, String>,
pub creation_timestamp: DateTime<Utc>,
/// Updated with each change
pub resource_version: Uuid,
/// Incremented with each change to the .spec section
pub generation: u64,
/// Incremented with each change
pub revision: u64,
pub deletion_timestamp: Option<DateTime<Utc>>,
pub finalizers: Vec<String>,

Expand All @@ -53,6 +57,7 @@ pub struct Application {

diffable!(Application);
generation!(Application => generation);
revision!(Application => revision);
default_resource!(Application);

impl Resource for Application {
Expand Down Expand Up @@ -193,6 +198,7 @@ impl<'c, C: Client> PostgresApplicationAccessor<'c, C> {

creation_timestamp: row.try_get("CREATION_TIMESTAMP")?,
generation: row.try_get::<_, i64>("GENERATION")? as u64,
revision: row.try_get::<_, i64>("REVISION")? as u64,
resource_version: row.try_get("RESOURCE_VERSION")?,
labels: super::row_to_map(&row, "LABELS")?,
annotations: super::row_to_map(&row, "ANNOTATIONS")?,
Expand Down Expand Up @@ -250,6 +256,7 @@ SELECT
A2.LABELS,
A2.CREATION_TIMESTAMP,
A2.GENERATION,
A2.REVISION,
A2.RESOURCE_VERSION,
A2.ANNOTATIONS,
A2.DELETION_TIMESTAMP,
Expand Down Expand Up @@ -301,6 +308,7 @@ SELECT
ANNOTATIONS,
CREATION_TIMESTAMP,
GENERATION,
REVISION,
RESOURCE_VERSION,
DELETION_TIMESTAMP,
FINALIZERS,
Expand Down Expand Up @@ -359,6 +367,7 @@ INSERT INTO APPLICATIONS (
ANNOTATIONS,
CREATION_TIMESTAMP,
GENERATION,
REVISION,
RESOURCE_VERSION,
FINALIZERS,
OWNER,
Expand All @@ -373,7 +382,8 @@ INSERT INTO APPLICATIONS (
$7,
$8,
$9,
$10
$10,
$11
)"#,
&[
&name,
Expand All @@ -382,6 +392,7 @@ INSERT INTO APPLICATIONS (
&Json(annotations),
&Utc::now(),
&(application.generation as i64),
&(application.revision as i64),
&Uuid::new_v4(),
&application.finalizers,
&application.owner,
Expand Down Expand Up @@ -415,10 +426,11 @@ SET
LABELS = $2,
ANNOTATIONS = $3,
GENERATION = $4,
RESOURCE_VERSION = $5,
DELETION_TIMESTAMP = $6,
FINALIZERS = $7,
DATA = $8
REVISION = $5,
RESOURCE_VERSION = $6,
DELETION_TIMESTAMP = $7,
FINALIZERS = $8,
DATA = $9
WHERE
NAME = $1
"#,
Expand All @@ -427,6 +439,7 @@ WHERE
&Json(labels),
&Json(annotations),
&(application.generation as i64),
&(application.revision as i64),
&Uuid::new_v4(),
&application.deletion_timestamp,
&application.finalizers,
Expand Down