Skip to content

Commit

Permalink
k8s: support setting cluster and service subnets on create. (#1253)
Browse files Browse the repository at this point in the history
* k8s: support setting cluster and service subnets on create.

* docs: mention support for setting cluster and service subnets.

* k8s: Update test to use /22
  • Loading branch information
andrewsomething authored Oct 18, 2024
1 parent 09dfe73 commit 651186a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
22 changes: 18 additions & 4 deletions digitalocean/kubernetes/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@ func ResourceDigitalOceanKubernetesCluster() *schema.Resource {
},

"cluster_subnet": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
ValidateFunc: validation.IsCIDR,
Optional: true,
Computed: true,
ForceNew: true,
},

"service_subnet": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
ValidateFunc: validation.IsCIDR,
Optional: true,
Computed: true,
ForceNew: true,
},

"ipv4_address": {
Expand Down Expand Up @@ -291,6 +297,14 @@ func resourceDigitalOceanKubernetesClusterCreate(ctx context.Context, d *schema.
opts.VPCUUID = vpc.(string)
}

if clusterSubnet, ok := d.GetOk("cluster_subnet"); ok {
opts.ClusterSubnet = clusterSubnet.(string)
}

if serviceSubnet, ok := d.GetOk("service_subnet"); ok {
opts.ServiceSubnet = serviceSubnet.(string)
}

if autoUpgrade, ok := d.GetOk("auto_upgrade"); ok {
opts.AutoUpgrade = autoUpgrade.(bool)
}
Expand Down
41 changes: 41 additions & 0 deletions digitalocean/kubernetes/resource_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,29 @@ func TestAccDigitalOceanKubernetesCluster_DestroyAssociated(t *testing.T) {
})
}

func TestAccDigitalOceanKubernetesCluster_VPCNative(t *testing.T) {
rName := acceptance.RandomTestName()
var k8s godo.KubernetesCluster

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanKubernetesClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccDigitalOceanKubernetesConfigVPCNative(testClusterVersionLatest, 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", "nyc1"),
resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "cluster_subnet", "192.168.0.0/20"),
resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "service_subnet", "192.168.16.0/22"),
),
},
},
})
}

func testAccDigitalOceanKubernetesConfigBasic(testClusterVersion string, rName string) string {
return fmt.Sprintf(`%s
Expand Down Expand Up @@ -930,6 +953,24 @@ resource "digitalocean_kubernetes_cluster" "foobar" {
`, testClusterVersion, rName)
}

func testAccDigitalOceanKubernetesConfigVPCNative(testClusterVersion string, rName string) string {
return fmt.Sprintf(`%s
resource "digitalocean_kubernetes_cluster" "foobar" {
name = "%s"
region = "nyc1"
version = data.digitalocean_kubernetes_versions.test.latest_version
cluster_subnet = "192.168.0.0/20"
service_subnet = "192.168.16.0/22"
node_pool {
name = "default"
size = "s-1vcpu-2gb"
node_count = 1
}
}
`, testClusterVersion, rName)
}

func testAccCheckDigitalOceanKubernetesClusterDestroy(s *terraform.State) error {
client := acceptance.TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

Expand Down
4 changes: 2 additions & 2 deletions docs/resources/kubernetes_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ The following arguments are supported:
* `name` - (Required) A name for the Kubernetes cluster.
* `region` - (Required) The slug identifier for the region where the Kubernetes cluster will be created.
* `version` - (Required) The slug identifier for the version of Kubernetes used for the cluster. Use [doctl](https://github.com/digitalocean/doctl) to find the available versions `doctl kubernetes options versions`. (**Note:** A cluster may only be upgraded to newer versions in-place. If the version is decreased, a new resource will be created.)
* `cluster_subnet` - (Optional) The range of IP addresses in the overlay network of the Kubernetes cluster. For more information, see [here](https://docs.digitalocean.com/products/kubernetes/how-to/create-clusters/#create-with-vpc-native).
* `service_subnet` - (Optional) The range of assignable IP addresses for services running in the Kubernetes cluster. For more information, see [here](https://docs.digitalocean.com/products/kubernetes/how-to/create-clusters/#create-with-vpc-native).
* `vpc_uuid` - (Optional) The ID of the VPC where the Kubernetes cluster will be located.
* `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: true
Expand Down Expand Up @@ -172,8 +174,6 @@ This resource supports [customized create timeouts](https://www.terraform.io/doc
In addition to the arguments listed above, the following additional attributes are exported:

* `id` - A unique ID that can be used to identify and reference a Kubernetes cluster.
* `cluster_subnet` - The range of IP addresses in the overlay network of the Kubernetes cluster.
* `service_subnet` - The range of assignable IP addresses for services running in the Kubernetes cluster.
* `ipv4_address` - The public IPv4 address of the Kubernetes master node. This will not be set if high availability is configured on the cluster (v1.21+)
* `endpoint` - The base URL of the API server on the Kubernetes master node.
* `status` - A string indicating the current status of the cluster. Potential values include running, provisioning, and errored.
Expand Down

0 comments on commit 651186a

Please sign in to comment.