Skip to content

Commit

Permalink
Merge pull request #59 from phamann/b-service-external-traffic-policy
Browse files Browse the repository at this point in the history
Add `external_traffic_policy` to kubernetes_service
  • Loading branch information
alexsomesan authored May 22, 2019
2 parents 526c8d8 + 8238910 commit 5e11ec4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
8 changes: 8 additions & 0 deletions kubernetes/data_source_kubernetes_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -35,6 +36,13 @@ func dataSourceKubernetesService() *schema.Resource {
Description: "The external reference that kubedns or equivalent will return as a CNAME record for this service. No proxying will be involved. Must be a valid DNS name and requires `type` to be `ExternalName`.",
Computed: true,
},
"external_traffic_policy": {
Type: schema.TypeString,
Description: "Denotes if this Service desires to route external traffic to node-local or cluster-wide endpoints. `Local` preserves the client source IP and avoids a second hop for LoadBalancer and Nodeport type services, but risks potentially imbalanced traffic spreading. `Cluster` obscures the client source IP and may cause a second hop to another node, but should have good overall load-spreading. More info: https://kubernetes.io/docs/tutorials/services/source-ip/",
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"Local", "Cluster"}, false),
},
"load_balancer_ip": {
Type: schema.TypeString,
Description: "Only applies to `type = LoadBalancer`. LoadBalancer will get created with the IP specified in this field. This feature depends on whether the underlying cloud-provider supports specifying this field when a load balancer is created. This field will be ignored if the cloud-provider does not support the feature.",
Expand Down
8 changes: 8 additions & 0 deletions kubernetes/resource_kubernetes_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -53,6 +54,13 @@ func resourceKubernetesService() *schema.Resource {
Description: "The external reference that kubedns or equivalent will return as a CNAME record for this service. No proxying will be involved. Must be a valid DNS name and requires `type` to be `ExternalName`.",
Optional: true,
},
"external_traffic_policy": {
Type: schema.TypeString,
Description: "Denotes if this Service desires to route external traffic to node-local or cluster-wide endpoints. `Local` preserves the client source IP and avoids a second hop for LoadBalancer and Nodeport type services, but risks potentially imbalanced traffic spreading. `Cluster` obscures the client source IP and may cause a second hop to another node, but should have good overall load-spreading. More info: https://kubernetes.io/docs/tutorials/services/source-ip/",
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"Local", "Cluster"}, false),
},
"load_balancer_ip": {
Type: schema.TypeString,
Description: "Only applies to `type = LoadBalancer`. LoadBalancer will get created with the IP specified in this field. This feature depends on whether the underlying cloud-provider supports specifying this field when a load balancer is created. This field will be ignored if the cloud-provider does not support the feature.",
Expand Down
3 changes: 3 additions & 0 deletions kubernetes/resource_kubernetes_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func TestAccKubernetesService_loadBalancer(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.1452553500", "10.0.0.4"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.3371212991", "10.0.0.3"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_name", "ext-name-"+name),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_traffic_policy", "Cluster"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.#", "2"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.138364083", "10.0.0.5/32"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.445311837", "10.0.0.6/32"),
Expand Down Expand Up @@ -170,6 +171,7 @@ func TestAccKubernetesService_loadBalancer(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.1452553500", "10.0.0.4"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.563283338", "10.0.0.5"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_name", "ext-name-modified-"+name),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_traffic_policy", "Local"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.#", "2"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.2271073252", "10.0.0.1/32"),
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.2515041290", "10.0.0.2/32"),
Expand Down Expand Up @@ -695,6 +697,7 @@ resource "kubernetes_service" "test" {
external_name = "ext-name-modified-%s"
external_ips = ["10.0.0.4", "10.0.0.5"]
load_balancer_source_ranges = ["10.0.0.1/32", "10.0.0.2/32"]
external_traffic_policy = "Local"
selector = {
App = "MyModifiedApp"
Expand Down
13 changes: 12 additions & 1 deletion kubernetes/structure_service_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func flattenServiceSpec(in v1.ServiceSpec) []interface{} {
}
att["publish_not_ready_addresses"] = in.PublishNotReadyAddresses

if in.ExternalTrafficPolicy != "" {
att["external_traffic_policy"] = string(in.ExternalTrafficPolicy)
}
return []interface{}{att}
}

Expand Down Expand Up @@ -135,7 +138,9 @@ func expandServiceSpec(l []interface{}) v1.ServiceSpec {
if v, ok := in["publish_not_ready_addresses"].(bool); ok {
obj.PublishNotReadyAddresses = v
}

if v, ok := in["external_traffic_policy"].(string); ok {
obj.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyType(v)
}
return obj
}

Expand Down Expand Up @@ -204,6 +209,12 @@ func patchServiceSpec(keyPrefix, pathPrefix string, d *schema.ResourceData, v *v
Value: d.Get(keyPrefix + "external_name").(string),
})
}
if d.HasChange(keyPrefix + "external_traffic_policy") {
ops = append(ops, &ReplaceOperation{
Path: pathPrefix + "externalTrafficPolicy",
Value: d.Get(keyPrefix + "external_traffic_policy").(string),
})
}
if d.HasChange(keyPrefix + "publish_not_ready_addresses") {
p := pathPrefix + "publishNotReadyAddresses"
v := d.Get(keyPrefix + "publish_not_ready_addresses").(bool)
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The following arguments are supported:
* `cluster_ip` - The IP address of the service. It is usually assigned randomly by the master. If an address is specified manually and is not in use by others, it will be allocated to the service; otherwise, creation of the service will fail. `None` can be specified for headless services when proxying is not required. Ignored if type is `ExternalName`. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services#virtual-ips-and-service-proxies)
* `external_ips` - A list of IP addresses for which nodes in the cluster will also accept traffic for this service. These IPs are not managed by Kubernetes. The user is responsible for ensuring that traffic arrives at a node with this IP. A common example is external load-balancers that are not part of the Kubernetes system.
* `external_name` - The external reference that kubedns or equivalent will return as a CNAME record for this service. No proxying will be involved. Must be a valid DNS name and requires `type` to be `ExternalName`.
* `external_traffic_policy` - (Optional) Denotes if this Service desires to route external traffic to node-local or cluster-wide endpoints. `Local` preserves the client source IP and avoids a second hop for LoadBalancer and Nodeport type services, but risks potentially imbalanced traffic spreading. `Cluster` obscures the client source IP and may cause a second hop to another node, but should have good overall load-spreading. More info: https://kubernetes.io/docs/tutorials/services/source-ip/
* `load_balancer_ip` - Only applies to `type = LoadBalancer`. LoadBalancer will get created with the IP specified in this field. This feature depends on whether the underlying cloud-provider supports specifying this field when a load balancer is created. This field will be ignored if the cloud-provider does not support the feature.
* `load_balancer_source_ranges` - If specified and supported by the platform, this will restrict traffic through the cloud-provider load-balancer will be restricted to the specified client IPs. This field will be ignored if the cloud-provider does not support the feature. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services-firewalls)
* `port` - The list of ports that are exposed by this service. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services#virtual-ips-and-service-proxies)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/la
* `cluster_ip` - (Optional) The IP address of the service. It is usually assigned randomly by the master. If an address is specified manually and is not in use by others, it will be allocated to the service; otherwise, creation of the service will fail. `None` can be specified for headless services when proxying is not required. Ignored if type is `ExternalName`. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services#virtual-ips-and-service-proxies)
* `external_ips` - (Optional) A list of IP addresses for which nodes in the cluster will also accept traffic for this service. These IPs are not managed by Kubernetes. The user is responsible for ensuring that traffic arrives at a node with this IP. A common example is external load-balancers that are not part of the Kubernetes system.
* `external_name` - (Optional) The external reference that kubedns or equivalent will return as a CNAME record for this service. No proxying will be involved. Must be a valid DNS name and requires `type` to be `ExternalName`.
* `external_traffic_policy` - (Optional) Denotes if this Service desires to route external traffic to node-local or cluster-wide endpoints. `Local` preserves the client source IP and avoids a second hop for LoadBalancer and Nodeport type services, but risks potentially imbalanced traffic spreading. `Cluster` obscures the client source IP and may cause a second hop to another node, but should have good overall load-spreading. More info: https://kubernetes.io/docs/tutorials/services/source-ip/
* `load_balancer_ip` - (Optional) Only applies to `type = LoadBalancer`. LoadBalancer will get created with the IP specified in this field. This feature depends on whether the underlying cloud-provider supports specifying this field when a load balancer is created. This field will be ignored if the cloud-provider does not support the feature.
* `load_balancer_source_ranges` - (Optional) If specified and supported by the platform, this will restrict traffic through the cloud-provider load-balancer will be restricted to the specified client IPs. This field will be ignored if the cloud-provider does not support the feature. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services-firewalls)
* `port` - (Required) The list of ports that are exposed by this service. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services#virtual-ips-and-service-proxies)
Expand Down

0 comments on commit 5e11ec4

Please sign in to comment.