Skip to content

Commit

Permalink
Add the ability to use the GKE recurring maintenance window in beta.
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
nat-henderson authored and modular-magician committed Oct 23, 2019
1 parent e1b5947 commit 795825f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 31 deletions.
76 changes: 48 additions & 28 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ func resourceContainerCluster() *schema.Resource {
"daily_maintenance_window": {
Type: schema.TypeList,
Required: true,

MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -763,7 +764,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
cluster := &containerBeta.Cluster{
Name: clusterName,
InitialNodeCount: int64(d.Get("initial_node_count").(int)),
MaintenancePolicy: expandMaintenancePolicy(d.Get("maintenance_policy")),
MaintenancePolicy: expandMaintenancePolicy(d, meta),
MasterAuthorizedNetworksConfig: expandMasterAuthorizedNetworksConfig(d.Get("master_authorized_networks_config")),
InitialClusterVersion: d.Get("min_master_version").(string),
ClusterIpv4Cidr: d.Get("cluster_ipv4_cidr").(string),
Expand Down Expand Up @@ -1117,15 +1118,8 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
}

if d.HasChange("maintenance_policy") {
var req *containerBeta.SetMaintenancePolicyRequest
if mp, ok := d.GetOk("maintenance_policy"); ok {
req = &containerBeta.SetMaintenancePolicyRequest{
MaintenancePolicy: expandMaintenancePolicy(mp),
}
} else {
req = &containerBeta.SetMaintenancePolicyRequest{
NullFields: []string{"MaintenancePolicy"},
}
req := &containerBeta.SetMaintenancePolicyRequest{
MaintenancePolicy: expandMaintenancePolicy(d, meta),
}

updateF := func() error {
Expand Down Expand Up @@ -1736,22 +1730,44 @@ func expandIPAllocationPolicy(configured interface{}) *containerBeta.IPAllocatio
}
}

func expandMaintenancePolicy(configured interface{}) *containerBeta.MaintenancePolicy {
func expandMaintenancePolicy(d *schema.ResourceData, meta interface{}) *containerBeta.MaintenancePolicy {
config := meta.(*Config)
// We have to perform a full Get() as part of this, to get the fingerprint. We can't do this
// at any other time, because the fingerprint update might happen between plan and apply.
// We can omit error checks, since to have gotten this far, a project is definitely configured.
project, _ := getProject(d, config)
location, _ := getLocation(d, config)
clusterName := d.Get("name").(string)
name := containerClusterFullName(project, location, clusterName)
cluster, _ := config.clientContainerBeta.Projects.Locations.Clusters.Get(name).Do()
resourceVersion := ""
if cluster != nil && cluster.MaintenancePolicy != nil {
resourceVersion = cluster.MaintenancePolicy.ResourceVersion
}

configured := d.Get("maintenance_policy")
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil
return &containerBeta.MaintenancePolicy{
ResourceVersion: resourceVersion,
}
}

maintenancePolicy := l[0].(map[string]interface{})
dailyMaintenanceWindow := maintenancePolicy["daily_maintenance_window"].([]interface{})[0].(map[string]interface{})
startTime := dailyMaintenanceWindow["start_time"].(string)
return &containerBeta.MaintenancePolicy{
Window: &containerBeta.MaintenanceWindow{
DailyMaintenanceWindow: &containerBeta.DailyMaintenanceWindow{
StartTime: startTime,

if dailyMaintenanceWindow, ok := maintenancePolicy["daily_maintenance_window"]; ok && len(dailyMaintenanceWindow.([]interface{})) > 0 {
dmw := dailyMaintenanceWindow.([]interface{})[0].(map[string]interface{})
startTime := dmw["start_time"].(string)
return &containerBeta.MaintenancePolicy{
Window: &containerBeta.MaintenanceWindow{
DailyMaintenanceWindow: &containerBeta.DailyMaintenanceWindow{
StartTime: startTime,
},
},
},
ResourceVersion: resourceVersion,
}
}

return nil
}

func expandMasterAuth(configured interface{}) *containerBeta.MasterAuth {
Expand Down Expand Up @@ -1969,19 +1985,23 @@ func flattenIPAllocationPolicy(c *containerBeta.Cluster, d *schema.ResourceData,
}

func flattenMaintenancePolicy(mp *containerBeta.MaintenancePolicy) []map[string]interface{} {
if mp == nil || mp.Window == nil || mp.Window.DailyMaintenanceWindow == nil {
if mp == nil || mp.Window == nil {
return nil
}
return []map[string]interface{}{
{
"daily_maintenance_window": []map[string]interface{}{
{
"start_time": mp.Window.DailyMaintenanceWindow.StartTime,
"duration": mp.Window.DailyMaintenanceWindow.Duration,
if mp.Window.DailyMaintenanceWindow != nil {
return []map[string]interface{}{
{
"daily_maintenance_window": []map[string]interface{}{
{
"start_time": mp.Window.DailyMaintenanceWindow.StartTime,
"duration": mp.Window.DailyMaintenanceWindow.Duration,
},
},
},
},
}
}

return nil
}

func flattenMasterAuth(ma *containerBeta.MasterAuth) []map[string]interface{} {
Expand Down
28 changes: 26 additions & 2 deletions google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,30 @@ resource "google_container_cluster" "with_maintenance_window" {
}`, clusterName, maintenancePolicy)
}

func testAccContainerCluster_withRecurringMaintenanceWindow(clusterName string, startTime, endTime string) string {
maintenancePolicy := ""
if len(startTime) > 0 {
maintenancePolicy = fmt.Sprintf(`
maintenance_policy {
recurring_window {
start_time = "%s"
end_time = "%s"
recurrence = "FREQ=DAILY"
}
}`, startTime, endTime)
}

return fmt.Sprintf(`
resource "google_container_cluster" "with_recurring_maintenance_window" {
name = "cluster-test-%s"
zone = "us-central1-a"
initial_node_count = 1
%s
}`, clusterName, maintenancePolicy)

}

func testAccContainerCluster_withIPAllocationPolicy_existingSecondaryRanges(cluster string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
Expand Down Expand Up @@ -2331,8 +2355,8 @@ resource "google_container_cluster" "cidr_error_overlap" {
initial_node_count = 1
ip_allocation_policy {
cluster_ipv4_cidr_block = "10.0.0.0/16"
services_ipv4_cidr_block = "10.1.0.0/16"
cluster_ipv4_cidr_block = "10.0.0.0/16"
services_ipv4_cidr_block = "10.1.0.0/16"
}
}
`, initConfig, secondCluster)
Expand Down
13 changes: 13 additions & 0 deletions google/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ func validateRFC1918Network(min, max int) schema.SchemaValidateFunc {
}
}

func validateRFC5545Recurrence(v interface{}, k string) (warnings []string, errors []error) {
// These are pretty complicated - I don't want to parse them.
return
}

func validateRFC3339Date(v interface{}, k string) (warnings []string, errors []error) {
_, err := time.Parse(time.RFC3339, v.(string))
if err != nil {
errors = append(errors, err)
}
return
}

func validateRFC3339Time(v interface{}, k string) (warnings []string, errors []error) {
time := v.(string)
if len(time) != 5 || time[2] != ':' {
Expand Down
22 changes: 21 additions & 1 deletion website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ The `authenticator_groups_config` block supports:

The `maintenance_policy` block supports:

* `daily_maintenance_window` - (Required) Time window specified for daily maintenance operations.
* `daily_maintenance_window` - (Required in GA, Optional in Beta) Time window specified for daily maintenance operations.
Specify `start_time` in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) format "HH:MM”,
where HH : \[00-23\] and MM : \[00-59\] GMT. For example:

Expand All @@ -414,6 +414,26 @@ maintenance_policy {
}
```

* `recurring_window` - (Optional, [Beta](https://terraform.io/docs/providers/google/provider_versions.html)) Time window for
recurring maintenance operations.

Specify `start_time` and `end_time` in [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) date format. The start time's date is
the initial date that the window starts, and the end time is used for calculating duration. Specify `recurrence` in
[RFC5545](https://tools.ietf.org/html/rfc5545#section-3.8.5.3) RRULE format, to specify when this recurs.

For example:
```
maintenance_policy {
recurring_window {
start_time = "2019-01-01T03:00"
end_time = "2019-01-01T06:00"
recurrence = "FREQ=DAILY"
}
}
```

In beta, one or the other of `recurring_window` and `daily_maintenance_window` is required if a `maintenance_policy` block is supplied.

The `ip_allocation_policy` block supports:

* `use_ip_aliases` - (Optional) Whether alias IPs will be used for pod IPs in
Expand Down

0 comments on commit 795825f

Please sign in to comment.