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

Allow fuzzy matching GKE versions in the GKE version datasource #506

Merged
merged 1 commit into from
Mar 6, 2019
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
35 changes: 28 additions & 7 deletions google-beta/data_source_google_container_engine_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -15,6 +16,10 @@ func dataSourceGoogleContainerEngineVersions() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"version_prefix": {
Type: schema.TypeString,
Optional: true,
},
"zone": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -72,16 +77,32 @@ func dataSourceGoogleContainerEngineVersionsRead(d *schema.ResourceData, meta in
return fmt.Errorf("Error retrieving available container cluster versions: %s", err.Error())
}

d.Set("valid_master_versions", resp.ValidMasterVersions)
d.Set("default_cluster_version", resp.DefaultClusterVersion)
d.Set("valid_node_versions", resp.ValidNodeVersions)
if len(resp.ValidMasterVersions) > 0 {
d.Set("latest_master_version", resp.ValidMasterVersions[0])
validMasterVersions := make([]string, 0)
for _, v := range resp.ValidMasterVersions {
if strings.HasPrefix(v, d.Get("version_prefix").(string)) {
validMasterVersions = append(validMasterVersions, v)
}
}
if len(resp.ValidNodeVersions) > 0 {
d.Set("latest_node_version", resp.ValidNodeVersions[0])

validNodeVersions := make([]string, 0)
for _, v := range resp.ValidNodeVersions {
if strings.HasPrefix(v, d.Get("version_prefix").(string)) {
validNodeVersions = append(validNodeVersions, v)
}
}

d.Set("valid_master_versions", validMasterVersions)
if len(validMasterVersions) > 0 {
d.Set("latest_master_version", validMasterVersions[0])
}

d.Set("valid_node_versions", validNodeVersions)
if len(validNodeVersions) > 0 {
d.Set("latest_node_version", validNodeVersions[0])
}

d.Set("default_cluster_version", resp.DefaultClusterVersion)

d.SetId(time.Now().UTC().String())
return nil
}
25 changes: 25 additions & 0 deletions google-beta/data_source_google_container_engine_versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ func TestAccContainerEngineVersions_basic(t *testing.T) {
})
}

func TestAccContainerEngineVersions_filtered(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGoogleContainerEngineVersions_filtered,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_container_engine_versions.versions", "valid_master_versions.#", "0"),
resource.TestCheckResourceAttr("data.google_container_engine_versions.versions", "valid_node_versions.#", "0"),
),
},
},
})
}

func TestAccContainerEngineVersions_regional(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -120,6 +138,13 @@ data "google_container_engine_versions" "versions" {
}
`

var testAccCheckGoogleContainerEngineVersions_filtered = `
data "google_container_engine_versions" "versions" {
zone = "us-central1-b"
version_prefix = "1.1."
}
`

var testAccCheckGoogleContainerEngineVersionsRegionalConfig = `
data "google_container_engine_versions" "versions" {
region = "us-central1"
Expand Down
10 changes: 9 additions & 1 deletion website/docs/d/google_container_engine_versions.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ to the datasource. A `region` can have a different set of supported versions tha

```hcl
data "google_container_engine_versions" "central1b" {
zone = "us-central1-b"
zone = "us-central1-b"
version_prefix = "1.12."
}

resource "google_container_cluster" "foo" {
Expand Down Expand Up @@ -48,6 +49,13 @@ The following arguments are supported:
* `project` (optional) - ID of the project to list available cluster versions for. Should match the project the cluster will be deployed to.
Defaults to the project that the provider is authenticated with.

* `version_prefix` (optional) - If provided, Terraform will only return versions
that match the string prefix. For example, `1.11.` will match all `1.11` series
releases. Since this is just a string match, it's recommended that you append a
`.` after minor versions to ensure that prefixes such as `1.1` don't match
versions like `1.12.5-gke.10` accidentally. See [the docs on versioning schema](https://cloud.google.com/kubernetes-engine/versioning-and-upgrades#versioning_scheme)
for full details on how version strings are formatted.

## Attributes Reference

The following attributes are exported:
Expand Down
11 changes: 8 additions & 3 deletions website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ output "cluster_ca_certificate" {
If unset, the cluster's version will be set by GKE to the version of the most recent
official release (which is not necessarily the latest version). Most users will find
the `google_container_engine_versions` data source useful - it indicates which versions
are available. If you intend to specify versions manually, [the docs](https://cloud.google.com/kubernetes-engine/versioning-and-upgrades#specifying_cluster_version)
are available, and can be use to approximate fuzzy versions in a
Terraform-compatible way. If you intend to specify versions manually,
[the docs](https://cloud.google.com/kubernetes-engine/versioning-and-upgrades#specifying_cluster_version)
describe the various acceptable formats for this field.

-> If you are using the `google_container_engine_versions` datasource with a regional cluster, ensure that you have provided a `region`
Expand Down Expand Up @@ -249,8 +251,11 @@ to the datasource. A `region` can have a different set of supported versions tha
* `node_version` - (Optional) The Kubernetes version on the nodes. Must either be unset
or set to the same value as `min_master_version` on create. Defaults to the default
version set by GKE which is not necessarily the latest version. This only affects
nodes in the default node pool. To update nodes in other node pools, use the `version`
attribute on the node pool.
nodes in the default node pool. While a fuzzy version can be specified, it's
recommended that you specify explicit versions as Terraform will see spurious diffs
when fuzzy versions are used. See the `google_container_engine_versions` data source's
`version_prefix` field to approximate fuzzy versions in a Terraform-compatible way.
To update nodes in other node pools, use the `version` attribute on the node pool.

* `pod_security_policy_config` - (Optional, [Beta](https://terraform.io/docs/providers/google/provider_versions.html)) Configuration for the
[PodSecurityPolicy](https://cloud.google.com/kubernetes-engine/docs/how-to/pod-security-policies) feature.
Expand Down
5 changes: 4 additions & 1 deletion website/docs/r/container_node_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ resource "google_container_cluster" "primary" {

* `version` - (Optional) The Kubernetes version for the nodes in this pool. Note that if this field
and `auto_upgrade` are both specified, they will fight each other for what the node version should
be, so setting both is highly discouraged.
be, so setting both is highly discouraged. While a fuzzy version can be specified, it's
recommended that you specify explicit versions as Terraform will see spurious diffs
when fuzzy versions are used. See the `google_container_engine_versions` data source's
`version_prefix` field to approximate fuzzy versions in a Terraform-compatible way.

The `autoscaling` block supports:

Expand Down