diff --git a/google-beta/data_source_google_container_engine_versions.go b/google-beta/data_source_google_container_engine_versions.go index 6cd4bd945d..adbb35f962 100644 --- a/google-beta/data_source_google_container_engine_versions.go +++ b/google-beta/data_source_google_container_engine_versions.go @@ -2,6 +2,7 @@ package google import ( "fmt" + "strings" "time" "github.com/hashicorp/terraform/helper/schema" @@ -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, @@ -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 } diff --git a/google-beta/data_source_google_container_engine_versions_test.go b/google-beta/data_source_google_container_engine_versions_test.go index 4a6cef8b6b..e3f809dfda 100644 --- a/google-beta/data_source_google_container_engine_versions_test.go +++ b/google-beta/data_source_google_container_engine_versions_test.go @@ -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() @@ -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" diff --git a/website/docs/d/google_container_engine_versions.html.markdown b/website/docs/d/google_container_engine_versions.html.markdown index 4590c02d72..245e800e47 100644 --- a/website/docs/d/google_container_engine_versions.html.markdown +++ b/website/docs/d/google_container_engine_versions.html.markdown @@ -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" { @@ -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: diff --git a/website/docs/r/container_cluster.html.markdown b/website/docs/r/container_cluster.html.markdown index 85a5123e80..9296688c80 100644 --- a/website/docs/r/container_cluster.html.markdown +++ b/website/docs/r/container_cluster.html.markdown @@ -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` @@ -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. diff --git a/website/docs/r/container_node_pool.html.markdown b/website/docs/r/container_node_pool.html.markdown index 3694f72d95..b46d1d73c7 100644 --- a/website/docs/r/container_node_pool.html.markdown +++ b/website/docs/r/container_node_pool.html.markdown @@ -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: