Skip to content

Commit

Permalink
Add entitlement option to Satellite cluster/workerpool create (#4894)
Browse files Browse the repository at this point in the history
* Add entitlement option to Satellite cluster/workerpool create

* SSH pub key from env var

* Use release github.com/IBM-Cloud/container-services-go-sdk
  • Loading branch information
TwoDCube committed Nov 16, 2023
1 parent 2bcce90 commit 2aad24a
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/IBM-Cloud/bluemix-go v0.0.0-20231017073329-75ebe90c98ba
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20230822142550-30562e113de9
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20231106114255-c50117860a3c
github.com/IBM-Cloud/power-go-client v1.5.2
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca
github.com/IBM/appconfiguration-go-admin-sdk v0.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ github.com/IBM-Cloud/bluemix-go v0.0.0-20231017073329-75ebe90c98ba h1:8U4HByOYJi
github.com/IBM-Cloud/bluemix-go v0.0.0-20231017073329-75ebe90c98ba/go.mod h1:mt+O8ryLVANrBKlA4RxKdENp3q6Q7mKQIi2nkiibZbU=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20230822142550-30562e113de9 h1:sXRzCK3Glxpyu66Tu2NjztLdT5sDwj4qly+MJKRhdWY=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20230822142550-30562e113de9/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20231106114255-c50117860a3c h1:tRS4VuOG3lHNG+yrsh3vZZQDVNLuFJB0oZbTJp9YXds=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20231106114255-c50117860a3c/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.2 h1:p8+vY9+wtr4BIa2+Y4EVI0jlvRg+FLWsbOpWYNlKXDw=
github.com/IBM-Cloud/power-go-client v1.5.2/go.mod h1:Vd8aSxWA30soUhE2U+tmzaYDUVNOmQE3/npny7BsN6Y=
Expand Down
17 changes: 17 additions & 0 deletions ibm/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ var (
CeResourceKeyID string
)

// Satellite tests
var (
SatelliteSSHPubKey string
)

// for IAM Identity
var IamIdentityAssignmentTargetAccountId string

Expand Down Expand Up @@ -1525,6 +1530,11 @@ func init() {
CeResourceKeyID = ""
fmt.Println("[WARN] Set the environment variable IBM_CODE_ENGINE_RESOURCE_KEY_ID with the ID of a resource key to access a service instance")
}

SatelliteSSHPubKey = os.Getenv("IBM_SATELLITE_SSH_PUB_KEY")
if SatelliteSSHPubKey == "" {
fmt.Println("[WARN] Set the environment variable IBM_SATELLITE_SSH_PUB_KEY with a ssh public key or ibm_satellite_* tests may fail")
}
}

var (
Expand Down Expand Up @@ -1714,6 +1724,13 @@ func TestAccPreCheckScc(t *testing.T) {
}
}

func TestAccPreCheckSatelliteSSH(t *testing.T) {
TestAccPreCheck(t)
if SatelliteSSHPubKey == "" {
t.Fatal("IBM_SATELLITE_SSH_PUB_KEY missing. Set the environment variable IBM_SATELLITE_SSH_PUB_KEY with a VALID ssh public key")
}
}

func TestAccProviderFactories() map[string]func() (*schema.Provider, error) {
return map[string]func() (*schema.Provider, error){
ProviderName: func() (*schema.Provider, error) { return provider.Provider(), nil },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func DataSourceIBMSatelliteClusterWorkerPool() *schema.Resource {
Computed: true,
Description: "Enable auto scalling for worker pool",
},
"openshift_license_source": {
Type: schema.TypeString,
Computed: true,
Description: "License source for Openshift",
},
},
}
}
Expand Down Expand Up @@ -154,6 +159,7 @@ func dataSourceIBMSatelliteClusterWorkerPoolRead(d *schema.ResourceData, meta in
d.Set("auto_scale_enabled", *workerPool.AutoscaleEnabled)
d.Set("state", *workerPool.Lifecycle.ActualState)
d.Set("isolation", *workerPool.Isolation)
d.Set("openshift_license_source", *workerPool.OpenshiftLicense)
d.SetId(*workerPool.ID)

return nil
Expand Down
11 changes: 11 additions & 0 deletions ibm/service/satellite/resource_ibm_satellite_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ func ResourceIBMSatelliteCluster() *schema.Resource {
},
Description: "The OpenShift Container Platform version",
},
"entitlement": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: flex.ApplyOnce,
Description: "Entitlement option reduces additional OCP Licence cost in Openshift Clusters",
},
"operating_system": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -390,6 +396,11 @@ func resourceIBMSatelliteClusterCreate(d *schema.ResourceData, meta interface{})
createClusterOptions.Labels = hostLabels
}

if v, ok := d.GetOk("entitlement"); ok {
entitlement := v.(string)
createClusterOptions.DefaultWorkerPoolEntitlement = &entitlement
}

if v, ok := d.GetOk("crn_token"); ok {
crnToken := v.(string)
createRemoteClusterOptions := &kubernetesserviceapiv1.CreateSatelliteClusterRemoteOptions{}
Expand Down
139 changes: 139 additions & 0 deletions ibm/service/satellite/resource_ibm_satellite_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE
})
}

func TestAccSatelliteCluster_Entitlement(t *testing.T) {
var instance string
clusterName := fmt.Sprintf("tf-satellitecluster-%d", acctest.RandIntRange(10, 100))
locationName := fmt.Sprintf("tf-satellitelocation-%d", acctest.RandIntRange(10, 100))
managed_from := "dal10"
operatingSystem := "REDHAT_8_64"
zones := []string{"us-south-1", "us-south-2", "us-south-3"}
resource_group := "default"
region := "us-south"
resource_prefix := "tf-satellite"
host_provider := "ibm"
publicKey := acc.SatelliteSSHPubKey

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheckSatelliteSSH(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckSatelliteClusterDestroy,
Steps: []resource.TestStep{

{
Config: testAccCheckSatelliteEntitlementClusterCreate(clusterName, locationName, managed_from, operatingSystem, resource_group, resource_prefix, region, publicKey, host_provider, zones),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckSatelliteClusterExists("ibm_satellite_cluster.create_cluster", instance),
resource.TestCheckResourceAttr("data.ibm_satellite_cluster_worker_pool.read_default_wp", "openshift_license_source", "cloud_pak"),
),
},
},
})
}

func TestAccSatelliteCluster_Import(t *testing.T) {
var instance string
clusterName := fmt.Sprintf("tf-satellitecluster-%d", acctest.RandIntRange(10, 100))
Expand Down Expand Up @@ -382,3 +412,112 @@ func testAccCheckSatelliteSingleNodeClusterCreate(clusterName, locationName, man
`, locationName, managed_from, resource_group, resource_prefix, resource_prefix, region, resource_prefix, publicKey, resource_prefix, region, resource_prefix, host_provider, clusterName)
}

func testAccCheckSatelliteEntitlementClusterCreate(clusterName, locationName, managed_from, operatingSystem, resource_group, resource_prefix, region, publicKey, host_provider string, zones []string) string {
return fmt.Sprintf(`
variable "location_zones" {
description = "Allocate your hosts across these three zones"
type = list(string)
default = ["us-south-1", "us-south-2", "us-south-3"]
}
data "ibm_is_image" "rhel8" {
name = "ibm-redhat-8-8-minimal-amd64-2"
}
resource "ibm_satellite_location" "location" {
location = "%s"
managed_from = "%s"
zones = var.location_zones
coreos_enabled = true
}
data "ibm_satellite_attach_host_script" "script" {
location = ibm_satellite_location.location.id
labels = ["env:prod"]
host_provider = "ibm"
}
data "ibm_resource_group" "resource_group" {
name = "%s"
}
resource "ibm_is_vpc" "satellite_vpc" {
name = "%s-vpc-1"
}
resource "ibm_is_subnet" "satellite_subnet" {
count = 3
name = "%s-subnet-${count.index}"
vpc = ibm_is_vpc.satellite_vpc.id
total_ipv4_address_count = 256
zone = "%s-${count.index + 1}"
}
resource "ibm_is_ssh_key" "satellite_ssh" {
name = "%s-ibm-ssh"
public_key = "%s"
}
resource "ibm_is_instance" "satellite_instance" {
count = 3
name = "%s-instance-${count.index}"
vpc = ibm_is_vpc.satellite_vpc.id
zone = "%s-${count.index + 1}"
image = data.ibm_is_image.rhel8.id
profile = "mx2-8x64"
keys = [ibm_is_ssh_key.satellite_ssh.id]
resource_group = data.ibm_resource_group.resource_group.id
user_data = data.ibm_satellite_attach_host_script.script.host_script
primary_network_interface {
subnet = ibm_is_subnet.satellite_subnet[count.index].id
}
}
resource "ibm_is_floating_ip" "satellite_ip" {
count = 3
name = "%s-fip-${count.index}"
target = ibm_is_instance.satellite_instance[count.index].primary_network_interface[0].id
}
resource "ibm_satellite_host" "assign_host" {
count = 3
location = ibm_satellite_location.location.id
host_id = element(ibm_is_instance.satellite_instance[*].name, count.index)
labels = ["env:prod"]
zone = element(var.location_zones, count.index)
host_provider = "%s"
}
resource "ibm_satellite_cluster" "create_cluster" {
name = "%s"
location = ibm_satellite_host.assign_host.0.location
enable_config_admin = true
kube_version = "4.13_openshift"
operating_system = "%s"
entitlement = "cloud_pak"
wait_for_worker_update = true
dynamic "zones" {
for_each = var.location_zones
content {
id = zones.value
}
}
default_worker_pool_labels = {
"test" = "test-pool1"
"test1" = "test-pool2"
}
}
data "ibm_satellite_cluster_worker_pool" "read_default_wp" {
name = "default"
cluster = ibm_satellite_cluster.create_cluster.id
}
`, locationName, managed_from, resource_group, resource_prefix, resource_prefix, region, resource_prefix, publicKey, resource_prefix, region, resource_prefix, host_provider, clusterName, operatingSystem)
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ func ResourceIBMSatelliteClusterWorkerPool() *schema.Resource {
Computed: true,
},
"entitlement": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: flex.ApplyOnce,
Description: "Entitlement option reduces additional OCP Licence cost in Openshift Clusters",
},
"operating_system": {
Type: schema.TypeString,
Expand Down Expand Up @@ -261,6 +263,11 @@ func resourceIBMSatelliteClusterWorkerPoolCreate(d *schema.ResourceData, meta in
createWorkerPoolOptions.Isolation = &isolation
}

if v, ok := d.GetOk("entitlement"); ok {
entitlement := v.(string)
createWorkerPoolOptions.Entitlement = &entitlement
}

instance, response, err := satClient.CreateSatelliteWorkerPool(createWorkerPoolOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Creating Satellite cluster worker pool: %s\n%s", err, response)
Expand Down

0 comments on commit 2aad24a

Please sign in to comment.