Commit 34b497bc by chenxx

add new ingress

1 parent 46bc1a76
...@@ -84,6 +84,26 @@ module "service" { ...@@ -84,6 +84,26 @@ module "service" {
] ]
} }
module "ingress" {
source = "../"
app_name = module.service.name
app_namespace = var.namespace
annotations = {
kubernetes.io/ingress.class : "nginx"
}
rule = [
{
domain = var.domain_name
external_port = var.ports[0] // Service port
path = var.path
path_type = var.path_type
}
]
}
data "kubernetes_service" "service" { data "kubernetes_service" "service" {
depends_on = [module.service] depends_on = [module.service]
......
No preview for this file type
# Add any directories, files, or patterns you don't want to be tracked by version control
**/credentials
.idea
.terraform
*.tfstate
*.tfstate.backup
*.tfstate.lock.info
\ No newline at end of file \ No newline at end of file
Terraform module for Kubernetes Ingress
==========================================
## Usage (more in example direcotry)
With default labels, backend finds pods with `app` label which equal to `app_name`
### Example with ClusterIP
```
module "ingress" {
source = "../"
app_name = module.service.name
app_namespace = var.namespace
annotations = {
"app" = "test"
}
rule = [
{
domain = local.url
external_port = var.ports_mapping.0.external_port // Service port
}
]
tls_hosts = [
{
secret_name = "cert-manager-ingress"
hosts = [example.com]
}
]
}
```
## Terraform Requirements
| Name | Version |
|------|---------|
| terraform | >= 0.12.26 |
| kubernetes | >= 2.0.0 |
## Inputs
See in example directory & variables.tf
## Outputs
| Name | Description |
|------|:-----------:|
| id | Kubernetes resource id |
| urls | URLs in ingress ||
\ No newline at end of file \ No newline at end of file
module "url_jenkins" {
source = "../"
domain_name = var.domain_name
service_name = var.service_name
service_namespace = var.service_namespace
rule = var.rule_ports
annotations = {
"kubernetes.io/ingress.class" = "nginx" // Require if used addional annontations. By default exist.
"nginx.ingress.kubernetes.io/whitelist-source-range" = "10.10.10.0/24, 30.0.0.10/25"
}
tls = [ // You can define few TLS. Kube will automatically choose correct certificate for url. If you want to use http traffit, please don't write tls option.
kubernetes_secret.wilcart-tls.metadata[0].name,
kubernetes_secret.domain-tls.metadata[0].name
]
}
\ No newline at end of file \ No newline at end of file
variable "rule_ports" {
default = [
{
sub_domain = "jenkins." //(Optional) ( subdomain must consist " . " in the end)
path = "/jenkins-dev" //(Optional) (Path in url)
internal_port = "8080" //(Required) Internal pod port
},
{
sub_domain = "jenkins." //(Optional) ( subdomain must consist " . " in the end)
path = "/jenkins-prod" //(Optional) (Path in url)
internal_port = "9090" //(Required) Internal pod port
},
{
domain = "another.com" //(Optional) (Redefine domain for this url) (Important! by default will use var.domain_name, if this field empty)
internal_port = "9090" //(Required) Internal pod port
},
]
}
variable "domain_name" {
default = "example.com"
}
variable "service_name" {
default = "jenkins"
}
variable "service_namespace" {
default = "ci-tools"
}
resource "kubernetes_ingress_v1" "ingress" {
count = var.ingress_v1_enable ? 1 : 0
metadata {
name = var.ingress_name != null ? var.ingress_name : var.service_name
namespace = var.service_namespace
annotations = var.annotations
}
spec {
ingress_class_name = var.ingress_class_name
dynamic "rule" {
for_each = var.rule
content {
host = "${lookup(rule.value, "sub_domain", "")}${lookup(rule.value, "domain", var.domain_name)}"
http {
path {
path = lookup(rule.value, "path", null)
path_type = lookup(rule.value, "path_type", var.path_type)
backend {
service {
name = lookup(rule.value, "service_name", var.service_name)
port {
number = rule.value.external_port
}
}
}
}
}
}
}
dynamic "tls" {
for_each = var.tls
content {
secret_name = tls.value
}
}
dynamic "tls" {
for_each = var.tls_hosts
content {
secret_name = tls.value.secret_name
hosts = tls.value.hosts
}
}
}
}
resource "kubernetes_ingress" "ingress" {
count = var.ingress_v1_enable ? 0 : 1
metadata {
name = var.ingress_name != null ? var.ingress_name : var.service_name
namespace = var.service_namespace
annotations = var.annotations
}
spec {
ingress_class_name = var.ingress_class_name
dynamic "rule" {
for_each = var.rule
content {
host = "${lookup(rule.value, "sub_domain", "")}${lookup(rule.value, "domain", var.domain_name)}"
http {
path {
path = lookup(rule.value, "path", null)
backend {
service_name = lookup(rule.value, "service_name", var.service_name)
service_port = rule.value.external_port
}
}
}
}
}
dynamic "tls" {
for_each = var.tls
content {
secret_name = tls.value
}
}
dynamic "tls" {
for_each = var.tls_hosts
content {
secret_name = tls.value.secret_name
hosts = tls.value.hosts
}
}
}
}
\ No newline at end of file \ No newline at end of file
output "id" {
value = var.ingress_v1_enable ? kubernetes_ingress_v1.ingress[0].id : kubernetes_ingress.ingress[0].id
}
output "urls" {
value = var.ingress_v1_enable ? kubernetes_ingress_v1.ingress[0].spec[*].rule[*].host : kubernetes_ingress.ingress[0].spec[*].rule[*].host
}
\ No newline at end of file \ No newline at end of file
variable "service_name" {
type = string
description = "(Required) Application name for attach to ingress. Service name."
}
variable "ingress_name" {
type = string
description = "Specific ingress name instead name as service"
default = null
}
variable "ingress_v1_enable" {
type = bool
description = "Enable ingress v1 tf resource type"
default = true
}
variable "service_namespace" {
type = string
description = "(Required) Namespace where located service"
}
variable "rule" {
description = "(Required) External Service port, ingress will redirect request to this service port. Also could add subdomain ( example: subdomain.domainname.com). And path for access ( example: domain.com/path ). And redefine domain."
}
variable "domain_name" {
type = string
description = "(Otional) Global domain name for all URL ( example: google.com)."
default = "example.com"
}
variable "tls" {
type = list(string)
description = "(Optional) Enable https traffic & and include SSL Certificate"
default = []
}
variable "annotations" {
description = "(Optional) Custom Annotations"
default = null
}
variable "ingress_class_name" {
description = "Ingress Class name"
type = string
default = "nginx"
}
variable "tls_hosts" {
description = "(Optional) Enable https traffic & and include SSL Certificate with hosts match"
default = []
}
variable "path_type" {
description = "Path type for ingress rule"
default = "ImplementationSpecific"
}
\ No newline at end of file \ No newline at end of file
...@@ -12,6 +12,7 @@ variable "ports" { ...@@ -12,6 +12,7 @@ variable "ports" {
default = [80] default = [80]
} }
# @label "Environment Variables" # @label "Environment Variables"
# @options ["dev", "test", "release", "beta"]
# @group "Basic" # @group "Basic"
variable "env" { variable "env" {
type = map(string) type = map(string)
...@@ -26,6 +27,7 @@ variable "replicas" { ...@@ -26,6 +27,7 @@ variable "replicas" {
default = 1 default = 1
} }
# @group "Resources" # @group "Resources"
# @options ["0.2","0.3", "0.5","1", "2"]
# @label "CPU Request" # @label "CPU Request"
variable "request_cpu" { variable "request_cpu" {
type = string type = string
...@@ -33,6 +35,7 @@ variable "request_cpu" { ...@@ -33,6 +35,7 @@ variable "request_cpu" {
default = "0.1" default = "0.1"
} }
# @group "Resources" # @group "Resources"
# @options ["128Mi","512Mi", "1G"]
# @label "Memory Request" # @label "Memory Request"
variable "request_memory" { variable "request_memory" {
type = string type = string
...@@ -40,25 +43,27 @@ variable "request_memory" { ...@@ -40,25 +43,27 @@ variable "request_memory" {
default = "128Mi" default = "128Mi"
} }
# @group "Resources" # @group "Resources"
# @options ["0.2","0.3", "0.5","1", "2"]
# @label "CPU Limit" # @label "CPU Limit"
variable "limit_cpu" { variable "limit_cpu" {
type = string type = string
description = "CPU limit. e.g. 0.5, 1, 2" description = "CPU limit. e.g. 0.5, 1, 2"
default = "" default = "0.5"
} }
# @group "Resources" # @group "Resources"
# @options ["128Mi","512Mi", "1G",2Gi]
# @label "Memory Limit" # @label "Memory Limit"
variable "limit_memory" { variable "limit_memory" {
type = string type = string
description = "Memory limit. e.g. 128Mi, 512Mi, 1Gi, 2Gi, 4Gi" description = "Memory limit. e.g. 128Mi, 512Mi, 1Gi, 2Gi, 4Gi"
default = "" default = "512Mi"
} }
# @label "Namespace" # @label "Namespace"
# @group "Advanced" # @group "Advanced"
variable "namespace" { variable "namespace" {
type = string type = string
description = "Namespace to deploy. Auto-generated if empty." description = "Namespace to deploy. Auto-generated if empty."
default = "" default = "default"
} }
# @label "Deployment Name" # @label "Deployment Name"
# @group "Advanced" # @group "Advanced"
...@@ -93,7 +98,7 @@ variable "probe_http_port" { ...@@ -93,7 +98,7 @@ variable "probe_http_port" {
# @group "Advanced" # @group "Advanced"
variable "app_log_path_volume_name" { variable "app_log_path_volume_name" {
type = string type = string
description = "名字只能使用-,例: abc-22 abc-cc" description = "应用日志挂载卷名,名字只能使用-,例: abc-22 abc-cc"
default = "app-log-path" default = "app-log-path"
} }
...@@ -120,3 +125,28 @@ variable "args" { ...@@ -120,3 +125,28 @@ variable "args" {
description = "(Optional) Arguments to the entrypoint" description = "(Optional) Arguments to the entrypoint"
default = ["release"] default = ["release"]
} }
# @label "域名名字"
# @group "ingress"
variable "domain_name" {
type = string
description = "域名"
default = ""
}
# @label "path_type"
#options ["ImplementationSpecific","Exact","Prefix"]
# @group "ingress"
variable "path_type" {
type = string
description = "Path type for ingress rule"
default = "ImplementationSpecific"
}
# @label "path"
# @group "ingress"
variable "path" {
type = string
description = "Path"
default = "/"
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!