Skip to content

Commit

Permalink
Do not set default namespace for replication controller and deploymen…
Browse files Browse the repository at this point in the history
…t pod templates
  • Loading branch information
pdecat committed Jul 22, 2019
1 parent 1c75691 commit 473b979
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func resourceKubernetesDeployment() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"metadata": namespacedMetadataSchema("pod", true),
"metadata": namespacedMetadataSchemaIsTemplate("pod", true, true),
"spec": {
Type: schema.TypeList,
Description: "Spec defines the specification of the desired behavior of the deployment. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/#deployment-v1-apps",
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_replication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func resourceKubernetesReplicationController() *schema.Resource {
}

func replicationControllerTemplateFieldSpec() map[string]*schema.Schema {
metadata := namespacedMetadataSchema("replication controller's template", true)
metadata := namespacedMetadataSchemaIsTemplate("replication controller's template", true, true)
// TODO: make this required once the legacy fields are removed
metadata.Computed = true
metadata.Required = false
Expand Down
9 changes: 9 additions & 0 deletions kubernetes/schema_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kubernetes

func conditionalDefault(condition bool, defaultValue interface{}) interface{} {
if !condition {
return nil
}

return defaultValue
}
6 changes: 5 additions & 1 deletion kubernetes/schema_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ func metadataSchema(objectName string, generatableName bool) *schema.Schema {
}

func namespacedMetadataSchema(objectName string, generatableName bool) *schema.Schema {
return namespacedMetadataSchemaIsTemplate(objectName, generatableName, false)
}

func namespacedMetadataSchemaIsTemplate(objectName string, generatableName, isTemplate bool) *schema.Schema {
fields := metadataFields(objectName)
fields["namespace"] = &schema.Schema{
Type: schema.TypeString,
Description: fmt.Sprintf("Namespace defines the space within which name of the %s must be unique.", objectName),
Optional: true,
ForceNew: true,
Default: "default",
Default: conditionalDefault(!isTemplate, "default"),
}
if generatableName {
fields["generate_name"] = &schema.Schema{
Expand Down
19 changes: 6 additions & 13 deletions kubernetes/schema_pod_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func podSpecFields(isUpdatable, isDeprecated, isComputed bool) map[string]*schem
Type: schema.TypeString,
Optional: true,
Computed: isComputed,
Default: defaultIfNotComputed(isComputed, "ClusterFirst"),
Default: conditionalDefault(!isComputed, "ClusterFirst"),
Description: "Set DNS policy for containers within the pod. Valid values are 'ClusterFirstWithHostNet', 'ClusterFirst', 'Default' or 'None'. DNS parameters given in DNSConfig will be merged with the policy selected with DNSPolicy. To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'. Optional: Defaults to 'ClusterFirst', see [Kubernetes reference](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy).",
Deprecated: deprecatedMessage,
},
Expand Down Expand Up @@ -136,15 +136,15 @@ func podSpecFields(isUpdatable, isDeprecated, isComputed bool) map[string]*schem
Type: schema.TypeBool,
Optional: true,
Computed: isComputed,
Default: defaultIfNotComputed(isComputed, false),
Default: conditionalDefault(!isComputed, false),
Description: "Use the host's ipc namespace. Optional: Defaults to false.",
Deprecated: deprecatedMessage,
},
"host_network": {
Type: schema.TypeBool,
Optional: true,
Computed: isComputed,
Default: defaultIfNotComputed(isComputed, false),
Default: conditionalDefault(!isComputed, false),
Description: "Host networking requested for this pod. Use the host's network namespace. If this option is set, the ports that will be used must be specified.",
Deprecated: deprecatedMessage,
},
Expand All @@ -153,7 +153,7 @@ func podSpecFields(isUpdatable, isDeprecated, isComputed bool) map[string]*schem
Type: schema.TypeBool,
Optional: true,
Computed: isComputed,
Default: defaultIfNotComputed(isComputed, false),
Default: conditionalDefault(!isComputed, false),
Description: "Use the host's pid namespace.",
Deprecated: deprecatedMessage,
},
Expand Down Expand Up @@ -199,7 +199,7 @@ func podSpecFields(isUpdatable, isDeprecated, isComputed bool) map[string]*schem
Type: schema.TypeString,
Optional: true,
Computed: isComputed,
Default: defaultIfNotComputed(isComputed, "Always"),
Default: conditionalDefault(!isComputed, "Always"),
Description: "Restart policy for all containers within the pod. One of Always, OnFailure, Never. More info: http://kubernetes.io/docs/user-guide/pod-states#restartpolicy.",
Deprecated: deprecatedMessage,
},
Expand Down Expand Up @@ -276,7 +276,7 @@ func podSpecFields(isUpdatable, isDeprecated, isComputed bool) map[string]*schem
Type: schema.TypeInt,
Optional: true,
Computed: isComputed,
Default: defaultIfNotComputed(isComputed, 30),
Default: conditionalDefault(!isComputed, 30),
ValidateFunc: validateTerminationGracePeriodSeconds,
Description: "Optional duration in seconds the pod needs to terminate gracefully. May be decreased in delete request. Value must be non-negative integer. The value zero indicates delete immediately. If this value is nil, the default grace period will be used instead. The grace period is the duration in seconds after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal. Set this value longer than the expected cleanup time for your process.",
Deprecated: deprecatedMessage,
Expand Down Expand Up @@ -347,13 +347,6 @@ func podSpecFields(isUpdatable, isDeprecated, isComputed bool) map[string]*schem
return s
}

func defaultIfNotComputed(isComputed bool, defaultValue interface{}) interface{} {
if isComputed {
return nil
}
return defaultValue
}

func volumeSchema() *schema.Resource {
v := commonVolumeSources()

Expand Down

0 comments on commit 473b979

Please sign in to comment.