Skip to content

Commit 10b9f40

Browse files
Add support for aliases like composer-2 in Composer image version field (#5787) (#11296)
Signed-off-by: Modular Magician <[email protected]>
1 parent 7983463 commit 10b9f40

5 files changed

+79
-40
lines changed

.changelog/5787.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
composer: Added support for `composer-1` and `composer-2` aliases in image version argument
3+
```

google/resource_composer_environment.go

+52-25
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
const (
1818
composerEnvironmentEnvVariablesRegexp = "[a-zA-Z_][a-zA-Z0-9_]*."
1919
composerEnvironmentReservedAirflowEnvVarRegexp = "AIRFLOW__[A-Z0-9_]+__[A-Z0-9_]+"
20-
composerEnvironmentVersionRegexp = `composer-([0-9]+\.[0-9]+\.[0-9]+(-preview\.[0-9]+)?|latest)-airflow-([0-9]+\.[0-9]+(\.[0-9]+.*)?)`
20+
composerEnvironmentVersionRegexp = `composer-(([0-9]+)(\.[0-9]+\.[0-9]+(-preview\.[0-9]+)?)?|latest)-airflow-(([0-9]+\.[0-9]+)(\.[0-9]+)?)`
2121
)
2222

2323
var composerEnvironmentReservedEnvVar = map[string]struct{}{
@@ -322,7 +322,7 @@ func resourceComposerEnvironment() *schema.Resource {
322322
AtLeastOneOf: composerSoftwareConfigKeys,
323323
ValidateFunc: validateRegexp(composerEnvironmentVersionRegexp),
324324
DiffSuppressFunc: composerImageVersionDiffSuppress,
325-
Description: `The version of the software running in the environment. This encapsulates both the version of Cloud Composer functionality and the version of Apache Airflow. It must match the regular expression composer-[0-9]+\.[0-9]+(\.[0-9]+)?-airflow-[0-9]+\.[0-9]+(\.[0-9]+.*)?. The Cloud Composer portion of the version is a semantic version. The portion of the image version following 'airflow-' is an official Apache Airflow repository release name. See documentation for allowed release names.`,
325+
Description: `The version of the software running in the environment. This encapsulates both the version of Cloud Composer functionality and the version of Apache Airflow. It must match the regular expression composer-([0-9]+(\.[0-9]+\.[0-9]+(-preview\.[0-9]+)?)?|latest)-airflow-([0-9]+\.[0-9]+(\.[0-9]+)?). The Cloud Composer portion of the image version is a full semantic version, or an alias in the form of major version number or 'latest'. The Apache Airflow portion of the image version is a full semantic version that points to one of the supported Apache Airflow versions, or an alias in the form of only major and minor versions specified. See documentation for more details and version list.`,
326326
},
327327
"python_version": {
328328
Type: schema.TypeString,
@@ -1903,48 +1903,75 @@ func composerImageVersionDiffSuppress(_, old, new string, _ *schema.ResourceData
19031903
versionRe := regexp.MustCompile(composerEnvironmentVersionRegexp)
19041904
oldVersions := versionRe.FindStringSubmatch(old)
19051905
newVersions := versionRe.FindStringSubmatch(new)
1906-
if oldVersions == nil || len(oldVersions) < 4 {
1906+
if oldVersions == nil || len(oldVersions) < 8 {
19071907
// Somehow one of the versions didn't match the regexp or didn't
19081908
// have values in the capturing groups. In that case, fall back to
19091909
// an equality check.
19101910
if old != "" {
1911-
log.Printf("[WARN] Composer version didn't match regexp: %s", old)
1911+
log.Printf("[WARN] Image version didn't match regexp: %s", old)
19121912
}
19131913
return old == new
19141914
}
1915-
if newVersions == nil || len(newVersions) < 3 {
1915+
if newVersions == nil || len(newVersions) < 8 {
19161916
// Somehow one of the versions didn't match the regexp or didn't
19171917
// have values in the capturing groups. In that case, fall back to
19181918
// an equality check.
19191919
if new != "" {
1920-
log.Printf("[WARN] Composer version didn't match regexp: %s", new)
1920+
log.Printf("[WARN] Image version didn't match regexp: %s", new)
19211921
}
19221922
return old == new
19231923
}
19241924

1925-
// Check airflow version using the version package to account for
1926-
// diffs like 1.10 and 1.10.0
1927-
eq, err := versionsEqual(oldVersions[3], newVersions[3])
1928-
if err != nil {
1929-
log.Printf("[WARN] Could not parse airflow version, %s", err)
1930-
}
1931-
if !eq {
1932-
return false
1925+
oldAirflow := oldVersions[5]
1926+
oldAirflowMajorMinor := oldVersions[6]
1927+
newAirflow := newVersions[5]
1928+
newAirflowMajorMinor := newVersions[6]
1929+
// Check Airflow versions.
1930+
if oldAirflow == oldAirflowMajorMinor || newAirflow == newAirflowMajorMinor {
1931+
// If one of the Airflow versions specifies only major and minor version
1932+
// (like 1.10), we can only compare major and minor versions.
1933+
eq, err := versionsEqual(oldAirflowMajorMinor, newAirflowMajorMinor)
1934+
if err != nil {
1935+
log.Printf("[WARN] Could not parse airflow version, %s", err)
1936+
}
1937+
if !eq {
1938+
return false
1939+
}
1940+
} else {
1941+
// Otherwise, we compare the full Airflow versions (like 1.10.15).
1942+
eq, err := versionsEqual(oldAirflow, newAirflow)
1943+
if err != nil {
1944+
log.Printf("[WARN] Could not parse airflow version, %s", err)
1945+
}
1946+
if !eq {
1947+
return false
1948+
}
19331949
}
19341950

1935-
// Check composer version. Assume that "latest" means we should
1936-
// suppress the diff, because we don't have any other way of
1937-
// knowing what the latest version actually is.
1938-
if oldVersions[1] == "latest" || newVersions[1] == "latest" {
1951+
oldComposer := oldVersions[1]
1952+
oldComposerMajor := oldVersions[2]
1953+
newComposer := newVersions[1]
1954+
newComposerMajor := newVersions[2]
1955+
// Check Composer versions.
1956+
if oldComposer == "latest" || newComposer == "latest" {
1957+
// We don't know what the latest version is so we suppress the diff.
19391958
return true
1959+
} else if oldComposer == oldComposerMajor || newComposer == newComposerMajor {
1960+
// If one of the Composer versions specifies only major version
1961+
// (like 1), we can only compare major versions.
1962+
eq, err := versionsEqual(oldComposerMajor, newComposerMajor)
1963+
if err != nil {
1964+
log.Printf("[WARN] Could not parse composer version, %s", err)
1965+
}
1966+
return eq
1967+
} else {
1968+
// Otherwise, we compare the full Composer versions (like 1.18.1).
1969+
eq, err := versionsEqual(oldComposer, newComposer)
1970+
if err != nil {
1971+
log.Printf("[WARN] Could not parse composer version, %s", err)
1972+
}
1973+
return eq
19401974
}
1941-
// If neither version is "latest", check them using the version
1942-
// package like we did for airflow.
1943-
eq, err = versionsEqual(oldVersions[1], newVersions[1])
1944-
if err != nil {
1945-
log.Printf("[WARN] Could not parse composer version, %s", err)
1946-
}
1947-
return eq
19481975
}
19491976

19501977
func versionsEqual(old, new string) (bool, error) {

google/resource_composer_environment_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ func TestComposerImageVersionDiffSuppress(t *testing.T) {
3535
expected bool
3636
}{
3737
{"matches", "composer-1.4.0-airflow-1.10.0", "composer-1.4.0-airflow-1.10.0", true},
38+
{"preview matches", "composer-1.17.0-preview.0-airflow-2.0.1", "composer-1.17.0-preview.0-airflow-2.0.1", true},
3839
{"old latest", "composer-latest-airflow-1.10.0", "composer-1.4.1-airflow-1.10.0", true},
3940
{"new latest", "composer-1.4.1-airflow-1.10.0", "composer-latest-airflow-1.10.0", true},
40-
{"airflow equivalent", "composer-1.4.0-airflow-1.10.0", "composer-1.4.0-airflow-1.10", true},
41-
{"airflow different", "composer-1.4.0-airflow-1.10.0", "composer-1.4-airflow-1.9.0", false},
42-
{"preview matches", "composer-1.17.0-preview.0-airflow-2.0.1", "composer-1.17.0-preview.0-airflow-2.0.1", true},
41+
{"composer alias equivalent", "composer-1.4.0-airflow-1.10.0", "composer-1-airflow-1.10", true},
42+
{"composer alias different", "composer-1.4.0-airflow-2.1.4", "composer-2-airflow-2.2", false},
43+
{"composer different", "composer-1.4.0-airflow-1.10.0", "composer-1.4.1-airflow-1.10.0", false},
44+
{"airflow alias equivalent", "composer-1.4.0-airflow-1.10.0", "composer-1.4.0-airflow-1.10", true},
45+
{"airflow alias different", "composer-1.4.0-airflow-2.1.4", "composer-1.4.0-airflow-2.2", false},
46+
{"airflow different", "composer-1.4.0-airflow-1.10.0", "composer-1.4.0-airflow-1.9.0", false},
4347
}
4448

4549
for _, tc := range cases {

website/docs/d/composer_image_versions.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ The following arguments are supported:
4343
The following attributes are exported:
4444

4545
* `image_versions` - A list of composer image versions available in the given project and location. Each `image_version` contains:
46-
* `image_version_id` - The string identifier of the image version, in the form: "composer-x.y.z-airflow-a.b(.c)"
46+
* `image_version_id` - The string identifier of the image version, in the form: "composer-x.y.z-airflow-a.b.c"
4747
* `supported_python_versions` - Supported python versions for this image version

website/docs/r/composer_environment.html.markdown

+16-11
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ resource "google_composer_environment" "test" {
6060
6161
config {
6262
software_config {
63-
image_version = "composer-2.0.0-preview.3-airflow-2.1.2"
63+
image_version = "composer-2-airflow-2.2.3"
6464
}
6565
}
6666
}
@@ -157,7 +157,7 @@ resource "google_composer_environment" "test" {
157157
config {
158158
159159
software_config {
160-
image_version = "composer-2.0.0-preview.3-airflow-2.1.2"
160+
image_version = "composer-2-airflow-2.2.3"
161161
}
162162
163163
workloads_config {
@@ -451,9 +451,11 @@ The following arguments are supported:
451451

452452
The version of the software running in the environment. This encapsulates both the version of Cloud Composer
453453
functionality and the version of Apache Airflow. It must match the regular expression
454-
`composer-[0-9]+\.[0-9]+(\.[0-9]+)?-airflow-[0-9]+\.[0-9]+(\.[0-9]+.*)?`.
455-
The Cloud Composer portion of the version is a semantic version.
456-
The portion of the image version following 'airflow-' is an official Apache Airflow repository release name.
454+
`composer-([0-9]+(\.[0-9]+\.[0-9]+(-preview\.[0-9]+)?)?|latest)-airflow-([0-9]+\.[0-9]+(\.[0-9]+)?)`.
455+
The Cloud Composer portion of the image version is a full semantic version, or an alias in the form of major
456+
version number or 'latest'.
457+
The Apache Airflow portion of the image version is a full semantic version that points to one of the
458+
supported Apache Airflow versions, or an alias in the form of only major and minor versions specified.
457459
For more information about Cloud Composer images, see
458460
[Cloud Composer version list](https://cloud.google.com/composer/docs/concepts/versioning/composer-versions).
459461

@@ -763,11 +765,14 @@ The `software_config` block supports:
763765

764766
The version of the software running in the environment. This encapsulates both the version of Cloud Composer
765767
functionality and the version of Apache Airflow. It must match the regular expression
766-
`composer-[0-9]+\.[0-9]+(\.[0-9]+)?-airflow-[0-9]+\.[0-9]+(\.[0-9]+.*)?`.
767-
The Cloud Composer portion of the version is a semantic version.
768-
The portion of the image version following 'airflow-' is an official Apache Airflow repository release name.
769-
**Important**: You can only upgrade in-place between minor Cloud Composer versions. For example, you can upgrade
770-
your environment from `composer-1.16.x` to `composer-1.17.x`. You cannot upgrade between major Cloud Composer
768+
`composer-([0-9]+(\.[0-9]+\.[0-9]+(-preview\.[0-9]+)?)?|latest)-airflow-([0-9]+\.[0-9]+(\.[0-9]+)?)`.
769+
The Cloud Composer portion of the image version is a full semantic version, or an alias in the form of major
770+
version number or 'latest'.
771+
The Apache Airflow portion of the image version is a full semantic version that points to one of the
772+
supported Apache Airflow versions, or an alias in the form of only major and minor versions specified.
773+
**Important**: You can only upgrade in-place between minor or patch versions of Cloud Composer or Apache
774+
Airflow. For example, you can upgrade your environment from `composer-1.16.x` to `composer-1.17.x`, or from
775+
`airflow-2.1.x` to `airflow-2.2.x`. You cannot upgrade between major Cloud Composer or Apache Airflow
771776
versions (from `1.x.x` to `2.x.x`). To do so, create a new environment.
772777

773778

@@ -963,4 +968,4 @@ Environment can be imported using any of these accepted formats:
963968
$ terraform import google_composer_environment.default projects/{{project}}/locations/{{region}}/environments/{{name}}
964969
$ terraform import google_composer_environment.default {{project}}/{{region}}/{{name}}
965970
$ terraform import google_composer_environment.default {{name}}
966-
```
971+
```

0 commit comments

Comments
 (0)