Skip to content

Commit

Permalink
Add module + example mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Natarov authored and Vitaliy Natarov committed Jan 12, 2025
1 parent 97592c3 commit 24464d0
Show file tree
Hide file tree
Showing 5 changed files with 502 additions and 6 deletions.
103 changes: 101 additions & 2 deletions oracle_cloud/examples/container_instances/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,103 @@
# Work with container_instances via terraform
# Work with CONTAINER_INSTANCES via terraform

A terraform module for making container_instances.
A terraform module for making CONTAINER_INSTANCES.


## Usage
----------------------
Import the module and retrieve with ```terraform get``` or ```terraform get --update```. Adding a module resource to your template, e.g. `main.tf`:

```
#
# MAINTAINER Vitaliy Natarov "vitaliy.natarov@yahoo.com"
#
terraform {
required_version = "~> 1.0"
required_providers {
oci = {
source = "oracle/oci"
version = "6.21.0"
}
}
}
provider "oci" {
fingerprint = var.provider_oci_fingerprint
private_key_path = var.provider_oci_private_key_path
region = var.provider_oci_region
tenancy_ocid = var.provider_oci_tenancy_ocid
user_ocid = var.provider_oci_user_ocid
alias = var.provider_oci_alias
}
module "container_instances" {
source = "../../modules/container_instances"
enable_container_instance = true
container_instance_availability_domain = ""
container_instance_compartment_id = ""
container_instance_shape = ""
container_instance_containers = [
{
}
]
container_instance_shape_config = {}
container_instance_vnics = []
container_instance_display_name = ""
tags = {}
}
```

## Module Input Variables
----------------------
- `name` - The name for resources (`default = test`)
- `environment` - The environment for resources (`default = dev`)
- `tags` - Add additional tags (`default = {}`)
- `enable_container_instance` - Enable container instance usages (`default = False`)
- `container_instance_availability_domain` - (Required) The availability domain where the container instance runs. (`default = null`)
- `container_instance_compartment_id` - (Required) (Updatable) The compartment OCID. (`default = null`)
- `container_instance_containers` - (Required) The containers to create on this container instance. (`default = []`)
- `container_instance_shape` - (Required) The shape of the container instance. The shape determines the resources available to the container instance. (`default = null`)
- `container_instance_shape_config` - (Required) The size and amount of resources available to the container instance. (`default = {}`)
- `container_instance_vnics` - (Required) The networks available to containers on this container instance. (`default = []`)
- `container_instance_container_restart_policy` - (Optional) Container restart policy (`default = null`)
- `container_instance_display_name` - (Optional) (Updatable) A user-friendly name. Does not have to be unique, and it's changeable. Avoid entering confidential information. If you don't provide a name, a name is generated automatically. (`default = ""`)
- `container_instance_fault_domain` - (Optional) The fault domain where the container instance runs. (`default = null`)
- `container_instance_graceful_shutdown_timeout_in_seconds` - (Optional) The amount of time that processes in a container have to gracefully end when the container must be stopped. For example, when you delete a container instance. After the timeout is reached, the processes are sent a signal to be deleted. (`default = null`)
- `container_instance_dns_config` - (Optional) Allow customers to define DNS settings for containers. If this is not provided, the containers use the default DNS settings of the subnet. (`default = {}`)
- `container_instance_image_pull_secrets` - (Optional) The image pulls secrets so you can access private registry to pull container images. (`default = []`)
- `container_instance_volumes` - (Optional) A volume is a directory with data that is accessible across multiple containers in a container instance. (`default = []`)
- `ccc_infrastructure_freeform_tags` - (Optional) Simple key-value pair that is applied without any predefined name, type or scope. Exists for cross-compatibility only. Example: {'bar-key': 'value'} (`default = {}`)
- `container_instance_timeouts` - The timeouts block allows you to specify timeouts for certain operations: * create - (Defaults to 20 minutes), when creating the Container Instance * update - (Defaults to 20 minutes), when updating the Container Instance * delete - (Defaults to 20 minutes), when destroying the Container Instance (`default = {}`)

## Module Output Variables
----------------------
- `container_instance_availability_domain` - The availability domain to place the container instance.
- `container_instance_compartment_id` - The OCID of the compartment.
- `container_instance_container_count` - The number of containers on the container instance.
- `container_instance_container_restart_policy` - The container restart policy is applied for all containers in container instance.
- `container_instance_containers` - The containers on the container instance.
- `container_instance_display_name` - A user-friendly name. Does not have to be unique, and it's changeable. Avoid entering confidential information.
- `container_instance_dns_config` - DNS settings for containers
- `container_instance_id` - An OCID that cannot be changed.
- `container_instance_image_pull_secrets` - The image pulls secrets so you can access private registry to pull container images.
- `container_instance_shape` - The shape of the container instance. The shape determines the number of OCPUs, amount of memory, and other resources that are allocated to a container instance.
- `container_instance_state` - The current state of the container instance.


## Authors

Created and maintained by [Vitaliy Natarov](https://github.com/SebastianUA). An email: [vitaliy.natarov@yahoo.com](vitaliy.natarov@yahoo.com).

## License

Apache 2 Licensed. See [LICENSE](https://github.com/SebastianUA/terraform/blob/master/LICENSE) for full details.
18 changes: 18 additions & 0 deletions oracle_cloud/examples/container_instances/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,22 @@ provider "oci" {
module "container_instances" {
source = "../../modules/container_instances"

enable_container_instance = true
container_instance_availability_domain = ""
container_instance_compartment_id = ""
container_instance_shape = ""

container_instance_containers = [
{

}
]

container_instance_shape_config = {}

container_instance_vnics = []

container_instance_display_name = ""

tags = {}
}
234 changes: 232 additions & 2 deletions oracle_cloud/modules/container_instances/container_instance.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,235 @@
#-----------------------------------------------------------
# container_instances_container_instance
# container instances container instance
#-----------------------------------------------------------
resource "oci_container_instances_container_instance" "container_instance" {
count = var.enable_container_instance ? 1 : 0

# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/container_instances_container_instance
# Required
availability_domain = var.container_instance_availability_domain
compartment_id = var.container_instance_compartment_id
shape = var.container_instance_shape
dynamic "containers" {
iterator = containers
for_each = var.container_instance_containers

content {
# Required
image_url = lookup(containers.value, "image_url", null)

# Optional
arguments = lookup(containers.value, "arguments", null)
command = lookup(containers.value, "command", null)
defined_tags = lookup(containers.value, "defined_tags", null)
display_name = lookup(containers.value, "display_name", null)
environment_variables = lookup(containers.value, "environment_variables", null)
freeform_tags = lookup(containers.value, "freeform_tags", null)

is_resource_principal_disabled = lookup(containers.value, "is_resource_principal_disabled", null)
working_directory = lookup(containers.value, "working_directory", null)

dynamic "health_checks" {
iterator = health_checks
for_each = lookup(containers.value, "health_checks", [])

content {
# Required
health_check_type = lookup(health_checks.value, "health_check_type", null)

# Optional
command = lookup(health_checks.value, "command", null)
failure_action = lookup(health_checks.value, "failure_action", null)
failure_threshold = lookup(health_checks.value, "failure_threshold", null)

dynamic "headers" {
iterator = headers
for_each = lookup(health_checks.value, "headers", [])

content {
# Optional
name = lookup(headers.value, "name", null)
value = lookup(headers.value, "value", null)
}
}

initial_delay_in_seconds = lookup(health_checks.value, "initial_delay_in_seconds", null)
interval_in_seconds = lookup(health_checks.value, "interval_in_seconds", null)
name = lookup(health_checks.value, "name", null)
path = lookup(health_checks.value, "path", null)
port = lookup(health_checks.value, "port", null)
success_threshold = lookup(health_checks.value, "success_threshold", null)
timeout_in_seconds = lookup(health_checks.value, "timeout_in_seconds", null)
}
}

dynamic "resource_config" {
iterator = resource_config
for_each = length(keys(lookup(containers.value, "resource_config", {}))) > 0 ? [lookup(containers.value, "resource_config", {})] : []

content {
# Optional
memory_limit_in_gbs = lookup(resource_config.value, "memory_limit_in_gbs", [])
vcpus_limit = lookup(resource_config.value, "vcpus_limit", [])
}
}

dynamic "security_context" {
iterator = security_context
for_each = lookup(containers.value, "security_context", [])

content {
# Optional
dynamic "capabilities" {
iterator = capabilities
for_each = lookup(security_context.value, "capabilities", [])

content {
# Optional
add_capabilities = lookup(capabilities.value, "add_capabilities", null)
drop_capabilities = lookup(capabilities.value, "drop_capabilities", null)
}
}
is_non_root_user_check_enabled = lookup(security_context.value, "is_non_root_user_check_enabled", null)
is_root_file_system_readonly = lookup(security_context.value, "is_root_file_system_readonly", null)
run_as_group = lookup(security_context.value, "run_as_group", null)
run_as_user = lookup(security_context.value, "run_as_user", null)
security_context_type = lookup(security_context.value, "security_context_type", null)
}
}
dynamic "volume_mounts" {
iterator = volume_mounts
for_each = lookup(containers.value, "volume_mounts", [])

content {
# Required
mount_path = lookup(volume_mounts.value, "mount_path", null)
volume_name = lookup(volume_mounts.value, "volume_name", null)

# Optional
is_read_only = lookup(volume_mounts.value, "is_read_only", null)
partition = lookup(volume_mounts.value, "partition", null)
sub_path = lookup(volume_mounts.value, "sub_path", null)
}
}

}
}

dynamic "shape_config" {
iterator = shape_config
for_each = length(keys(var.container_instance_shape_config)) > 0 ? [var.container_instance_shape_config] : []

content {
# Required
ocpus = lookup(shape_config.value, "ocpus", null)

# Optional
memory_in_gbs = lookup(shape_config.value, "memory_in_gbs", null)
}
}

dynamic "vnics" {
iterator = vnics
for_each = var.container_instance_vnics

content {
# Required
subnet_id = lookup(vnics.value, "subnet_id", null)

# Optional
defined_tags = lookup(vnics.value, "defined_tags", null)
display_name = lookup(vnics.value, "display_name", null)
freeform_tags = lookup(vnics.value, "freeform_tags", null)
hostname_label = lookup(vnics.value, "hostname_label", null)
is_public_ip_assigned = lookup(vnics.value, "is_public_ip_assigned", null)
nsg_ids = lookup(vnics.value, "nsg_ids", null)
private_ip = lookup(vnics.value, "private_ip", null)
skip_source_dest_check = lookup(vnics.value, "skip_source_dest_check", null)
}
}

# Optional
container_restart_policy = var.container_instance_container_restart_policy
display_name = var.container_instance_display_name != "" ? var.container_instance_display_name : "${lower(var.name)}-container-instance-${lower(var.environment)}"
fault_domain = var.container_instance_fault_domain
graceful_shutdown_timeout_in_seconds = var.container_instance_graceful_shutdown_timeout_in_seconds

dynamic "dns_config" {
iterator = dns_config
for_each = length(keys(var.container_instance_dns_config)) > 0 ? [var.container_instance_dns_config] : []

content {
# Optional
nameservers = lookup(dns_config.value, "nameservers", null)
options = lookup(dns_config.value, "options", null)
searches = lookup(dns_config.value, "searches", null)
}
}

dynamic "image_pull_secrets" {
iterator = image_pull_secrets
for_each = var.container_instance_image_pull_secrets

content {
# Required
registry_endpoint = lookup(image_pull_secrets.value, "registry_endpoint", null)
secret_type = lookup(image_pull_secrets.value, "secret_type", null)

# Optional
password = lookup(image_pull_secrets.value, "password", null)
secret_id = lookup(image_pull_secrets.value, "secret_id", null)
username = lookup(image_pull_secrets.value, "username", null)
}
}

dynamic "volumes" {
iterator = volumes
for_each = var.container_instance_volumes

content {
# Required
name = lookup(volumes.value, "name", null)
volume_type = lookup(volumes.value, "volume_type", null)

# Optional
backing_store = lookup(volumes.value, "backing_store", null)
dynamic "configs" {
iterator = configs
for_each = length(keys(lookup(volumes.value, "configs", {}))) > 0 ? [lookup(volumes.value, "configs", {})] : []

content {
# Optional
data = lookup(configs.value, "data", null)
file_name = lookup(configs.value, "file_name", null)
path = lookup(configs.value, "path", null)
}
}
}
}

defined_tags = merge(
{
Name = var.container_instance_display_name != "" ? var.container_instance_display_name : "${lower(var.name)}-container-instance-${lower(var.environment)}"
},
var.tags
)

freeform_tags = var.ccc_infrastructure_freeform_tags

dynamic "timeouts" {
iterator = timeouts
for_each = length(keys(var.container_instance_timeouts)) > 0 ? [var.container_instance_timeouts] : []

content {
create = lookup(timeouts.value, "create", null)
update = lookup(timeouts.value, "update", null)
delete = lookup(timeouts.value, "delete", null)
}
}

lifecycle {
create_before_destroy = true
ignore_changes = []
}

depends_on = []
}
Loading

0 comments on commit 24464d0

Please sign in to comment.