Skip to content

Commit 67c47fb

Browse files
authored
feat: add support for IKS and OCP clusters on classic infrastructure (#336)
1 parent 6ac686d commit 67c47fb

File tree

6 files changed

+90
-12
lines changed

6 files changed

+90
-12
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ provider "helm" {
5757
module "observability_agents" {
5858
source = "terraform-ibm-modules/observability-agents/ibm"
5959
version = "X.X.X" # Replace "X.X.X" with a release version to lock into a specific release
60+
is_vpc_cluster = true # Change to false if target cluster is running on classic infrastructure
6061
cluster_id = "cluster id" # update this with your cluster id where the agents will be installed
6162
cluster_resource_group_id = "resource group id" # update this with the Id of your IBM Cloud resource group
6263
log_analysis_ingestion_key = "XXXXXXXX"
@@ -124,6 +125,7 @@ No modules.
124125
|------|------|
125126
| [helm_release.cloud_monitoring_agent](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
126127
| [helm_release.log_analysis_agent](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
128+
| [ibm_container_cluster.cluster](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_cluster) | data source |
127129
| [ibm_container_cluster_config.cluster_config](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_cluster_config) | data source |
128130
| [ibm_container_vpc_cluster.cluster](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_vpc_cluster) | data source |
129131

@@ -145,6 +147,7 @@ No modules.
145147
| <a name="input_cluster_config_endpoint_type"></a> [cluster\_config\_endpoint\_type](#input\_cluster\_config\_endpoint\_type) | Specify which type of endpoint to use for for cluster config access: 'default', 'private', 'vpe', 'link'. 'default' value will use the default endpoint of the cluster. | `string` | `"default"` | no |
146148
| <a name="input_cluster_id"></a> [cluster\_id](#input\_cluster\_id) | The ID of the cluster you wish to deploy the agents in | `string` | n/a | yes |
147149
| <a name="input_cluster_resource_group_id"></a> [cluster\_resource\_group\_id](#input\_cluster\_resource\_group\_id) | The Resource Group ID of the cluster | `string` | n/a | yes |
150+
| <a name="input_is_vpc_cluster"></a> [is\_vpc\_cluster](#input\_is\_vpc\_cluster) | Specify true if the target cluster for the observability agents is a VPC cluster, false if it is a classic cluster. | `bool` | `true` | no |
148151
| <a name="input_log_analysis_add_cluster_name"></a> [log\_analysis\_add\_cluster\_name](#input\_log\_analysis\_add\_cluster\_name) | If true, configure the log analysis agent to attach a tag containing the cluster name to all log messages. | `bool` | `true` | no |
149152
| <a name="input_log_analysis_agent_custom_line_exclusion"></a> [log\_analysis\_agent\_custom\_line\_exclusion](#input\_log\_analysis\_agent\_custom\_line\_exclusion) | Log Analysis agent custom configuration for line exclusion setting LOGDNA\_K8S\_METADATA\_LINE\_EXCLUSION. See https://github.com/logdna/logdna-agent-v2/blob/master/docs/KUBERNETES.md#configuration-for-kubernetes-metadata-filtering for more info. | `string` | `null` | no |
150153
| <a name="input_log_analysis_agent_custom_line_inclusion"></a> [log\_analysis\_agent\_custom\_line\_inclusion](#input\_log\_analysis\_agent\_custom\_line\_inclusion) | Log Analysis agent custom configuration for line inclusion setting LOGDNA\_K8S\_METADATA\_LINE\_INCLUSION. See https://github.com/logdna/logdna-agent-v2/blob/master/docs/KUBERNETES.md#configuration-for-kubernetes-metadata-filtering for more info. | `string` | `null` | no |

examples/basic/main.tf

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,28 @@ module "observability_instances" {
3535
##############################################################################
3636

3737
resource "ibm_is_vpc" "example_vpc" {
38+
count = var.is_vpc_cluster ? 1 : 0
3839
name = "${var.prefix}-vpc"
3940
resource_group = module.resource_group.resource_group_id
4041
tags = var.resource_tags
4142
}
4243

4344
resource "ibm_is_public_gateway" "public_gateway" {
45+
count = var.is_vpc_cluster ? 1 : 0
4446
name = "${var.prefix}-gateway-1"
45-
vpc = ibm_is_vpc.example_vpc.id
47+
vpc = ibm_is_vpc.example_vpc[0].id
4648
resource_group = module.resource_group.resource_group_id
4749
zone = "${var.region}-1"
4850
}
4951

5052
resource "ibm_is_subnet" "testacc_subnet" {
53+
count = var.is_vpc_cluster ? 1 : 0
5154
name = "${var.prefix}-subnet"
52-
vpc = ibm_is_vpc.example_vpc.id
55+
vpc = ibm_is_vpc.example_vpc[0].id
5356
zone = "${var.region}-1"
5457
total_ipv4_address_count = 256
5558
resource_group = module.resource_group.resource_group_id
56-
public_gateway = ibm_is_public_gateway.public_gateway.id
59+
public_gateway = ibm_is_public_gateway.public_gateway[0].id
5760
}
5861

5962
resource "ibm_resource_instance" "cos_instance" {
@@ -72,9 +75,11 @@ locals {
7275
default_version = var.is_openshift ? "${data.ibm_container_cluster_versions.cluster_versions.default_openshift_version}_openshift" : data.ibm_container_cluster_versions.cluster_versions.default_kube_version
7376
}
7477

78+
# Create either a VPC or classic cluster, depending on the is_vpc_cluster variable
7579
resource "ibm_container_vpc_cluster" "cluster" {
80+
count = var.is_vpc_cluster ? 1 : 0
7681
name = var.prefix
77-
vpc_id = ibm_is_vpc.example_vpc.id
82+
vpc_id = ibm_is_vpc.example_vpc[0].id
7883
kube_version = local.default_version
7984
flavor = "bx2.4x16"
8085
worker_count = "2"
@@ -83,7 +88,7 @@ resource "ibm_container_vpc_cluster" "cluster" {
8388
force_delete_storage = true
8489
wait_till = "Normal"
8590
zones {
86-
subnet_id = ibm_is_subnet.testacc_subnet.id
91+
subnet_id = ibm_is_subnet.testacc_subnet[0].id
8792
name = "${var.region}-1"
8893
}
8994
resource_group_id = module.resource_group.resource_group_id
@@ -95,8 +100,43 @@ resource "ibm_container_vpc_cluster" "cluster" {
95100
}
96101
}
97102

103+
resource "ibm_container_cluster" "cluster" {
104+
#checkov:skip=CKV2_IBM_7:Public endpoint is required for testing purposes
105+
count = var.is_vpc_cluster ? 0 : 1
106+
name = var.prefix
107+
datacenter = var.datacenter
108+
default_pool_size = 2
109+
hardware = "shared"
110+
kube_version = local.default_version
111+
entitlement = var.is_openshift ? "cloud_pak" : null
112+
force_delete_storage = true
113+
machine_type = "b3c.4x16"
114+
public_vlan_id = ibm_network_vlan.public_vlan[0].id
115+
private_vlan_id = ibm_network_vlan.private_vlan[0].id
116+
wait_till = "Normal"
117+
resource_group_id = module.resource_group.resource_group_id
118+
tags = var.resource_tags
119+
120+
timeouts {
121+
delete = "2h"
122+
create = "3h"
123+
}
124+
}
125+
126+
resource "ibm_network_vlan" "public_vlan" {
127+
count = var.is_vpc_cluster ? 0 : 1
128+
datacenter = var.datacenter
129+
type = "PUBLIC"
130+
}
131+
132+
resource "ibm_network_vlan" "private_vlan" {
133+
count = var.is_vpc_cluster ? 0 : 1
134+
datacenter = var.datacenter
135+
type = "PRIVATE"
136+
}
137+
98138
data "ibm_container_cluster_config" "cluster_config" {
99-
cluster_name_id = ibm_container_vpc_cluster.cluster.id
139+
cluster_name_id = var.is_vpc_cluster ? ibm_container_vpc_cluster.cluster[0].id : ibm_container_cluster.cluster[0].id
100140
resource_group_id = module.resource_group.resource_group_id
101141
}
102142

@@ -114,7 +154,8 @@ resource "time_sleep" "wait_operators" {
114154
module "observability_agents" {
115155
source = "../.."
116156
depends_on = [time_sleep.wait_operators]
117-
cluster_id = ibm_container_vpc_cluster.cluster.id
157+
is_vpc_cluster = var.is_vpc_cluster
158+
cluster_id = var.is_vpc_cluster ? ibm_container_vpc_cluster.cluster[0].id : ibm_container_cluster.cluster[0].id
118159
cluster_resource_group_id = module.resource_group.resource_group_id
119160
log_analysis_instance_region = module.observability_instances.region
120161
log_analysis_ingestion_key = module.observability_instances.log_analysis_ingestion_key

examples/basic/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ variable "is_openshift" {
1010
default = true
1111
}
1212

13+
variable "is_vpc_cluster" {
14+
type = bool
15+
description = "Specify true if the target cluster for the observability agents is a VPC cluster, false if it is classic cluster."
16+
default = true
17+
}
18+
1319
variable "prefix" {
1420
type = string
1521
description = "Prefix for name of all resource created by this example"
@@ -33,3 +39,9 @@ variable "region" {
3339
description = "Region where resources are created"
3440
default = "ca-tor"
3541
}
42+
43+
variable "datacenter" {
44+
type = string
45+
description = "If creating a classic cluster, the data center where the cluster is created"
46+
default = "syd01"
47+
}

main.tf

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
# terraform-ibm-observability-agents
33
##############################################################################
44

5-
# Lookup cluster name from ID
5+
# Lookup cluster name from ID. The is_vpc_cluster variable defines whether to use the VPC data block or the Classic data block
66
data "ibm_container_vpc_cluster" "cluster" {
7+
count = var.is_vpc_cluster ? 1 : 0
8+
name = var.cluster_id
9+
resource_group_id = var.cluster_resource_group_id
10+
}
11+
12+
data "ibm_container_cluster" "cluster" {
13+
count = var.is_vpc_cluster ? 0 : 1
714
name = var.cluster_id
815
resource_group_id = var.cluster_resource_group_id
916
}
@@ -18,7 +25,7 @@ data "ibm_container_cluster_config" "cluster_config" {
1825

1926
locals {
2027
# LOCALS
21-
cluster_name = data.ibm_container_vpc_cluster.cluster.resource_name # Not publically documented in provider. See https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4485
28+
cluster_name = var.is_vpc_cluster ? data.ibm_container_vpc_cluster.cluster[0].resource_name : data.ibm_container_cluster.cluster[0].resource_name # Not publically documented in provider. See https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4485
2229
log_analysis_chart_location = "${path.module}/chart/logdna-agent"
2330
log_analysis_image_tag_digest = "3.10.0-20240620.7524d812f60db3d2@sha256:8d73adc74bbd398128aac67037e708e6286ebc4cfcabbfe4d118f7d2ceeb775b" # datasource: icr.io/ext/logdna-agent versioning=regex:^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)-(?<build>\d+)
2431
log_analysis_agent_registry = "icr.io/ext/logdna-agent"

tests/pr_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,19 @@ func TestRunUpgrade(t *testing.T) {
7777
func TestRunBasicAgentsKubernetes(t *testing.T) {
7878
t.Parallel()
7979

80-
var extTerraformVarsK8s = map[string]interface{}{}
81-
extTerraformVarsK8s["is_openshift"] = false
80+
options := setupOptions(t, "basic-obs-agents-k8s", terraformDirOther, extTerraformVars)
81+
options.TerraformVars["is_openshift"] = false
8282

83-
options := setupOptions(t, "basic-obs-agents-k8s", terraformDirOther, extTerraformVarsK8s)
83+
output, err := options.RunTestConsistency()
84+
assert.Nil(t, err, "This should not have errored")
85+
assert.NotNil(t, output, "Expected some output")
86+
}
87+
88+
func TestRunBasicAgentsClassic(t *testing.T) {
89+
t.Parallel()
90+
91+
options := setupOptions(t, "basic-obs-agents-classic", terraformDirOther, extTerraformVars)
92+
options.TerraformVars["is_vpc_cluster"] = false
8493

8594
output, err := options.RunTestConsistency()
8695
assert.Nil(t, err, "This should not have errored")

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ variable "cluster_config_endpoint_type" {
2323
}
2424
}
2525

26+
variable "is_vpc_cluster" {
27+
description = "Specify true if the target cluster for the observability agents is a VPC cluster, false if it is a classic cluster."
28+
type = bool
29+
default = true
30+
}
31+
2632
##############################################################################
2733
# Log Analysis variables
2834
##############################################################################

0 commit comments

Comments
 (0)