Skip to content

Commit 65f0cd2

Browse files
piyush-tiwariravinitp
authored andcommitted
Added - override_existing flag for container_engine.addon resource
1 parent 890edb5 commit 65f0cd2

File tree

5 files changed

+123
-139
lines changed

5 files changed

+123
-139
lines changed

examples/container_engine/addons/main.tf

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ provider "oci" {
2020
}
2121

2222
/*
23-
A complete example to setup a cluster, then configure add-ons, then create node pool.
23+
A complete example to setup a cluster, then configure add-ons.
2424
*/
2525
data "oci_identity_availability_domain" "ad1" {
2626
compartment_id = var.tenancy_ocid
@@ -51,17 +51,16 @@ resource "oci_core_route_table" "test_route_table" {
5151
}
5252
}
5353

54-
resource "oci_core_subnet" "nodePool_Subnet_1" {
55-
#Required
56-
availability_domain = data.oci_identity_availability_domain.ad1.name
57-
cidr_block = "10.0.22.0/24"
58-
compartment_id = var.compartment_ocid
59-
vcn_id = oci_core_vcn.test_vcn.id
60-
61-
# Provider code tries to maintain compatibility with old versions.
62-
security_list_ids = [oci_core_vcn.test_vcn.default_security_list_id]
63-
display_name = "tfSubNet1ForNodePool"
64-
route_table_id = oci_core_route_table.test_route_table.id
54+
resource "oci_core_subnet" "api_endpoint_subnet" {
55+
#Required
56+
cidr_block = "10.0.23.0/24"
57+
compartment_id = var.compartment_ocid
58+
vcn_id = oci_core_vcn.test_vcn.id
59+
60+
# Provider code tries to maintain compatibility with old versions.
61+
security_list_ids = [oci_core_vcn.test_vcn.default_security_list_id]
62+
display_name = "apiEndpointSubnet"
63+
route_table_id = oci_core_route_table.test_route_table.id
6564
}
6665

6766
resource "oci_containerengine_cluster" "test_cluster" {
@@ -71,6 +70,9 @@ resource "oci_containerengine_cluster" "test_cluster" {
7170
name = "tfTestCluster"
7271
vcn_id = oci_core_vcn.test_vcn.id
7372
type = "ENHANCED_CLUSTER"
73+
endpoint_config {
74+
subnet_id = oci_core_subnet.api_endpoint_subnet.id
75+
}
7476
}
7577

7678
resource "oci_containerengine_addon" "dashboard" {
@@ -80,6 +82,11 @@ resource "oci_containerengine_addon" "dashboard" {
8082
cluster_id = oci_containerengine_cluster.test_cluster.id
8183
#Required, remove the resource on addon deletion
8284
remove_addon_resources_on_delete = true
85+
86+
#Optional, will override an existing installation if true and Addon already exists
87+
override_existing = false
88+
89+
#Optional
8390
dynamic configurations {
8491
for_each = local.addon_mappings
8592

@@ -90,60 +97,11 @@ resource "oci_containerengine_addon" "dashboard" {
9097
}
9198
}
9299

93-
resource "oci_containerengine_node_pool" "test_node_pool" {
94-
#Required
95-
cluster_id = oci_containerengine_cluster.test_cluster.id
96-
compartment_id = var.compartment_ocid
97-
kubernetes_version = reverse(data.oci_containerengine_cluster_option.test_cluster_option.kubernetes_versions)[0]
98-
name = "tfPool"
99-
node_shape = "VM.Standard2.1"
100-
101-
node_config_details {
102-
size = 1
103-
placement_configs {
104-
availability_domain = data.oci_identity_availability_domain.ad1.name
105-
subnet_id = oci_core_subnet.nodePool_Subnet_1.id
106-
}
107-
}
108-
109-
node_source_details {
110-
#Required
111-
image_id = local.image_id
112-
source_type = "IMAGE"
113-
114-
#Optional
115-
boot_volume_size_in_gbs = "60"
116-
}
117-
118-
//use terraform depends_on to enforce cluster->add-on->node pool DAG
119-
depends_on = [oci_containerengine_addon.dashboard]
120-
}
121-
122100
data "oci_containerengine_cluster_option" "test_cluster_option" {
123101
cluster_option_id = "all"
124102
}
125103

126-
data "oci_containerengine_node_pool_option" "test_node_pool_option" {
127-
node_pool_option_id = "all"
128-
compartment_id = var.compartment_ocid
129-
}
130-
131-
data "oci_core_images" "shape_specific_images" {
132-
#Required
133-
compartment_id = var.tenancy_ocid
134-
shape = "VM.Standard2.1"
135-
}
136-
137104
locals {
138-
all_images = "${data.oci_core_images.shape_specific_images.images}"
139-
all_sources = "${data.oci_containerengine_node_pool_option.test_node_pool_option.sources}"
140-
141-
compartment_images = [for image in local.all_images : image.id if length(regexall("Oracle-Linux-[0-9]*.[0-9]*-20[0-9]*",image.display_name)) > 0 ]
142-
143-
oracle_linux_images = [for source in local.all_sources : source.image_id if length(regexall("Oracle-Linux-[0-9]*.[0-9]*-20[0-9]*",source.source_name)) > 0]
144-
145-
image_id = tolist(setintersection( toset(local.compartment_images), toset(local.oracle_linux_images)))[0]
146-
147105
addon_mappings = {
148106
mapping1 = {
149107
key = "numOfReplicas"

internal/integrationtest/containerengine_addon_test.go

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ var (
3636
addonConfigValue = "1"
3737
addonConfigValueUpdate = "2"
3838

39+
essentialAddonName = "CoreDNS"
40+
essentialAddonConfigKey = "minReplica"
41+
essentialAddonConfigValue = "4"
42+
3943
ContainerengineAddonSingularDataSourceRepresentation = map[string]interface{}{
4044
"addon_name": acctest.Representation{RepType: acctest.Required, Create: `${oci_containerengine_addon.test_addon.addon_name}`},
4145
"cluster_id": acctest.Representation{RepType: acctest.Required, Create: `${oci_containerengine_cluster.test_cluster.id}`},
@@ -57,71 +61,31 @@ var (
5761
"version": acctest.Representation{RepType: acctest.Optional, Create: nil, Update: `${data.oci_containerengine_addon_options.adddon_options_dashboard.addon_options[0].versions[0].version_number}`},
5862
}
5963

64+
ContainerengineEssentialAddonRepresentation = map[string]interface{}{
65+
"cluster_id": acctest.Representation{RepType: acctest.Required, Create: `${oci_containerengine_cluster.test_cluster.id}`},
66+
"addon_name": acctest.Representation{RepType: acctest.Required, Create: essentialAddonName},
67+
"remove_addon_resources_on_delete": acctest.Representation{RepType: acctest.Required, Create: `false`},
68+
"override_existing": acctest.Representation{RepType: acctest.Optional, Create: `true`},
69+
"configurations": acctest.RepresentationGroup{RepType: acctest.Optional, Group: ContainerengineEssentialAddonConfigurationsRepresentation},
70+
}
71+
6072
ContainerengineAddonConfigurationsRepresentation = map[string]interface{}{
6173
"key": acctest.Representation{RepType: acctest.Optional, Create: addonConfigKey, Update: addonConfigKey},
6274
"value": acctest.Representation{RepType: acctest.Optional, Create: addonConfigValue, Update: addonConfigValueUpdate},
6375
}
6476

77+
ContainerengineEssentialAddonConfigurationsRepresentation = map[string]interface{}{
78+
"key": acctest.Representation{RepType: acctest.Optional, Create: essentialAddonConfigKey},
79+
"value": acctest.Representation{RepType: acctest.Optional, Create: essentialAddonConfigValue},
80+
}
81+
6582
ContainerengineAddonRequiredOnlyResourceCreate = acctest.GenerateResourceFromRepresentationMap("oci_containerengine_addon", "test_addon", acctest.Required, acctest.Create, ContainerengineAddonRepresentation)
6683

6784
ContainerengineAddonOptionalResourceCreate = acctest.GenerateResourceFromRepresentationMap("oci_containerengine_addon", "test_addon", acctest.Optional, acctest.Create, ContainerengineAddonRepresentation)
6885

6986
ContainerengineAddonOptionalResourceConfigUpdate = acctest.GenerateResourceFromRepresentationMap("oci_containerengine_addon", "test_addon", acctest.Optional, acctest.Update, ContainerengineAddonRepresentation)
7087

71-
clusterOptionAddonDataSourceRepresentation = map[string]interface{}{
72-
"cluster_option_id": acctest.Representation{RepType: acctest.Required, Create: `all`},
73-
"compartment_id": acctest.Representation{RepType: acctest.Optional, Create: `${var.compartment_id}`},
74-
}
75-
76-
clusterAddonVcnRepresentation = map[string]interface{}{
77-
"cidr_block": acctest.Representation{RepType: acctest.Required, Create: `10.0.0.0/16`},
78-
"compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`},
79-
"defined_tags": acctest.Representation{RepType: acctest.Optional, Create: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "value")}`, Update: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "updatedValue")}`},
80-
"display_name": acctest.Representation{RepType: acctest.Optional, Create: `displayName`, Update: `displayName2`},
81-
"dns_label": acctest.Representation{RepType: acctest.Optional, Create: `dnslabel`},
82-
"freeform_tags": acctest.Representation{RepType: acctest.Optional, Create: map[string]string{"Department": "Finance"}, Update: map[string]string{"Department": "Accounting"}},
83-
"lifecycle": acctest.RepresentationGroup{RepType: acctest.Required, Group: ignoreDefinedTagsChangesRep},
84-
}
85-
86-
clusterAddonSubnetRepresentation = map[string]interface{}{
87-
"cidr_block": acctest.Representation{RepType: acctest.Required, Create: `10.0.0.0/24`, Update: "10.0.0.0/16"},
88-
"compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`},
89-
"vcn_id": acctest.Representation{RepType: acctest.Required, Create: `${oci_core_vcn.test_vcn.id}`},
90-
"availability_domain": acctest.Representation{RepType: acctest.Optional, Create: `${lower("${data.oci_identity_availability_domains.test_availability_domains.availability_domains.0.name}")}`},
91-
"defined_tags": acctest.Representation{RepType: acctest.Optional, Create: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "value")}`, Update: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "updatedValue")}`},
92-
"dhcp_options_id": acctest.Representation{RepType: acctest.Optional, Create: `${oci_core_vcn.test_vcn.default_dhcp_options_id}`, Update: `${oci_core_dhcp_options.test_dhcp_options.id}`},
93-
"display_name": acctest.Representation{RepType: acctest.Optional, Create: `MySubnet`, Update: `displayName2`},
94-
"dns_label": acctest.Representation{RepType: acctest.Optional, Create: `dnslabel`},
95-
"freeform_tags": acctest.Representation{RepType: acctest.Optional, Create: map[string]string{"Department": "Finance"}, Update: map[string]string{"Department": "Accounting"}},
96-
"prohibit_public_ip_on_vnic": acctest.Representation{RepType: acctest.Optional, Create: `false`},
97-
"prohibit_internet_ingress": acctest.Representation{RepType: acctest.Optional, Create: `false`},
98-
"route_table_id": acctest.Representation{RepType: acctest.Optional, Create: `${oci_core_vcn.test_vcn.default_route_table_id}`, Update: `${oci_core_route_table.test_route_table.id}`},
99-
"security_list_ids": acctest.Representation{RepType: acctest.Optional, Create: []string{`${oci_core_vcn.test_vcn.default_security_list_id}`}, Update: []string{`${oci_core_security_list.test_security_list.id}`}},
100-
"lifecycle": acctest.RepresentationGroup{RepType: acctest.Required, Group: ignoreDefinedTagsChangesRep},
101-
}
102-
103-
containerengineClusterRepresentation = map[string]interface{}{
104-
"compartment_id": acctest.Representation{RepType: acctest.Required, Create: `${var.compartment_id}`},
105-
"kubernetes_version": acctest.Representation{RepType: acctest.Required, Create: `${data.oci_containerengine_cluster_option.test_cluster_option.kubernetes_versions[length(data.oci_containerengine_cluster_option.test_cluster_option.kubernetes_versions)-2]}`, Update: `${data.oci_containerengine_cluster_option.test_cluster_option.kubernetes_versions[length(data.oci_containerengine_cluster_option.test_cluster_option.kubernetes_versions)-1]}`},
106-
"name": acctest.Representation{RepType: acctest.Required, Create: `name`, Update: `name2`},
107-
"vcn_id": acctest.Representation{RepType: acctest.Required, Create: `${oci_core_vcn.test_vcn.id}`},
108-
"defined_tags": acctest.Representation{RepType: acctest.Optional, Create: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "value")}`, Update: `${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "updatedValue")}`},
109-
"endpoint_config": acctest.RepresentationGroup{RepType: acctest.Optional, Group: clusterAddonEndpointConfigRepresentation},
110-
"freeform_tags": acctest.Representation{RepType: acctest.Optional, Create: map[string]string{"Department": "Finance"}, Update: map[string]string{"Department": "Accounting"}},
111-
"image_policy_config": acctest.RepresentationGroup{RepType: acctest.Optional, Group: clusterAddonImagePolicyConfigRepresentation},
112-
"kms_key_id": acctest.Representation{RepType: acctest.Optional, Create: `${lookup(data.oci_kms_keys.test_keys_dependency.keys[0], "id")}`},
113-
"options": acctest.RepresentationGroup{RepType: acctest.Optional, Group: ContainerengineClusterOptionsRepresentation},
114-
}
115-
116-
clusterAddonEndpointConfigRepresentation = map[string]interface{}{
117-
"nsg_ids": acctest.Representation{RepType: acctest.Optional, Create: []string{`${oci_core_network_security_group.test_network_security_group.id}`}, Update: []string{}},
118-
"subnet_id": acctest.Representation{RepType: acctest.Required, Create: `${oci_core_subnet.test_subnet.id}`},
119-
}
120-
121-
clusterAddonImagePolicyConfigRepresentation = map[string]interface{}{
122-
"is_policy_enabled": acctest.Representation{RepType: acctest.Optional, Create: `false`, Update: `true`},
123-
"key_details": acctest.RepresentationGroup{RepType: acctest.Optional, Group: ContainerengineClusterImagePolicyConfigKeyDetailsRepresentation},
124-
}
88+
ContainerengineEssentialAddonResourceCreate = acctest.GenerateResourceFromRepresentationMap("oci_containerengine_addon", "test_essential_addon", acctest.Optional, acctest.Create, ContainerengineEssentialAddonRepresentation)
12589

12690
AddonOptionDashboardDataSourceRepresentation = map[string]interface{}{
12791
"kubernetes_version": acctest.Representation{RepType: acctest.Required, Create: `${data.oci_containerengine_cluster_option.test_cluster_option.kubernetes_versions[length(data.oci_containerengine_cluster_option.test_cluster_option.kubernetes_versions)-2]}`},
@@ -130,9 +94,13 @@ var (
13094

13195
ContainerengineAddonResourceDependencies = acctest.GenerateResourceFromRepresentationMap("oci_containerengine_cluster", "test_cluster", acctest.Required, acctest.Create,
13296
acctest.RepresentationCopyWithNewProperties(ContainerengineClusterRepresentation, map[string]interface{}{
133-
"type": acctest.Representation{RepType: acctest.Required, Create: `ENHANCED_CLUSTER`, Update: `ENHANCED_CLUSTER`},
97+
"type": acctest.Representation{RepType: acctest.Required, Create: `ENHANCED_CLUSTER`, Update: `ENHANCED_CLUSTER`},
98+
"cluster_pod_network_options": acctest.RepresentationGroup{RepType: acctest.Required, Group: clusterClusterPodNetworkOptionsRepresentation},
99+
"endpoint_config": acctest.RepresentationGroup{RepType: acctest.Required, Group: ContainerengineClusterEndpointConfigRepresentation},
134100
})) +
135101
acctest.GenerateDataSourceFromRepresentationMap("oci_containerengine_cluster_option", "test_cluster_option", acctest.Required, acctest.Create, ContainerengineContainerengineClusterOptionSingularDataSourceRepresentation) +
102+
acctest.GenerateResourceFromRepresentationMap("oci_core_network_security_group", "test_network_security_group", acctest.Required, acctest.Create, CoreNetworkSecurityGroupRepresentation) +
103+
acctest.GenerateResourceFromRepresentationMap("oci_core_subnet", "test_subnet", acctest.Required, acctest.Create, CoreSubnetRepresentation) +
136104
acctest.GenerateResourceFromRepresentationMap("oci_core_vcn", "test_vcn", acctest.Required, acctest.Create, acctest.RepresentationCopyWithNewProperties(CoreVcnRepresentation, map[string]interface{}{
137105
"dns_label": acctest.Representation{RepType: acctest.Required, Create: `dnslabel`},
138106
})) +
@@ -145,12 +113,15 @@ func TestContainerengineAddonResource_basic(t *testing.T) {
145113
httpreplay.SetScenario("TestContainerengineAddonResource_basic")
146114
defer httpreplay.SaveScenario()
147115

116+
fmt.Printf("ContainerengineEssentialAddonResourceCreate: %v", ContainerengineEssentialAddonResourceCreate)
117+
148118
config := acctest.ProviderTestConfig()
149119

150120
compartmentId := utils.GetEnvSettingWithBlankDefault("compartment_ocid")
151121
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
152122

153123
resourceName := "oci_containerengine_addon.test_addon"
124+
essentialAddonResourceName := "oci_containerengine_addon.test_essential_addon"
154125
datasourceName := "data.oci_containerengine_addons.test_addons"
155126
singularDatasourceName := "data.oci_containerengine_addon.test_addon"
156127

@@ -230,6 +201,20 @@ func TestContainerengineAddonResource_basic(t *testing.T) {
230201
},
231202
),
232203
},
204+
// verify update-on-install of an essential addon
205+
{
206+
Config: baseConfig + ContainerengineAddonOptionalResourceConfigUpdate + ContainerengineEssentialAddonResourceCreate,
207+
208+
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
209+
resource.TestCheckResourceAttrSet(essentialAddonResourceName, "cluster_id"),
210+
resource.TestCheckResourceAttr(essentialAddonResourceName, "configurations.#", "1"),
211+
resource.TestCheckResourceAttr(essentialAddonResourceName, "configurations.0.key", essentialAddonConfigKey),
212+
resource.TestCheckResourceAttr(essentialAddonResourceName, "configurations.0.value", essentialAddonConfigValue),
213+
resource.TestCheckResourceAttrSet(essentialAddonResourceName, "current_installed_version"),
214+
resource.TestCheckResourceAttr(essentialAddonResourceName, "addon_name", essentialAddonName),
215+
resource.TestCheckResourceAttrSet(essentialAddonResourceName, "state"),
216+
),
217+
},
233218
// verify datasource
234219
{
235220
Config: baseConfig + ContainerengineAddonDataSource + ContainerengineAddonOptionalResourceConfigUpdate,
@@ -246,7 +231,6 @@ func TestContainerengineAddonResource_basic(t *testing.T) {
246231
Config: baseConfig + ContainerengineAddonSingularDataSource + ContainerengineAddonOptionalResourceConfigUpdate,
247232

248233
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
249-
resource.TestCheckNoResourceAttr(singularDatasourceName, "addon_error"),
250234
resource.TestCheckResourceAttrSet(singularDatasourceName, "time_created"),
251235
resource.TestCheckResourceAttr(singularDatasourceName, "configurations.#", "1"),
252236
resource.TestCheckResourceAttr(singularDatasourceName, "configurations.0.key", addonConfigKey),
@@ -262,7 +246,7 @@ func TestContainerengineAddonResource_basic(t *testing.T) {
262246
Config: baseConfig + ContainerengineAddonRequiredOnlyResourceCreate,
263247
ImportState: true,
264248
ImportStateVerify: true,
265-
ImportStateVerifyIgnore: []string{"remove_addon_resources_on_delete"},
249+
ImportStateVerifyIgnore: []string{"remove_addon_resources_on_delete", "override_existing"},
266250
ResourceName: resourceName,
267251
},
268252
})
@@ -272,7 +256,7 @@ func testAccCheckContainerengineAddonDestroy(s *terraform.State) error {
272256
noResourceFound := true
273257
client := acctest.TestAccProvider.Meta().(*tf_client.OracleClients).ContainerEngineClient()
274258
for _, rs := range s.RootModule().Resources {
275-
if rs.Type == "oci_containerengine_addon" {
259+
if rs.Type == "oci_containerengine_addon" && rs.Primary.Attributes["addon_name"] != essentialAddonName {
276260
noResourceFound = false
277261
request := oci_containerengine.GetAddonRequest{}
278262

0 commit comments

Comments
 (0)