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

feat: reactivate matrix with google cloud storage instead of PVC #334

Open
wants to merge 7 commits into
base: legacy
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
3 changes: 3 additions & 0 deletions resources/google/gke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const cluster = new google.container.v1.Cluster(
releaseChannel: { channel: 'REGULAR' },
location: region,
autopilot: { enabled: true },
addonsConfig: {
gcsFuseCsiDriverConfig: { enabled: true },
},
},
{ provider: mainProvider, protect: true },
);
2 changes: 1 addition & 1 deletion resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import './kubernetes/provider';
// Kubernetes Deployments
import './kubernetes/deployments/abax-minuba';
import './kubernetes/deployments/abax-procore';
// import './kubernetes/deployments/matrix';
import './kubernetes/deployments/matrix';
import './kubernetes/deployments/abax-vwfs';
import './kubernetes/todoist-github/deployment';
import './kubernetes/todoist-github/ingress';
Expand Down
19 changes: 18 additions & 1 deletion resources/kubernetes/components/standard-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ export interface StandardDeploymentArgs
*/
securityContext?: pulumi.Input<k8s.types.input.core.v1.PodSecurityContext>;

/**
* Annotations is an unstructured key value map stored with a resource that may be set by external
* tools to store and retrieve arbitrary metadata.
* They are not queryable and should be preserved when modifying objects.
* More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations
*/
annotations?: Record<string, pulumi.Input<string>>;

/**
* ServiceAccountName is the name of the ServiceAccount to use to run this pod.
* More info: https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
*/
serviceAccountName?: pulumi.Input<string>;

/**
* Arguments to the entrypoint. The container image's CMD is used if this is not provided.
* Variable references $(VAR_NAME) are expanded using the container's environment.
Expand Down Expand Up @@ -229,6 +243,8 @@ export class StandardDeployment extends pulumi.ComponentResource {
args: entrypointArgs = undefined,
volumeMounts = [],
securityContext = undefined,
annotations = {},
serviceAccountName = undefined,
} = args;

const publicPort = ports.find(p => p.name === 'public');
Expand Down Expand Up @@ -318,14 +334,14 @@ export class StandardDeployment extends pulumi.ComponentResource {
}
: undefined,
};

this.deployment = new k8s.apps.v1.Deployment(
name,
{
metadata: {
name,
annotations: {
'pulumi.com/skipAwait': 'true',
...annotations,
},
},
spec: {
Expand All @@ -343,6 +359,7 @@ export class StandardDeployment extends pulumi.ComponentResource {
},
spec: {
securityContext,
serviceAccountName,
volumes,
initContainers: pulumi.output(initContainers).apply(ic =>
ic.map(initContainer => ({
Expand Down
78 changes: 23 additions & 55 deletions resources/kubernetes/deployments/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as pulumi from '@pulumi/pulumi';
import * as yaml from 'js-yaml';
import { StandardDatabase } from '../components/standard-database';
import { StandardDeployment } from '../components/standard-deployment';
import { matrixDataBucket } from '../matrix/google';
import { matrixK8sServiceAccount } from '../matrix/k8s';
import { provider } from '../provider';

const config = new pulumi.Config('matrix');
Expand All @@ -21,45 +23,7 @@ export const synapseDatabase = new StandardDatabase(

const registrationSecret = config.requireSecret('registration-secret');

const mediaVolume = new k8s.core.v1.PersistentVolumeClaim(
'matrix-media-volume',
{
metadata: {
name: 'matrix-media-volume',
},
spec: {
accessModes: ['ReadWriteOnce'],
resources: {
requests: {
storage: '1Gi',
},
},
storageClassName: 'standard',
},
},
{ provider },
);

const dataVolume = new k8s.core.v1.PersistentVolumeClaim(
'matrix-data-volume',
{
metadata: {
name: 'matrix-data-volume',
},
spec: {
accessModes: ['ReadWriteOnce'],
resources: {
requests: {
storage: '1Gi',
},
},
storageClassName: 'standard',
},
},
{ provider },
);

const secretVolume = new k8s.core.v1.Secret(
const secret = new k8s.core.v1.Secret(
'matrix-registration-secret',
{
metadata: {
Expand All @@ -75,7 +39,9 @@ const secretVolume = new k8s.core.v1.Secret(
},
{ provider },
);

const host = config.require('host');

export const homeserverConfig = new k8s.core.v1.ConfigMap(
'matrix-homeserver-config',
{
Expand All @@ -93,7 +59,7 @@ export const homeserverConfig = new k8s.core.v1.ConfigMap(
enable_registration_captcha: true,
registration_requires_token: true,
report_stats: true,
media_store_path: '/synapse/media_store',
media_store_path: '/synapse/data/media_store',
pid_file: '/synapse/data/homeserver.pid',
signing_key_path: '/synapse/data/bjerk.io.signing.key',
database: {
Expand Down Expand Up @@ -124,14 +90,21 @@ export const synapseDeployment = new StandardDeployment(
image: config.require('synapse-image'),
tag: config.require('synapse-tag'),
host,
annotations: {
'gke-gcsfuse/volumes': 'true',
'gke-gcsfuse/cpu-limit': '500m',
'gke-gcsfuse/memory-limit': '1Gi',
'gke-gcsfuse/ephemeral-storage-limit': '50Gi',
},
serviceAccountName: matrixK8sServiceAccount.metadata.name,
healthCheckHttpPath: '/health',
databaseDetails: synapseDatabase.databaseDetails,
volumes: [
{
name: 'secrets',
emptyDir: {},
secret: {
secretName: secretVolume.metadata.name,
secretName: secret.metadata.name,
},
},
{
Expand All @@ -142,15 +115,14 @@ export const synapseDeployment = new StandardDeployment(
},
},
{
name: 'data',
persistentVolumeClaim: {
claimName: dataVolume.metadata.name,
},
},
{
name: 'media',
persistentVolumeClaim: {
claimName: mediaVolume.metadata.name,
name: 'gcs-fuse-csi-ephemeral',
csi: {
driver: 'gcs.csi.infra.gke.io',
readOnly: true,
volumeAttributes: {
bucketName: matrixDataBucket.name,
mountOptions: 'implicit-dirs',
},
},
},
],
Expand All @@ -164,13 +136,9 @@ export const synapseDeployment = new StandardDeployment(
mountPath: '/config',
},
{
name: 'data',
name: 'gcs-fuse-csi-ephemeral',
mountPath: '/synapse/data',
},
{
name: 'media',
mountPath: '/synapse/media_store',
},
],
// This is needed to tell synapse to load the secrets and config from these files
command: [
Expand Down
17 changes: 17 additions & 0 deletions resources/kubernetes/matrix/fuse-csi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as google from '@pulumi/google-native';
import * as pulumi from '@pulumi/pulumi';
import { mainProvider, project } from '../../google/project';
import { matrixSynapseServiceAccount } from './google';
import { matrixK8sServiceAccount } from './k8s';

new google.iam.v1.ServiceAccountIamBinding(
'iamServiceAccountIamBinding',
{
name: matrixSynapseServiceAccount.name,
role: 'roles/iam.serviceAccountTokenCreator',
members: [
pulumi.interpolate`serviceAccount:${project.projectId}.svc.id.goog[${matrixK8sServiceAccount.metadata.namespace}/${matrixK8sServiceAccount.metadata.name}]`,
],
},
{ provider: mainProvider },
);
39 changes: 39 additions & 0 deletions resources/kubernetes/matrix/google.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as google from '@pulumi/google-native';
import { region } from '../../config';
import { mainProvider } from '../../google/project';

// Create a Google Cloud Storage bucket
export const matrixDataBucket = new google.storage.v1.Bucket(
'matrix-synapse-media-bucket',
{
name: 'matrix-synapse-media-bucket',
location: region,
},
{ provider: mainProvider },
);

// Create a Google Cloud service account
export const matrixSynapseServiceAccount = new google.iam.v1.ServiceAccount(
'matrix-synapse-service-account',
{
accountId: 'matrix-synapse-service-account',
name: 'matrix-synapse-service-account',
displayName: 'Matrix Service Account',
},
{ provider: mainProvider },
);

// Grant the service account the role of Storage Object Viewer on the bucket
new google.storage.v1.BucketIamBinding(
'bucket-iam-binding',
{
name: matrixDataBucket.name,
role: 'roles/storage.objectViewer',
members: [
matrixSynapseServiceAccount.email.apply(
email => `serviceAccount:${email}`,
),
],
},
{ provider: mainProvider },
);
28 changes: 28 additions & 0 deletions resources/kubernetes/matrix/k8s.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as k8s from '@pulumi/kubernetes';
import { provider } from '../provider';
import { matrixSynapseServiceAccount } from './google';

export const matrixK8sServiceAccountNamespace = new k8s.core.v1.Namespace(
'matrix-service-account-namespace',
{
metadata: {
name: 'service-account',
},
},
{ provider },
);

export const matrixK8sServiceAccount = new k8s.core.v1.ServiceAccount(
'matrix-service-account',
{
metadata: {
name: 'branches',
namespace: matrixK8sServiceAccountNamespace.metadata.name,
annotations: {
'iam.gke.io/gcp-service-account':
matrixSynapseServiceAccount.email.apply(email => email),
},
},
},
{ provider },
);
Loading