######################################################## # Versions ######################################################## terraform { required_version = ">= 1.0" required_providers { helm = { source = "hashicorp/helm" version = "~> 2.17" } kubernetes = { source = "hashicorp/kubernetes" version = ">= 2.25" } } } ######################################################## # Variables ######################################################## variable "scaleops_token" { description = "ScaleOps token for authentication to the ScaleOps registry" type = string sensitive = true validation { condition = length(var.scaleops_token) > 0 error_message = "ScaleOps token must not be empty." } } variable "cluster_name" { description = "Name of the Kubernetes cluster where ScaleOps will be installed" type = string validation { condition = length(var.cluster_name) > 0 error_message = "Cluster name must not be empty." } } variable "chart_version" { description = "Version of the ScaleOps Helm chart to install, or 'latest'" type = string default = "latest" validation { condition = var.chart_version == "latest" || can(regex("^[0-9]+\\.[0-9]+\\.[0-9]+", var.chart_version)) error_message = "Chart version must be 'latest' or follow semantic versioning (e.g., '1.2.3')." } } variable "scaleops_namespace" { description = "Kubernetes namespace where ScaleOps will be installed" type = string default = "scaleops-system" } variable "helm_repository" { description = "ScaleOps Helm chart repository URL" type = string default = "https://registry.scaleops.com/charts" } variable "image_registry" { description = "Container image registry for ScaleOps images" type = string default = "registry.scaleops.com" } variable "extra_values" { description = "Additional Helm values as YAML documents, applied on top of the module defaults" type = list(string) default = [] } ######################################################## # Locals ######################################################## locals { # The Helm providers treat a null version as "latest available" chart_version = var.chart_version == "latest" ? null : var.chart_version helm_values = { global = { image = { registry = var.image_registry } } } } ######################################################## # Namespace ######################################################## resource "kubernetes_namespace_v1" "scaleops_system" { metadata { name = var.scaleops_namespace } } ######################################################## # CRDs ######################################################## # Render the chart CRD templates without installing the release data "helm_template" "scaleops_crds" { name = "scaleops" namespace = var.scaleops_namespace repository = var.helm_repository repository_username = "scaleops" repository_password = var.scaleops_token chart = "scaleops" version = local.chart_version include_crds = true } locals { scaleops_crds_by_name = { for crd in data.helm_template.scaleops_crds.crds : yamldecode(crd).metadata.name => crd } # The chart version resolved by the CRD template, taken from the standard # helm.sh/chart label, so the release installs the exact same version resolved_chart_version = try( distinct(flatten([ for manifest in values(data.helm_template.scaleops_crds.manifests) : regexall("helm.sh/chart: scaleops-([0-9][\\w.+-]*)", manifest) ]))[0], local.chart_version ) } resource "kubernetes_manifest" "scaleops_crds" { for_each = local.scaleops_crds_by_name manifest = yamldecode(each.value) wait { condition { type = "Established" status = "True" } } } ######################################################## # ScaleOps Release ######################################################## resource "helm_release" "scaleops" { depends_on = [kubernetes_manifest.scaleops_crds] name = "scaleops" namespace = kubernetes_namespace_v1.scaleops_system.metadata[0].name repository = var.helm_repository repository_username = "scaleops" repository_password = var.scaleops_token chart = "scaleops" version = local.resolved_chart_version skip_crds = true max_history = 10 timeout = 600 set_sensitive { name = "scaleopsToken" value = var.scaleops_token } set { name = "clusterName" value = var.cluster_name } values = concat([yamlencode(local.helm_values)], var.extra_values) } ######################################################## # Outputs ######################################################## output "namespace" { description = "Namespace where ScaleOps was installed" value = kubernetes_namespace_v1.scaleops_system.metadata[0].name } output "chart_version" { description = "Installed ScaleOps chart version" value = helm_release.scaleops.version } # Usage example (providers are configured by the caller): # # provider "helm" { # kubernetes { # config_path = "~/.kube/config" # } # } # # provider "kubernetes" { # config_path = "~/.kube/config" # } # # module "scaleops" { # source = "https://scriptshelf.scaleops.com/install/integrate.tf.tar.gz" # # scaleops_token = "SCALEOPS_TOKEN_FROM_DASHBOARD" # cluster_name = "my-cluster" # }