Skip to content

Commit

Permalink
Merge pull request #3118 from neilpeterson/aci-add-probes
Browse files Browse the repository at this point in the history
Add Probe support for Azure Container Instances
  • Loading branch information
tombuildsstuff authored Apr 8, 2019
2 parents 6c6c9cc + 50b42c8 commit 1e8e955
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 4 deletions.
90 changes: 90 additions & 0 deletions azurerm/helpers/azure/container_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package azure

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
)

func SchemaContainerGroupProbe() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"exec": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
},

"http_get": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"path": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},
"port": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validate.PortNumber,
},
"scheme": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Http",
"Https",
}, false),
},
},
},
},

"initial_delay_seconds": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},

"period_seconds": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},

"failure_threshold": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},

"success_threshold": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},

"timeout_seconds": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
},
}
}
133 changes: 132 additions & 1 deletion azurerm/resource_arm_container_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -307,6 +307,10 @@ func resourceArmContainerGroup() *schema.Resource {
},
},
},

"liveness_probe": azure.SchemaContainerGroupProbe(),

"readiness_probe": azure.SchemaContainerGroupProbe(),
},
},
},
Expand Down Expand Up @@ -661,6 +665,14 @@ func expandContainerGroupContainers(d *schema.ResourceData) (*[]containerinstanc
}
}

if v, ok := data["liveness_probe"]; ok {
container.ContainerProperties.LivenessProbe = expandContainerProbe(v)
}

if v, ok := data["readiness_probe"]; ok {
container.ContainerProperties.ReadinessProbe = expandContainerProbe(v)
}

containers = append(containers, container)
}

Expand Down Expand Up @@ -762,6 +774,66 @@ func expandContainerVolumes(input interface{}) (*[]containerinstance.VolumeMount
return &volumeMounts, &containerGroupVolumes
}

func expandContainerProbe(input interface{}) *containerinstance.ContainerProbe {
probe := containerinstance.ContainerProbe{}
probeRaw := input.([]interface{})

if len(probeRaw) == 0 {
return nil
}

for _, p := range probeRaw {
probeConfig := p.(map[string]interface{})

if v := probeConfig["initial_delay_seconds"].(int); v > 0 {
probe.InitialDelaySeconds = utils.Int32(int32(v))
}

if v := probeConfig["period_seconds"].(int); v > 0 {
probe.PeriodSeconds = utils.Int32(int32(v))
}

if v := probeConfig["failure_threshold"].(int); v > 0 {
probe.FailureThreshold = utils.Int32(int32(v))
}

if v := probeConfig["success_threshold"].(int); v > 0 {
probe.SuccessThreshold = utils.Int32(int32(v))
}

if v := probeConfig["timeout_seconds"].(int); v > 0 {
probe.TimeoutSeconds = utils.Int32(int32(v))
}

commands := probeConfig["exec"].([]interface{})
if len(commands) > 0 {
exec := containerinstance.ContainerExec{
Command: utils.ExpandStringArray(commands),
}
probe.Exec = &exec
}

httpRaw := probeConfig["http_get"].([]interface{})
if len(httpRaw) > 0 {

for _, httpget := range httpRaw {
x := httpget.(map[string]interface{})

path := x["path"].(string)
port := x["port"].(int)
scheme := x["scheme"].(string)

probe.HTTPGet = &containerinstance.ContainerHTTPGet{
Path: utils.String(path),
Port: utils.Int32(int32(port)),
Scheme: containerinstance.Scheme(scheme),
}
}
}
}
return &probe
}

func flattenContainerImageRegistryCredentials(d *schema.ResourceData, input *[]containerinstance.ImageRegistryCredential) []interface{} {
if input == nil {
return nil
Expand Down Expand Up @@ -907,6 +979,9 @@ func flattenContainerGroupContainers(d *schema.ResourceData, containers *[]conta
containerConfig["volume"] = flattenContainerVolumes(container.VolumeMounts, containerGroupVolumes, containerVolumesConfig)
}

containerConfig["liveness_probe"] = flattenContainerProbes(container.LivenessProbe)
containerConfig["readiness_probe"] = flattenContainerProbes(container.ReadinessProbe)

containerCfg = append(containerCfg, containerConfig)
}

Expand Down Expand Up @@ -1002,6 +1077,62 @@ func flattenContainerVolumes(volumeMounts *[]containerinstance.VolumeMount, cont
return volumeConfigs
}

func flattenContainerProbes(input *containerinstance.ContainerProbe) []interface{} {
outputs := make([]interface{}, 0)
if input == nil {
return outputs
}

output := make(map[string]interface{})

if v := input.Exec; v != nil {
output["exec"] = *v.Command
}

httpGets := make([]interface{}, 0)
if get := input.HTTPGet; get != nil {
httpGet := make(map[string]interface{})

if v := get.Path; v != nil {
httpGet["path"] = *v
}

if v := get.Port; v != nil {
httpGet["port"] = *v
}

if get.Scheme != "" {
httpGet["scheme"] = get.Scheme
}

httpGets = append(httpGets, httpGet)
}
output["http_get"] = httpGets

if v := input.FailureThreshold; v != nil {
output["failure_threshold"] = *v
}

if v := input.InitialDelaySeconds; v != nil {
output["initial_delay_seconds"] = *v
}

if v := input.PeriodSeconds; v != nil {
output["period_seconds"] = *v
}

if v := input.SuccessThreshold; v != nil {
output["success_threshold"] = *v
}

if v := input.TimeoutSeconds; v != nil {
output["timeout_seconds"] = *v
}

outputs = append(outputs, output)
return outputs
}

func expandContainerGroupDiagnostics(input []interface{}) *containerinstance.ContainerGroupDiagnostics {
if len(input) == 0 {
return nil
Expand Down
Loading

0 comments on commit 1e8e955

Please sign in to comment.