Skip to content

Commit c4e197f

Browse files
Changes to support namespace label scoped backups (#15047) (#24427)
[upstream:7134387a0b2b8f034912dc148ac82435e0132946] Signed-off-by: Modular Magician <[email protected]>
1 parent 3d02b70 commit c4e197f

File tree

5 files changed

+269
-3
lines changed

5 files changed

+269
-3
lines changed

.changelog/15047.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
gkebackup: added `backup_config.selected_namespace_labels` field to `google_gke_backup_backup_plan` resource
3+
```

google/services/gkebackup/resource_gke_backup_backup_plan.go

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func ResourceGKEBackupBackupPlan() *schema.Resource {
8787
Type: schema.TypeBool,
8888
Optional: true,
8989
Description: `If True, include all namespaced resources.`,
90-
ExactlyOneOf: []string{"backup_config.0.all_namespaces", "backup_config.0.selected_namespaces", "backup_config.0.selected_applications"},
90+
ExactlyOneOf: []string{"backup_config.0.all_namespaces", "backup_config.0.selected_namespaces", "backup_config.0.selected_applications", "backup_config.0.selected_namespace_labels"},
9191
},
9292
"encryption_key": {
9393
Type: schema.TypeList,
@@ -154,7 +154,37 @@ non-standard or requires additional setup to restore.`,
154154
},
155155
},
156156
},
157-
ExactlyOneOf: []string{"backup_config.0.all_namespaces", "backup_config.0.selected_namespaces", "backup_config.0.selected_applications"},
157+
ExactlyOneOf: []string{"backup_config.0.all_namespaces", "backup_config.0.selected_namespaces", "backup_config.0.selected_applications", "backup_config.0.selected_namespace_labels"},
158+
},
159+
"selected_namespace_labels": {
160+
Type: schema.TypeList,
161+
Optional: true,
162+
Description: `If set, include just the resources in the listed namespace Labels.`,
163+
MaxItems: 1,
164+
Elem: &schema.Resource{
165+
Schema: map[string]*schema.Schema{
166+
"resource_labels": {
167+
Type: schema.TypeList,
168+
Required: true,
169+
Description: `A list of Kubernetes Namespace labels.`,
170+
Elem: &schema.Resource{
171+
Schema: map[string]*schema.Schema{
172+
"key": {
173+
Type: schema.TypeString,
174+
Required: true,
175+
Description: `The key of the kubernetes label.`,
176+
},
177+
"value": {
178+
Type: schema.TypeString,
179+
Required: true,
180+
Description: `The value of the Label.`,
181+
},
182+
},
183+
},
184+
},
185+
},
186+
},
187+
ExactlyOneOf: []string{"backup_config.0.all_namespaces", "backup_config.0.selected_namespaces", "backup_config.0.selected_applications", "backup_config.0.selected_namespace_labels"},
158188
},
159189
"selected_namespaces": {
160190
Type: schema.TypeList,
@@ -173,7 +203,7 @@ non-standard or requires additional setup to restore.`,
173203
},
174204
},
175205
},
176-
ExactlyOneOf: []string{"backup_config.0.all_namespaces", "backup_config.0.selected_namespaces", "backup_config.0.selected_applications"},
206+
ExactlyOneOf: []string{"backup_config.0.all_namespaces", "backup_config.0.selected_namespaces", "backup_config.0.selected_applications", "backup_config.0.selected_namespace_labels"},
177207
},
178208
},
179209
},
@@ -1244,6 +1274,8 @@ func flattenGKEBackupBackupPlanBackupConfig(v interface{}, d *schema.ResourceDat
12441274
flattenGKEBackupBackupPlanBackupConfigSelectedNamespaces(original["selectedNamespaces"], d, config)
12451275
transformed["selected_applications"] =
12461276
flattenGKEBackupBackupPlanBackupConfigSelectedApplications(original["selectedApplications"], d, config)
1277+
transformed["selected_namespace_labels"] =
1278+
flattenGKEBackupBackupPlanBackupConfigSelectedNamespaceLabels(original["selectedNamespaceLabels"], d, config)
12471279
transformed["permissive_mode"] =
12481280
flattenGKEBackupBackupPlanBackupConfigPermissiveMode(original["permissiveMode"], d, config)
12491281
return []interface{}{transformed}
@@ -1334,6 +1366,46 @@ func flattenGKEBackupBackupPlanBackupConfigSelectedApplicationsNamespacedNamesNa
13341366
return v
13351367
}
13361368

1369+
func flattenGKEBackupBackupPlanBackupConfigSelectedNamespaceLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1370+
if v == nil {
1371+
return nil
1372+
}
1373+
original := v.(map[string]interface{})
1374+
if len(original) == 0 {
1375+
return nil
1376+
}
1377+
transformed := make(map[string]interface{})
1378+
transformed["resource_labels"] =
1379+
flattenGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabels(original["resourceLabels"], d, config)
1380+
return []interface{}{transformed}
1381+
}
1382+
func flattenGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1383+
if v == nil {
1384+
return v
1385+
}
1386+
l := v.([]interface{})
1387+
transformed := make([]interface{}, 0, len(l))
1388+
for _, raw := range l {
1389+
original := raw.(map[string]interface{})
1390+
if len(original) < 1 {
1391+
// Do not include empty json objects coming back from the api
1392+
continue
1393+
}
1394+
transformed = append(transformed, map[string]interface{}{
1395+
"key": flattenGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabelsKey(original["key"], d, config),
1396+
"value": flattenGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabelsValue(original["value"], d, config),
1397+
})
1398+
}
1399+
return transformed
1400+
}
1401+
func flattenGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabelsKey(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1402+
return v
1403+
}
1404+
1405+
func flattenGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabelsValue(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1406+
return v
1407+
}
1408+
13371409
func flattenGKEBackupBackupPlanBackupConfigPermissiveMode(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
13381410
return v
13391411
}
@@ -1747,6 +1819,13 @@ func expandGKEBackupBackupPlanBackupConfig(v interface{}, d tpgresource.Terrafor
17471819
transformed["selectedApplications"] = transformedSelectedApplications
17481820
}
17491821

1822+
transformedSelectedNamespaceLabels, err := expandGKEBackupBackupPlanBackupConfigSelectedNamespaceLabels(original["selected_namespace_labels"], d, config)
1823+
if err != nil {
1824+
return nil, err
1825+
} else if val := reflect.ValueOf(transformedSelectedNamespaceLabels); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1826+
transformed["selectedNamespaceLabels"] = transformedSelectedNamespaceLabels
1827+
}
1828+
17501829
transformedPermissiveMode, err := expandGKEBackupBackupPlanBackupConfigPermissiveMode(original["permissive_mode"], d, config)
17511830
if err != nil {
17521831
return nil, err
@@ -1871,6 +1950,62 @@ func expandGKEBackupBackupPlanBackupConfigSelectedApplicationsNamespacedNamesNam
18711950
return v, nil
18721951
}
18731952

1953+
func expandGKEBackupBackupPlanBackupConfigSelectedNamespaceLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1954+
l := v.([]interface{})
1955+
if len(l) == 0 || l[0] == nil {
1956+
return nil, nil
1957+
}
1958+
raw := l[0]
1959+
original := raw.(map[string]interface{})
1960+
transformed := make(map[string]interface{})
1961+
1962+
transformedResourceLabels, err := expandGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabels(original["resource_labels"], d, config)
1963+
if err != nil {
1964+
return nil, err
1965+
} else if val := reflect.ValueOf(transformedResourceLabels); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1966+
transformed["resourceLabels"] = transformedResourceLabels
1967+
}
1968+
1969+
return transformed, nil
1970+
}
1971+
1972+
func expandGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1973+
l := v.([]interface{})
1974+
req := make([]interface{}, 0, len(l))
1975+
for _, raw := range l {
1976+
if raw == nil {
1977+
continue
1978+
}
1979+
original := raw.(map[string]interface{})
1980+
transformed := make(map[string]interface{})
1981+
1982+
transformedKey, err := expandGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabelsKey(original["key"], d, config)
1983+
if err != nil {
1984+
return nil, err
1985+
} else if val := reflect.ValueOf(transformedKey); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1986+
transformed["key"] = transformedKey
1987+
}
1988+
1989+
transformedValue, err := expandGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabelsValue(original["value"], d, config)
1990+
if err != nil {
1991+
return nil, err
1992+
} else if val := reflect.ValueOf(transformedValue); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1993+
transformed["value"] = transformedValue
1994+
}
1995+
1996+
req = append(req, transformed)
1997+
}
1998+
return req, nil
1999+
}
2000+
2001+
func expandGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabelsKey(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
2002+
return v, nil
2003+
}
2004+
2005+
func expandGKEBackupBackupPlanBackupConfigSelectedNamespaceLabelsResourceLabelsValue(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
2006+
return v, nil
2007+
}
2008+
18742009
func expandGKEBackupBackupPlanBackupConfigPermissiveMode(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
18752010
return v, nil
18762011
}

google/services/gkebackup/resource_gke_backup_backup_plan_generated_meta.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ fields:
1212
- field: 'backup_config.permissive_mode'
1313
- field: 'backup_config.selected_applications.namespaced_names.name'
1414
- field: 'backup_config.selected_applications.namespaced_names.namespace'
15+
- field: 'backup_config.selected_namespace_labels.resource_labels.key'
16+
- field: 'backup_config.selected_namespace_labels.resource_labels.value'
1517
- field: 'backup_config.selected_namespaces.namespaces'
1618
- field: 'backup_schedule.cron_schedule'
1719
- field: 'backup_schedule.paused'

google/services/gkebackup/resource_gke_backup_backup_plan_generated_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,72 @@ resource "google_kms_key_ring" "key_ring" {
230230
`, context)
231231
}
232232

233+
func TestAccGKEBackupBackupPlan_gkebackupBackupplanNslabelsExample(t *testing.T) {
234+
t.Parallel()
235+
236+
context := map[string]interface{}{
237+
"project": envvar.GetTestProjectFromEnv(),
238+
"deletion_protection": false,
239+
"network_name": acctest.BootstrapSharedTestNetwork(t, "gke-cluster"),
240+
"subnetwork_name": acctest.BootstrapSubnet(t, "gke-cluster", acctest.BootstrapSharedTestNetwork(t, "gke-cluster")),
241+
"random_suffix": acctest.RandString(t, 10),
242+
}
243+
244+
acctest.VcrTest(t, resource.TestCase{
245+
PreCheck: func() { acctest.AccTestPreCheck(t) },
246+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
247+
CheckDestroy: testAccCheckGKEBackupBackupPlanDestroyProducer(t),
248+
Steps: []resource.TestStep{
249+
{
250+
Config: testAccGKEBackupBackupPlan_gkebackupBackupplanNslabelsExample(context),
251+
},
252+
{
253+
ResourceName: "google_gke_backup_backup_plan.nslabels",
254+
ImportState: true,
255+
ImportStateVerify: true,
256+
ImportStateVerifyIgnore: []string{"labels", "location", "terraform_labels"},
257+
},
258+
},
259+
})
260+
}
261+
262+
func testAccGKEBackupBackupPlan_gkebackupBackupplanNslabelsExample(context map[string]interface{}) string {
263+
return acctest.Nprintf(`
264+
resource "google_container_cluster" "primary" {
265+
name = "tf-test-nslabels-cluster%{random_suffix}"
266+
location = "us-central1"
267+
initial_node_count = 1
268+
workload_identity_config {
269+
workload_pool = "%{project}.svc.id.goog"
270+
}
271+
addons_config {
272+
gke_backup_agent_config {
273+
enabled = true
274+
}
275+
}
276+
deletion_protection = %{deletion_protection}
277+
network = "%{network_name}"
278+
subnetwork = "%{subnetwork_name}"
279+
}
280+
281+
resource "google_gke_backup_backup_plan" "nslabels" {
282+
name = "tf-test-nslabels-plan%{random_suffix}"
283+
cluster = google_container_cluster.primary.id
284+
location = "us-central1"
285+
backup_config {
286+
include_volume_data = true
287+
include_secrets = true
288+
selected_namespace_labels {
289+
resource_labels {
290+
key = "key1"
291+
value ="value1"
292+
}
293+
}
294+
}
295+
}
296+
`, context)
297+
}
298+
233299
func TestAccGKEBackupBackupPlan_gkebackupBackupplanFullExample(t *testing.T) {
234300
t.Parallel()
235301

website/docs/r/gke_backup_backup_plan.html.markdown

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,43 @@ resource "google_kms_key_ring" "key_ring" {
148148
location = "us-central1"
149149
}
150150
```
151+
## Example Usage - Gkebackup Backupplan Nslabels
152+
153+
154+
```hcl
155+
resource "google_container_cluster" "primary" {
156+
name = "nslabels-cluster"
157+
location = "us-central1"
158+
initial_node_count = 1
159+
workload_identity_config {
160+
workload_pool = "my-project-name.svc.id.goog"
161+
}
162+
addons_config {
163+
gke_backup_agent_config {
164+
enabled = true
165+
}
166+
}
167+
deletion_protection = true
168+
network = "default"
169+
subnetwork = "default"
170+
}
171+
172+
resource "google_gke_backup_backup_plan" "nslabels" {
173+
name = "nslabels-plan"
174+
cluster = google_container_cluster.primary.id
175+
location = "us-central1"
176+
backup_config {
177+
include_volume_data = true
178+
include_secrets = true
179+
selected_namespace_labels {
180+
resource_labels {
181+
key = "key1"
182+
value ="value1"
183+
}
184+
}
185+
}
186+
}
187+
```
151188
## Example Usage - Gkebackup Backupplan Full
152189

153190

@@ -624,6 +661,11 @@ The following arguments are supported:
624661
A list of namespaced Kubernetes Resources.
625662
Structure is [documented below](#nested_backup_config_selected_applications).
626663

664+
* `selected_namespace_labels` -
665+
(Optional)
666+
If set, include just the resources in the listed namespace Labels.
667+
Structure is [documented below](#nested_backup_config_selected_namespace_labels).
668+
627669
* `permissive_mode` -
628670
(Optional)
629671
This flag specifies whether Backups will not fail when
@@ -661,6 +703,24 @@ The following arguments are supported:
661703
(Required)
662704
The name of a Kubernetes Resource.
663705

706+
<a name="nested_backup_config_selected_namespace_labels"></a>The `selected_namespace_labels` block supports:
707+
708+
* `resource_labels` -
709+
(Required)
710+
A list of Kubernetes Namespace labels.
711+
Structure is [documented below](#nested_backup_config_selected_namespace_labels_resource_labels).
712+
713+
714+
<a name="nested_backup_config_selected_namespace_labels_resource_labels"></a>The `resource_labels` block supports:
715+
716+
* `key` -
717+
(Required)
718+
The key of the kubernetes label.
719+
720+
* `value` -
721+
(Required)
722+
The value of the Label.
723+
664724
## Attributes Reference
665725

666726
In addition to the arguments listed above, the following computed attributes are exported:

0 commit comments

Comments
 (0)