Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validations for K8s resource fields #2772

Merged
merged 7 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions azurerm/helpers/validate/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package validate

import (
"regexp"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func KubernetesAdminUserName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile(`^[A-Za-z][-A-Za-z0-9_]*$`),
"AdminUserName must start with alphabet and/or continue with alphanumeric characters, underscores, hyphens.")
}

func KubernetesCidr() schema.SchemaValidateFunc {
metacpp marked this conversation as resolved.
Show resolved Hide resolved
return validation.StringMatch(
regexp.MustCompile(`^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$`),
"CIDR must start with IPV4 address and/or slash, number of bits (0-32) as prefix. Example: 127.0.0.1/8.")
}

func KubernetesDNSServiceIP() schema.SchemaValidateFunc {
metacpp marked this conversation as resolved.
Show resolved Hide resolved
return validation.StringMatch(
regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`),
"DNSServiceIP must follow IPV4 address format.")
}

func KubernetesAgentPoolName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-z]{1}[a-z0-9]{0,11}$"),
"Agent Pool names must start with a lowercase letter, have max length of 12, and only have characters a-z0-9.",
)
}

func KubernetesDnsPrefix() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{0,43}[a-zA-Z0-9]$"),
"The DNS name must contain between 3 and 45 characters. The name can contain only letters, numbers, and hyphens. The name must start with a letter and must end with a letter or a number.",
)
}
150 changes: 77 additions & 73 deletions azurerm/resource_arm_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"log"
"regexp"
"strings"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/kubernetes"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"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 @@ -64,9 +64,10 @@ func resourceArmKubernetesCluster() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"location": locationSchema(),
Expand All @@ -77,13 +78,14 @@ func resourceArmKubernetesCluster() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateKubernetesClusterDnsPrefix(),
ValidateFunc: validate.KubernetesDnsPrefix(),
},

"kubernetes_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.NoEmptyStrings,
},

"agent_pool_profile": {
Expand All @@ -96,7 +98,7 @@ func resourceArmKubernetesCluster() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateKubernetesClusterAgentPoolName(),
ValidateFunc: validate.KubernetesAgentPoolName(),
},

"count": {
Expand Down Expand Up @@ -124,6 +126,7 @@ func resourceArmKubernetesCluster() *schema.Resource {
Required: true,
ForceNew: true,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validate.NoEmptyStrings,
},

"os_disk_size_gb": {
Expand All @@ -135,9 +138,10 @@ func resourceArmKubernetesCluster() *schema.Resource {
},

"vnet_subnet_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
metacpp marked this conversation as resolved.
Show resolved Hide resolved
},

"os_type": {
Expand Down Expand Up @@ -170,16 +174,18 @@ func resourceArmKubernetesCluster() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"client_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"client_secret": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
Sensitive: true,
Type: schema.TypeString,
ForceNew: true,
Required: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
},
Expand Down Expand Up @@ -225,8 +231,9 @@ func resourceArmKubernetesCluster() *schema.Resource {
Required: true,
},
"log_analytics_workspace_id": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
metacpp marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
Expand All @@ -243,8 +250,9 @@ func resourceArmKubernetesCluster() *schema.Resource {
Required: true,
},
"subnet_name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
},
Expand All @@ -260,9 +268,10 @@ func resourceArmKubernetesCluster() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"admin_username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.KubernetesAdminUserName(),
},
"ssh_key": {
Type: schema.TypeList,
Expand All @@ -272,9 +281,10 @@ func resourceArmKubernetesCluster() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key_data": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
},
Expand Down Expand Up @@ -302,31 +312,35 @@ func resourceArmKubernetesCluster() *schema.Resource {
},

"dns_service_ip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.KubernetesDNSServiceIP(),
metacpp marked this conversation as resolved.
Show resolved Hide resolved
},

"docker_bridge_cidr": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.KubernetesCidr(),
},

"pod_cidr": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.KubernetesCidr(),
},

"service_cidr": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.KubernetesCidr(),
},
},
},
Expand All @@ -353,30 +367,34 @@ func resourceArmKubernetesCluster() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"client_app_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use azure.ValidateResourceID here"

Copy link
Contributor Author

@metacpp metacpp Jan 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@katbyte client_app_id is more like GUID, instead of resource ID. Shall we use UUID instead here ?

},

"server_app_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use azure.ValidateResourceID here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above question.

},

"server_app_secret": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
Sensitive: true,
Type: schema.TypeString,
ForceNew: true,
Required: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},

"tenant_id": {
// this can be sourced from the client config if it's not specified
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
metacpp marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
Expand Down Expand Up @@ -1187,20 +1205,6 @@ func resourceKubernetesClusterServicePrincipalProfileHash(v interface{}) int {
return hashcode.String(buf.String())
}

func validateKubernetesClusterAgentPoolName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-z]{1}[a-z0-9]{0,11}$"),
"Agent Pool names must start with a lowercase letter, have max length of 12, and only have characters a-z0-9.",
)
}

func validateKubernetesClusterDnsPrefix() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{0,43}[a-zA-Z0-9]$"),
"The DNS name must contain between 3 and 45 characters. The name can contain only letters, numbers, and hyphens. The name must start with a letter and must end with a letter or a number.",
)
}

func flattenKubernetesClusterKubeConfig(config kubernetes.KubeConfig) []interface{} {
values := make(map[string]interface{})

Expand Down
3 changes: 2 additions & 1 deletion azurerm/resource_arm_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
)

func TestAzureRMKubernetesCluster_agentPoolName(t *testing.T) {
metacpp marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestAzureRMKubernetesCluster_agentPoolName(t *testing.T) {
}

for _, tc := range cases {
_, errors := validateKubernetesClusterAgentPoolName()(tc.Input, "")
_, errors := validate.KubernetesAgentPoolName()(tc.Input, "")

hasError := len(errors) > 0

Expand Down