Skip to content

Commit

Permalink
Add a basic Sql Datasource for operator (#5363)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP authored Oct 21, 2024
1 parent 6865792 commit 20c04bb
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.apicurio.registry.operator.OperatorException;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.api.v1.spec.Sql;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
Expand Down Expand Up @@ -33,6 +34,12 @@ public class AppDeploymentResource extends CRUDKubernetesDependentResource<Deplo

private static final Logger log = LoggerFactory.getLogger(AppDeploymentResource.class);

public static final String ENV_APICURIO_STORAGE_KIND = "APICURIO_STORAGE_KIND";
public static final String ENV_APICURIO_STORAGE_SQL_KIND = "APICURIO_STORAGE_SQL_KIND";
public static final String ENV_APICURIO_DATASOURCE_URL = "APICURIO_DATASOURCE_URL";
public static final String ENV_APICURIO_DATASOURCE_USERNAME = "APICURIO_DATASOURCE_USERNAME";
public static final String ENV_APICURIO_DATASOURCE_PASSWORD = "APICURIO_DATASOURCE_PASSWORD";

public AppDeploymentResource() {
super(Deployment.class);
}
Expand All @@ -58,13 +65,34 @@ protected Deployment desired(ApicurioRegistry3 primary, Context<ApicurioRegistry
addEnvVar(envVars, new EnvVarBuilder().withName("APICURIO_APIS_V2_DATE_FORMAT").withValue("yyyy-MM-dd''T''HH:mm:ssZ").build());
// spotless:on

configureSqlDatasource(envVars, primary.getSpec().getApp().getSql());

var container = getContainer(d, APP_CONTAINER_NAME);
container.setEnv(envVars.values().stream().toList());

log.debug("Desired {} is {}", APP_DEPLOYMENT_KEY.getId(), toYAML(d));
return d;
}

private static void configureSqlDatasource(Map<String, EnvVar> map, Sql sql) {
if (sql != null && sql.getDatasource() != null) {
var datasource = sql.getDatasource();

addEnvVar(map, new EnvVarBuilder().withName(ENV_APICURIO_STORAGE_KIND).withValue("sql").build());
addEnvVar(map, new EnvVarBuilder().withName(ENV_APICURIO_STORAGE_SQL_KIND).withValue("postgresql")
.build());

addEnvVar(map, new EnvVarBuilder().withName(ENV_APICURIO_DATASOURCE_URL)
.withValue(datasource.getUrl()).build());
addEnvVar(map, new EnvVarBuilder().withName(ENV_APICURIO_DATASOURCE_USERNAME)
.withValue(datasource.getUsername()).build());
addEnvVar(map, new EnvVarBuilder().withName(ENV_APICURIO_DATASOURCE_PASSWORD)
.withValue(datasource.getPassword()).build());
} else {
log.info("No SQL datasource configured");
}
}

public static void addEnvVar(Map<String, EnvVar> map, EnvVar envVar) {
if (!map.containsKey(envVar.getName())) {
map.put(envVar.getName(), envVar);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.apicurio.registry.operator.it;

import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.api.v1.spec.Sql;
import io.apicurio.registry.operator.api.v1.spec.sql.Datasource;
import io.apicurio.registry.operator.resource.ResourceFactory;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

@QuarkusTest
public class SqlDatasourceITTest extends ITBase {

private static final Logger log = LoggerFactory.getLogger(SqlDatasourceITTest.class);

@Test
void testSqlDatasource() {
client.load(SqlDatasourceITTest.class.getResourceAsStream("/k8s/example-postgres.yaml")).create();
// await for postgres to be available
await().ignoreExceptions().until(() -> (1 == client.apps().statefulSets().inNamespace(namespace)
.withName("postgresql-db").get().getStatus().getReadyReplicas()));

var registry = ResourceFactory.deserialize("/k8s/examples/simple.apicurioregistry3.yaml",
ApicurioRegistry3.class);
registry.getMetadata().setNamespace(namespace);
var sql = new Sql();
registry.getSpec().getApp().setSql(sql);
var datasource = new Datasource();
sql.setDatasource(datasource);
datasource.setUrl("jdbc:postgresql://postgres-db:5432/apicurio");
datasource.setUsername("postgres-username");
datasource.setPassword("postgres-password");

client.resource(registry).create();

await().ignoreExceptions().until(() -> {
assertThat(client.apps().deployments().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-deployment").get().getStatus()
.getReadyReplicas().intValue()).isEqualTo(1);
var podName = client.pods().inNamespace(namespace).list().getItems().stream()
.map(pod -> pod.getMetadata().getName())
.filter(podN -> podN.startsWith("simple-app-deployment")).findFirst().get();
assertThat(client.pods().inNamespace(namespace).withName(podName).getLog())
.contains("Database type: postgresql");

return true;
});
}
}
44 changes: 44 additions & 0 deletions operator/controller/src/test/resources/k8s/example-postgres.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# PostgreSQL StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgresql-db
spec:
serviceName: postgresql-db-service
selector:
matchLabels:
app: postgresql-db
replicas: 1
template:
metadata:
labels:
app: postgresql-db
spec:
containers:
- name: postgresql-db
image: quay.io/sclorg/postgresql-15-c9s:latest
volumeMounts:
- mountPath: /var/lib/pgsql/data
name: cache-volume
env:
- name: POSTGRESQL_USER
value: postgres-username
- name: POSTGRESQL_PASSWORD
value: postgres-password
- name: POSTGRESQL_DATABASE
value: apicurio
volumes:
- name: cache-volume
emptyDir: {}
---
# PostgreSQL StatefulSet Service
apiVersion: v1
kind: Service
metadata:
name: postgres-db
spec:
selector:
app: postgresql-db
ports:
- port: 5432
targetPort: 5432
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.JsonDeserializer.None;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.apicurio.registry.operator.api.v1.spec.Sql;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import lombok.*;
Expand All @@ -12,7 +13,7 @@
import java.util.List;

@JsonInclude(Include.NON_NULL)
@JsonPropertyOrder({ "env", "host" })
@JsonPropertyOrder({ "env", "host", "sql" })
@JsonDeserialize(using = None.class)
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -47,4 +48,10 @@ public class ApicurioRegistry3SpecApp implements KubernetesResource {
If you create the Ingress manually, you have to manually set the REGISTRY_API_URL environment variable for the backend component.""")
@JsonSetter(nulls = Nulls.SKIP)
private String host;

@JsonProperty("sql")
@JsonPropertyDescription("""
Configuration of Apicurio Registry SQL storage.""")
@JsonSetter(nulls = Nulls.SKIP)
private Sql sql;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.apicurio.registry.operator.api.v1.spec;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.apicurio.registry.operator.api.v1.spec.sql.Datasource;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "datasource" })
@JsonDeserialize(using = JsonDeserializer.None.class)
@Getter
@Setter
@ToString
public class Sql {

@JsonProperty("datasource")
@JsonPropertyDescription("""
SQL data source.""")
@JsonSetter(nulls = Nulls.SKIP)
private Datasource datasource;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.apicurio.registry.operator.api.v1.spec.sql;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "url", "username", "password" })
@JsonDeserialize(using = JsonDeserializer.None.class)
@Getter
@Setter
@ToString
public class Datasource {

@JsonProperty("url")
@JsonPropertyDescription("""
Data source URL: \\n URL of the PostgreSQL
database, for example: `jdbc:postgresql://<service name>.<namespace>.svc:5432/<database name>`.""")
@JsonSetter(nulls = Nulls.SKIP)
private String url;

@JsonProperty("username")
@JsonPropertyDescription("""
Data source username.""")
@JsonSetter(nulls = Nulls.SKIP)
private String username;

@JsonProperty("password")
@JsonPropertyDescription("""
Data source password.""")
@JsonSetter(nulls = Nulls.SKIP)
private String password;

// TODO: support values from Secrets:
//
// Proposal to support secrets, add alternative fields like:
// @JsonProperty("passwordFrom")
// @JsonPropertyDescription("""
// Data source password from Secret/ConfigMap/...""")
// @JsonSetter(nulls = Nulls.SKIP)
// private EnvVarSource passwordFrom;

}

0 comments on commit 20c04bb

Please sign in to comment.