Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add satellite features and add support to the user can direct which security groups are added to their workers #4953

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ vendor/
!command/test-fixtures/**/*.tfstate
!command/test-fixtures/**/.terraform/

*.sh
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/IBM-Cloud/terraform-provider-ibm
go 1.19

require (
github.com/IBM-Cloud/bluemix-go v0.0.0-20231204080125-462fa9e436bc
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20231116055201-2a84da7b9bd6
github.com/IBM-Cloud/bluemix-go v0.0.0-20240110132033-6ead1f81a985
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20231207111718-a3b74cc935fa
github.com/IBM-Cloud/power-go-client v1.5.8
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca
github.com/IBM/appconfiguration-go-admin-sdk v0.3.0
Expand Down Expand Up @@ -83,7 +83,7 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21 // indirect
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/coreos/pkg v0.0.0-20220810130054-c7d1c02cb6cf // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ github.com/DataDog/datadog-go v2.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3
github.com/DataDog/datadog-go v3.2.0+incompatible h1:qSG2N4FghB1He/r2mFrWKCaL7dXCilEuNEeAn20fdD4=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/zstd v1.4.4/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/IBM-Cloud/bluemix-go v0.0.0-20231204080125-462fa9e436bc h1:AeooCa6UMWycgKJ9n0do9PEZaNlYZZHqspfwUzPvopc=
github.com/IBM-Cloud/bluemix-go v0.0.0-20231204080125-462fa9e436bc/go.mod h1:jIGLnIfj+uBv2ALz3rVHzNbNwt0V/bEWNeJKECa8Q+k=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20231116055201-2a84da7b9bd6 h1:QXU1Atl/JSI3ZtYB9tHbWLhrFYE1E+5Iww1sjQ7mqdo=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20231116055201-2a84da7b9bd6/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
github.com/IBM-Cloud/bluemix-go v0.0.0-20240110132033-6ead1f81a985 h1:Rsi0y9dJZNkF9zIa0Yjf9rdYHb5UqMMGbZvOcsESq90=
github.com/IBM-Cloud/bluemix-go v0.0.0-20240110132033-6ead1f81a985/go.mod h1:jIGLnIfj+uBv2ALz3rVHzNbNwt0V/bEWNeJKECa8Q+k=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20231207111718-a3b74cc935fa h1:tsgTFGt4j1V3PQmzZbA4wJAeT5rz24OgY4AvY2QGek0=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20231207111718-a3b74cc935fa/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.5.3/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs=
github.com/IBM-Cloud/power-go-client v1.5.8 h1:4l9PmnYRXV/KfVNBRuc9hya6kW5cQZhN4UMUMdpn1JU=
github.com/IBM-Cloud/power-go-client v1.5.8/go.mod h1:y4WDw/l9+29CKX98ngCCvGoHdzX49LL00B1euoAbWzQ=
Expand Down Expand Up @@ -314,8 +314,9 @@ github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6D
github.com/circonus-labs/circonusllhist v0.1.3 h1:TJH+oke8D16535+jHExHj4nQvzlZrj7ug5D7I/orNUA=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cloudfoundry-community/go-cfclient v0.0.0-20190201205600-f136f9222381/go.mod h1:e5+USP2j8Le2M0Jo3qKPFnNhuo1wueU4nWHCXBOfQ14=
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21 h1:tuijfIjZyjZaHq9xDUh0tNitwXshJpbLkqMOJv4H3do=
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21/go.mod h1:po7NpZ/QiTKzBKyrsEAxwnTamCoh8uDk/egRpQ7siIc=
Expand Down
30 changes: 26 additions & 4 deletions ibm/flex/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ func FlattenUsersSet(userList *schema.Set) []string {
return users
}

func FlattenSet(set *schema.Set) []string {
setList := set.List()
elems := make([]string, 0, len(setList))
for _, elem := range setList {
elems = append(elems, elem.(string))
}
return elems
}

func ExpandMembers(configured []interface{}) []datatypes.Network_LBaaS_LoadBalancerServerInstanceInfo {
members := make([]datatypes.Network_LBaaS_LoadBalancerServerInstanceInfo, 0, len(configured))
for _, lRaw := range configured {
Expand Down Expand Up @@ -429,6 +438,19 @@ func FlattenZones(list []containerv1.WorkerPoolZoneResponse) []map[string]interf
return zones
}

func FlattenZonesv2(list []containerv2.ZoneResp) []map[string]interface{} {
zones := make([]map[string]interface{}, len(list))
for i, zone := range list {
l := map[string]interface{}{
"zone": zone.ID,
"subnets": zone.Subnets,
"worker_count": zone.WorkerCount,
}
zones[i] = l
}
return zones
}

func FlattenWorkerPools(list []containerv1.WorkerPoolResponse) []map[string]interface{} {
workerPools := make([]map[string]interface{}, len(list))
for i, workerPool := range list {
Expand Down Expand Up @@ -3242,13 +3264,13 @@ func FlattenOpaqueSecret(fields containerv2.Fields) []map[string]interface{} {
return flattenedOpaqueSecret
}

// flattenHostLabels ..
func FlattenHostLabels(hostLabels []interface{}) map[string]string {
// flatten the provided key-value pairs
func FlattenKeyValues(keyValues []interface{}) map[string]string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you rename this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously it was used only for flatten Host Labels, but I renamed it and we can use it for everything when we should flatten key-values (map[string]string), and I used it too (and it was not host labels)

labels := make(map[string]string)
for _, v := range hostLabels {
for _, v := range keyValues {
parts := strings.Split(v.(string), ":")
if len(parts) != 2 {
log.Fatal("Entered label " + v.(string) + "is in incorrect format.")
log.Fatal("Entered key-value " + v.(string) + "is in incorrect format.")
}
labels[parts[0]] = parts[1]
}
Expand Down
14 changes: 14 additions & 0 deletions ibm/service/kubernetes/resource_ibm_container_vpc_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ func ResourceIBMContainerVpcCluster() *schema.Resource {
RequiredWith: []string{"kms_instance_id", "crk"},
},

"security_groups": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like this parameter can only be set at create
you can use DiffSuppressFunc: flex.ApplyOnce to ignore any changes to the parameter

or

it can be marked with forcenew to recreate the cluster if this param is changed. this means the original cluster will be deleted, so this should be handled with extra care

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should go with ApplyOnce rather than ForceNew.

At the moment, these values can not be changed in the API. If the ability to change the security groups was added in the future and customers update the security groups outside of TF, TF would replace the cluster.

If we go with ApplyOnce, the changes would be ignored until the TF provider supported that functionality at which point we could provide guidance to customers on importing the updated changes and avoid cluster deletion.

Type: schema.TypeSet,
Optional: true,
Description: "Allow user to set which security groups added to their workers",
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
DiffSuppressFunc: flex.ApplyOnce,
},

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like it is missing from the API, but...
should we be able to set SecurityGroupIDs to the default workerpool too?

//Get Cluster info Request
"state": {
Type: schema.TypeString,
Expand Down Expand Up @@ -587,6 +596,11 @@ func resourceIBMContainerVpcClusterCreate(d *schema.ResourceData, meta interface
params.CosInstanceCRN = v.(string)
}

if v, ok := d.GetOk("security_groups"); ok {
securityGroups := flex.FlattenSet(v.(*schema.Set))
params.SecurityGroupIDs = securityGroups
}

targetEnv, err := getVpcClusterTargetHeader(d, meta)
if err != nil {
return err
Expand Down
89 changes: 89 additions & 0 deletions ibm/service/kubernetes/resource_ibm_container_vpc_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ func TestAccIBMContainerVpcClusterDedicatedHost(t *testing.T) {
)
}

func TestAccIBMContainerVpcClusterSecurityGroups(t *testing.T) {
name := fmt.Sprintf("tf-vpc-cluster-%d", acctest.RandIntRange(10, 100))
var conf *v2.ClusterInfo

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMContainerVpcClusterSecurityGroups(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMContainerVpcExists("ibm_container_vpc_cluster.cluster", conf),
resource.TestCheckResourceAttr(
"ibm_container_vpc_cluster.cluster", "name", name),
),
},
{
ResourceName: "ibm_container_vpc_cluster.cluster",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"wait_till", "update_all_workers", "kms_config", "force_delete_storage", "wait_for_worker_update"},
},
},
})
}

func testAccCheckIBMContainerVpcClusterDestroy(s *terraform.State) error {
csClient, err := acc.TestAccProvider.Meta().(conns.ClientSession).VpcContainerAPI()
if err != nil {
Expand Down Expand Up @@ -275,6 +302,68 @@ resource "ibm_container_vpc_cluster" "cluster" {
}`, name)
}

// preveously you have to create securitygroups and use them instead
func testAccCheckIBMContainerVpcClusterSecurityGroups(name string) string {
return fmt.Sprintf(`
data "ibm_resource_group" "resource_group" {
is_default = "true"
//name = "Default"
}
resource "ibm_is_vpc" "vpc" {
name = "%[1]s"
}
resource "ibm_is_security_group" "security_group" {
name = "example-security-group"
vpc = ibm_is_vpc.vpc.id
}
resource "ibm_is_subnet" "subnet" {
name = "%[1]s"
vpc = ibm_is_vpc.vpc.id
zone = "us-south-1"
total_ipv4_address_count = 256
}
resource "ibm_resource_instance" "kms_instance" {
name = "%[1]s"
service = "kms"
plan = "tiered-pricing"
location = "eu-de"
}

resource "ibm_kms_key" "test" {
instance_id = ibm_resource_instance.kms_instance.guid
key_name = "%[1]s"
standard_key = false
force_delete = true
}
resource "ibm_container_vpc_cluster" "cluster" {
name = "%[1]s"
vpc_id = ibm_is_vpc.vpc.id
flavor = "cx2.2x4"
worker_count = 1
wait_till = "OneWorkerNodeReady"
resource_group_id = data.ibm_resource_group.resource_group.id
zones {
subnet_id = ibm_is_subnet.subnet.id
name = "us-south-1"
}
kms_config {
instance_id = ibm_resource_instance.kms_instance.guid
crk_id = ibm_kms_key.test.key_id
private_endpoint = false
}
worker_labels = {
"test" = "test-default-pool"
"test1" = "test-default-pool1"
"test2" = "test-default-pool2"
}

security_groups = [
ibm_is_security_group.security_group.id,
"cluster",
]
}`, name)
}

func testAccCheckIBMContainerVpcClusterUpdate(name string) string {
return fmt.Sprintf(`
provider "ibm" {
Expand Down
14 changes: 14 additions & 0 deletions ibm/service/kubernetes/resource_ibm_container_vpc_worker_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ func ResourceIBMContainerVpcWorkerPool() *schema.Resource {
Computed: true,
Description: "Autoscaling is enabled on the workerpool",
},

"security_groups": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add the ApplyOnce diff suppress here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, will do

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add a test for this too?

Type: schema.TypeSet,
Optional: true,
Description: "Allow user to set which security groups added to their workers",
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
DiffSuppressFunc: flex.ApplyOnce,
},
},
}
}
Expand Down Expand Up @@ -283,6 +292,11 @@ func resourceIBMContainerVpcWorkerPoolCreate(d *schema.ResourceData, meta interf
},
}

if v, ok := d.GetOk("security_groups"); ok {
securityGroups := flex.FlattenSet(v.(*schema.Set))
params.SecurityGroupIDs = securityGroups
}

if kmsid, ok := d.GetOk("kms_instance_id"); ok {
crk := d.Get("crk").(string)
wve := v2.WorkerVolumeEncryption{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ func TestAccIBMContainerVpcClusterWorkerPoolDedicatedHost(t *testing.T) {
})
}

func TestAccIBMContainerVpcClusterWorkerPoolSecurityGroups(t *testing.T) {

name := fmt.Sprintf("tf-vpc-worker-pool-%d", acctest.RandIntRange(10, 100))
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMVpcContainerWorkerPoolSecurityGroups(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"ibm_container_vpc_worker_pool.test_pool", "flavor", "cx2.2x4"),
resource.TestCheckResourceAttr(
"ibm_container_vpc_worker_pool.test_pool", "zones.#", "1"),
),
},
{
ResourceName: "ibm_container_vpc_worker_pool.test_pool",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckIBMVpcContainerWorkerPoolDestroy(s *terraform.State) error {

wpClient, err := acc.TestAccProvider.Meta().(conns.ClientSession).VpcContainerAPI()
Expand Down Expand Up @@ -173,6 +198,70 @@ func testAccCheckIBMVpcContainerWorkerPoolBasic(name string) string {
`, name)
}

func testAccCheckIBMVpcContainerWorkerPoolSecurityGroups(name string) string {
return fmt.Sprintf(`
data "ibm_resource_group" "resource_group" {
is_default=true
}
resource "ibm_is_vpc" "vpc" {
name = "%[1]s"
}
resource "ibm_is_security_group" "security_group1" {
name = "%[1]s-security-group-1"
vpc = ibm_is_vpc.vpc.id
}
resource "ibm_is_security_group" "security_group2" {
name = "%[1]s-security-group-2"
vpc = ibm_is_vpc.vpc.id
}
resource "ibm_is_subnet" "subnet1" {
name = "%[1]s-subnet-1"
vpc = ibm_is_vpc.vpc.id
zone = "us-south-1"
total_ipv4_address_count = 256
}
resource "ibm_is_subnet" "subnet2" {
name = "%[1]s-subnet-2"
vpc = ibm_is_vpc.vpc.id
zone = "us-south-2"
total_ipv4_address_count = 256
}

resource "ibm_container_vpc_cluster" "cluster" {
name = "%[1]s"
vpc_id = ibm_is_vpc.vpc.id
flavor = "cx2.2x4"
worker_count = 1
resource_group_id = data.ibm_resource_group.resource_group.id
wait_till = "MasterNodeReady"
zones {
subnet_id = ibm_is_subnet.subnet1.id
name = ibm_is_subnet.subnet1.zone
}
security_groups = [
ibm_is_security_group.security_group1.id,
"cluster",
]
}
resource "ibm_container_vpc_worker_pool" "test_pool" {
cluster = ibm_container_vpc_cluster.cluster.id
worker_pool_name = "%[1]s"
flavor = "cx2.2x4"
vpc_id = ibm_is_vpc.vpc.id
worker_count = 1
resource_group_id = data.ibm_resource_group.resource_group.id
zones {
subnet_id = ibm_is_subnet.subnet2.id
name = ibm_is_subnet.subnet2.zone
}
security_groups = [
ibm_is_security_group.security_group2.id,
]

}
`, name)
}

func testAccCheckIBMVpcContainerWorkerPoolUpdate(name string) string {
return fmt.Sprintf(`
provider "ibm" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func dataSourceIBMSatelliteAttachHostScriptRead(d *schema.ResourceData, meta int
labels := make(map[string]string)
if v, ok := d.GetOk("labels"); ok {
l := v.(*schema.Set)
labels = flex.FlattenHostLabels(l.List())
labels = flex.FlattenKeyValues(l.List())
d.Set("labels", l)
}

Expand Down
Loading
Loading