Skip to content

Commit

Permalink
Add registry_integration to Schema (#963)
Browse files Browse the repository at this point in the history
* Add registry_integration to Schema

registry_integration can be true to integrate the kubernetes cluster
with container registry, or false to disable this integration
If there is no container registry in the team's or user's account
then terraform will taint the resource and re-create the clsuter
on next apply, it is recommended to make your doks resource
depends on the Digital Ocean container registry resource
to avoid re-creating the kubernetes cluster

* Don't try to disable registry integration if it is not enabled when creating a new DOKS

* Add docs for registry_integration argument

* Add acceptance tests for registry_integration option

* Format terraform

* Consolidate and clean up acceptance tests.

* Copy edit on new docs.

---------

Co-authored-by: Mouhsen Ibrahim <[email protected]>
Co-authored-by: Andrew Starr-Bochicchio <[email protected]>
  • Loading branch information
3 people authored Apr 17, 2023
1 parent a376b0f commit 88d2fd4
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
38 changes: 38 additions & 0 deletions digitalocean/kubernetes/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ func ResourceDigitalOceanKubernetesCluster() *schema.Resource {
ForceNew: true,
},

"registry_integration": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: false,
},

"version": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -294,6 +301,13 @@ func resourceDigitalOceanKubernetesClusterCreate(ctx context.Context, d *schema.
return diag.Errorf("Error creating Kubernetes cluster: %s", err)
}

if d.Get("registry_integration") == true {
err = enableRegistryIntegration(client, cluster.ID)
if err != nil {
return diag.Errorf("Error enabling registry integration: %s", err)
}
}

return resourceDigitalOceanKubernetesClusterRead(ctx, d, meta)
}

Expand Down Expand Up @@ -450,6 +464,20 @@ func resourceDigitalOceanKubernetesClusterUpdate(ctx context.Context, d *schema.
}
}

if d.HasChanges("registry_integration") {
if d.Get("registry_integration") == true {
err := enableRegistryIntegration(client, d.Id())
if err != nil {
return diag.Errorf("Error enabling registry integration: %s", err)
}
} else {
err := disableRegistryIntegration(client, d.Id())
if err != nil {
return diag.Errorf("Error disabling registry integration: %s", err)
}
}
}

return resourceDigitalOceanKubernetesClusterRead(ctx, d, meta)
}

Expand Down Expand Up @@ -562,6 +590,16 @@ func resourceDigitalOceanKubernetesClusterImportState(d *schema.ResourceData, me
return resourceDatas, nil
}

func enableRegistryIntegration(client *godo.Client, cluster_uuid string) error {
_, err := client.Kubernetes.AddRegistry(context.Background(), &godo.KubernetesClusterRegistryRequest{ClusterUUIDs: []string{cluster_uuid}})
return err
}

func disableRegistryIntegration(client *godo.Client, cluster_uuid string) error {
_, err := client.Kubernetes.RemoveRegistry(context.Background(), &godo.KubernetesClusterRegistryRequest{ClusterUUIDs: []string{cluster_uuid}})
return err
}

func waitForKubernetesClusterCreate(client *godo.Client, d *schema.ResourceData) (*godo.KubernetesCluster, error) {
var (
tickerInterval = 10 * time.Second
Expand Down
113 changes: 113 additions & 0 deletions digitalocean/kubernetes/resource_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestAccDigitalOceanKubernetesCluster_Basic(t *testing.T) {
resource.TestMatchResourceAttr("digitalocean_kubernetes_cluster.foobar", "urn", expectedURNRegEx),
resource.TestCheckResourceAttrSet("digitalocean_kubernetes_cluster.foobar", "maintenance_policy.0.day"),
resource.TestCheckResourceAttrSet("digitalocean_kubernetes_cluster.foobar", "maintenance_policy.0.start_time"),
resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "registry_integration", "false"),
),
},
// Update: remove default node_pool taints
Expand Down Expand Up @@ -171,6 +172,118 @@ resource "digitalocean_kubernetes_cluster" "foobar" {
})
}

func TestAccDigitalOceanKubernetesCluster_CreateWithRegistry(t *testing.T) {
var (
rName = acceptance.RandomTestName()
k8s godo.KubernetesCluster
registryConfig = fmt.Sprintf(`
resource "digitalocean_container_registry" "foobar" {
name = "%s"
region = "nyc3"
subscription_tier_slug = "starter"
}`, rName)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanKubernetesClusterDestroy,
Steps: []resource.TestStep{
// Create container registry
{
Config: registryConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("digitalocean_container_registry.foobar", "name", rName),
resource.TestCheckResourceAttr("digitalocean_container_registry.foobar", "endpoint", "registry.digitalocean.com/"+rName),
resource.TestCheckResourceAttr("digitalocean_container_registry.foobar", "server_url", "registry.digitalocean.com"),
resource.TestCheckResourceAttr("digitalocean_container_registry.foobar", "subscription_tier_slug", "starter"),
resource.TestCheckResourceAttr("digitalocean_container_registry.foobar", "region", "nyc3"),
resource.TestCheckResourceAttrSet("digitalocean_container_registry.foobar", "created_at"),
resource.TestCheckResourceAttrSet("digitalocean_container_registry.foobar", "storage_usage_bytes"),
),
},
// Create cluster with registry integration enabled
{
Config: fmt.Sprintf(`%s
%s
resource "digitalocean_kubernetes_cluster" "foobar" {
name = "%s"
region = "nyc3"
registry_integration = true
version = data.digitalocean_kubernetes_versions.test.latest_version
node_pool {
name = "default"
size = "s-1vcpu-2gb"
node_count = 1
}
}
`, testClusterVersionLatest, registryConfig, rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckDigitalOceanKubernetesClusterExists("digitalocean_kubernetes_cluster.foobar", &k8s),
resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "name", rName),
resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "region", "nyc3"),
resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "registry_integration", "true"),
resource.TestCheckResourceAttrPair("digitalocean_kubernetes_cluster.foobar", "version", "data.digitalocean_kubernetes_versions.test", "latest_version"),
resource.TestCheckResourceAttrSet("digitalocean_kubernetes_cluster.foobar", "status"),
resource.TestCheckResourceAttrSet("digitalocean_kubernetes_cluster.foobar", "created_at"),
resource.TestCheckResourceAttrSet("digitalocean_kubernetes_cluster.foobar", "updated_at"),
resource.TestCheckResourceAttrSet("digitalocean_kubernetes_cluster.foobar", "endpoint"),
),
},
// Disable registry integration
{
Config: fmt.Sprintf(`%s
%s
resource "digitalocean_kubernetes_cluster" "foobar" {
name = "%s"
region = "nyc3"
version = data.digitalocean_kubernetes_versions.test.latest_version
node_pool {
name = "default"
size = "s-1vcpu-2gb"
node_count = 1
}
}
`, testClusterVersionLatest, registryConfig, rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckDigitalOceanKubernetesClusterExists("digitalocean_kubernetes_cluster.foobar", &k8s),
resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "registry_integration", "false"),
),
},
// Re-enable registry integration
{
Config: fmt.Sprintf(`%s
%s
resource "digitalocean_kubernetes_cluster" "foobar" {
name = "%s"
region = "nyc3"
version = data.digitalocean_kubernetes_versions.test.latest_version
registry_integration = true
node_pool {
name = "default"
size = "s-1vcpu-2gb"
node_count = 1
}
}
`, testClusterVersionLatest, registryConfig, rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckDigitalOceanKubernetesClusterExists("digitalocean_kubernetes_cluster.foobar", &k8s),
resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "registry_integration", "true"),
),
},
},
})
}

func TestAccDigitalOceanKubernetesCluster_UpdateCluster(t *testing.T) {
rName := acceptance.RandomTestName()
var k8s godo.KubernetesCluster
Expand Down
1 change: 1 addition & 0 deletions docs/resources/kubernetes_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ The following arguments are supported:
* `auto_upgrade` - (Optional) A boolean value indicating whether the cluster will be automatically upgraded to new patch releases during its maintenance window.
* `surge_upgrade` - (Optional) Enable/disable surge upgrades for a cluster. Default: false
* `ha` - (Optional) Enable/disable the high availability control plane for a cluster. High availability can only be set when creating a cluster. Any update will create a new cluster. Default: false
* `registry_integration` - (optional) Enables or disables the DigitalOcean container registry integration for the cluster. This requires that a container registry has first been created for the account. Default: false
* `node_pool` - (Required) A block representing the cluster's default node pool. Additional node pools may be added to the cluster using the `digitalocean_kubernetes_node_pool` resource. The following arguments may be specified:
- `name` - (Required) A name for the node pool.
- `size` - (Required) The slug identifier for the type of Droplet to be used as workers in the node pool.
Expand Down

0 comments on commit 88d2fd4

Please sign in to comment.