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

Add mongodb res pack #16

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Changes from 1 commit
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
Next Next commit
Add mongodb res pack
mathieu-benoit committed Sep 17, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 975fa950ee2253517a0f0793f88a1c2ce8ec9067
79 changes: 79 additions & 0 deletions examples/mongodb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Example: mongodb resource using a Kubernetes Deployment

This example configures a [mongodb](https://developer.humanitec.com/platform-orchestrator/reference/resource-types/#mongodb) Resource Definition using Kubernetes `Deployment`.

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| terraform | >= 1.3.0 |
| humanitec | ~> 1.0 |

## Providers

| Name | Version |
|------|---------|
| humanitec | ~> 1.0 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| mongodb\_basic | ../../humanitec-resource-defs/mongodb/basic | n/a |

## Resources

| Name | Type |
|------|------|
| [humanitec_application.example](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/application) | resource |
| [humanitec_resource_definition_criteria.mongodb_basic](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition_criteria) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| name | Name of the example application | `string` | `"hum-rp-mongodb-example"` | no |
| prefix | Prefix of the created resources | `string` | `"hum-rp-mongodb-ex-"` | no |
<!-- END_TF_DOCS -->

## Deploy and use this example

To deploy this resource definition, run these commands below:
```bash
git clone https://github.com/humanitec-architecture/resource-packs-in-cluster

cd examples/mongodb/

humctl login
humctl config set org YOUR-ORG

terraform init
terraform plan
terraform apply
```

The created Resource Definition can be used in your Score file like illustrated below:
```yaml
apiVersion: score.dev/v1b1
metadata:
name: my-workload
containers:
my-container:
image: nginx:latest # this container image is just used as an example, it's not talking to mongodb.
variables:
MONGODB_CONNECTION_STRING: "${resources.my-mongodb.connection}"
resources:
my-mongodb:
type: mongodb
```
This Score file when deployed to Humanitec will provision the `mongodb` database and inject the outputs in the associated environment variable.

Here is how to deploy this Score file, for example to the `hum-rp-mongodb-example` Application and `development` Environment:
```bash
humctl score deploy \
-f score.yaml \
--app hum-rp-mongodb-example \
--env development
```
19 changes: 19 additions & 0 deletions examples/mongodb/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
resource "humanitec_application" "example" {
id = var.name
name = var.name
}

# mysql resource definition

module "mongodb_basic" {
source = "../../humanitec-resource-defs/mongodb/basic"

prefix = var.prefix
}

resource "humanitec_resource_definition_criteria" "mongodb_basic" {
resource_definition_id = module.mongodb_basic.id
app_id = humanitec_application.example.id
class = "default"
force_delete = true
}
12 changes: 12 additions & 0 deletions examples/mongodb/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
humanitec = {
source = "humanitec/humanitec"
version = "~> 1.0"
}
}
required_version = ">= 1.3.0"
}


provider "humanitec" {}
6 changes: 6 additions & 0 deletions examples/mongodb/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# Name of the example application
name = "hum-rp-mongodb-example"

# Prefix of the created resources
prefix = "hum-rp-mongodb-ex-"
11 changes: 11 additions & 0 deletions examples/mongodb/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "name" {
description = "Name of the example application"
type = string
default = "hum-rp-mongodb-example"
}

variable "prefix" {
description = "Prefix of the created resources"
type = string
default = "hum-rp-mongodb-ex-"
}
33 changes: 33 additions & 0 deletions humanitec-resource-defs/mongodb/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| terraform | >= 1.3.0 |
| humanitec | ~> 1.0 |

## Providers

| Name | Version |
|------|---------|
| humanitec | ~> 1.0 |

## Resources

| Name | Type |
|------|------|
| [humanitec_resource_definition.main](https://registry.terraform.io/providers/humanitec/humanitec/latest/docs/resources/resource_definition) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| prefix | Prefix for all resources | `string` | n/a | yes |
| name | Name of the deployment | `string` | `"mongo-{{ \"${context.res.id}\" | replace \".\" \"-\" | substr 0 41 }}\n"` | no |

## Outputs

| Name | Description |
|------|-------------|
| id | n/a |
<!-- END_TF_DOCS -->
112 changes: 112 additions & 0 deletions humanitec-resource-defs/mongodb/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
resource "humanitec_resource_definition" "main" {
id = "${var.prefix}mongodb"
name = "${var.prefix}mongodb"
type = "mongodb"
driver_type = "humanitec/template"

driver_inputs = {
values_string = jsonencode({
templates = {
cookie = <<EOL
rootUsername: {{ .init.rootUsername }}
name: {{ .init.name }}
rootPassword: {{ .init.rootPassword }}
port: {{ .init.port }}
EOL
init = <<EOL
{{- if .cookie}}
rootUsername: {{ .cookie.rootUsername }}
name: {{ .cookie.name }}
rootPassword: {{ .cookie.rootPassword }}
port: {{ .cookie.port }}
{{- else }}
rootUsername: {{ randAlpha 8 | lower | quote }}
rootPassword: {{ randAlphaNum 16 | quote }}
name: db_{{ randAlpha 10}}
port: 27017
{{- end }}
id: mongodb-{{ .id }}
EOL
manifests = <<EOL
deployment.yaml:
location: namespace
data:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .init.id }}
spec:
selector:
matchLabels:
app: {{ .init.id }}
template:
metadata:
labels:
app: {{ .init.id }}
spec:
containers:
- name: mongodb
image: mongo:7
ports:
- containerPort: {{ .init.port }}
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: {{ .init.rootUsername }}
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .init.id }}
key: MONGO_INITDB_ROOT_PASSWORD
volumeMounts:
- name: data
mountPath: /data/db
volumes:
- name: data
persistentVolumeClaim:
claimName: pvc-{{ .init.id }}
pvc.yaml:
location: namespace
data:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-{{ .init.id }}
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10G
service.yaml:
location: namespace
data:
apiVersion: v1
kind: Service
metadata:
name: {{ .init.id }}
spec:
type: ClusterIP
selector:
app: {{ .init.id }}
ports:
- name: tcp-mongodb
port: {{ .init.port }}
targetPort: {{ .init.port }}
secret.yaml:
location: namespace
data:
apiVersion: v1
kind: Secret
metadata:
name: {{ .init.id }}
type: Opaque
data:
MONGO_INITDB_ROOT_PASSWORD: {{ .init.rootPassword | b64enc }}
EOL
secrets = <<EOL
connection: mongodb://{{ .init.rootUsername }}:{{ .init.rootPassword }}@{{ .init.id }}:{{ .init.port }}/
EOL
}
})
}
}
3 changes: 3 additions & 0 deletions humanitec-resource-defs/mongodb/basic/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "id" {
value = humanitec_resource_definition.main.id
}
9 changes: 9 additions & 0 deletions humanitec-resource-defs/mongodb/basic/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
humanitec = {
source = "humanitec/humanitec"
version = "~> 1.0"
}
}
required_version = ">= 1.3.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# Name of the deployment
name = "mongo-{{ \"${context.res.id}\" | replace \".\" \"-\" | substr 0 41 }}\n"

# Prefix for all resources
prefix = ""
15 changes: 15 additions & 0 deletions humanitec-resource-defs/mongodb/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "prefix" {
description = "Prefix for all resources"
type = string
}

variable "name" {
description = "Name of the deployment"
type = string

# Deployment names shouldn't be > 47 https://pauldally.medium.com/why-you-try-to-keep-your-deployment-names-to-47-characters-or-less-1f93a848d34c
# 47 - 6 (prefix) = 41
default = <<EOT
mongo-{{ "$${context.res.id}" | replace "." "-" | substr 0 41 }}
EOT
}