|
| 1 | +locals { |
| 2 | + role_name = "castai-eks-role" |
| 3 | + |
| 4 | + default_node_cfg = { |
| 5 | + default = { |
| 6 | + subnets = var.subnets |
| 7 | + tags = var.tags |
| 8 | + security_groups = var.security_groups |
| 9 | + instance_profile_arn = module.castai-eks-role-iam.instance_profile_arn |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + default_node_tmpl = { |
| 14 | + default_by_castai = { |
| 15 | + name = "default-by-castai" |
| 16 | + configuration_id = module.castai-eks-cluster.castai_node_configurations[ |
| 17 | + "default" |
| 18 | + ] |
| 19 | + is_default = true |
| 20 | + is_enabled = true |
| 21 | + should_taint = false |
| 22 | + |
| 23 | + constraints = { |
| 24 | + on_demand = true |
| 25 | + spot = true |
| 26 | + use_spot_fallbacks = true |
| 27 | + |
| 28 | + enable_spot_diversity = false |
| 29 | + spot_diversity_price_increase_limit_percent = 20 |
| 30 | + |
| 31 | + spot_interruption_predictions_enabled = true |
| 32 | + spot_interruption_predictions_type = "aws-rebalance-recommendations" |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + node_configuration = merge(local.default_node_cfg, { |
| 38 | + live = { |
| 39 | + subnets = var.subnets, |
| 40 | + instance_profile_arn = module.castai-eks-role-iam.instance_profile_arn |
| 41 | + security_groups = var.security_groups |
| 42 | + init_script = base64encode(templatefile("${path.module}/eks-init-script.sh", { |
| 43 | + live_proxy_version = trimspace(var.live_proxy_version) |
| 44 | + })) |
| 45 | + container_runtime = "containerd" |
| 46 | + eks_image_family = "al2023" |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + node_templates = merge(local.default_node_tmpl, { |
| 51 | + live_tmpl = { |
| 52 | + configuration_id = module.castai-eks-cluster.castai_node_configurations["live"] |
| 53 | + is_enabled = true |
| 54 | + should_taint = true |
| 55 | + } |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +# Configure Data sources and providers required for CAST AI connection. |
| 60 | +data "aws_caller_identity" "current" {} |
| 61 | + |
| 62 | +resource "castai_eks_user_arn" "castai_user_arn" { |
| 63 | + cluster_id = castai_eks_clusterid.cluster_id.id |
| 64 | +} |
| 65 | + |
| 66 | +# Create AWS IAM policies and a user to connect to CAST AI. |
| 67 | +module "castai-eks-role-iam" { |
| 68 | + source = "castai/eks-role-iam/castai" |
| 69 | + |
| 70 | + aws_account_id = data.aws_caller_identity.current.account_id |
| 71 | + aws_cluster_region = var.cluster_region |
| 72 | + aws_cluster_name = var.cluster_name |
| 73 | + aws_cluster_vpc_id = var.vpc_id |
| 74 | + |
| 75 | + castai_user_arn = castai_eks_user_arn.castai_user_arn.arn |
| 76 | + |
| 77 | + create_iam_resources_per_cluster = true |
| 78 | +} |
| 79 | + |
| 80 | +# Configure EKS cluster connection using CAST AI eks-cluster module. |
| 81 | +resource "castai_eks_clusterid" "cluster_id" { |
| 82 | + account_id = data.aws_caller_identity.current.account_id |
| 83 | + region = var.cluster_region |
| 84 | + cluster_name = var.cluster_name |
| 85 | +} |
| 86 | + |
| 87 | +module "castai-eks-cluster" { |
| 88 | + source = "castai/eks-cluster/castai" |
| 89 | + |
| 90 | + api_url = var.castai_api_url |
| 91 | + castai_api_token = var.castai_api_token |
| 92 | + grpc_url = var.castai_grpc_url |
| 93 | + wait_for_cluster_ready = true |
| 94 | + |
| 95 | + aws_account_id = data.aws_caller_identity.current.account_id |
| 96 | + aws_cluster_region = var.cluster_region |
| 97 | + aws_cluster_name = var.cluster_name |
| 98 | + |
| 99 | + aws_assume_role_arn = module.castai-eks-role-iam.role_arn |
| 100 | + delete_nodes_on_disconnect = var.delete_nodes_on_disconnect |
| 101 | + |
| 102 | + default_node_configuration = module.castai-eks-cluster.castai_node_configurations["default"] |
| 103 | + |
| 104 | + node_configurations = local.node_configuration |
| 105 | + |
| 106 | + node_templates = local.node_templates |
| 107 | + |
| 108 | + autoscaler_settings = { |
| 109 | + enabled = true |
| 110 | + is_scoped_mode = false |
| 111 | + node_templates_partial_matching_enabled = false |
| 112 | + |
| 113 | + unschedulable_pods = { |
| 114 | + enabled = true |
| 115 | + } |
| 116 | + |
| 117 | + node_downscaler = { |
| 118 | + enabled = true |
| 119 | + |
| 120 | + empty_nodes = { |
| 121 | + enabled = true |
| 122 | + } |
| 123 | + |
| 124 | + evictor = { |
| 125 | + aggressive_mode = false |
| 126 | + cycle_interval = "5m10s" |
| 127 | + dry_run = false |
| 128 | + enabled = true |
| 129 | + node_grace_period_minutes = 10 |
| 130 | + scoped_mode = false |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + cluster_limits = { |
| 135 | + enabled = true |
| 136 | + |
| 137 | + cpu = { |
| 138 | + max_cores = 20 |
| 139 | + min_cores = 1 |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + # depends_on helps Terraform with creating proper dependencies graph in case of resource creation and in this case destroy. |
| 145 | + # module "castai-eks-cluster" has to be destroyed before module "castai-eks-role-iam". |
| 146 | + depends_on = [module.castai-eks-role-iam] |
| 147 | +} |
| 148 | + |
| 149 | +resource "helm_release" "live-helm" { |
| 150 | + name = "castai-live" |
| 151 | + |
| 152 | + repository = "https://castai.github.io/helm-charts" |
| 153 | + chart = "castai-live" |
| 154 | + version = var.live_helm_version |
| 155 | + |
| 156 | + namespace = "castai-live" |
| 157 | + create_namespace = true |
| 158 | + dependency_update = true |
| 159 | + |
| 160 | + set { |
| 161 | + name = "castai-aws-vpc-cni.enabled" |
| 162 | + value = "true" |
| 163 | + } |
| 164 | + |
| 165 | + set { |
| 166 | + name = "castai.clusterID" |
| 167 | + value = castai_eks_clusterid.cluster_id.id |
| 168 | + } |
| 169 | + |
| 170 | + set { |
| 171 | + name = "castai.apiKey" |
| 172 | + value = var.castai_api_token |
| 173 | + } |
| 174 | + |
| 175 | + wait = false |
| 176 | + |
| 177 | + depends_on = [module.castai-eks-cluster] |
| 178 | +} |
0 commit comments