Skip to content

Commit

Permalink
[Refactor] CCE: Refactor CCE Addons (#2770)
Browse files Browse the repository at this point in the history
[Refactor] CCE: Refactor CCE Addons

Summary of the Pull Request
Apply changes from gopher in provider
PR Checklist

 Tests added/passed.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestAccCCEAddonV3Basic
=== PAUSE TestAccCCEAddonV3Basic
=== RUN   TestAccCCEAddonV3CoreDNS
=== PAUSE TestAccCCEAddonV3CoreDNS
=== RUN   TestAccCCEAddonV3Flavor
=== PAUSE TestAccCCEAddonV3Flavor
=== CONT  TestAccCCEAddonV3Basic
=== CONT  TestAccCCEAddonV3CoreDNS
=== CONT  TestAccCCEAddonV3Flavor
--- PASS: TestAccCCEAddonV3CoreDNS (666.39s)
--- PASS: TestAccCCEAddonV3Flavor (734.45s)
--- PASS: TestAccCCEAddonV3Basic (769.78s)
PASS

Skipped: TestAccCCEAddonV3ForceNewCCE

Reviewed-by: Anton Sidelnikov
  • Loading branch information
muneeb-jan authored Dec 19, 2024
1 parent 8054ca6 commit 6b85863
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 96 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241216095705-f4669448c474
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241217130728-c35f3bd67003
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.31.0
golang.org/x/sync v0.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241216095705-f4669448c474 h1:ostsbG++ZcDj+lut7axnbonRAdRDs9/fpZBVOZC5FdQ=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241216095705-f4669448c474/go.mod h1:la8cQVYopRoEbNe2L7HlGTdLxUQOwIqHp1VHtjE/5qA=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241217130728-c35f3bd67003 h1:yZzN1AJUxA1fpFZSsgokbCfBWS+/McwRSlc56A2oqjU=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241217130728-c35f3bd67003/go.mod h1:la8cQVYopRoEbNe2L7HlGTdLxUQOwIqHp1VHtjE/5qA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,30 @@ import (
const resourceAddonName = "opentelekomcloud_cce_addon_v3.autoscaler"
const resourceAddonNameDns = "opentelekomcloud_cce_addon_v3.coredns"

func getCceAddonResourceFunc(cfg *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := cfg.CceV3AddonClient(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating CCE v3 Addon Client: %s", err)
}
return addons.Get(client, state.Primary.ID, state.Primary.Attributes["cluster_id"])
}

func TestAccCCEAddonV3Basic(t *testing.T) {
var addon addons.Addon
rc := common.InitResourceCheck(
resourceAddonName,
&addon,
getCceAddonResourceFunc,
)

clusterName := randClusterName()
t.Parallel()
quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEAddonV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEAddonV3Basic(clusterName),
Expand All @@ -40,23 +55,6 @@ func TestAccCCEAddonV3Basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceAddonName, "values.0.custom.scaleDownDelayAfterDelete", "8"),
),
},
},
})
}

func TestAccCCEAddonV3ImportBasic(t *testing.T) {
t.Parallel()
clusterName := randClusterName()
quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEAddonV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccCCEAddonV3Basic(clusterName),
},
{
ResourceName: resourceAddonName,
ImportState: true,
Expand All @@ -70,33 +68,21 @@ func TestAccCCEAddonV3ImportBasic(t *testing.T) {
})
}

func testAccCCEAddonV3ImportStateIdFunc() resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
var clusterID string
var addonID string
for _, rs := range s.RootModule().Resources {
if rs.Type == "opentelekomcloud_cce_cluster_v3" {
clusterID = rs.Primary.ID
} else if rs.Type == "opentelekomcloud_cce_addon_v3" {
addonID = rs.Primary.ID
}
}
if clusterID == "" || addonID == "" {
return "", fmt.Errorf("resource not found: %s/%s", clusterID, addonID)
}
return fmt.Sprintf("%s/%s", clusterID, addonID), nil
}
}

func TestAccCCEAddonV3ForceNewCCE(t *testing.T) {
var addon addons.Addon
rc := common.InitResourceCheck(
resourceAddonName,
&addon,
getCceAddonResourceFunc,
)
clusterName := randClusterName()
t.Parallel()
quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEAddonV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEAddonV3Basic(clusterName),
Expand All @@ -117,14 +103,20 @@ func TestAccCCEAddonV3ForceNewCCE(t *testing.T) {
}

func TestAccCCEAddonV3CoreDNS(t *testing.T) {
var addon addons.Addon
rc := common.InitResourceCheck(
resourceAddonNameDns,
&addon,
getCceAddonResourceFunc,
)
clusterName := randClusterName()
t.Parallel()
quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEAddonV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEAddonV3StubDomains(clusterName),
Expand All @@ -140,14 +132,20 @@ const flavorRef = " {\n \"description\": \"Has only one instance\",\
const flavorRefUpdate = " {\n \"description\": \"Has only one instance\",\n \"name\": \"Single\",\n \"replicas\": 1,\n \"resources\": [\n {\n \"limitsCpu\": \"8000m\",\n \"limitsMem\": \"4Gi\",\n \"name\": \"autoscaler\",\n \"requestsCpu\": \"4000m\",\n \"requestsMem\": \"2Gi\"\n }\n ]\n }\n"

func TestAccCCEAddonV3Flavor(t *testing.T) {
var addon addons.Addon
rc := common.InitResourceCheck(
resourceAddonName,
&addon,
getCceAddonResourceFunc,
)
clusterName := randClusterName()
t.Parallel()
quotas.BookOne(t, quotas.CCEClusterQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEAddonV3Destroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccCCEAddonV3Flavor(clusterName),
Expand All @@ -167,25 +165,22 @@ func TestAccCCEAddonV3Flavor(t *testing.T) {
})
}

func testAccCheckCCEAddonV3Destroy(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.CceV3Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud CCEv3 client: %w", err)
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "opentelekomcloud_cce_addon_v3" {
continue
func testAccCCEAddonV3ImportStateIdFunc() resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
var clusterID string
var addonID string
for _, rs := range s.RootModule().Resources {
if rs.Type == "opentelekomcloud_cce_cluster_v3" {
clusterID = rs.Primary.ID
} else if rs.Type == "opentelekomcloud_cce_addon_v3" {
addonID = rs.Primary.ID
}
}

_, err := addons.Get(client, rs.Primary.ID, rs.Primary.Attributes["cluster_id"]).Extract()
if err == nil {
return fmt.Errorf("addon still exists")
if clusterID == "" || addonID == "" {
return "", fmt.Errorf("resource not found: %s/%s", clusterID, addonID)
}
return fmt.Sprintf("%s/%s", clusterID, addonID), nil
}

return nil
}

func checkScaleDownForAutoscaler(name string, enabled bool) resource.TestCheckFunc {
Expand All @@ -205,7 +200,7 @@ func checkScaleDownForAutoscaler(name string, enabled bool) resource.TestCheckFu
return fmt.Errorf("error creating opentelekomcloud CCE client: %w", err)
}

found, err := addons.Get(client, rs.Primary.ID, rs.Primary.Attributes["cluster_id"]).Extract()
found, err := addons.Get(client, rs.Primary.ID, rs.Primary.Attributes["cluster_id"])
if err != nil {
return err
}
Expand Down Expand Up @@ -233,21 +228,21 @@ resource opentelekomcloud_cce_cluster_v3 cluster_1 {
flavor_id = "cce.s1.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
cluster_version = "v1.27"
cluster_version = "v1.29"
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
}
resource "opentelekomcloud_cce_addon_v3" "autoscaler" {
template_name = "autoscaler"
template_version = "1.27.53"
template_version = "1.29.17"
cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id
values {
basic = {
"cceEndpoint" : "https://cce.eu-de.otc.t-systems.com",
"ecsEndpoint" : "https://ecs.eu-de.otc.t-systems.com",
"image_version" : "1.27.53",
"image_version" : "1.29.17",
"region" : "eu-de",
"swr_addr" : "100.125.7.25:20202",
"swr_user" : "cce-addons"
Expand Down Expand Up @@ -306,21 +301,21 @@ resource opentelekomcloud_cce_cluster_v3 cluster_1 {
flavor_id = "cce.s1.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
cluster_version = "v1.27"
cluster_version = "v1.29"
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
}
resource "opentelekomcloud_cce_addon_v3" "autoscaler" {
template_name = "autoscaler"
template_version = "1.27.53"
template_version = "1.29.17"
cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id
values {
basic = {
"cceEndpoint" : "https://cce.eu-de.otc.t-systems.com",
"ecsEndpoint" : "https://ecs.eu-de.otc.t-systems.com",
"image_version" : "1.27.53",
"image_version" : "1.29.17",
"region" : "eu-de",
"swr_addr" : "100.125.7.25:20202",
"swr_user" : "cce-addons"
Expand Down Expand Up @@ -379,21 +374,21 @@ resource opentelekomcloud_cce_cluster_v3 cluster_1 {
flavor_id = "cce.s1.medium"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
cluster_version = "v1.19"
cluster_version = "v1.29"
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
}
resource "opentelekomcloud_cce_addon_v3" "autoscaler" {
template_name = "autoscaler"
template_version = "1.19.1"
template_version = "1.29.17"
cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id
values {
basic = {
"cceEndpoint" : "https://cce.eu-de.otc.t-systems.com",
"ecsEndpoint" : "https://ecs.eu-de.otc.t-systems.com",
"image_version" : "1.19.1",
"image_version" : "1.29.17",
"platform" : "linux-amd64",
"region" : "eu-de",
"swr_addr" : "100.125.7.25:20202",
Expand Down Expand Up @@ -448,21 +443,23 @@ resource opentelekomcloud_cce_cluster_v3 cluster_1 {
flavor_id = "cce.s1.medium"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
cluster_version = "v1.23"
cluster_version = "v1.29"
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
no_addons = true
}
resource "opentelekomcloud_cce_addon_v3" "coredns" {
template_name = "coredns"
template_version = "1.28.4"
template_version = "1.29.4"
cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id
values {
basic = {
"cluster_ip" : "10.247.3.10",
"image_version" : "1.29.4",
"swr_addr" : "100.125.7.25:20202",
"swr_user" : "hwofficial"
"swr_user" : "cce-addons"
}
custom = {
"stub_domains" : "{\"test\":[\"10.10.40.10\"], \"test2\":[\"10.10.40.20\"]}"
Expand All @@ -484,21 +481,21 @@ resource opentelekomcloud_cce_cluster_v3 cluster_1 {
flavor_id = "cce.s1.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
cluster_version = "v1.27"
cluster_version = "v1.29"
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
}
resource "opentelekomcloud_cce_addon_v3" "autoscaler" {
template_name = "autoscaler"
template_version = "1.27.53"
template_version = "1.29.17"
cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id
values {
basic = {
"cceEndpoint" : "https://cce.eu-de.otc.t-systems.com",
"ecsEndpoint" : "https://ecs.eu-de.otc.t-systems.com",
"image_version" : "1.27.53",
"image_version" : "1.29.17",
"region" : "eu-de",
"swr_addr" : "100.125.7.25:20202",
"swr_user" : "cce-addons"
Expand Down Expand Up @@ -557,21 +554,21 @@ resource opentelekomcloud_cce_cluster_v3 cluster_1 {
flavor_id = "cce.s1.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
cluster_version = "v1.27"
cluster_version = "v1.29"
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
}
resource "opentelekomcloud_cce_addon_v3" "autoscaler" {
template_name = "autoscaler"
template_version = "1.27.53"
template_version = "1.29.17"
cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id
values {
basic = {
"cceEndpoint" : "https://cce.eu-de.otc.t-systems.com",
"ecsEndpoint" : "https://ecs.eu-de.otc.t-systems.com",
"image_version" : "1.27.53",
"image_version" : "1.29.17",
"region" : "eu-de",
"swr_addr" : "100.125.7.25:20202",
"swr_user" : "cce-addons"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func dataSourceCceAddonTemplateV3Read(_ context.Context, d *schema.ResourceData,
return fmterr.Errorf(cceClientError, err)
}

addonTemplates, err := addons.GetTemplates(client).Extract()
addonTemplates, err := addons.GetTemplates(client)
if err != nil {
return fmterr.Errorf("unable to retrieve templates: %s", err)
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func dataSourceCceAddonTemplateV3Read(_ context.Context, d *schema.ResourceData,
log.Printf("[DEBUG] Retrieved Template using given filter: %s", template.Metadata.Id)
d.SetId(template.Metadata.Id)

inputData := result.Input["basic"].(map[string]interface{})
inputData := result.Input.Basic
mErr := multierror.Append(
d.Set("cluster_ip", inputData["cluster_ip"]),
d.Set("image_version", inputData["image_version"]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func dataSourceCceAddonTemplatesV3Read(_ context.Context, d *schema.ResourceData
return fmterr.Errorf(cceClientError, err)
}

addonTemplates, err := addons.GetTemplates(client).Extract()
addonTemplates, err := addons.GetTemplates(client)
if err != nil {
return fmterr.Errorf("unable to retrieve templates: %s", err)
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func dataSourceCceAddonTemplatesV3Read(_ context.Context, d *schema.ResourceData

result := make([]map[string]interface{}, len(templates))
for i, item := range templates {
inputData := item.Input["basic"].(map[string]interface{})
inputData := item.Input.Basic
addon := map[string]interface{}{
"addon_version": item.Version,
"cluster_ip": inputData["cluster_ip"],
Expand Down
Loading

0 comments on commit 6b85863

Please sign in to comment.