Добавлен код и промты.
This commit is contained in:
34
terraform/infra/dev/env.tf
Normal file
34
terraform/infra/dev/env.tf
Normal file
@@ -0,0 +1,34 @@
|
||||
# This file handles environment variable loading from .env file
|
||||
|
||||
locals {
|
||||
# Parse .env file if it exists and make variables available
|
||||
env_file_exists = fileexists("${path.module}/.env")
|
||||
|
||||
# Read and parse .env file content
|
||||
env_file_content = local.env_file_exists ? file("${path.module}/.env") : ""
|
||||
|
||||
# Convert lines to key-value pairs
|
||||
env_vars = local.env_file_exists ? {
|
||||
for line in compact(split("\n", local.env_file_content)) :
|
||||
trimspace(split("=", line)[0]) => trimspace(join("=", slice(split("=", line), 1, length(split("=", line)))))
|
||||
if length(split("=", line)) > 1 && !startswith(trimspace(line), "#")
|
||||
} : {}
|
||||
|
||||
# Environment variables that can override Terraform variables
|
||||
server_ip_override = lookup(local.env_vars, "SERVER_IP", "")
|
||||
worker_ips_override = lookup(local.env_vars, "WORKER_IPS", "")
|
||||
ssh_user_override = lookup(local.env_vars, "SSH_USER", "")
|
||||
ssh_private_key_override = lookup(local.env_vars, "SSH_PRIVATE_KEY", "")
|
||||
domain_override = lookup(local.env_vars, "DOMAIN", "")
|
||||
k3s_version_override = lookup(local.env_vars, "K3S_VERSION", "")
|
||||
}
|
||||
|
||||
# Use environment variables if provided, otherwise use terraform.tfvars values
|
||||
locals {
|
||||
effective_server_ip = local.server_ip_override != "" ? local.server_ip_override : var.server_ip
|
||||
effective_worker_ips = local.worker_ips_override != "" ? split(",", local.worker_ips_override) : var.worker_ips
|
||||
effective_ssh_user = local.ssh_user_override != "" ? local.ssh_user_override : var.ssh_user
|
||||
effective_ssh_private_key = local.ssh_private_key_override != "" ? local.ssh_private_key_override : var.ssh_private_key
|
||||
effective_domain = local.domain_override != "" ? local.domain_override : var.domain
|
||||
effective_k3s_version = local.k3s_version_override != "" ? local.k3s_version_override : var.k3s_version
|
||||
}
|
||||
56
terraform/infra/dev/main.tf
Normal file
56
terraform/infra/dev/main.tf
Normal file
@@ -0,0 +1,56 @@
|
||||
locals {
|
||||
# Load environment variables if .env file exists
|
||||
env_vars = fileexists("${path.module}/.env") ? {
|
||||
for line in [
|
||||
for line in split("\n", file("${path.module}/.env"))
|
||||
: line if length(regexall("^[A-Za-z][A-Za-z0-9_]*=.*$", line)) > 0
|
||||
] : split("=", line)[0] => join("=", slice(split("=", line), 1, length(split("=", line))))
|
||||
} : {}
|
||||
}
|
||||
|
||||
module "k3s_cluster" {
|
||||
source = "../../modules/k3s-install"
|
||||
|
||||
# Required parameters
|
||||
server_ip = var.server_ip
|
||||
worker_ips = var.worker_ips
|
||||
ssh_user = var.ssh_user
|
||||
ssh_private_key = var.ssh_private_key
|
||||
replace_url = var.replace_url != "" ? var.replace_url : var.server_ip
|
||||
|
||||
# Optional parameters with defaults
|
||||
k3s_version = var.k3s_version
|
||||
k3s_channel = var.k3s_channel
|
||||
kubeconfig_path = var.kubeconfig_path
|
||||
node_token_path = var.node_token_path
|
||||
|
||||
# Conditional extra arguments
|
||||
k3s_extra_server_args = join(" ", compact([
|
||||
var.enable_traefik_dashboard ? "--set traefik.dashboard.enabled=true" : "",
|
||||
var.enable_ssl ? "--tls-san=${var.domain}" : "",
|
||||
var.k3s_extra_server_args
|
||||
]))
|
||||
|
||||
k3s_extra_agent_args = var.k3s_extra_agent_args
|
||||
}
|
||||
|
||||
# Output information about the deployed cluster
|
||||
output "kubeconfig" {
|
||||
description = "Path to the kubeconfig file"
|
||||
value = module.k3s_cluster.kubeconfig_path
|
||||
}
|
||||
|
||||
output "server_ip" {
|
||||
description = "IP address of the K3s server"
|
||||
value = module.k3s_cluster.server_ip
|
||||
}
|
||||
|
||||
output "worker_ips" {
|
||||
description = "IP addresses of K3s workers"
|
||||
value = module.k3s_cluster.worker_ips
|
||||
}
|
||||
|
||||
output "cluster_ready" {
|
||||
description = "Indicator that the cluster is ready"
|
||||
value = module.k3s_cluster.cluster_ready
|
||||
}
|
||||
17
terraform/infra/dev/providers.tf
Normal file
17
terraform/infra/dev/providers.tf
Normal file
@@ -0,0 +1,17 @@
|
||||
terraform {
|
||||
required_version = ">= 1.0.0"
|
||||
required_providers {
|
||||
null = {
|
||||
source = "hashicorp/null"
|
||||
version = "~> 3.2.0"
|
||||
}
|
||||
local = {
|
||||
source = "hashicorp/local"
|
||||
version = "~> 2.4.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Provider configuration
|
||||
provider "null" {}
|
||||
provider "local" {}
|
||||
28
terraform/infra/dev/terraform.tfvars
Normal file
28
terraform/infra/dev/terraform.tfvars
Normal file
@@ -0,0 +1,28 @@
|
||||
# K3s Server Configuration
|
||||
server_ip = "192.168.1.100" # Replace with your server IP
|
||||
worker_ips = [ # Replace with your worker IPs
|
||||
"192.168.1.101",
|
||||
"192.168.1.102"
|
||||
]
|
||||
ssh_user = "ubuntu" # Replace with your SSH username
|
||||
ssh_private_key = "~/.ssh/id_rsa" # Path to your private SSH key
|
||||
replace_url = "" # Optional: URL to replace in kubeconfig, leave empty to use server_ip
|
||||
|
||||
# Cluster Configuration
|
||||
domain = "reg.benadis.org" # Your domain name
|
||||
k3s_version = "v1.27.3+k3s1" # K3s version to install
|
||||
k3s_channel = "stable" # K3s release channel
|
||||
kubeconfig_path = "~/.kube/config" # Where to save kubeconfig
|
||||
node_token_path = "/tmp/node-token" # Where to save node token
|
||||
|
||||
# Optional Features
|
||||
enable_traefik_dashboard = false # Enable Traefik dashboard
|
||||
enable_ssl = false # Enable SSL
|
||||
ssl_cert_path = "" # Path to SSL certificate
|
||||
ssl_key_path = "" # Path to SSL key
|
||||
install_argocd = false # Install ArgoCD
|
||||
enable_ssh_tunnel = false # Enable SSH tunneling
|
||||
|
||||
# Advanced Configuration
|
||||
k3s_extra_server_args = "" # Extra args for K3s server
|
||||
k3s_extra_agent_args = "" # Extra args for K3s agent
|
||||
105
terraform/infra/dev/variables.tf
Normal file
105
terraform/infra/dev/variables.tf
Normal file
@@ -0,0 +1,105 @@
|
||||
variable "server_ip" {
|
||||
description = "IP address of the K3s server node"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "worker_ips" {
|
||||
description = "List of IP addresses for K3s worker nodes"
|
||||
type = list(string)
|
||||
default = []
|
||||
}
|
||||
|
||||
variable "ssh_user" {
|
||||
description = "SSH username for connecting to nodes"
|
||||
type = string
|
||||
default = "root"
|
||||
}
|
||||
|
||||
variable "ssh_private_key" {
|
||||
description = "Path to the SSH private key for authentication"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "replace_url" {
|
||||
description = "URL to replace in the kubeconfig (usually the server_ip or domain name)"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "domain" {
|
||||
description = "Domain name for the cluster"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "k3s_version" {
|
||||
description = "Version of K3s to install"
|
||||
type = string
|
||||
default = "v1.27.3+k3s1"
|
||||
}
|
||||
|
||||
variable "k3s_channel" {
|
||||
description = "Release channel of K3s to install (stable, latest, etc.)"
|
||||
type = string
|
||||
default = "stable"
|
||||
}
|
||||
|
||||
variable "kubeconfig_path" {
|
||||
description = "Path where to save the kubeconfig file locally"
|
||||
type = string
|
||||
default = "~/.kube/config"
|
||||
}
|
||||
|
||||
variable "node_token_path" {
|
||||
description = "Path where to save the node token locally"
|
||||
type = string
|
||||
default = "/tmp/node-token"
|
||||
}
|
||||
|
||||
variable "enable_traefik_dashboard" {
|
||||
description = "Whether to enable the Traefik dashboard"
|
||||
type = bool
|
||||
default = false
|
||||
}
|
||||
|
||||
variable "enable_ssl" {
|
||||
description = "Whether to configure SSL for the cluster"
|
||||
type = bool
|
||||
default = false
|
||||
}
|
||||
|
||||
variable "ssl_cert_path" {
|
||||
description = "Path to SSL certificate"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "ssl_key_path" {
|
||||
description = "Path to SSL key"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "install_argocd" {
|
||||
description = "Whether to install ArgoCD for GitOps"
|
||||
type = bool
|
||||
default = false
|
||||
}
|
||||
|
||||
variable "enable_ssh_tunnel" {
|
||||
description = "Whether to enable SSH tunneling for local access"
|
||||
type = bool
|
||||
default = false
|
||||
}
|
||||
|
||||
variable "k3s_extra_server_args" {
|
||||
description = "Additional arguments to pass to the K3s server installation"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "k3s_extra_agent_args" {
|
||||
description = "Additional arguments to pass to the K3s agent installation"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
Reference in New Issue
Block a user